3 # Copyright 2008 Google Inc. All Rights Reserved.
7 import unittest
, os
, sys
, StringIO
, urllib2
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
):
61 parse_info
= topic_common
.item_parse_info
62 test_parse_info
= parse_info(attribute_name
='testing',
63 inline_option
='inline',
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',
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',
88 self
.assertRaises(topic_common
.CliError
,
89 test_parse_info
.get_values
, options
, leftover
)
92 def test_file_list_wrong_file(self
):
94 flist
= './does_not_exist'
95 self
.__test
_parsing
_flist
_bad
(opt())
98 def test_file_list_empty_file(self
):
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
):
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
):
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
):
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
):
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',
134 def test_file_list_one_line_comma_space(self
):
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',
142 def test_file_list_line_end_comma_space(self
):
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',
150 def test_file_list_no_eof(self
):
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
):
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
):
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
):
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
):
181 self
.__test
_parsing
_inline
_good
(opt(), ['a'])
184 def test_file_list_opt_list_space(self
):
187 self
.__test
_parsing
_inline
_good
(opt(), ['a', 'b', 'c'])
190 def test_file_list_opt_list_mix_space_comma(self
):
193 self
.__test
_parsing
_inline
_good
(opt(), ['a', 'b', 'c', 'd', 'e'])
196 def test_file_list_opt_list_mix_comma_space(self
):
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
):
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
):
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
):
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'],
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
):
252 flist_obj
= cli_mock
.create_file('f\ng\nh\n')
253 flist
= flist_obj
.name
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
):
262 flist_obj
= cli_mock
.create_file('')
263 flist
= flist_obj
.name
265 self
.__test
_parsing
_all
_bad
(opt(), ['i', 'j'])
268 def test_file_list_all_opt_in_common(self
):
270 flist_obj
= cli_mock
.create_file('f\nc\na\n')
271 flist
= flist_obj
.name
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
):
279 flist_obj
= cli_mock
.create_file('a b c\nd,e\nf\ng')
280 flist
= flist_obj
.name
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
):
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
):
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',
307 def test_file_list_all_opt_in_common_escaped_commas_slashes(self
):
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',
317 class atest_unittest(cli_mock
.cli_unittest
):
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']
328 super(atest_unittest
, self
).tearDown()
331 def test_invalid_arg_kill(self
):
332 self
.atest
.kill_on_failure
= True
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
):
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
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
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',
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')
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',
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',
471 'kill_on_failure': True,
473 'debug': False}, options
)
474 self
.assertEqual(leftover
, [])
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',
493 'kill_on_failure': True,
495 'debug': True}, options
)
496 self
.assertEqual(leftover
, ['left1', 'left2'])
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',
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
,
525 'user': 'myuser,youruser',
529 'kill_on_failure': True,
531 'debug': True}, options
)
532 self
.assertEqual(leftover
, [])
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',
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
,
562 'user': 'myuser,youruser',
566 'kill_on_failure': True,
568 'debug': True}, options
)
569 self
.assertEqual(leftover
, [])
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',
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
,
598 'user': 'myuser,youruser',
602 'kill_on_failure': True,
604 'debug': True}, options
)
605 self
.assertEqual(leftover
, [])
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',
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
,
636 'kill_on_failure': True,
638 'debug': False}, options
)
639 self
.assertEqual(leftover
, [])
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',
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
,
657 self
.assertEqualNoOrder(self
.atest
.users
,
658 ['myuser', 'youruser'])
660 self
.assertEqual({'mlist': None,
662 'user': 'myuser,youruser',
666 'kill_on_failure': True,
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',
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',
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
,
684 self
.assertEqualNoOrder(self
.atest
.users
,
685 ['myuser', 'youruser'])
687 self
.assertEqual({'user': 'myuser,youruser',
692 'kill_on_failure': True,
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
],
705 self
.assertEqualNoOrder(self
.atest
.users
,
706 ['myuser', 'youruser'])
708 self
.assertEqual({'user': 'myuser,youruser',
712 'kill_on_failure': 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',
723 user_info
= topic_common
.item_parse_info(attribute_name
='users',
724 inline_option
='user')
726 sys
.exit
.expect_call(1).and_raises(cli_mock
.ExitException
)
727 self
.assertRaises(cli_mock
.ExitException
,
729 [user_info
, acl_info
],
731 self
.assertEqualNoOrder(self
.atest
.users
,
732 ['myuser', 'youruser'])
734 self
.assertEqualNoOrder(self
.atest
.acls
, [])
735 self
.god
.check_playback()
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)
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',
755 'kill_on_failure': 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')
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)
776 def __test_print_fields(self
, func
, expected
, **dargs
):
777 if not dargs
.has_key('items'):
778 dargs
['items']=[{'hostname': 'h0',
780 'labels': [u
'l0', u
'l1'],
786 'labels': [u
'l2', u
'l3'],
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
,
804 def test_print_fields_std_one_str(self
):
805 self
.__test
_print
_fields
_std
(['hostname'],
810 def test_print_fields_std_conv_bool(self
):
811 """Make sure the conversion functions are called"""
812 self
.__test
_print
_fields
_std
(['locked'],
817 def test_print_fields_std_conv_label(self
):
818 """Make sure the conversion functions are called"""
819 self
.__test
_print
_fields
_std
(['labels'],
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'],
838 def __test_print_fields_parse(self
, keys
, expected
):
839 self
.__test
_print
_fields
(self
.atest
.print_fields_parse
,
843 def test_print_fields_parse_one_str(self
):
844 self
.__test
_print
_fields
_parse
(['hostname'],
849 def test_print_fields_parse_conv_bool(self
):
850 self
.__test
_print
_fields
_parse
(['locked'],
855 def test_print_fields_parse_conv_label(self
):
856 self
.__test
_print
_fields
_parse
(['labels'],
861 def test_print_fields_parse_all_fields(self
):
862 self
.__test
_print
_fields
_parse
(['hostname', 'platform', 'locked'],
863 'Host=h0|Platform=p0|'
865 'Host=h1|Platform=p1|'
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'
883 # TODO JME - add long fields tests
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',
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',
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',
921 'labels': [u
'l2', u
'l3'],
925 keys_header
=['hostname', 'platform',
930 # Print mix table standard
932 def __test_print_mix_table_std(self
, keys_header
, sublist_keys
,
934 self
.__test
_print
_fields
(self
.atest
.print_table_std
,
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'], [],
947 def test_print_mix_table_sublist(self
):
948 self
.__test
_print
_mix
_table
_std
(['name', 'hostname'], ['labels'],
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
,
966 def test_print_by_ids_std_all_fields(self
):
967 self
.__test
_print
_by
_ids
_std
('Id Name\n'
975 def __test_print_by_ids_parse(self
, expected
):
976 self
.__test
_print
_fields
(self
.atest
.print_by_ids_parse
,
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__':