Add primary key id in table afe_hosts_labels
[autotest-zwu.git] / cli / topic_common_unittest.py
blobd3bdacbb91476e6cd7c97a3ed042262f813175cd
1 #!/usr/bin/python
3 # Copyright 2008 Google Inc. All Rights Reserved.
5 """Test for atest."""
7 import unittest, os, sys, StringIO, urllib2
9 import common
10 from autotest_lib.cli import cli_mock, topic_common, rpc
11 from autotest_lib.frontend.afe.json_rpc import proxy
14 class topic_common_misc_tests(unittest.TestCase):
15 def test_get_item_key(self):
16 get_item_key = topic_common._get_item_key
17 self.assertRaises(ValueError, get_item_key, {}, '')
18 self.assertRaises(ValueError, get_item_key, {}, '.')
19 self.assertRaises(KeyError, get_item_key, {}, 'a')
20 self.assertRaises(KeyError, get_item_key, {}, 'a.')
21 self.assertRaises(ValueError, get_item_key, {'a': {}}, 'a.')
22 self.assertRaises(KeyError, get_item_key, {'a': {}}, 'a.b')
23 self.assertEquals(2, get_item_key({'a.b': 2, 'a': {}}, 'a.b'))
24 self.assertEquals(9, get_item_key({'a': {'b': 9}}, 'a.b'))
25 self.assertEquals(3, get_item_key({'a': {'b': {'c': 3}}}, 'a.b.c'))
26 self.assertEquals(5, get_item_key({'a': 5}, 'a'))
27 self.assertEquals({'b': 9}, get_item_key({'a': {'b': 9}}, 'a'))
30 class item_parse_info_unittest(cli_mock.cli_unittest):
31 def __test_parsing_flist_bad(self, options):
32 parse_info = topic_common.item_parse_info
33 test_parse_info = parse_info(attribute_name='testing',
34 filename_option='flist')
35 self.assertRaises(topic_common.CliError,
36 test_parse_info.get_values, options, [])
39 def __test_parsing_flist_good(self, options, expected):
40 parse_info = topic_common.item_parse_info
41 test_parse_info = parse_info(attribute_name='testing',
42 filename_option='flist')
43 result, leftover = test_parse_info.get_values(options, [])
45 self.assertEqualNoOrder(expected, result)
46 os.unlink(options.flist)
49 def __test_parsing_inline_good(self, options, expected):
50 parse_info = topic_common.item_parse_info
51 test_parse_info = parse_info(attribute_name='testing',
52 inline_option='inline')
53 result, leftover = test_parse_info.get_values(options, [])
55 self.assertEqualNoOrder(expected, result)
58 def __test_parsing_leftover_good(self, leftover, expected):
59 class opt(object):
60 pass
61 parse_info = topic_common.item_parse_info
62 test_parse_info = parse_info(attribute_name='testing',
63 inline_option='inline',
64 use_leftover=True)
65 result, leftover = test_parse_info.get_values(opt(), leftover)
67 self.assertEqualNoOrder(expected, result)
70 def __test_parsing_all_good(self, options, leftover, expected):
71 parse_info = topic_common.item_parse_info
72 test_parse_info = parse_info(attribute_name='testing',
73 inline_option='inline',
74 filename_option='flist',
75 use_leftover=True)
76 result, leftover = test_parse_info.get_values(options, leftover)
78 self.assertEqualNoOrder(expected, result)
79 os.unlink(options.flist)
82 def __test_parsing_all_bad(self, options, leftover):
83 parse_info = topic_common.item_parse_info
84 test_parse_info = parse_info(attribute_name='testing',
85 inline_option='inline',
86 filename_option='flist',
87 use_leftover=True)
88 self.assertRaises(topic_common.CliError,
89 test_parse_info.get_values, options, leftover)
92 def test_file_list_wrong_file(self):
93 class opt(object):
94 flist = './does_not_exist'
95 self.__test_parsing_flist_bad(opt())
98 def test_file_list_empty_file(self):
99 class opt(object):
100 flist_obj = cli_mock.create_file('')
101 flist = flist_obj.name
102 self.__test_parsing_flist_bad(opt())
105 def test_file_list_ok(self):
106 class opt(object):
107 flist_obj = cli_mock.create_file('a\nb\nc\n')
108 flist = flist_obj.name
109 self.__test_parsing_flist_good(opt(), ['a', 'b', 'c'])
112 def test_file_list_one_line_space(self):
113 class opt(object):
114 flist_obj = cli_mock.create_file('a b c\nd e\nf\n')
115 flist = flist_obj.name
116 self.__test_parsing_flist_good(opt(), ['a', 'b', 'c', 'd', 'e', 'f'])
119 def test_file_list_one_line_comma(self):
120 class opt(object):
121 flist_obj = cli_mock.create_file('a,b,c\nd,e\nf\n')
122 flist = flist_obj.name
123 self.__test_parsing_flist_good(opt(), ['a', 'b', 'c', 'd', 'e', 'f'])
126 def test_file_list_one_line_mix(self):
127 class opt(object):
128 flist_obj = cli_mock.create_file('a,b c\nd,e\nf\ng h,i')
129 flist = flist_obj.name
130 self.__test_parsing_flist_good(opt(), ['a', 'b', 'c', 'd', 'e',
131 'f', 'g', 'h', 'i'])
134 def test_file_list_one_line_comma_space(self):
135 class opt(object):
136 flist_obj = cli_mock.create_file('a, b c\nd,e\nf\ng h,i')
137 flist = flist_obj.name
138 self.__test_parsing_flist_good(opt(), ['a', 'b', 'c', 'd', 'e',
139 'f', 'g', 'h', 'i'])
142 def test_file_list_line_end_comma_space(self):
143 class opt(object):
144 flist_obj = cli_mock.create_file('a, b c\nd,e, \nf,\ng h,i ,')
145 flist = flist_obj.name
146 self.__test_parsing_flist_good(opt(), ['a', 'b', 'c', 'd', 'e',
147 'f', 'g', 'h', 'i'])
150 def test_file_list_no_eof(self):
151 class opt(object):
152 flist_obj = cli_mock.create_file('a\nb\nc')
153 flist = flist_obj.name
154 self.__test_parsing_flist_good(opt(), ['a', 'b', 'c'])
157 def test_file_list_blank_line(self):
158 class opt(object):
159 flist_obj = cli_mock.create_file('\na\nb\n\nc\n')
160 flist = flist_obj.name
161 self.__test_parsing_flist_good(opt(), ['a', 'b', 'c'])
164 def test_file_list_escaped_commas(self):
165 class opt(object):
166 flist_obj = cli_mock.create_file('a\nb\\,c\\,d\nef\\,g')
167 flist = flist_obj.name
168 self.__test_parsing_flist_good(opt(), ['a', 'b,c,d', 'ef,g'])
171 def test_file_list_escaped_commas_slashes(self):
172 class opt(object):
173 flist_obj = cli_mock.create_file('a\nb\\\\\\,c\\,d\nef\\\\,g')
174 flist = flist_obj.name
175 self.__test_parsing_flist_good(opt(), ['a', 'b\\,c,d', 'ef\\', 'g'])
178 def test_file_list_opt_list_one(self):
179 class opt(object):
180 inline = 'a'
181 self.__test_parsing_inline_good(opt(), ['a'])
184 def test_file_list_opt_list_space(self):
185 class opt(object):
186 inline = 'a b c'
187 self.__test_parsing_inline_good(opt(), ['a', 'b', 'c'])
190 def test_file_list_opt_list_mix_space_comma(self):
191 class opt(object):
192 inline = 'a b,c,d e'
193 self.__test_parsing_inline_good(opt(), ['a', 'b', 'c', 'd', 'e'])
196 def test_file_list_opt_list_mix_comma_space(self):
197 class opt(object):
198 inline = 'a b,c, d e'
199 self.__test_parsing_inline_good(opt(), ['a', 'b', 'c', 'd', 'e'])
202 def test_file_list_opt_list_end_comma_space(self):
203 class opt(object):
204 inline = 'a b, ,c,, d e, '
205 self.__test_parsing_inline_good(opt(), ['a', 'b', 'c', 'd', 'e'])
208 def test_file_list_opt_list_escaped_commas(self):
209 class opt(object):
210 inline = 'a\\,b,c, d'
211 self.__test_parsing_inline_good(opt(), ['a,b', 'c', 'd'])
214 def test_file_list_opt_list_escaped_commas_slashes(self):
215 class opt(object):
216 inline = 'a\\,b\\\\\\,c,d,e'
217 self.__test_parsing_inline_good(opt(), ['a,b\\,c', 'd', 'e'])
220 def test_file_list_add_on_space(self):
221 self.__test_parsing_leftover_good(['a','c','b'],
222 ['a', 'b', 'c'])
225 def test_file_list_add_on_mix_space_comma(self):
226 self.__test_parsing_leftover_good(['a', 'c','b,d'],
227 ['a', 'b', 'c', 'd'])
230 def test_file_list_add_on_mix_comma_space(self):
231 self.__test_parsing_leftover_good(['a', 'c', 'b,', 'd'],
232 ['a', 'b', 'c', 'd'])
235 def test_file_list_add_on_end_comma_space(self):
236 self.__test_parsing_leftover_good(['a', 'c', 'b,', 'd,', ','],
237 ['a', 'b', 'c', 'd'])
240 def test_file_list_add_on_escaped_commas(self):
241 self.__test_parsing_leftover_good(['a', 'c', 'b,', 'd\\,e\\,f'],
242 ['a', 'b', 'c', 'd,e,f'])
245 def test_file_list_add_on_escaped_commas_slashes(self):
246 self.__test_parsing_leftover_good(['a', 'c', 'b,', 'd\\\\\\,e,f'],
247 ['a', 'b', 'c', 'd\\,e', 'f'])
250 def test_file_list_all_opt(self):
251 class opt(object):
252 flist_obj = cli_mock.create_file('f\ng\nh\n')
253 flist = flist_obj.name
254 inline = 'a b,c,d e'
255 self.__test_parsing_all_good(opt(), ['i', 'j'],
256 ['a', 'b', 'c', 'd', 'e',
257 'f', 'g', 'h', 'i', 'j'])
260 def test_file_list_all_opt_empty_file(self):
261 class opt(object):
262 flist_obj = cli_mock.create_file('')
263 flist = flist_obj.name
264 inline = 'a b,c,d e'
265 self.__test_parsing_all_bad(opt(), ['i', 'j'])
268 def test_file_list_all_opt_in_common(self):
269 class opt(object):
270 flist_obj = cli_mock.create_file('f\nc\na\n')
271 flist = flist_obj.name
272 inline = 'a b,c,d e'
273 self.__test_parsing_all_good(opt(), ['i','j,d'],
274 ['a', 'b', 'c', 'd', 'e', 'f', 'i', 'j'])
277 def test_file_list_all_opt_in_common_space(self):
278 class opt(object):
279 flist_obj = cli_mock.create_file('a b c\nd,e\nf\ng')
280 flist = flist_obj.name
281 inline = 'a b,c,d h'
282 self.__test_parsing_all_good(opt(), ['i','j,d'],
283 ['a', 'b', 'c', 'd', 'e',
284 'f', 'g', 'h', 'i', 'j'])
287 def test_file_list_all_opt_in_common_weird(self):
288 class opt(object):
289 flist_obj = cli_mock.create_file('a b c\nd,e\nf\ng, \n, ,,')
290 flist = flist_obj.name
291 inline = 'a b,c,d h, , ,, '
292 self.__test_parsing_all_good(opt(), ['i','j,d'],
293 ['a', 'b', 'c', 'd', 'e',
294 'f', 'g', 'h', 'i', 'j'])
297 def test_file_list_all_opt_in_common_escaped_commas(self):
298 class opt(object):
299 flist_obj = cli_mock.create_file('a\\,b\\,c\nd,e\nf\ng')
300 flist = flist_obj.name
301 inline = 'a\\,b\\,c,d h'
302 self.__test_parsing_all_good(opt(), ['i','j,d'],
303 ['a,b,c', 'd', 'e', 'f', 'g', 'h',
304 'i', 'j'])
307 def test_file_list_all_opt_in_common_escaped_commas_slashes(self):
308 class opt(object):
309 flist_obj = cli_mock.create_file('a\\,b\\\\\\,c\nd,e\nf,ghi, ,, j,')
310 flist = flist_obj.name
311 inline = 'a\\,b\\\\\\,c,d h,ijk'
312 self.__test_parsing_all_good(opt(), ['i','j,d'],
313 ['a,b\\,c', 'd', 'e', 'f', 'ghi', 'h',
314 'i', 'j', 'ijk'])
317 class atest_unittest(cli_mock.cli_unittest):
318 def setUp(self):
319 super(atest_unittest, self).setUp()
320 self.atest = topic_common.atest()
321 self.atest.afe = rpc.afe_comm()
322 if 'AUTOTEST_WEB' in os.environ:
323 del os.environ['AUTOTEST_WEB']
326 def tearDown(self):
327 self.atest = None
328 super(atest_unittest, self).tearDown()
331 def test_invalid_arg_kill(self):
332 self.atest.kill_on_failure = True
333 self.god.mock_io()
334 sys.exit.expect_call(1).and_raises(cli_mock.ExitException)
335 self.assertRaises(cli_mock.ExitException,
336 self.atest.invalid_arg, 'This is bad')
337 (output, err) = self.god.unmock_io()
338 self.god.check_playback()
339 self.assert_(err.find('This is bad') >= 0)
342 def test_invalid_arg_continue(self):
343 self.god.mock_io()
344 self.atest.invalid_arg('This is sort of ok')
345 (output, err) = self.god.unmock_io()
346 self.assert_(err.find('This is sort of ok') >= 0)
349 def test_failure_continue(self):
350 self.atest.failure('This is partly bad', item='item0',
351 what_failed='something important')
352 err = self.atest.failed['something important']
353 self.assert_('This is partly bad' in err.keys())
356 def test_failure_continue_multiple_different_errors(self):
357 self.atest.failure('This is partly bad', item='item0',
358 what_failed='something important')
359 self.atest.failure('This is really bad', item='item0',
360 what_failed='something really important')
361 err = self.atest.failed['something important']
362 self.assert_('This is partly bad' in err)
363 self.assert_('This is really bad' not in err)
364 err = self.atest.failed['something really important']
365 self.assert_('This is partly bad' not in err)
366 self.assert_('This is really bad' in err)
369 def test_failure_continue_multiple_same_errors(self):
370 self.atest.failure('This is partly bad', item='item0',
371 what_failed='something important')
372 self.atest.failure('This is really bad', item='item1',
373 what_failed='something important')
374 errs = self.atest.failed['something important']
375 self.assert_('This is partly bad' in errs)
376 self.assert_('This is really bad' in errs)
377 self.assert_(set(['item0']) in errs.values())
378 self.assert_(set(['item1']) in errs.values())
381 def test_failure_continue_multiple_errors_mixed(self):
382 self.atest.failure('This is partly bad', item='item0',
383 what_failed='something important')
384 self.atest.failure('This is really bad', item='item0',
385 what_failed='something really important')
386 self.atest.failure('This is really bad', item='item1',
387 what_failed='something important')
388 errs = self.atest.failed['something important']
389 self.assert_('This is partly bad' in errs)
390 self.assert_('This is really bad' in errs)
391 self.assert_(set(['item0']) in errs.values())
392 self.assert_(set(['item1']) in errs.values())
394 errs = self.atest.failed['something really important']
395 self.assert_('This is really bad' in errs)
396 self.assert_('This is partly bad' not in errs)
397 self.assert_(set(['item0']) in errs.values())
398 self.assert_(set(['item1']) not in errs.values())
401 def test_failure_continue_multiple_errors_mixed_same_error(self):
402 self.atest.failure('This is partly bad', item='item0',
403 what_failed='something important')
404 self.atest.failure('This is really bad', item='item0',
405 what_failed='something really important')
406 self.atest.failure('This is partly bad', item='item1',
407 what_failed='something important')
408 errs = self.atest.failed['something important']
409 self.assert_('This is partly bad' in errs)
410 self.assert_('This is really bad' not in errs)
411 self.assert_(set(['item0', 'item1']) in errs.values())
413 errs = self.atest.failed['something really important']
414 self.assert_('This is really bad' in errs)
415 self.assert_('This is partly bad' not in errs)
416 self.assert_(set(['item0']) in errs.values())
417 self.assert_(set(['item1']) not in errs.values())
420 def test_failure_exit(self):
421 self.atest.kill_on_failure = True
422 self.god.mock_io()
423 sys.exit.expect_call(1).and_raises(cli_mock.ExitException)
424 self.assertRaises(cli_mock.ExitException,
425 self.atest.failure, 'This is partly bad')
426 (output, err) = self.god.unmock_io()
427 self.god.check_playback()
428 self.assert_(err.find('This is partly bad') >= 0)
431 def test_failure_exit_item(self):
432 self.atest.kill_on_failure = True
433 self.god.mock_io()
434 sys.exit.expect_call(1).and_raises(cli_mock.ExitException)
435 self.assertRaises(cli_mock.ExitException,
436 self.atest.failure, 'This is partly bad',
437 item='item0')
438 (output, err) = self.god.unmock_io()
439 self.god.check_playback()
440 self.assertWords(err, ['This is partly bad'], ['item0'])
443 def test_show_all_failures_common(self):
444 self.atest.failure('This is partly bad', item='item0',
445 what_failed='something important')
446 self.atest.failure('This is partly bad', item='item1',
447 what_failed='something important')
448 self.god.mock_io()
449 self.atest.show_all_failures()
450 (output, err) = self.god.unmock_io()
451 self.assertWords(err, ['something important',
452 'This is partly bad', 'item0', 'item1'])
455 def test_parse_add_on(self):
456 flist = cli_mock.create_file('host1\nhost2\nleft2')
457 sys.argv = ['atest', '--web', 'fooweb', '--parse',
458 '--kill-on-failure', 'left1', 'left2', '-M', flist.name]
459 self.atest.parser.add_option('-M', '--mlist', type='string')
460 item_info = topic_common.item_parse_info(attribute_name='hosts',
461 filename_option='mlist',
462 use_leftover=True)
463 (options, leftover) = self.atest.parse([item_info])
464 self.assertEqualNoOrder(self.atest.hosts,
465 ['left1', 'left2', 'host1', 'host2'])
467 self.assertEqual({'mlist': flist.name,
468 'web_server': 'fooweb',
469 'parse': True,
470 'parse_delim': '|',
471 'kill_on_failure': True,
472 'verbose': False,
473 'debug': False}, options)
474 self.assertEqual(leftover, [])
475 flist.clean()
478 def test_parse_no_add_on(self):
479 flist = cli_mock.create_file('host1\nhost2\nleft2')
480 sys.argv = ['atest', '--web', 'fooweb', '--parse', '-g',
481 '--kill-on-failure', 'left1', 'left2', '-M', flist.name]
482 self.atest.parser.add_option('-M', '--mlist', type='string')
483 item_info = topic_common.item_parse_info(attribute_name='hosts',
484 filename_option='mlist')
485 (options, leftover) = self.atest.parse([item_info])
486 self.assertEqualNoOrder(self.atest.hosts,
487 ['left2', 'host1', 'host2'])
489 self.assertEqual({'mlist': flist.name,
490 'web_server': 'fooweb',
491 'parse': True,
492 'parse_delim': '|',
493 'kill_on_failure': True,
494 'verbose': False,
495 'debug': True}, options)
496 self.assertEqual(leftover, ['left1', 'left2'])
497 flist.clean()
500 def test_parse_add_on_first(self):
501 flist = cli_mock.create_file('host1\nhost2\nleft2')
502 ulist = cli_mock.create_file('user1\nuser2\nuser3\n')
503 sys.argv = ['atest', '-g', '--parse', '--ulist', ulist.name,
504 '-u', 'myuser,youruser',
505 '--kill-on-failure', 'left1', 'left2', '-M', flist.name]
506 self.atest.parser.add_option('-M', '--mlist', type='string')
507 self.atest.parser.add_option('-U', '--ulist', type='string')
508 self.atest.parser.add_option('-u', '--user', type='string')
509 host_info = topic_common.item_parse_info(attribute_name='hosts',
510 filename_option='mlist',
511 use_leftover=True)
512 user_info = topic_common.item_parse_info(attribute_name='users',
513 inline_option='user',
514 filename_option='ulist')
516 (options, leftover) = self.atest.parse([host_info, user_info])
517 self.assertEqualNoOrder(self.atest.hosts,
518 ['left1', 'left2', 'host1', 'host2'])
519 self.assertEqualNoOrder(self.atest.users,
520 ['user1', 'user2', 'user3',
521 'myuser', 'youruser'])
523 self.assertEqual({'mlist': flist.name,
524 'ulist': ulist.name,
525 'user': 'myuser,youruser',
526 'web_server': None,
527 'parse': True,
528 'parse_delim': '|',
529 'kill_on_failure': True,
530 'verbose': False,
531 'debug': True}, options)
532 self.assertEqual(leftover, [])
533 flist.clean()
534 ulist.clean()
537 def test_parse_add_on_second(self):
538 flist = cli_mock.create_file('host1\nhost2\nleft2')
539 ulist = cli_mock.create_file('user1\nuser2\nuser3\n')
540 sys.argv = ['atest', '-g', '--parse', '-U', ulist.name,
541 '-u', 'myuser,youruser',
542 '--kill-on-failure', 'left1', 'left2', '-M', flist.name]
543 self.atest.parser.add_option('-M', '--mlist', type='string')
544 self.atest.parser.add_option('-U', '--ulist', type='string')
545 self.atest.parser.add_option('-u', '--user', type='string')
546 host_info = topic_common.item_parse_info(attribute_name='hosts',
547 filename_option='mlist',
548 use_leftover=True)
549 user_info = topic_common.item_parse_info(attribute_name='users',
550 inline_option='user',
551 filename_option='ulist')
552 (options, leftover) = self.atest.parse([host_info, user_info])
554 self.assertEqualNoOrder(self.atest.hosts,
555 ['left1', 'left2', 'host1', 'host2'])
556 self.assertEqualNoOrder(self.atest.users,
557 ['user1', 'user2', 'user3',
558 'myuser', 'youruser'])
560 self.assertEqual({'mlist': flist.name,
561 'ulist': ulist.name,
562 'user': 'myuser,youruser',
563 'web_server': None,
564 'parse': True,
565 'parse_delim': '|',
566 'kill_on_failure': True,
567 'verbose': False,
568 'debug': True}, options)
569 self.assertEqual(leftover, [])
570 flist.clean()
571 ulist.clean()
574 def test_parse_all_opts(self):
575 flist = cli_mock.create_file('host1\nhost2\nleft2')
576 ulist = cli_mock.create_file('user1\nuser2\nuser3\n')
577 sys.argv = ['atest', '-g', '--parse', '--ulist', ulist.name,
578 '-u', 'myuser,youruser',
579 '--kill-on-failure', '-M', flist.name, 'left1', 'left2']
580 self.atest.parser.add_option('-M', '--mlist', type='string')
581 self.atest.parser.add_option('-U', '--ulist', type='string')
582 self.atest.parser.add_option('-u', '--user', type='string')
583 host_info = topic_common.item_parse_info(attribute_name='hosts',
584 filename_option='mlist',
585 use_leftover=True)
586 user_info = topic_common.item_parse_info(attribute_name='users',
587 inline_option='user',
588 filename_option='ulist')
589 (options, leftover) = self.atest.parse([host_info, user_info])
590 self.assertEqualNoOrder(self.atest.hosts,
591 ['left1', 'left2', 'host1', 'host2'])
592 self.assertEqualNoOrder(self.atest.users,
593 ['user1', 'user2', 'user3',
594 'myuser', 'youruser'])
596 self.assertEqual({'mlist': flist.name,
597 'ulist': ulist.name,
598 'user': 'myuser,youruser',
599 'web_server': None,
600 'parse': True,
601 'parse_delim': '|',
602 'kill_on_failure': True,
603 'verbose': False,
604 'debug': True}, options)
605 self.assertEqual(leftover, [])
606 flist.clean()
607 ulist.clean()
610 def test_parse_no_add_on(self):
611 flist = cli_mock.create_file('host1\nhost2\nleft2')
612 ulist = cli_mock.create_file('user1\nuser2\nuser3\n')
613 sys.argv = ['atest', '-U', ulist.name,
614 '--kill-on-failure', '-M', flist.name]
615 self.atest.parser.add_option('-M', '--mlist', type='string')
616 self.atest.parser.add_option('-U', '--ulist', type='string')
617 self.atest.parser.add_option('-u', '--user', type='string')
618 host_info = topic_common.item_parse_info(attribute_name='hosts',
619 filename_option='mlist',
620 use_leftover=True)
621 user_info = topic_common.item_parse_info(attribute_name='users',
622 inline_option='user',
623 filename_option='ulist')
624 (options, leftover) = self.atest.parse([host_info, user_info])
625 self.assertEqualNoOrder(self.atest.hosts,
626 ['left2', 'host1', 'host2'])
627 self.assertEqualNoOrder(self.atest.users,
628 ['user1', 'user2', 'user3'])
630 self.assertEqual({'mlist': flist.name,
631 'ulist': ulist.name,
632 'user': None,
633 'web_server': None,
634 'parse': False,
635 'parse_delim': '|',
636 'kill_on_failure': True,
637 'verbose': False,
638 'debug': False}, options)
639 self.assertEqual(leftover, [])
640 flist.clean()
641 ulist.clean()
644 def test_parse_no_flist_add_on(self):
645 sys.argv = ['atest', '-g', '--parse', '-u', 'myuser,youruser',
646 '--kill-on-failure', 'left1', 'left2']
647 self.atest.parser.add_option('-M', '--mlist', type='string')
648 self.atest.parser.add_option('-U', '--ulist', type='string')
649 self.atest.parser.add_option('-u', '--user', type='string')
650 host_info = topic_common.item_parse_info(attribute_name='hosts',
651 use_leftover=True)
652 user_info = topic_common.item_parse_info(attribute_name='users',
653 inline_option='user')
654 (options, leftover) = self.atest.parse([host_info, user_info])
655 self.assertEqualNoOrder(self.atest.hosts,
656 ['left1', 'left2'])
657 self.assertEqualNoOrder(self.atest.users,
658 ['myuser', 'youruser'])
660 self.assertEqual({'mlist': None,
661 'ulist': None,
662 'user': 'myuser,youruser',
663 'web_server': None,
664 'parse': True,
665 'parse_delim': '|',
666 'kill_on_failure': True,
667 'verbose': False,
668 'debug': True}, options)
669 self.assertEqual(leftover, [])
672 def test_parse_no_flist_no_add_on(self):
673 sys.argv = ['atest', '-u', 'myuser,youruser', '--kill-on-failure',
674 '-a', 'acl1,acl2']
675 self.atest.parser.add_option('-u', '--user', type='string')
676 self.atest.parser.add_option('-a', '--acl', type='string')
677 acl_info = topic_common.item_parse_info(attribute_name='acls',
678 inline_option='acl')
679 user_info = topic_common.item_parse_info(attribute_name='users',
680 inline_option='user')
681 (options, leftover) = self.atest.parse([user_info, acl_info])
682 self.assertEqualNoOrder(self.atest.acls,
683 ['acl1', 'acl2'])
684 self.assertEqualNoOrder(self.atest.users,
685 ['myuser', 'youruser'])
687 self.assertEqual({'user': 'myuser,youruser',
688 'acl': 'acl1,acl2',
689 'web_server': None,
690 'parse': False,
691 'parse_delim': '|',
692 'kill_on_failure': True,
693 'verbose': False,
694 'debug': False}, options)
695 self.assertEqual(leftover, [])
698 def test_parse_req_items_ok(self):
699 sys.argv = ['atest', '-u', 'myuser,youruser']
700 self.atest.parser.add_option('-u', '--user', type='string')
701 user_info = topic_common.item_parse_info(attribute_name='users',
702 inline_option='user')
703 (options, leftover) = self.atest.parse([user_info],
704 req_items='users')
705 self.assertEqualNoOrder(self.atest.users,
706 ['myuser', 'youruser'])
708 self.assertEqual({'user': 'myuser,youruser',
709 'web_server': None,
710 'parse': False,
711 'parse_delim': '|',
712 'kill_on_failure': False,
713 'verbose': False,
714 'debug': False}, options)
715 self.assertEqual(leftover, [])
718 def test_parse_req_items_missing(self):
719 sys.argv = ['atest', '-u', 'myuser,youruser', '--kill-on-failure']
720 self.atest.parser.add_option('-u', '--user', type='string')
721 acl_info = topic_common.item_parse_info(attribute_name='acls',
722 inline_option='acl')
723 user_info = topic_common.item_parse_info(attribute_name='users',
724 inline_option='user')
725 self.god.mock_io()
726 sys.exit.expect_call(1).and_raises(cli_mock.ExitException)
727 self.assertRaises(cli_mock.ExitException,
728 self.atest.parse,
729 [user_info, acl_info],
730 'acls')
731 self.assertEqualNoOrder(self.atest.users,
732 ['myuser', 'youruser'])
734 self.assertEqualNoOrder(self.atest.acls, [])
735 self.god.check_playback()
736 self.god.unmock_io()
739 def test_parse_bad_option(self):
740 sys.argv = ['atest', '--unknown']
741 self.god.stub_function(self.atest.parser, 'error')
742 self.atest.parser.error.expect_call('no such option: --unknown').and_return(None)
743 self.atest.parse()
744 self.god.check_playback()
747 def test_parse_all_set(self):
748 sys.argv = ['atest', '--web', 'fooweb', '--parse', '--debug',
749 '--kill-on-failure', '--verbose', 'left1', 'left2',
750 '--parse-delim', '?']
751 (options, leftover) = self.atest.parse()
752 self.assertEqual({'web_server': 'fooweb',
753 'parse': True,
754 'parse_delim': '?',
755 'kill_on_failure': True,
756 'verbose': True,
757 'debug': True}, options)
758 self.assertEqual(leftover, ['left1', 'left2'])
761 def test_execute_rpc_bad_server(self):
762 self.atest.afe = rpc.afe_comm('http://does_not_exist')
763 self.god.mock_io()
764 rpc.afe_comm.run.expect_call('myop').and_raises(urllib2.URLError("<urlopen error (-2, 'Name or service not known')>"))
765 sys.exit.expect_call(1).and_raises(cli_mock.ExitException)
766 self.assertRaises(cli_mock.ExitException,
767 self.atest.execute_rpc, 'myop')
768 (output, err) = self.god.unmock_io()
769 self.god.check_playback()
770 self.assert_(err.find('http://does_not_exist') >= 0)
774 # Print Unit tests
776 def __test_print_fields(self, func, expected, **dargs):
777 if not dargs.has_key('items'):
778 dargs['items']=[{'hostname': 'h0',
779 'platform': 'p0',
780 'labels': [u'l0', u'l1'],
781 'locked': 1,
782 'id': 'id0',
783 'name': 'name0'},
784 {'hostname': 'h1',
785 'platform': 'p1',
786 'labels': [u'l2', u'l3'],
787 'locked': 0,
788 'id': 'id1',
789 'name': 'name1'}]
790 self.god.mock_io()
791 func(**dargs)
792 (output, err) = self.god.unmock_io()
793 self.assertEqual(expected, output)
797 # Print fields Standard
799 def __test_print_fields_std(self, keys, expected):
800 self.__test_print_fields(self.atest.print_fields_std,
801 expected, keys=keys)
804 def test_print_fields_std_one_str(self):
805 self.__test_print_fields_std(['hostname'],
806 'Host: h0\n'
807 'Host: h1\n')
810 def test_print_fields_std_conv_bool(self):
811 """Make sure the conversion functions are called"""
812 self.__test_print_fields_std(['locked'],
813 'Locked: True\n'
814 'Locked: False\n')
817 def test_print_fields_std_conv_label(self):
818 """Make sure the conversion functions are called"""
819 self.__test_print_fields_std(['labels'],
820 'Labels: l0, l1\n'
821 'Labels: l2, l3\n')
824 def test_print_fields_std_all_fields(self):
825 """Make sure the conversion functions are called"""
826 self.__test_print_fields_std(['hostname', 'platform','locked'],
827 'Host: h0\n'
828 'Platform: p0\n'
829 'Locked: True\n'
830 'Host: h1\n'
831 'Platform: p1\n'
832 'Locked: False\n')
836 # Print fields parse
838 def __test_print_fields_parse(self, keys, expected):
839 self.__test_print_fields(self.atest.print_fields_parse,
840 expected, keys=keys)
843 def test_print_fields_parse_one_str(self):
844 self.__test_print_fields_parse(['hostname'],
845 'Host=h0\n'
846 'Host=h1\n')
849 def test_print_fields_parse_conv_bool(self):
850 self.__test_print_fields_parse(['locked'],
851 'Locked=True\n'
852 'Locked=False\n')
855 def test_print_fields_parse_conv_label(self):
856 self.__test_print_fields_parse(['labels'],
857 'Labels=l0, l1\n'
858 'Labels=l2, l3\n')
861 def test_print_fields_parse_all_fields(self):
862 self.__test_print_fields_parse(['hostname', 'platform', 'locked'],
863 'Host=h0|Platform=p0|'
864 'Locked=True\n'
865 'Host=h1|Platform=p1|'
866 'Locked=False\n')
870 # Print table standard
872 def __test_print_table_std(self, keys, expected):
873 self.__test_print_fields(self.atest.print_table_std,
874 expected, keys_header=keys)
877 def test_print_table_std_all_fields(self):
878 self.__test_print_table_std(['hostname', 'platform','locked'],
879 'Host Platform Locked\n'
880 'h0 p0 True\n'
881 'h1 p1 False\n')
883 # TODO JME - add long fields tests
887 # Print table parse
889 def __test_print_table_parse(self, keys, expected):
890 self.__test_print_fields(self.atest.print_table_parse,
891 expected, keys_header=keys)
894 def test_print_table_parse_all_fields(self):
895 self.__test_print_table_parse(['hostname', 'platform',
896 'locked'],
897 'Host=h0|Platform=p0|Locked=True\n'
898 'Host=h1|Platform=p1|Locked=False\n')
901 def test_print_table_parse_all_fields(self):
902 self.atest.parse_delim = '?'
903 self.__test_print_table_parse(['hostname', 'platform',
904 'locked'],
905 'Host=h0?Platform=p0?Locked=True\n'
906 'Host=h1?Platform=p1?Locked=False\n')
909 def test_print_table_parse_empty_fields(self):
910 self.__test_print_fields(self.atest.print_table_parse,
911 'Host=h0|Platform=p0\n'
912 'Host=h1|Platform=p1|Labels=l2, l3\n',
913 items=[{'hostname': 'h0',
914 'platform': 'p0',
915 'labels': [],
916 'locked': 1,
917 'id': 'id0',
918 'name': 'name0'},
919 {'hostname': 'h1',
920 'platform': 'p1',
921 'labels': [u'l2', u'l3'],
922 'locked': 0,
923 'id': 'id1',
924 'name': 'name1'}],
925 keys_header=['hostname', 'platform',
926 'labels'])
930 # Print mix table standard
932 def __test_print_mix_table_std(self, keys_header, sublist_keys,
933 expected):
934 self.__test_print_fields(self.atest.print_table_std,
935 expected,
936 keys_header=keys_header,
937 sublist_keys=sublist_keys)
940 def test_print_mix_table(self):
941 self.__test_print_mix_table_std(['name', 'hostname'], [],
942 'Name Host\n'
943 'name0 h0\n'
944 'name1 h1\n')
947 def test_print_mix_table_sublist(self):
948 self.__test_print_mix_table_std(['name', 'hostname'], ['labels'],
949 'Name Host\n'
950 'name0 h0\n'
951 'Labels: \n'
952 '\tl0, l1\n\n\n'
953 'name1 h1\n'
954 'Labels: \n'
955 '\tl2, l3\n\n\n')
959 # Print by ID standard
961 def __test_print_by_ids_std(self, expected):
962 self.__test_print_fields(self.atest.print_by_ids_std,
963 expected)
966 def test_print_by_ids_std_all_fields(self):
967 self.__test_print_by_ids_std('Id Name\n'
968 'id0 name0\n'
969 'id1 name1\n')
973 # Print by ID parse
975 def __test_print_by_ids_parse(self, expected):
976 self.__test_print_fields(self.atest.print_by_ids_parse,
977 expected)
980 def test_print_by_ids_parse_all_fields(self):
981 self.__test_print_by_ids_parse('Id=id0|Name=name0|'
982 'Id=id1|Name=name1\n')
985 if __name__ == '__main__':
986 unittest.main()