blob: feb9aceb416d446a469cef66d33480fe87edfd1d [file] [log] [blame]
mbligh7c8ea992009-06-22 19:03:08 +00001#!/usr/bin/python
mbligh0b4fe6e2008-05-06 20:41:37 +00002
3__author__ = 'raphtee@google.com (Travis Miller)'
4
5import unittest
6import common
7from autotest_lib.server import utils
8
9
10class UtilsTest(unittest.TestCase):
jadmanski0afbb632008-06-06 21:10:57 +000011
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"))
mbligh0b4fe6e2008-05-06 20:41:37 +000021
22
jadmanski0afbb632008-06-06 21:10:57 +000023 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)
mbligh0b4fe6e2008-05-06 20:41:37 +000027
28
Eric Li861b2d52011-02-04 14:50:35 -080029 # 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)),
37 )
38 for machine, result in gooddata:
39 self.assertEquals(utils.parse_machine(machine), result)
40
41
42 def test_parse_machine_override(self):
43 '''Test that parse_machine() defaults can be overridden'''
44 self.assertEquals(utils.parse_machine('host', 'bob', 'foo', 1234),
45 ('host', 'bob', 'foo', 1234))
46
47
48 def test_parse_machine_bad(self):
49 '''test that bad data passed to parse_machine() will raise an exception'''
50 baddata = (('host:port', ValueError), # pass a non-integer string for port
51 ('host:22:33', ValueError), # pass two ports
52 (':22', ValueError), # neglect to pass a hostname #1
53 ('user@', ValueError), # neglect to pass a hostname #2
54 ('user@:22', ValueError), # neglect to pass a hostname #3
55 (':pass@host', ValueError), # neglect to pass a username
56 )
57 for machine, exception in baddata:
58 self.assertRaises(exception, utils.parse_machine, machine)
59
60
mbligh0b4fe6e2008-05-06 20:41:37 +000061if __name__ == "__main__":
jadmanski0afbb632008-06-06 21:10:57 +000062 unittest.main()