blob: 30882fc6965cca6ef150d8bc0a0fff35e6d5a2c6 [file] [log] [blame]
mblighbe630eb2008-08-01 16:41:48 +00001#!/usr/bin/python
2#
3# Copyright 2008 Google Inc. All Rights Reserved.
4
5"""Test for atest."""
6
7import unittest, os, sys, tempfile, StringIO, urllib2
8
9import common
10from autotest_lib.cli import cli_mock, topic_common, rpc
11from autotest_lib.frontend.afe.json_rpc import proxy
12
13class topic_common_unittest(cli_mock.cli_unittest):
14 def setUp(self):
15 super(topic_common_unittest, self).setUp()
16 self.atest = topic_common.atest()
17 self.atest.afe = rpc.afe_comm()
18 if 'AUTOTEST_WEB' in os.environ:
19 del os.environ['AUTOTEST_WEB']
20
21
22 def tearDown(self):
23 self.atest = None
24 super(topic_common_unittest, self).tearDown()
25
26
27 def test_file_list_wrong_file(self):
28 self.god.mock_io()
29 class opt(object):
30 mlist = './does_not_exist'
31 options = opt()
32 sys.exit.expect_call(1).and_raises(cli_mock.ExitException)
33 self.assertRaises(cli_mock.ExitException,
34 self.atest._file_list, options, opt_file='mlist')
35 self.god.check_playback()
36 (output, err) = self.god.unmock_io()
37 self.assert_(err.find('./does_not_exist') >= 0)
38
39
40 def test_file_list_empty_file(self):
41 self.god.mock_io()
42 class opt(object):
43 flist = cli_mock.create_file('')
44 options = opt()
45 sys.exit.expect_call(1).and_raises(cli_mock.ExitException)
46 self.assertRaises(cli_mock.ExitException,
47 self.atest._file_list, options, opt_file='flist')
48 self.god.check_playback()
49 (output, err) = self.god.unmock_io()
50 self.assert_(err.find(options.flist) >= 0)
51
52
53 def test_file_list_ok(self):
54 class opt(object):
55 filename = cli_mock.create_file('a\nb\nc\n')
56 options = opt()
57 self.assertEqualNoOrder(['a', 'b', 'c'],
58 self.atest._file_list(options,
59 opt_file='filename'))
60 os.unlink(options.filename)
61
62
63 def test_file_list_one_line_space(self):
64 class opt(object):
65 filename = cli_mock.create_file('a b c\nd e\nf\n')
66 options = opt()
67 self.assertEqualNoOrder(['a', 'b', 'c', 'd', 'e', 'f'],
68 self.atest._file_list(options,
69 opt_file='filename'))
70 os.unlink(options.filename)
71
72
73 def test_file_list_one_line_comma(self):
74 class opt(object):
75 filename = cli_mock.create_file('a,b,c\nd,e\nf\n')
76 options = opt()
77 self.assertEqualNoOrder(['a', 'b', 'c', 'd', 'e', 'f'],
78 self.atest._file_list(options,
79 opt_file='filename'))
80 os.unlink(options.filename)
81
82
83 def test_file_list_one_line_mix(self):
84 class opt(object):
85 filename = cli_mock.create_file('a,b c\nd,e\nf\ng h,i')
86 options = opt()
87 self.assertEqualNoOrder(['a', 'b', 'c', 'd', 'e',
88 'f', 'g', 'h', 'i'],
89 self.atest._file_list(options,
90 opt_file='filename'))
91 os.unlink(options.filename)
92
93
mblighf1733342008-09-04 16:45:46 +000094 def test_file_list_one_line_comma_space(self):
95 class opt(object):
96 filename = cli_mock.create_file('a, b c\nd,e\nf\ng h,i')
97 options = opt()
98 self.assertEqualNoOrder(['a', 'b', 'c', 'd', 'e',
99 'f', 'g', 'h', 'i'],
100 self.atest._file_list(options,
101 opt_file='filename'))
102 os.unlink(options.filename)
103
104
105 def test_file_list_line_end_comma_space(self):
106 class opt(object):
107 filename = cli_mock.create_file('a, b c\nd,e, \nf,\ng h,i ,')
108 options = opt()
109 self.assertEqualNoOrder(['a', 'b', 'c', 'd', 'e',
110 'f', 'g', 'h', 'i'],
111 self.atest._file_list(options,
112 opt_file='filename'))
113 os.unlink(options.filename)
114
115
mblighbe630eb2008-08-01 16:41:48 +0000116 def test_file_list_no_eof(self):
117 class opt(object):
118 filename = cli_mock.create_file('a\nb\nc')
119 options = opt()
120 self.assertEqualNoOrder(['a', 'b', 'c'],
121 self.atest._file_list(options,
122 opt_file='filename'))
123 os.unlink(options.filename)
124
125
126 def test_file_list_blank_line(self):
127 class opt(object):
128 filename = cli_mock.create_file('\na\nb\n\nc\n')
129 options = opt()
130 self.assertEqualNoOrder(['a', 'b', 'c'],
131 self.atest._file_list(options,
132 opt_file='filename'))
133 os.unlink(options.filename)
134
135
136 def test_file_list_opt_list_one(self):
137 class opt(object):
138 hlist = 'a'
139 options = opt()
140 self.assertEqualNoOrder(['a'],
141 self.atest._file_list(options,
142 opt_list='hlist'))
143
144
145 def test_file_list_opt_list_space(self):
146 class opt(object):
147 hlist = 'a b c'
148 options = opt()
149 self.assertEqualNoOrder(['a', 'b', 'c'],
150 self.atest._file_list(options,
151 opt_list='hlist'))
152
153
154 def test_file_list_opt_list_mix_space_comma(self):
155 class opt(object):
156 alist = 'a b,c,d e'
157 options = opt()
158 self.assertEqualNoOrder(['a', 'b', 'c', 'd', 'e'],
159 self.atest._file_list(options,
160 opt_list='alist'))
161
162
mblighf1733342008-09-04 16:45:46 +0000163 def test_file_list_opt_list_mix_comma_space(self):
164 class opt(object):
165 alist = 'a b,c, d e'
166 options = opt()
167 self.assertEqualNoOrder(['a', 'b', 'c', 'd', 'e'],
168 self.atest._file_list(options,
169 opt_list='alist'))
170
171
172 def test_file_list_opt_list_end_comma_space(self):
173 class opt(object):
174 alist = 'a b, ,c,, d e, '
175 options = opt()
176 self.assertEqualNoOrder(['a', 'b', 'c', 'd', 'e'],
177 self.atest._file_list(options,
178 opt_list='alist'))
179
180
mblighbe630eb2008-08-01 16:41:48 +0000181 def test_file_list_add_on_space(self):
182 class opt(object):
183 pass
184 options = opt()
185 self.assertEqualNoOrder(['a', 'b', 'c'],
186 self.atest._file_list(options,
187 add_on=['a','c','b']))
188
189
190 def test_file_list_add_on_mix_space_comma(self):
191 class opt(object):
192 pass
193 options = opt()
194 self.assertEqualNoOrder(['a', 'b', 'c', 'd'],
195 self.atest._file_list(options,
196 add_on=['a', 'c',
197 'b,d']))
198
199
mblighf1733342008-09-04 16:45:46 +0000200 def test_file_list_add_on_mix_comma_space(self):
201 class opt(object):
202 pass
203 options = opt()
204 self.assertEqualNoOrder(['a', 'b', 'c', 'd'],
205 self.atest._file_list(options,
206 add_on=['a', 'c',
207 'b,', 'd']))
208
209
210 def test_file_list_add_on_end_comma_space(self):
211 class opt(object):
212 pass
213 options = opt()
214 self.assertEqualNoOrder(['a', 'b', 'c', 'd'],
215 self.atest._file_list(options,
216 add_on=['a', 'c', 'b,',
217 'd,', ',']))
218
219
mblighbe630eb2008-08-01 16:41:48 +0000220 def test_file_list_all_opt(self):
221 class opt(object):
222 afile = cli_mock.create_file('f\ng\nh\n')
223 alist = 'a b,c,d e'
224 options = opt()
225 self.assertEqualNoOrder(['a', 'b', 'c', 'd', 'e',
226 'f', 'g', 'h', 'i', 'j'],
227 self.atest._file_list(options,
228 opt_file='afile',
229 opt_list='alist',
230 add_on=['i', 'j']))
231
232
233 def test_file_list_all_opt_empty_file(self):
234 self.god.mock_io()
235 class opt(object):
236 hfile = cli_mock.create_file('')
237 hlist = 'a b,c,d e'
238 options = opt()
239 sys.exit.expect_call(1).and_raises(cli_mock.ExitException)
240 self.assertRaises(cli_mock.ExitException,
241 self.atest._file_list,
242 options,
243 opt_file='hfile',
244 opt_list='hlist',
245 add_on=['i', 'j'])
246 (output, err) = self.god.unmock_io()
247 self.god.check_playback()
248 self.assert_(err.find(options.hfile) >= 0)
249
250
251 def test_file_list_all_opt_in_common(self):
252 class opt(object):
253 afile = cli_mock.create_file('f\nc\na\n')
254 alist = 'a b,c,d e'
255 options = opt()
256 self.assertEqualNoOrder(['a', 'b', 'c', 'd', 'e',
257 'f', 'i', 'j'],
258 self.atest._file_list(options,
259 opt_file='afile',
260 opt_list='alist',
261 add_on=['i','j,d']))
262
263
264 def test_file_list_all_opt_in_common_space(self):
265 class opt(object):
266 afile = cli_mock.create_file('a b c\nd,e\nf\ng')
267 alist = 'a b,c,d h'
268 options = opt()
269 self.assertEqualNoOrder(['a', 'b', 'c', 'd', 'e',
270 'f', 'g', 'h', 'i', 'j'],
271 self.atest._file_list(options,
272 opt_file='afile',
273 opt_list='alist',
274 add_on=['i','j,d']))
275
276
mblighf1733342008-09-04 16:45:46 +0000277 def test_file_list_all_opt_in_common_weird(self):
278 class opt(object):
279 afile = cli_mock.create_file('a b c\nd,e\nf\ng, \n, ,,')
280 alist = 'a b,c,d h, , ,, '
281 options = opt()
282 self.assertEqualNoOrder(['a', 'b', 'c', 'd', 'e',
283 'f', 'g', 'h', 'i', 'j'],
284 self.atest._file_list(options,
285 opt_file='afile',
286 opt_list='alist',
287 add_on=['i','j,d']))
288
289
mblighbe630eb2008-08-01 16:41:48 +0000290 def test_invalid_arg_kill(self):
291 self.atest.kill_on_failure = True
292 self.god.mock_io()
293 sys.exit.expect_call(1).and_raises(cli_mock.ExitException)
294 self.assertRaises(cli_mock.ExitException,
295 self.atest.invalid_arg, 'This is bad')
296 (output, err) = self.god.unmock_io()
297 self.god.check_playback()
298 self.assert_(err.find('This is bad') >= 0)
299
300
301 def test_invalid_arg_continue(self):
302 self.god.mock_io()
303 self.atest.invalid_arg('This is sort of ok')
304 (output, err) = self.god.unmock_io()
305 self.assert_(err.find('This is sort of ok') >= 0)
306
307
308 def test_failure_continue(self):
309 self.atest.failure('This is partly bad', item='item0',
310 what_failed='something important')
311 err = self.atest.failed['something important']
312 self.assert_('This is partly bad' in err.keys())
313
314
315 def test_failure_continue_multiple_different_errors(self):
316 self.atest.failure('This is partly bad', item='item0',
317 what_failed='something important')
318 self.atest.failure('This is really bad', item='item0',
319 what_failed='something really important')
320 err = self.atest.failed['something important']
321 self.assert_('This is partly bad' in err)
322 self.assert_('This is really bad' not in err)
323 err = self.atest.failed['something really important']
324 self.assert_('This is partly bad' not in err)
325 self.assert_('This is really bad' in err)
326
327
328 def test_failure_continue_multiple_same_errors(self):
329 self.atest.failure('This is partly bad', item='item0',
330 what_failed='something important')
331 self.atest.failure('This is really bad', item='item1',
332 what_failed='something important')
333 errs = self.atest.failed['something important']
334 self.assert_('This is partly bad' in errs)
335 self.assert_('This is really bad' in errs)
336 self.assert_(set(['item0']) in errs.values())
337 self.assert_(set(['item1']) in errs.values())
338
339
340 def test_failure_continue_multiple_errors_mixed(self):
341 self.atest.failure('This is partly bad', item='item0',
342 what_failed='something important')
343 self.atest.failure('This is really bad', item='item0',
344 what_failed='something really important')
345 self.atest.failure('This is really bad', item='item1',
346 what_failed='something important')
347 errs = self.atest.failed['something important']
348 self.assert_('This is partly bad' in errs)
349 self.assert_('This is really bad' in errs)
350 self.assert_(set(['item0']) in errs.values())
351 self.assert_(set(['item1']) in errs.values())
352
353 errs = self.atest.failed['something really important']
354 self.assert_('This is really bad' in errs)
355 self.assert_('This is partly bad' not in errs)
356 self.assert_(set(['item0']) in errs.values())
357 self.assert_(set(['item1']) not in errs.values())
358
359
360 def test_failure_continue_multiple_errors_mixed_same_error(self):
361 self.atest.failure('This is partly bad', item='item0',
362 what_failed='something important')
363 self.atest.failure('This is really bad', item='item0',
364 what_failed='something really important')
365 self.atest.failure('This is partly bad', item='item1',
366 what_failed='something important')
367 errs = self.atest.failed['something important']
368 self.assert_('This is partly bad' in errs)
369 self.assert_('This is really bad' not in errs)
370 self.assert_(set(['item0', 'item1']) in errs.values())
371
372 errs = self.atest.failed['something really important']
373 self.assert_('This is really bad' in errs)
374 self.assert_('This is partly bad' not in errs)
375 self.assert_(set(['item0']) in errs.values())
376 self.assert_(set(['item1']) not in errs.values())
377
378
379 def test_failure_exit(self):
380 self.atest.kill_on_failure = True
381 self.god.mock_io()
382 sys.exit.expect_call(1).and_raises(cli_mock.ExitException)
383 self.assertRaises(cli_mock.ExitException,
384 self.atest.failure, 'This is partly bad')
385 (output, err) = self.god.unmock_io()
386 self.god.check_playback()
387 self.assert_(err.find('This is partly bad') >= 0)
388
389
390 def test_failure_exit_item(self):
391 self.atest.kill_on_failure = True
392 self.god.mock_io()
393 sys.exit.expect_call(1).and_raises(cli_mock.ExitException)
394 self.assertRaises(cli_mock.ExitException,
395 self.atest.failure, 'This is partly bad',
396 item='item0')
397 (output, err) = self.god.unmock_io()
398 self.god.check_playback()
399 self.assertWords(err, ['This is partly bad'], ['item0'])
400
401
402 def test_show_all_failures_common(self):
403 self.atest.failure('This is partly bad', item='item0',
404 what_failed='something important')
405 self.atest.failure('This is partly bad', item='item1',
406 what_failed='something important')
407 self.god.mock_io()
408 self.atest.show_all_failures()
409 (output, err) = self.god.unmock_io()
410 self.assertWords(err, ['something important',
411 'This is partly bad', 'item0', 'item1'])
412
413
414 def test_parse_with_flist_add_on(self):
415 flist = cli_mock.create_file('host1\nhost2\nleft2')
416 sys.argv = ['atest', '--web', 'fooweb', '--parse',
417 '--kill-on-failure', 'left1', 'left2', '-M', flist]
418 self.atest.parser.add_option('-M', '--mlist', type='string')
419 (options, leftover) = self.atest.parse_with_flist([('hosts',
420 'mlist',
421 [],
422 True)],
423 None)
424 self.assertEqualNoOrder(self.atest.hosts,
425 ['left1', 'left2', 'host1', 'host2'])
426
427 self.assertEqual({'mlist': flist,
428 'web_server': 'fooweb',
429 'parse': True,
430 'kill_on_failure': True,
431 'verbose': False,
432 'debug': False}, options)
433 self.assertEqual(leftover, [])
434
435
436 def test_parse_with_flist_no_add_on(self):
437 flist = cli_mock.create_file('host1\nhost2\nleft2')
438 sys.argv = ['atest', '--web', 'fooweb', '--parse', '-g',
439 '--kill-on-failure', 'left1', 'left2', '-M', flist]
440 self.atest.parser.add_option('-M', '--mlist', type='string')
441 (options, leftover) = self.atest.parse_with_flist([('hosts',
442 'mlist',
443 [],
444 False)],
445 None)
446 self.assertEqualNoOrder(self.atest.hosts,
447 ['left2', 'host1', 'host2'])
448
449 self.assertEqual({'mlist': flist,
450 'web_server': 'fooweb',
451 'parse': True,
452 'kill_on_failure': True,
453 'verbose': False,
454 'debug': True}, options)
455 self.assertEqual(leftover, ['left1', 'left2'])
456
457
458 def test_parse_with_flists_add_on_first(self):
459 flist = cli_mock.create_file('host1\nhost2\nleft2')
460 ulist = cli_mock.create_file('user1\nuser2\nuser3\n')
461 sys.argv = ['atest', '-g', '--parse', '--ulist', ulist,
462 '-u', 'myuser,youruser',
463 '--kill-on-failure', 'left1', 'left2', '-M', flist]
464 self.atest.parser.add_option('-M', '--mlist', type='string')
465 self.atest.parser.add_option('-U', '--ulist', type='string')
466 self.atest.parser.add_option('-u', '--user', type='string')
467 (options, leftover) = self.atest.parse_with_flist([('hosts',
468 'mlist',
469 '',
470 True),
471 ('users',
472 'ulist',
473 'user',
474 False)],
475 None)
476 self.assertEqualNoOrder(self.atest.hosts,
477 ['left1', 'left2', 'host1', 'host2'])
478 self.assertEqualNoOrder(self.atest.users,
479 ['user1', 'user2', 'user3',
480 'myuser', 'youruser'])
481
482 self.assertEqual({'mlist': flist,
483 'ulist': ulist,
484 'user': 'myuser,youruser',
485 'web_server': None,
486 'parse': True,
487 'kill_on_failure': True,
488 'verbose': False,
489 'debug': True}, options)
490 self.assertEqual(leftover, [])
491
492
493 def test_parse_with_flists_add_on_second(self):
494 flist = cli_mock.create_file('host1\nhost2\nleft2')
495 ulist = cli_mock.create_file('user1\nuser2\nuser3\n')
496 sys.argv = ['atest', '-g', '--parse', '-U', ulist,
497 '-u', 'myuser,youruser',
498 '--kill-on-failure', 'left1', 'left2', '-M', flist]
499 self.atest.parser.add_option('-M', '--mlist', type='string')
500 self.atest.parser.add_option('-U', '--ulist', type='string')
501 self.atest.parser.add_option('-u', '--user', type='string')
502 (options, leftover) = self.atest.parse_with_flist([('users',
503 'ulist',
504 'user',
505 False),
506 ('hosts',
507 'mlist',
508 '',
509 True)],
510 None)
511 self.assertEqualNoOrder(self.atest.hosts,
512 ['left1', 'left2', 'host1', 'host2'])
513 self.assertEqualNoOrder(self.atest.users,
514 ['user1', 'user2', 'user3',
515 'myuser', 'youruser'])
516
517 self.assertEqual({'mlist': flist,
518 'ulist': ulist,
519 'user': 'myuser,youruser',
520 'web_server': None,
521 'parse': True,
522 'kill_on_failure': True,
523 'verbose': False,
524 'debug': True}, options)
525 self.assertEqual(leftover, [])
526
527
528 def test_parse_with_flists_all_opts(self):
529 flist = cli_mock.create_file('host1\nhost2\nleft2')
530 ulist = cli_mock.create_file('user1\nuser2\nuser3\n')
531 sys.argv = ['atest', '-g', '--parse', '--ulist', ulist,
532 '-u', 'myuser,youruser',
533 '--kill-on-failure', '-M', flist, 'left1', 'left2']
534 self.atest.parser.add_option('-M', '--mlist', type='string')
535 self.atest.parser.add_option('-U', '--ulist', type='string')
536 self.atest.parser.add_option('-u', '--user', type='string')
537 (options, leftover) = self.atest.parse_with_flist([('users',
538 'ulist',
539 'user',
540 False),
541 ('hosts',
542 'mlist',
543 '',
544 True)],
545 None)
546 self.assertEqualNoOrder(self.atest.hosts,
547 ['left1', 'left2', 'host1', 'host2'])
548 self.assertEqualNoOrder(self.atest.users,
549 ['user1', 'user2', 'user3',
550 'myuser', 'youruser'])
551
552 self.assertEqual({'mlist': flist,
553 'ulist': ulist,
554 'user': 'myuser,youruser',
555 'web_server': None,
556 'parse': True,
557 'kill_on_failure': True,
558 'verbose': False,
559 'debug': True}, options)
560 self.assertEqual(leftover, [])
561
562
563 def test_parse_with_flists_no_add_on(self):
564 flist = cli_mock.create_file('host1\nhost2\nleft2')
565 ulist = cli_mock.create_file('user1\nuser2\nuser3\n')
566 sys.argv = ['atest', '-U', ulist,
567 '--kill-on-failure', '-M', flist]
568 self.atest.parser.add_option('-M', '--mlist', type='string')
569 self.atest.parser.add_option('-U', '--ulist', type='string')
570 self.atest.parser.add_option('-u', '--user', type='string')
571 (options, leftover) = self.atest.parse_with_flist([('hosts',
572 'mlist',
573 '',
574 False),
575 ('users',
576 'ulist',
577 'user',
578 False)],
579 None)
580 self.assertEqualNoOrder(self.atest.hosts,
581 ['left2', 'host1', 'host2'])
582 self.assertEqualNoOrder(self.atest.users,
583 ['user1', 'user2', 'user3'])
584
585 self.assertEqual({'mlist': flist,
586 'ulist': ulist,
587 'user': None,
588 'web_server': None,
589 'parse': False,
590 'kill_on_failure': True,
591 'verbose': False,
592 'debug': False}, options)
593 self.assertEqual(leftover, [])
594
595
596 def test_parse_with_flists_no_flist_add_on(self):
597 sys.argv = ['atest', '-g', '--parse', '-u', 'myuser,youruser',
598 '--kill-on-failure', 'left1', 'left2']
599 self.atest.parser.add_option('-M', '--mlist', type='string')
600 self.atest.parser.add_option('-U', '--ulist', type='string')
601 self.atest.parser.add_option('-u', '--user', type='string')
602 (options, leftover) = self.atest.parse_with_flist([('users',
603 '',
604 'user',
605 False),
606 ('hosts',
607 '',
608 '',
609 True)],
610 None)
611 self.assertEqualNoOrder(self.atest.hosts,
612 ['left1', 'left2'])
613 self.assertEqualNoOrder(self.atest.users,
614 ['myuser', 'youruser'])
615
616 self.assertEqual({'mlist': None,
617 'ulist': None,
618 'user': 'myuser,youruser',
619 'web_server': None,
620 'parse': True,
621 'kill_on_failure': True,
622 'verbose': False,
623 'debug': True}, options)
624 self.assertEqual(leftover, [])
625
626
627 def test_parse_with_flists_no_flist_no_add_on(self):
628 sys.argv = ['atest', '-u', 'myuser,youruser', '--kill-on-failure',
629 '-a', 'acl1,acl2']
630 self.atest.parser.add_option('-u', '--user', type='string')
631 self.atest.parser.add_option('-a', '--acl', type='string')
632 (options, leftover) = self.atest.parse_with_flist([('users',
633 '',
634 'user',
635 False),
636 ('acls',
637 '',
638 'acl',
639 False)],
640 None)
641 self.assertEqualNoOrder(self.atest.acls,
642 ['acl1', 'acl2'])
643 self.assertEqualNoOrder(self.atest.users,
644 ['myuser', 'youruser'])
645
646 self.assertEqual({'user': 'myuser,youruser',
647 'acl': 'acl1,acl2',
648 'web_server': None,
649 'parse': False,
650 'kill_on_failure': True,
651 'verbose': False,
652 'debug': False}, options)
653 self.assertEqual(leftover, [])
654
655
656 def test_parse_with_flists_req_items_ok(self):
657 sys.argv = ['atest', '-u', 'myuser,youruser']
658 self.atest.parser.add_option('-u', '--user', type='string')
659 (options, leftover) = self.atest.parse_with_flist([('users',
660 '',
661 'user',
662 False)],
663 'users')
664 self.assertEqualNoOrder(self.atest.users,
665 ['myuser', 'youruser'])
666
667 self.assertEqual({'user': 'myuser,youruser',
668 'web_server': None,
669 'parse': False,
670 'kill_on_failure': False,
671 'verbose': False,
672 'debug': False}, options)
673 self.assertEqual(leftover, [])
674
675
676 def test_parse_with_flists_req_items_missing(self):
677 sys.argv = ['atest', '-u', 'myuser,youruser', '--kill-on-failure']
678 self.atest.parser.add_option('-u', '--user', type='string')
679 self.god.mock_io()
680 sys.exit.expect_call(1).and_raises(cli_mock.ExitException)
681 self.assertRaises(cli_mock.ExitException,
682 self.atest.parse_with_flist,
683 [('users', '', 'user', False),
684 ('acls', '', 'acl', False)],
685 'acls')
686 self.assertEqualNoOrder(self.atest.users,
687 ['myuser', 'youruser'])
688
689 self.assertEqualNoOrder(self.atest.acls, [])
690 self.god.check_playback()
691 self.god.unmock_io()
692
693
694 def test_parse_bad_option(self):
695 sys.argv = ['atest', '--unknown']
696 self.god.stub_function(self.atest.parser, 'error')
697 self.atest.parser.error.expect_call('no such option: --unknown').and_return(None)
698 self.atest.parse()
699 self.god.check_playback()
700
701
702 def test_parse_all_set(self):
703 sys.argv = ['atest', '--web', 'fooweb', '--parse', '--debug',
704 '--kill-on-failure', '--verbose', 'left1', 'left2']
705 (options, leftover) = self.atest.parse()
706 self.assertEqual({'web_server': 'fooweb',
707 'parse': True,
708 'kill_on_failure': True,
709 'verbose': True,
710 'debug': True}, options)
711 self.assertEqual(leftover, ['left1', 'left2'])
712
713
714 def test_execute_rpc_bad_server(self):
715 self.atest.afe = rpc.afe_comm('http://does_not_exist')
716 self.god.mock_io()
717 rpc.afe_comm.run.expect_call('myop').and_raises(urllib2.URLError("<urlopen error (-2, 'Name or service not known')>"))
718 sys.exit.expect_call(1).and_raises(cli_mock.ExitException)
719 self.assertRaises(cli_mock.ExitException,
720 self.atest.execute_rpc, 'myop')
721 (output, err) = self.god.unmock_io()
722 self.god.check_playback()
723 self.assert_(err.find('http://does_not_exist') >= 0)
724
725
726 #
727 # Print Unit tests
728 #
729 def __test_print_fields(self, func, expected, **dargs):
730 if not dargs.has_key('items'):
731 dargs['items']=[{'hostname': 'h0',
732 'platform': 'p0',
733 'labels': [u'l0', u'l1'],
734 'locked': 1,
735 'id': 'id0',
736 'name': 'name0'},
737 {'hostname': 'h1',
738 'platform': 'p1',
739 'labels': [u'l2', u'l3'],
740 'locked': 0,
741 'id': 'id1',
742 'name': 'name1'}]
743 self.god.mock_io()
744 func(**dargs)
745 (output, err) = self.god.unmock_io()
746 self.assertEqual(expected, output)
747
748
749 #
750 # Print fields Standard
751 #
752 def __test_print_fields_std(self, keys, expected):
753 self.__test_print_fields(self.atest.print_fields_std,
754 expected, keys=keys)
755
756
757 def test_print_fields_std_one_str(self):
758 self.__test_print_fields_std(['hostname'],
759 'Host: h0\n'
760 'Host: h1\n')
761
762
763 def test_print_fields_std_conv_bool(self):
764 """Make sure the conversion functions are called"""
765 self.__test_print_fields_std(['locked'],
766 'Locked: True\n'
767 'Locked: False\n')
768
769
770 def test_print_fields_std_conv_label(self):
771 """Make sure the conversion functions are called"""
772 self.__test_print_fields_std(['labels'],
773 'Labels: l0, l1\n'
774 'Labels: l2, l3\n')
775
776
777 def test_print_fields_std_all_fields(self):
778 """Make sure the conversion functions are called"""
779 self.__test_print_fields_std(['hostname', 'platform','locked'],
780 'Host: h0\n'
781 'Platform: p0\n'
782 'Locked: True\n'
783 'Host: h1\n'
784 'Platform: p1\n'
785 'Locked: False\n')
786
787
788 #
789 # Print fields parse
790 #
791 def __test_print_fields_parse(self, keys, expected):
792 self.__test_print_fields(self.atest.print_fields_parse,
793 expected, keys=keys)
794
795
796 def test_print_fields_parse_one_str(self):
797 self.__test_print_fields_parse(['hostname'],
798 'Host=h0\n'
799 'Host=h1\n')
800
801
802 def test_print_fields_parse_conv_bool(self):
803 self.__test_print_fields_parse(['locked'],
804 'Locked=True\n'
805 'Locked=False\n')
806
807
808 def test_print_fields_parse_conv_label(self):
809 self.__test_print_fields_parse(['labels'],
810 'Labels=l0, l1\n'
811 'Labels=l2, l3\n')
812
813
814 def test_print_fields_parse_all_fields(self):
815 self.__test_print_fields_parse(['hostname', 'platform', 'locked'],
816 'Host=h0:Platform=p0:'
817 'Locked=True\n'
818 'Host=h1:Platform=p1:'
819 'Locked=False\n')
820
821
822 #
823 # Print table standard
824 #
825 def __test_print_table_std(self, keys, expected):
826 self.__test_print_fields(self.atest.print_table_std,
827 expected, keys_header=keys)
828
829
830 def test_print_table_std_all_fields(self):
831 self.__test_print_table_std(['hostname', 'platform','locked'],
832 'Host Platform Locked\n'
833 'h0 p0 True\n'
834 'h1 p1 False\n')
835
836 # TODO JME - add long fields tests
837
838
839 #
840 # Print table parse
841 #
842 def __test_print_table_parse(self, keys, expected):
843 self.__test_print_fields(self.atest.print_table_parse,
844 expected, keys_header=keys)
845
846
847 def test_print_table_parse_all_fields(self):
848 self.__test_print_table_parse(['hostname', 'platform',
849 'locked'],
850 'Host=h0:Platform=p0:Locked=True\n'
851 'Host=h1:Platform=p1:Locked=False\n')
852
853 def test_print_table_parse_empty_fields(self):
854 self.__test_print_fields(self.atest.print_table_parse,
855 'Host=h0:Platform=p0\n'
856 'Host=h1:Platform=p1:Labels=l2, l3\n',
857 items=[{'hostname': 'h0',
858 'platform': 'p0',
859 'labels': [],
860 'locked': 1,
861 'id': 'id0',
862 'name': 'name0'},
863 {'hostname': 'h1',
864 'platform': 'p1',
865 'labels': [u'l2', u'l3'],
866 'locked': 0,
867 'id': 'id1',
868 'name': 'name1'}],
869 keys_header=['hostname', 'platform',
870 'labels'])
871
872
873 #
874 # Print mix table standard
875 #
876 def __test_print_mix_table_std(self, keys_header, sublist_keys,
877 expected):
878 self.__test_print_fields(self.atest.print_table_std,
879 expected,
880 keys_header=keys_header,
881 sublist_keys=sublist_keys)
882
883
884 def test_print_mix_table(self):
885 self.__test_print_mix_table_std(['name', 'hostname'],
886 ['hosts', 'users'],
887 'Name Host\n'
888 'name0 h0\n'
889 'name1 h1\n')
890
891 # TODO(jmeurin) Add actual test with sublist_keys.
892
893
894
895 #
896 # Print by ID standard
897 #
898 def __test_print_by_ids_std(self, expected):
899 self.__test_print_fields(self.atest.print_by_ids_std,
900 expected)
901
902
903 def test_print_by_ids_std_all_fields(self):
904 self.__test_print_by_ids_std('Id Name\n'
905 'id0 name0\n'
906 'id1 name1\n')
907
908
909 #
910 # Print by ID parse
911 #
912 def __test_print_by_ids_parse(self, expected):
913 self.__test_print_fields(self.atest.print_by_ids_parse,
914 expected)
915
916
917 def test_print_by_ids_parse_all_fields(self):
918 self.__test_print_by_ids_parse('Id=id0:Name=name0:'
919 'Id=id1:Name=name1\n')
920
921
922if __name__ == '__main__':
923 unittest.main()