Steve McIntyre | 1f2f47f | 2014-12-10 23:27:38 +0000 | [diff] [blame] | 1 | import unittest, os, sys |
| 2 | vlandpath = os.path.abspath(os.path.normpath(os.path.dirname(sys.argv[0]))) |
| 3 | sys.path.insert(0, vlandpath) |
| 4 | sys.path.insert(0, "%s/.." % vlandpath) |
| 5 | |
| 6 | os.chdir(vlandpath) |
| 7 | |
| 8 | from config.config import VlanConfig |
Steve McIntyre | 5fa2265 | 2015-04-01 18:01:45 +0100 | [diff] [blame] | 9 | from errors import ConfigError |
Steve McIntyre | 1f2f47f | 2014-12-10 23:27:38 +0000 | [diff] [blame] | 10 | |
| 11 | class 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 McIntyre | 5fa2265 | 2015-04-01 18:01:45 +0100 | [diff] [blame] | 17 | del config |
Steve McIntyre | 1f2f47f | 2014-12-10 23:27:38 +0000 | [diff] [blame] | 18 | |
Steve McIntyre | a987506 | 2014-12-12 17:15:15 +0000 | [diff] [blame] | 19 | # Check that we raise on broken database config values |
Steve McIntyre | 1f2f47f | 2014-12-10 23:27:38 +0000 | [diff] [blame] | 20 | def test_missing_database_config(self): |
| 21 | with self.assertRaisesRegexp(ConfigError, 'Invalid database'): |
| 22 | config = VlanConfig(filenames=("test-invalid-DB.cfg",)) |
Steve McIntyre | 5fa2265 | 2015-04-01 18:01:45 +0100 | [diff] [blame] | 23 | del config |
Steve McIntyre | 1f2f47f | 2014-12-10 23:27:38 +0000 | [diff] [blame] | 24 | |
| 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 McIntyre | 5fa2265 | 2015-04-01 18:01:45 +0100 | [diff] [blame] | 29 | del config |
Steve McIntyre | 1f2f47f | 2014-12-10 23:27:38 +0000 | [diff] [blame] | 30 | |
| 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 McIntyre | 5fa2265 | 2015-04-01 18:01:45 +0100 | [diff] [blame] | 35 | del config |
Steve McIntyre | 1f2f47f | 2014-12-10 23:27:38 +0000 | [diff] [blame] | 36 | |
Steve McIntyre | a987506 | 2014-12-12 17:15:15 +0000 | [diff] [blame] | 37 | # 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 McIntyre | 5fa2265 | 2015-04-01 18:01:45 +0100 | [diff] [blame] | 41 | del config |
Steve McIntyre | a987506 | 2014-12-12 17:15:15 +0000 | [diff] [blame] | 42 | |
Steve McIntyre | c370980 | 2015-10-19 18:34:53 +0100 | [diff] [blame^] | 43 | # 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 McIntyre | 1f2f47f | 2014-12-10 23:27:38 +0000 | [diff] [blame] | 49 | # 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 McIntyre | 5fa2265 | 2015-04-01 18:01:45 +0100 | [diff] [blame] | 53 | del config |
Steve McIntyre | 1f2f47f | 2014-12-10 23:27:38 +0000 | [diff] [blame] | 54 | |
| 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 McIntyre | 5fa2265 | 2015-04-01 18:01:45 +0100 | [diff] [blame] | 59 | del config |
Steve McIntyre | 1f2f47f | 2014-12-10 23:27:38 +0000 | [diff] [blame] | 60 | |
| 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 McIntyre | aab5fd4 | 2014-12-23 13:22:51 +0000 | [diff] [blame] | 64 | 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 McIntyre | 1f2f47f | 2014-12-10 23:27:38 +0000 | [diff] [blame] | 73 | self.assertEqual(len(config.switches), 3) |
Steve McIntyre | aab5fd4 | 2014-12-23 13:22:51 +0000 | [diff] [blame] | 74 | 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 McIntyre | 1f2f47f | 2014-12-10 23:27:38 +0000 | [diff] [blame] | 89 | self.assertEqual(config.switches["baz"].driver, 'CiscoSX300') |
Steve McIntyre | aab5fd4 | 2014-12-23 13:22:51 +0000 | [diff] [blame] | 90 | self.assertEqual(config.switches["baz"].username, 'cisco') |
| 91 | self.assertEqual(config.switches["baz"].password, 'cisco') |
Steve McIntyre | 1f2f47f | 2014-12-10 23:27:38 +0000 | [diff] [blame] | 92 | |
| 93 | if __name__ == '__main__': |
| 94 | unittest.main() |