|
| 1 | +import importlib.machinery |
| 2 | +import importlib.util |
| 3 | +import os |
| 4 | +import sys |
| 5 | +import swsssdk |
| 6 | + |
| 7 | +from parameterized import parameterized |
| 8 | +from unittest import TestCase, mock |
| 9 | +from tests.hostcfgd.test_vectors import hostcfgdTestVector |
| 10 | +from tests.hostcfgd.mock_configdb import MockConfigDb |
| 11 | + |
| 12 | + |
| 13 | +swsssdk.ConfigDBConnector = MockConfigDb |
| 14 | +test_path = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) |
| 15 | +modules_path = os.path.dirname(test_path) |
| 16 | +scripts_path = os.path.join(modules_path, "scripts") |
| 17 | +sys.path.insert(0, modules_path) |
| 18 | + |
| 19 | +# Load the file under test |
| 20 | +hostcfgd_path = os.path.join(scripts_path, 'hostcfgd') |
| 21 | +loader = importlib.machinery.SourceFileLoader('hostcfgd', hostcfgd_path) |
| 22 | +spec = importlib.util.spec_from_loader(loader.name, loader) |
| 23 | +hostcfgd = importlib.util.module_from_spec(spec) |
| 24 | +loader.exec_module(hostcfgd) |
| 25 | +sys.modules['hostcfgd'] = hostcfgd |
| 26 | + |
| 27 | + |
| 28 | +class TestHostcfgd(TestCase): |
| 29 | + """ |
| 30 | + Test hostcfd daemon - feature |
| 31 | + """ |
| 32 | + def __verify_table(self, table, expected_table): |
| 33 | + """ |
| 34 | + verify config db tables |
| 35 | +
|
| 36 | + Compares Config DB table (FEATURE) with expected output table |
| 37 | +
|
| 38 | + Args: |
| 39 | + table(dict): Current Config Db table |
| 40 | + expected_table(dict): Expected Config Db table |
| 41 | +
|
| 42 | + Returns: |
| 43 | + None |
| 44 | + """ |
| 45 | + isEqual = len(table) == len(expected_table) |
| 46 | + if isEqual: |
| 47 | + for key, fields in expected_table.items(): |
| 48 | + isEqual = isEqual and key in table |
| 49 | + if isEqual: |
| 50 | + for field, value in fields.items(): |
| 51 | + isEqual = isEqual and value == table[key][field] |
| 52 | + if not isEqual: |
| 53 | + break; |
| 54 | + else: |
| 55 | + break |
| 56 | + return isEqual |
| 57 | + |
| 58 | + @parameterized.expand(hostcfgdTestVector) |
| 59 | + def test_hostcfgd(self, name, testData): |
| 60 | + """ |
| 61 | + Test hostcfd daemon initialization |
| 62 | +
|
| 63 | + Args: |
| 64 | + name(str): test name |
| 65 | + test_data(dict): test data which contains initial Config Db tables, and expected results |
| 66 | +
|
| 67 | + Returns: |
| 68 | + None |
| 69 | + """ |
| 70 | + MockConfigDb.set_config_db(testData["config_db"]) |
| 71 | + with mock.patch("hostcfgd.subprocess") as mockSubprocess: |
| 72 | + hostConfigDaemon = hostcfgd.HostConfigDaemon() |
| 73 | + hostConfigDaemon.update_all_feature_states() |
| 74 | + assert self.__verify_table( |
| 75 | + MockConfigDb.get_config_db()["FEATURE"], |
| 76 | + testData["expected_config_db"]["FEATURE"] |
| 77 | + ), "Test failed for test data: {0}".format(testData) |
| 78 | + mockSubprocess.check_call.assert_has_calls(testData["expected_subprocess_calls"], any_order=True) |
0 commit comments