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