blob: d3bdacbb91476e6cd7c97a3ed042262f813175cd [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
mbligh41515392009-07-11 00:13:11 +00007import unittest, os, sys, StringIO, urllib2
mblighbe630eb2008-08-01 16:41:48 +00008
9import common
10from autotest_lib.cli import cli_mock, topic_common, rpc
11from autotest_lib.frontend.afe.json_rpc import proxy
12
mbligh9deeefa2009-05-01 23:11:08 +000013
showard088b8262009-07-01 22:12:35 +000014class 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'))
28
29
mbligh9deeefa2009-05-01 23:11:08 +000030class 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, [])
37
38
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, [])
44
45 self.assertEqualNoOrder(expected, result)
46 os.unlink(options.flist)
47
48
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, [])
54
55 self.assertEqualNoOrder(expected, result)
56
57
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)
66
67 self.assertEqualNoOrder(expected, result)
68
69
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)
77
78 self.assertEqualNoOrder(expected, result)
79 os.unlink(options.flist)
80
81
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)
90
91
92 def test_file_list_wrong_file(self):
93 class opt(object):
94 flist = './does_not_exist'
95 self.__test_parsing_flist_bad(opt())
96
97
98 def test_file_list_empty_file(self):
99 class opt(object):
mbligh41515392009-07-11 00:13:11 +0000100 flist_obj = cli_mock.create_file('')
101 flist = flist_obj.name
mbligh9deeefa2009-05-01 23:11:08 +0000102 self.__test_parsing_flist_bad(opt())
103
104
105 def test_file_list_ok(self):
106 class opt(object):
mbligh41515392009-07-11 00:13:11 +0000107 flist_obj = cli_mock.create_file('a\nb\nc\n')
108 flist = flist_obj.name
mbligh9deeefa2009-05-01 23:11:08 +0000109 self.__test_parsing_flist_good(opt(), ['a', 'b', 'c'])
110
111
112 def test_file_list_one_line_space(self):
113 class opt(object):
mbligh41515392009-07-11 00:13:11 +0000114 flist_obj = cli_mock.create_file('a b c\nd e\nf\n')
115 flist = flist_obj.name
mbligh9deeefa2009-05-01 23:11:08 +0000116 self.__test_parsing_flist_good(opt(), ['a', 'b', 'c', 'd', 'e', 'f'])
117
118
119 def test_file_list_one_line_comma(self):
120 class opt(object):
mbligh41515392009-07-11 00:13:11 +0000121 flist_obj = cli_mock.create_file('a,b,c\nd,e\nf\n')
122 flist = flist_obj.name
mbligh9deeefa2009-05-01 23:11:08 +0000123 self.__test_parsing_flist_good(opt(), ['a', 'b', 'c', 'd', 'e', 'f'])
124
125
126 def test_file_list_one_line_mix(self):
127 class opt(object):
mbligh41515392009-07-11 00:13:11 +0000128 flist_obj = cli_mock.create_file('a,b c\nd,e\nf\ng h,i')
129 flist = flist_obj.name
mbligh9deeefa2009-05-01 23:11:08 +0000130 self.__test_parsing_flist_good(opt(), ['a', 'b', 'c', 'd', 'e',
131 'f', 'g', 'h', 'i'])
132
133
134 def test_file_list_one_line_comma_space(self):
135 class opt(object):
mbligh41515392009-07-11 00:13:11 +0000136 flist_obj = cli_mock.create_file('a, b c\nd,e\nf\ng h,i')
137 flist = flist_obj.name
mbligh9deeefa2009-05-01 23:11:08 +0000138 self.__test_parsing_flist_good(opt(), ['a', 'b', 'c', 'd', 'e',
139 'f', 'g', 'h', 'i'])
140
141
142 def test_file_list_line_end_comma_space(self):
143 class opt(object):
mbligh41515392009-07-11 00:13:11 +0000144 flist_obj = cli_mock.create_file('a, b c\nd,e, \nf,\ng h,i ,')
145 flist = flist_obj.name
mbligh9deeefa2009-05-01 23:11:08 +0000146 self.__test_parsing_flist_good(opt(), ['a', 'b', 'c', 'd', 'e',
147 'f', 'g', 'h', 'i'])
148
149
150 def test_file_list_no_eof(self):
151 class opt(object):
mbligh41515392009-07-11 00:13:11 +0000152 flist_obj = cli_mock.create_file('a\nb\nc')
153 flist = flist_obj.name
mbligh9deeefa2009-05-01 23:11:08 +0000154 self.__test_parsing_flist_good(opt(), ['a', 'b', 'c'])
155
156
157 def test_file_list_blank_line(self):
158 class opt(object):
mbligh41515392009-07-11 00:13:11 +0000159 flist_obj = cli_mock.create_file('\na\nb\n\nc\n')
160 flist = flist_obj.name
mbligh9deeefa2009-05-01 23:11:08 +0000161 self.__test_parsing_flist_good(opt(), ['a', 'b', 'c'])
162
163
jamesrenc2863162010-07-12 21:20:51 +0000164 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'])
169
170
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'])
176
177
mbligh9deeefa2009-05-01 23:11:08 +0000178 def test_file_list_opt_list_one(self):
179 class opt(object):
180 inline = 'a'
181 self.__test_parsing_inline_good(opt(), ['a'])
182
183
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'])
188
189
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'])
194
195
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'])
200
201
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'])
206
207
jamesrenc2863162010-07-12 21:20:51 +0000208 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'])
212
213
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'])
218
219
mbligh9deeefa2009-05-01 23:11:08 +0000220 def test_file_list_add_on_space(self):
221 self.__test_parsing_leftover_good(['a','c','b'],
222 ['a', 'b', 'c'])
223
224
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'])
228
229
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'])
233
234
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'])
238
239
jamesrenc2863162010-07-12 21:20:51 +0000240 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'])
243
244
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'])
248
249
mbligh9deeefa2009-05-01 23:11:08 +0000250 def test_file_list_all_opt(self):
251 class opt(object):
mbligh41515392009-07-11 00:13:11 +0000252 flist_obj = cli_mock.create_file('f\ng\nh\n')
253 flist = flist_obj.name
mbligh9deeefa2009-05-01 23:11:08 +0000254 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'])
258
259
260 def test_file_list_all_opt_empty_file(self):
261 class opt(object):
mbligh41515392009-07-11 00:13:11 +0000262 flist_obj = cli_mock.create_file('')
263 flist = flist_obj.name
mbligh9deeefa2009-05-01 23:11:08 +0000264 inline = 'a b,c,d e'
265 self.__test_parsing_all_bad(opt(), ['i', 'j'])
266
267
268 def test_file_list_all_opt_in_common(self):
269 class opt(object):
mbligh41515392009-07-11 00:13:11 +0000270 flist_obj = cli_mock.create_file('f\nc\na\n')
271 flist = flist_obj.name
mbligh9deeefa2009-05-01 23:11:08 +0000272 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'])
275
276
277 def test_file_list_all_opt_in_common_space(self):
278 class opt(object):
mbligh41515392009-07-11 00:13:11 +0000279 flist_obj = cli_mock.create_file('a b c\nd,e\nf\ng')
280 flist = flist_obj.name
mbligh9deeefa2009-05-01 23:11:08 +0000281 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'])
285
286
287 def test_file_list_all_opt_in_common_weird(self):
288 class opt(object):
mbligh41515392009-07-11 00:13:11 +0000289 flist_obj = cli_mock.create_file('a b c\nd,e\nf\ng, \n, ,,')
290 flist = flist_obj.name
mbligh1ef218d2009-08-03 16:57:56 +0000291 inline = 'a b,c,d h, , ,, '
mbligh9deeefa2009-05-01 23:11:08 +0000292 self.__test_parsing_all_good(opt(), ['i','j,d'],
293 ['a', 'b', 'c', 'd', 'e',
294 'f', 'g', 'h', 'i', 'j'])
295
296
jamesrenc2863162010-07-12 21:20:51 +0000297 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'])
305
306
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'])
315
316
mbligh9deeefa2009-05-01 23:11:08 +0000317class atest_unittest(cli_mock.cli_unittest):
mblighbe630eb2008-08-01 16:41:48 +0000318 def setUp(self):
mbligh9deeefa2009-05-01 23:11:08 +0000319 super(atest_unittest, self).setUp()
mblighbe630eb2008-08-01 16:41:48 +0000320 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']
324
325
326 def tearDown(self):
327 self.atest = None
mbligh9deeefa2009-05-01 23:11:08 +0000328 super(atest_unittest, self).tearDown()
mblighf1733342008-09-04 16:45:46 +0000329
330
mblighbe630eb2008-08-01 16:41:48 +0000331 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)
340
341
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)
347
348
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())
354
355
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)
367
368
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())
379
380
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())
393
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())
399
400
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())
412
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())
418
419
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)
429
430
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'])
441
442
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'])
453
454
mbligh9deeefa2009-05-01 23:11:08 +0000455 def test_parse_add_on(self):
mblighbe630eb2008-08-01 16:41:48 +0000456 flist = cli_mock.create_file('host1\nhost2\nleft2')
457 sys.argv = ['atest', '--web', 'fooweb', '--parse',
mbligh41515392009-07-11 00:13:11 +0000458 '--kill-on-failure', 'left1', 'left2', '-M', flist.name]
mblighbe630eb2008-08-01 16:41:48 +0000459 self.atest.parser.add_option('-M', '--mlist', type='string')
mbligh9deeefa2009-05-01 23:11:08 +0000460 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])
mblighbe630eb2008-08-01 16:41:48 +0000464 self.assertEqualNoOrder(self.atest.hosts,
465 ['left1', 'left2', 'host1', 'host2'])
466
mbligh41515392009-07-11 00:13:11 +0000467 self.assertEqual({'mlist': flist.name,
mblighbe630eb2008-08-01 16:41:48 +0000468 'web_server': 'fooweb',
469 'parse': True,
mbligh47dc4d22009-02-12 21:48:34 +0000470 'parse_delim': '|',
mblighbe630eb2008-08-01 16:41:48 +0000471 'kill_on_failure': True,
472 'verbose': False,
473 'debug': False}, options)
474 self.assertEqual(leftover, [])
mbligh41515392009-07-11 00:13:11 +0000475 flist.clean()
mblighbe630eb2008-08-01 16:41:48 +0000476
477
mbligh9deeefa2009-05-01 23:11:08 +0000478 def test_parse_no_add_on(self):
mblighbe630eb2008-08-01 16:41:48 +0000479 flist = cli_mock.create_file('host1\nhost2\nleft2')
480 sys.argv = ['atest', '--web', 'fooweb', '--parse', '-g',
mbligh41515392009-07-11 00:13:11 +0000481 '--kill-on-failure', 'left1', 'left2', '-M', flist.name]
mblighbe630eb2008-08-01 16:41:48 +0000482 self.atest.parser.add_option('-M', '--mlist', type='string')
mbligh9deeefa2009-05-01 23:11:08 +0000483 item_info = topic_common.item_parse_info(attribute_name='hosts',
484 filename_option='mlist')
485 (options, leftover) = self.atest.parse([item_info])
mblighbe630eb2008-08-01 16:41:48 +0000486 self.assertEqualNoOrder(self.atest.hosts,
487 ['left2', 'host1', 'host2'])
488
mbligh41515392009-07-11 00:13:11 +0000489 self.assertEqual({'mlist': flist.name,
mblighbe630eb2008-08-01 16:41:48 +0000490 'web_server': 'fooweb',
491 'parse': True,
mbligh47dc4d22009-02-12 21:48:34 +0000492 'parse_delim': '|',
mblighbe630eb2008-08-01 16:41:48 +0000493 'kill_on_failure': True,
494 'verbose': False,
495 'debug': True}, options)
496 self.assertEqual(leftover, ['left1', 'left2'])
mbligh41515392009-07-11 00:13:11 +0000497 flist.clean()
mblighbe630eb2008-08-01 16:41:48 +0000498
499
mbligh9deeefa2009-05-01 23:11:08 +0000500 def test_parse_add_on_first(self):
mblighbe630eb2008-08-01 16:41:48 +0000501 flist = cli_mock.create_file('host1\nhost2\nleft2')
502 ulist = cli_mock.create_file('user1\nuser2\nuser3\n')
mbligh41515392009-07-11 00:13:11 +0000503 sys.argv = ['atest', '-g', '--parse', '--ulist', ulist.name,
mblighbe630eb2008-08-01 16:41:48 +0000504 '-u', 'myuser,youruser',
mbligh41515392009-07-11 00:13:11 +0000505 '--kill-on-failure', 'left1', 'left2', '-M', flist.name]
mblighbe630eb2008-08-01 16:41:48 +0000506 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')
mbligh9deeefa2009-05-01 23:11:08 +0000509 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')
515
516 (options, leftover) = self.atest.parse([host_info, user_info])
mblighbe630eb2008-08-01 16:41:48 +0000517 self.assertEqualNoOrder(self.atest.hosts,
518 ['left1', 'left2', 'host1', 'host2'])
519 self.assertEqualNoOrder(self.atest.users,
520 ['user1', 'user2', 'user3',
521 'myuser', 'youruser'])
522
mbligh41515392009-07-11 00:13:11 +0000523 self.assertEqual({'mlist': flist.name,
524 'ulist': ulist.name,
mblighbe630eb2008-08-01 16:41:48 +0000525 'user': 'myuser,youruser',
526 'web_server': None,
527 'parse': True,
mbligh47dc4d22009-02-12 21:48:34 +0000528 'parse_delim': '|',
mblighbe630eb2008-08-01 16:41:48 +0000529 'kill_on_failure': True,
530 'verbose': False,
531 'debug': True}, options)
532 self.assertEqual(leftover, [])
mbligh41515392009-07-11 00:13:11 +0000533 flist.clean()
534 ulist.clean()
mblighbe630eb2008-08-01 16:41:48 +0000535
536
mbligh9deeefa2009-05-01 23:11:08 +0000537 def test_parse_add_on_second(self):
mblighbe630eb2008-08-01 16:41:48 +0000538 flist = cli_mock.create_file('host1\nhost2\nleft2')
539 ulist = cli_mock.create_file('user1\nuser2\nuser3\n')
mbligh41515392009-07-11 00:13:11 +0000540 sys.argv = ['atest', '-g', '--parse', '-U', ulist.name,
mblighbe630eb2008-08-01 16:41:48 +0000541 '-u', 'myuser,youruser',
mbligh41515392009-07-11 00:13:11 +0000542 '--kill-on-failure', 'left1', 'left2', '-M', flist.name]
mblighbe630eb2008-08-01 16:41:48 +0000543 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')
mbligh9deeefa2009-05-01 23:11:08 +0000546 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])
553
mblighbe630eb2008-08-01 16:41:48 +0000554 self.assertEqualNoOrder(self.atest.hosts,
555 ['left1', 'left2', 'host1', 'host2'])
556 self.assertEqualNoOrder(self.atest.users,
557 ['user1', 'user2', 'user3',
558 'myuser', 'youruser'])
559
mbligh41515392009-07-11 00:13:11 +0000560 self.assertEqual({'mlist': flist.name,
561 'ulist': ulist.name,
mblighbe630eb2008-08-01 16:41:48 +0000562 'user': 'myuser,youruser',
563 'web_server': None,
564 'parse': True,
mbligh47dc4d22009-02-12 21:48:34 +0000565 'parse_delim': '|',
mblighbe630eb2008-08-01 16:41:48 +0000566 'kill_on_failure': True,
567 'verbose': False,
568 'debug': True}, options)
569 self.assertEqual(leftover, [])
mbligh41515392009-07-11 00:13:11 +0000570 flist.clean()
571 ulist.clean()
mblighbe630eb2008-08-01 16:41:48 +0000572
573
mbligh9deeefa2009-05-01 23:11:08 +0000574 def test_parse_all_opts(self):
mblighbe630eb2008-08-01 16:41:48 +0000575 flist = cli_mock.create_file('host1\nhost2\nleft2')
576 ulist = cli_mock.create_file('user1\nuser2\nuser3\n')
mbligh41515392009-07-11 00:13:11 +0000577 sys.argv = ['atest', '-g', '--parse', '--ulist', ulist.name,
mblighbe630eb2008-08-01 16:41:48 +0000578 '-u', 'myuser,youruser',
mbligh41515392009-07-11 00:13:11 +0000579 '--kill-on-failure', '-M', flist.name, 'left1', 'left2']
mblighbe630eb2008-08-01 16:41:48 +0000580 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')
mbligh9deeefa2009-05-01 23:11:08 +0000583 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])
mblighbe630eb2008-08-01 16:41:48 +0000590 self.assertEqualNoOrder(self.atest.hosts,
591 ['left1', 'left2', 'host1', 'host2'])
592 self.assertEqualNoOrder(self.atest.users,
593 ['user1', 'user2', 'user3',
594 'myuser', 'youruser'])
595
mbligh41515392009-07-11 00:13:11 +0000596 self.assertEqual({'mlist': flist.name,
597 'ulist': ulist.name,
mblighbe630eb2008-08-01 16:41:48 +0000598 'user': 'myuser,youruser',
599 'web_server': None,
600 'parse': True,
mbligh47dc4d22009-02-12 21:48:34 +0000601 'parse_delim': '|',
mblighbe630eb2008-08-01 16:41:48 +0000602 'kill_on_failure': True,
603 'verbose': False,
604 'debug': True}, options)
605 self.assertEqual(leftover, [])
mbligh41515392009-07-11 00:13:11 +0000606 flist.clean()
607 ulist.clean()
mblighbe630eb2008-08-01 16:41:48 +0000608
609
mbligh9deeefa2009-05-01 23:11:08 +0000610 def test_parse_no_add_on(self):
mblighbe630eb2008-08-01 16:41:48 +0000611 flist = cli_mock.create_file('host1\nhost2\nleft2')
612 ulist = cli_mock.create_file('user1\nuser2\nuser3\n')
mbligh41515392009-07-11 00:13:11 +0000613 sys.argv = ['atest', '-U', ulist.name,
614 '--kill-on-failure', '-M', flist.name]
mblighbe630eb2008-08-01 16:41:48 +0000615 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')
mbligh9deeefa2009-05-01 23:11:08 +0000618 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])
mblighbe630eb2008-08-01 16:41:48 +0000625 self.assertEqualNoOrder(self.atest.hosts,
626 ['left2', 'host1', 'host2'])
627 self.assertEqualNoOrder(self.atest.users,
628 ['user1', 'user2', 'user3'])
629
mbligh41515392009-07-11 00:13:11 +0000630 self.assertEqual({'mlist': flist.name,
631 'ulist': ulist.name,
mblighbe630eb2008-08-01 16:41:48 +0000632 'user': None,
633 'web_server': None,
634 'parse': False,
mbligh47dc4d22009-02-12 21:48:34 +0000635 'parse_delim': '|',
mblighbe630eb2008-08-01 16:41:48 +0000636 'kill_on_failure': True,
637 'verbose': False,
638 'debug': False}, options)
639 self.assertEqual(leftover, [])
mbligh41515392009-07-11 00:13:11 +0000640 flist.clean()
641 ulist.clean()
mblighbe630eb2008-08-01 16:41:48 +0000642
643
mbligh9deeefa2009-05-01 23:11:08 +0000644 def test_parse_no_flist_add_on(self):
mblighbe630eb2008-08-01 16:41:48 +0000645 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')
mbligh9deeefa2009-05-01 23:11:08 +0000650 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])
mblighbe630eb2008-08-01 16:41:48 +0000655 self.assertEqualNoOrder(self.atest.hosts,
656 ['left1', 'left2'])
657 self.assertEqualNoOrder(self.atest.users,
658 ['myuser', 'youruser'])
659
660 self.assertEqual({'mlist': None,
661 'ulist': None,
662 'user': 'myuser,youruser',
663 'web_server': None,
664 'parse': True,
mbligh47dc4d22009-02-12 21:48:34 +0000665 'parse_delim': '|',
mblighbe630eb2008-08-01 16:41:48 +0000666 'kill_on_failure': True,
667 'verbose': False,
668 'debug': True}, options)
669 self.assertEqual(leftover, [])
670
671
mbligh9deeefa2009-05-01 23:11:08 +0000672 def test_parse_no_flist_no_add_on(self):
mblighbe630eb2008-08-01 16:41:48 +0000673 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')
mbligh9deeefa2009-05-01 23:11:08 +0000677 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])
mblighbe630eb2008-08-01 16:41:48 +0000682 self.assertEqualNoOrder(self.atest.acls,
683 ['acl1', 'acl2'])
684 self.assertEqualNoOrder(self.atest.users,
685 ['myuser', 'youruser'])
686
687 self.assertEqual({'user': 'myuser,youruser',
688 'acl': 'acl1,acl2',
689 'web_server': None,
690 'parse': False,
mbligh47dc4d22009-02-12 21:48:34 +0000691 'parse_delim': '|',
mblighbe630eb2008-08-01 16:41:48 +0000692 'kill_on_failure': True,
693 'verbose': False,
694 'debug': False}, options)
695 self.assertEqual(leftover, [])
696
697
mbligh9deeefa2009-05-01 23:11:08 +0000698 def test_parse_req_items_ok(self):
mblighbe630eb2008-08-01 16:41:48 +0000699 sys.argv = ['atest', '-u', 'myuser,youruser']
700 self.atest.parser.add_option('-u', '--user', type='string')
mbligh9deeefa2009-05-01 23:11:08 +0000701 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')
mblighbe630eb2008-08-01 16:41:48 +0000705 self.assertEqualNoOrder(self.atest.users,
706 ['myuser', 'youruser'])
707
708 self.assertEqual({'user': 'myuser,youruser',
709 'web_server': None,
710 'parse': False,
mbligh47dc4d22009-02-12 21:48:34 +0000711 'parse_delim': '|',
mblighbe630eb2008-08-01 16:41:48 +0000712 'kill_on_failure': False,
713 'verbose': False,
714 'debug': False}, options)
715 self.assertEqual(leftover, [])
716
717
mbligh9deeefa2009-05-01 23:11:08 +0000718 def test_parse_req_items_missing(self):
mblighbe630eb2008-08-01 16:41:48 +0000719 sys.argv = ['atest', '-u', 'myuser,youruser', '--kill-on-failure']
720 self.atest.parser.add_option('-u', '--user', type='string')
mbligh9deeefa2009-05-01 23:11:08 +0000721 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')
mblighbe630eb2008-08-01 16:41:48 +0000725 self.god.mock_io()
726 sys.exit.expect_call(1).and_raises(cli_mock.ExitException)
727 self.assertRaises(cli_mock.ExitException,
mbligh9deeefa2009-05-01 23:11:08 +0000728 self.atest.parse,
729 [user_info, acl_info],
mblighbe630eb2008-08-01 16:41:48 +0000730 'acls')
731 self.assertEqualNoOrder(self.atest.users,
732 ['myuser', 'youruser'])
733
734 self.assertEqualNoOrder(self.atest.acls, [])
735 self.god.check_playback()
736 self.god.unmock_io()
737
738
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()
745
746
747 def test_parse_all_set(self):
748 sys.argv = ['atest', '--web', 'fooweb', '--parse', '--debug',
mbligh47dc4d22009-02-12 21:48:34 +0000749 '--kill-on-failure', '--verbose', 'left1', 'left2',
750 '--parse-delim', '?']
mblighbe630eb2008-08-01 16:41:48 +0000751 (options, leftover) = self.atest.parse()
752 self.assertEqual({'web_server': 'fooweb',
753 'parse': True,
mbligh47dc4d22009-02-12 21:48:34 +0000754 'parse_delim': '?',
mblighbe630eb2008-08-01 16:41:48 +0000755 'kill_on_failure': True,
756 'verbose': True,
757 'debug': True}, options)
758 self.assertEqual(leftover, ['left1', 'left2'])
759
760
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)
771
772
773 #
774 # Print Unit tests
775 #
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)
794
795
796 #
797 # Print fields Standard
798 #
799 def __test_print_fields_std(self, keys, expected):
800 self.__test_print_fields(self.atest.print_fields_std,
801 expected, keys=keys)
802
803
804 def test_print_fields_std_one_str(self):
805 self.__test_print_fields_std(['hostname'],
806 'Host: h0\n'
807 'Host: h1\n')
808
809
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')
815
816
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')
822
823
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')
833
834
835 #
836 # Print fields parse
837 #
838 def __test_print_fields_parse(self, keys, expected):
839 self.__test_print_fields(self.atest.print_fields_parse,
840 expected, keys=keys)
841
842
843 def test_print_fields_parse_one_str(self):
844 self.__test_print_fields_parse(['hostname'],
845 'Host=h0\n'
846 'Host=h1\n')
847
848
849 def test_print_fields_parse_conv_bool(self):
850 self.__test_print_fields_parse(['locked'],
851 'Locked=True\n'
852 'Locked=False\n')
853
854
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')
859
860
861 def test_print_fields_parse_all_fields(self):
862 self.__test_print_fields_parse(['hostname', 'platform', 'locked'],
mbligh47dc4d22009-02-12 21:48:34 +0000863 'Host=h0|Platform=p0|'
mblighbe630eb2008-08-01 16:41:48 +0000864 'Locked=True\n'
mbligh47dc4d22009-02-12 21:48:34 +0000865 'Host=h1|Platform=p1|'
mblighbe630eb2008-08-01 16:41:48 +0000866 'Locked=False\n')
867
868
869 #
870 # Print table standard
871 #
872 def __test_print_table_std(self, keys, expected):
873 self.__test_print_fields(self.atest.print_table_std,
874 expected, keys_header=keys)
875
876
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')
882
883 # TODO JME - add long fields tests
884
885
886 #
887 # Print table parse
888 #
889 def __test_print_table_parse(self, keys, expected):
890 self.__test_print_fields(self.atest.print_table_parse,
891 expected, keys_header=keys)
892
893
894 def test_print_table_parse_all_fields(self):
895 self.__test_print_table_parse(['hostname', 'platform',
896 'locked'],
mbligh47dc4d22009-02-12 21:48:34 +0000897 'Host=h0|Platform=p0|Locked=True\n'
898 'Host=h1|Platform=p1|Locked=False\n')
899
900
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')
907
mblighbe630eb2008-08-01 16:41:48 +0000908
909 def test_print_table_parse_empty_fields(self):
910 self.__test_print_fields(self.atest.print_table_parse,
mbligh47dc4d22009-02-12 21:48:34 +0000911 'Host=h0|Platform=p0\n'
912 'Host=h1|Platform=p1|Labels=l2, l3\n',
mblighbe630eb2008-08-01 16:41:48 +0000913 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'])
927
928
929 #
930 # Print mix table standard
931 #
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)
938
939
940 def test_print_mix_table(self):
mbligh838c7472009-05-13 20:56:50 +0000941 self.__test_print_mix_table_std(['name', 'hostname'], [],
mblighbe630eb2008-08-01 16:41:48 +0000942 'Name Host\n'
943 'name0 h0\n'
944 'name1 h1\n')
945
mblighbe630eb2008-08-01 16:41:48 +0000946
mbligh838c7472009-05-13 20:56:50 +0000947 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')
mblighbe630eb2008-08-01 16:41:48 +0000956
957
958 #
959 # Print by ID standard
960 #
961 def __test_print_by_ids_std(self, expected):
962 self.__test_print_fields(self.atest.print_by_ids_std,
963 expected)
964
965
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')
970
971
972 #
973 # Print by ID parse
974 #
975 def __test_print_by_ids_parse(self, expected):
976 self.__test_print_fields(self.atest.print_by_ids_parse,
977 expected)
978
979
980 def test_print_by_ids_parse_all_fields(self):
mbligh47dc4d22009-02-12 21:48:34 +0000981 self.__test_print_by_ids_parse('Id=id0|Name=name0|'
982 'Id=id1|Name=name1\n')
mblighbe630eb2008-08-01 16:41:48 +0000983
984
985if __name__ == '__main__':
986 unittest.main()