blob: a1edb223f3f4eee66e9bc0cb8ad966e066caa416 [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 McIntyre1f2f47f2014-12-10 23:27:38 +000049 # Check that we raise on repeated switch names
50 def test_missing_repeated_switch_names(self):
51 with self.assertRaisesRegexp(ConfigError, 'same name'):
52 config = VlanConfig(filenames=("test-reused-switch-names.cfg",))
Steve McIntyre5fa22652015-04-01 18:01:45 +010053 del config
Steve McIntyre1f2f47f2014-12-10 23:27:38 +000054
55 # Check that we raise on unknown config section
56 def test_unknown_config(self):
57 with self.assertRaisesRegexp(ConfigError, 'Unrecognised config'):
58 config = VlanConfig(filenames=("test-unknown-section.cfg",))
Steve McIntyre5fa22652015-04-01 18:01:45 +010059 del config
Steve McIntyre1f2f47f2014-12-10 23:27:38 +000060
61 # Check we get expected values on a known-good config
62 def test_known_good(self):
63 config = VlanConfig(filenames=("test-known-good.cfg",))
Steve McIntyreaab5fd42014-12-23 13:22:51 +000064 self.assertEqual(config.database.server, 'foo')
65 self.assertEqual(config.database.port, 123)
66 self.assertEqual(config.database.dbname, 'vland')
67 self.assertEqual(config.database.username, 'user')
68 self.assertEqual(config.database.password, 'pass')
69
70 self.assertEqual(config.vland.port, 9997)
71 self.assertEqual(config.vland.default_vlan_tag, 42)
72
Steve McIntyre1f2f47f2014-12-10 23:27:38 +000073 self.assertEqual(len(config.switches), 3)
Steve McIntyreaab5fd42014-12-23 13:22:51 +000074 self.assertEqual(config.switches["10.172.2.51"].name, '10.172.2.51')
75 self.assertEqual(config.switches["10.172.2.51"].driver, 'CiscoSX300')
76 self.assertEqual(config.switches["10.172.2.51"].username, 'cisco_user')
77 self.assertEqual(config.switches["10.172.2.51"].password, 'cisco_pass')
78 self.assertEqual(config.switches["10.172.2.51"].enable_password, 'foobar')
79 self.assertEqual(config.switches["10.172.2.51"].driver, 'CiscoSX300')
80
81 self.assertEqual(config.switches["10.172.2.52"].name, '10.172.2.52')
82 self.assertEqual(config.switches["10.172.2.52"].driver, 'CiscoSX300')
83 self.assertEqual(config.switches["10.172.2.52"].username, 'cisco')
84 self.assertEqual(config.switches["10.172.2.52"].password, 'cisco')
85 self.assertEqual(config.switches["10.172.2.52"].enable_password, None)
86 self.assertEqual(config.switches["10.172.2.52"].driver, 'CiscoSX300')
87
88 self.assertEqual(config.switches["baz"].name, 'baz')
Steve McIntyre1f2f47f2014-12-10 23:27:38 +000089 self.assertEqual(config.switches["baz"].driver, 'CiscoSX300')
Steve McIntyreaab5fd42014-12-23 13:22:51 +000090 self.assertEqual(config.switches["baz"].username, 'cisco')
91 self.assertEqual(config.switches["baz"].password, 'cisco')
Steve McIntyre1f2f47f2014-12-10 23:27:38 +000092
93if __name__ == '__main__':
94 unittest.main()