Mike Frysinger | d03e6b5 | 2019-08-03 12:49:01 -0400 | [diff] [blame] | 1 | #!/usr/bin/python2 |
mbligh | 0b4fe6e | 2008-05-06 20:41:37 +0000 | [diff] [blame] | 2 | |
| 3 | __author__ = 'raphtee@google.com (Travis Miller)' |
| 4 | |
| 5 | import unittest |
| 6 | import common |
| 7 | from autotest_lib.server import utils |
| 8 | |
| 9 | |
| 10 | class UtilsTest(unittest.TestCase): |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 11 | |
| 12 | def setUp(self): |
| 13 | # define out machines here |
| 14 | self.machines = ['mach1', 'mach2', 'mach3', 'mach4', 'mach5', |
| 15 | 'mach6', 'mach7'] |
| 16 | |
| 17 | self.ntuples = [['mach1', 'mach2'], ['mach3', 'mach4'], |
| 18 | ['mach5', 'mach6']] |
| 19 | self.failures = [] |
| 20 | self.failures.append(('mach7', "machine can not be tupled")) |
mbligh | 0b4fe6e | 2008-05-06 20:41:37 +0000 | [diff] [blame] | 21 | |
| 22 | |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 23 | def test_form_cell_mappings(self): |
| 24 | (ntuples, failures) = utils.form_ntuples_from_machines(self.machines) |
| 25 | self.assertEquals(self.ntuples, ntuples) |
| 26 | self.assertEquals(self.failures, failures) |
mbligh | 0b4fe6e | 2008-05-06 20:41:37 +0000 | [diff] [blame] | 27 | |
| 28 | |
Eric Li | 861b2d5 | 2011-02-04 14:50:35 -0800 | [diff] [blame] | 29 | # parse_machine() test cases |
| 30 | def test_parse_machine_good(self): |
| 31 | '''test that parse_machine() is outputting the correct data''' |
| 32 | gooddata = (('host', ('host', 'root', '', 22)), |
| 33 | ('host:21', ('host', 'root', '', 21)), |
| 34 | ('user@host', ('host', 'user', '', 22)), |
| 35 | ('user:pass@host', ('host', 'user', 'pass', 22)), |
| 36 | ('user:pass@host:1234', ('host', 'user', 'pass', 1234)), |
Marc Herbert | 21eb649 | 2015-11-13 15:48:53 -0800 | [diff] [blame] | 37 | |
| 38 | ('user:pass@10.3.2.1', |
| 39 | ('10.3.2.1', 'user', 'pass', 22)), |
| 40 | ('user:pass@10.3.2.1:1234', |
| 41 | ('10.3.2.1', 'user', 'pass', 1234)), |
| 42 | |
| 43 | ('::1', ('::1', 'root', '', 22)), |
| 44 | ('user:pass@abdc::ef', ('abdc::ef', 'user', 'pass', 22)), |
| 45 | ('abdc::ef:99', ('abdc::ef:99', 'root', '', 22)), |
| 46 | ('user:pass@[abdc::ef:99]', |
| 47 | ('abdc::ef:99', 'user', 'pass', 22)), |
| 48 | ('user:pass@[abdc::ef]:1234', |
| 49 | ('abdc::ef', 'user', 'pass', 1234)), |
Eric Li | 861b2d5 | 2011-02-04 14:50:35 -0800 | [diff] [blame] | 50 | ) |
| 51 | for machine, result in gooddata: |
| 52 | self.assertEquals(utils.parse_machine(machine), result) |
| 53 | |
| 54 | |
| 55 | def test_parse_machine_override(self): |
| 56 | '''Test that parse_machine() defaults can be overridden''' |
| 57 | self.assertEquals(utils.parse_machine('host', 'bob', 'foo', 1234), |
| 58 | ('host', 'bob', 'foo', 1234)) |
| 59 | |
| 60 | |
| 61 | def test_parse_machine_bad(self): |
| 62 | '''test that bad data passed to parse_machine() will raise an exception''' |
Marc Herbert | 21eb649 | 2015-11-13 15:48:53 -0800 | [diff] [blame] | 63 | baddata = ((':22', IndexError), # neglect to pass a hostname #1 |
| 64 | ('user@', IndexError), # neglect to pass a hostname #2 |
| 65 | ('user@:22', IndexError), # neglect to pass a hostname #3 |
Eric Li | 861b2d5 | 2011-02-04 14:50:35 -0800 | [diff] [blame] | 66 | (':pass@host', ValueError), # neglect to pass a username |
Marc Herbert | 21eb649 | 2015-11-13 15:48:53 -0800 | [diff] [blame] | 67 | ('host:', ValueError), # empty port after hostname |
| 68 | ('[1::2]:', ValueError), # empty port after IPv6 |
Eric Li | 861b2d5 | 2011-02-04 14:50:35 -0800 | [diff] [blame] | 69 | ) |
| 70 | for machine, exception in baddata: |
| 71 | self.assertRaises(exception, utils.parse_machine, machine) |
| 72 | |
| 73 | |
mbligh | 0b4fe6e | 2008-05-06 20:41:37 +0000 | [diff] [blame] | 74 | if __name__ == "__main__": |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 75 | unittest.main() |