blob: 0ce11f6dd37b8a2ce6f460dc9db685435a6db2fa [file] [log] [blame]
Steve McIntyre1f2f47f2014-12-10 23:27:38 +00001import unittest, os, sys
2vlandpath = os.path.abspath(os.path.normpath(os.path.dirname(sys.argv[0])))
3sys.path.insert(0, vlandpath)
4sys.path.insert(0, "%s/.." % vlandpath)
5
6os.chdir(vlandpath)
7
8from config.config import VlanConfig
Steve McIntyre5fa22652015-04-01 18:01:45 +01009from errors import ConfigError
Steve McIntyre1f2f47f2014-12-10 23:27:38 +000010
11class MyTest(unittest.TestCase):
12
13 # Check that we raise on missing database section
14 def test_missing_database_section(self):
15 with self.assertRaisesRegexp(ConfigError, 'No database'):
16 config = VlanConfig(filenames=("/dev/null",))
Steve McIntyre5fa22652015-04-01 18:01:45 +010017 del config
Steve McIntyre1f2f47f2014-12-10 23:27:38 +000018
Steve McIntyrea9875062014-12-12 17:15:15 +000019 # Check that we raise on broken database config values
Steve McIntyre1f2f47f2014-12-10 23:27:38 +000020 def test_missing_database_config(self):
21 with self.assertRaisesRegexp(ConfigError, 'Invalid database'):
22 config = VlanConfig(filenames=("test-invalid-DB.cfg",))
Steve McIntyre5fa22652015-04-01 18:01:45 +010023 del config
Steve McIntyre1f2f47f2014-12-10 23:27:38 +000024
25 # Check that we raise on missing database config values
26 def test_missing_dbname(self):
27 with self.assertRaisesRegexp(ConfigError, 'Database.*incomplete'):
28 config = VlanConfig(filenames=("test-missing-dbname.cfg",))
Steve McIntyre5fa22652015-04-01 18:01:45 +010029 del config
Steve McIntyre1f2f47f2014-12-10 23:27:38 +000030
31 # Check that we raise on missing database config values
32 def test_missing_db_username(self):
33 with self.assertRaisesRegexp(ConfigError, 'Database.*incomplete'):
34 config = VlanConfig(filenames=("test-missing-db-username.cfg",))
Steve McIntyre5fa22652015-04-01 18:01:45 +010035 del config
Steve McIntyre1f2f47f2014-12-10 23:27:38 +000036
Steve McIntyrea9875062014-12-12 17:15:15 +000037 # Check that we raise on broken vland config values
38 def test_missing_vlan_config(self):
39 with self.assertRaisesRegexp(ConfigError, 'Invalid vland'):
40 config = VlanConfig(filenames=("test-invalid-vland.cfg",))
Steve McIntyre5fa22652015-04-01 18:01:45 +010041 del config
Steve McIntyrea9875062014-12-12 17:15:15 +000042
Steve McIntyrec3709802015-10-19 18:34:53 +010043 # Check that we raise on broken logging level
44 def test_invalid_logging_level(self):
45 with self.assertRaisesRegexp(ConfigError, 'Invalid logging.*level'):
46 config = VlanConfig(filenames=("test-invalid-logging-level.cfg",))
47 del config
48
Steve McIntyre4e9fee52015-10-19 18:54:23 +010049 # Check that we raise when VLANd and visn are configured for the
50 # same port
51 def test_clashing_ports(self):
52 with self.assertRaisesRegexp(ConfigError, 'Invalid.*distinct port'):
53 config = VlanConfig(filenames=("test-clashing-ports.cfg",))
54 del config
55
Steve McIntyre1f2f47f2014-12-10 23:27:38 +000056 # Check that we raise on repeated switch names
57 def test_missing_repeated_switch_names(self):
58 with self.assertRaisesRegexp(ConfigError, 'same name'):
59 config = VlanConfig(filenames=("test-reused-switch-names.cfg",))
Steve McIntyre5fa22652015-04-01 18:01:45 +010060 del config
Steve McIntyre1f2f47f2014-12-10 23:27:38 +000061
62 # Check that we raise on unknown config section
63 def test_unknown_config(self):
64 with self.assertRaisesRegexp(ConfigError, 'Unrecognised config'):
65 config = VlanConfig(filenames=("test-unknown-section.cfg",))
Steve McIntyre5fa22652015-04-01 18:01:45 +010066 del config
Steve McIntyre1f2f47f2014-12-10 23:27:38 +000067
68 # Check we get expected values on a known-good config
69 def test_known_good(self):
70 config = VlanConfig(filenames=("test-known-good.cfg",))
Steve McIntyreaab5fd42014-12-23 13:22:51 +000071 self.assertEqual(config.database.server, 'foo')
72 self.assertEqual(config.database.port, 123)
73 self.assertEqual(config.database.dbname, 'vland')
74 self.assertEqual(config.database.username, 'user')
75 self.assertEqual(config.database.password, 'pass')
76
77 self.assertEqual(config.vland.port, 9997)
78 self.assertEqual(config.vland.default_vlan_tag, 42)
79
Steve McIntyre1f2f47f2014-12-10 23:27:38 +000080 self.assertEqual(len(config.switches), 3)
Steve McIntyreaab5fd42014-12-23 13:22:51 +000081 self.assertEqual(config.switches["10.172.2.51"].name, '10.172.2.51')
82 self.assertEqual(config.switches["10.172.2.51"].driver, 'CiscoSX300')
83 self.assertEqual(config.switches["10.172.2.51"].username, 'cisco_user')
84 self.assertEqual(config.switches["10.172.2.51"].password, 'cisco_pass')
85 self.assertEqual(config.switches["10.172.2.51"].enable_password, 'foobar')
86 self.assertEqual(config.switches["10.172.2.51"].driver, 'CiscoSX300')
87
88 self.assertEqual(config.switches["10.172.2.52"].name, '10.172.2.52')
89 self.assertEqual(config.switches["10.172.2.52"].driver, 'CiscoSX300')
90 self.assertEqual(config.switches["10.172.2.52"].username, 'cisco')
91 self.assertEqual(config.switches["10.172.2.52"].password, 'cisco')
92 self.assertEqual(config.switches["10.172.2.52"].enable_password, None)
93 self.assertEqual(config.switches["10.172.2.52"].driver, 'CiscoSX300')
94
95 self.assertEqual(config.switches["baz"].name, 'baz')
Steve McIntyre1f2f47f2014-12-10 23:27:38 +000096 self.assertEqual(config.switches["baz"].driver, 'CiscoSX300')
Steve McIntyreaab5fd42014-12-23 13:22:51 +000097 self.assertEqual(config.switches["baz"].username, 'cisco')
98 self.assertEqual(config.switches["baz"].password, 'cisco')
Steve McIntyre1f2f47f2014-12-10 23:27:38 +000099
100if __name__ == '__main__':
101 unittest.main()