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