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