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 | 4e9fee5 | 2015-10-19 18:54:23 +0100 | [diff] [blame] | 49 | # 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 McIntyre | 1f2f47f | 2014-12-10 23:27:38 +0000 | [diff] [blame] | 56 | # 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 McIntyre | 5fa2265 | 2015-04-01 18:01:45 +0100 | [diff] [blame] | 60 | del config |
Steve McIntyre | 1f2f47f | 2014-12-10 23:27:38 +0000 | [diff] [blame] | 61 | |
| 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 McIntyre | 5fa2265 | 2015-04-01 18:01:45 +0100 | [diff] [blame] | 66 | del config |
Steve McIntyre | 1f2f47f | 2014-12-10 23:27:38 +0000 | [diff] [blame] | 67 | |
| 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 McIntyre | aab5fd4 | 2014-12-23 13:22:51 +0000 | [diff] [blame] | 71 | 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 McIntyre | 1f2f47f | 2014-12-10 23:27:38 +0000 | [diff] [blame] | 80 | self.assertEqual(len(config.switches), 3) |
Steve McIntyre | aab5fd4 | 2014-12-23 13:22:51 +0000 | [diff] [blame] | 81 | 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 McIntyre | 1f2f47f | 2014-12-10 23:27:38 +0000 | [diff] [blame] | 96 | self.assertEqual(config.switches["baz"].driver, 'CiscoSX300') |
Steve McIntyre | aab5fd4 | 2014-12-23 13:22:51 +0000 | [diff] [blame] | 97 | self.assertEqual(config.switches["baz"].username, 'cisco') |
| 98 | self.assertEqual(config.switches["baz"].password, 'cisco') |
Steve McIntyre | 1f2f47f | 2014-12-10 23:27:38 +0000 | [diff] [blame] | 99 | |
| 100 | if __name__ == '__main__': |
| 101 | unittest.main() |