def test_hostcfgd(self, test_name, test_data, fs): """ Test hostcfd daemon initialization Args: test_name(str): test name test_data(dict): test data which contains initial Config Db tables, and expected results Returns: None """ fs.add_real_paths( swsscommon_package.__path__ ) # add real path of swsscommon for database_config.json fs.create_dir(hostcfgd.FeatureHandler.SYSTEMD_SYSTEM_DIR) MockConfigDb.set_config_db(test_data["config_db"]) with mock.patch("hostcfgd.subprocess") as mocked_subprocess: popen_mock = mock.Mock() attrs = test_data["popen_attributes"] popen_mock.configure_mock(**attrs) mocked_subprocess.Popen.return_value = popen_mock host_config_daemon = hostcfgd.HostConfigDaemon() host_config_daemon.feature_handler.update_all_features_config() assert self.__verify_table( MockConfigDb.get_config_db()["FEATURE"], test_data["expected_config_db"]["FEATURE"] ), "Test failed for test data: {0}".format(test_data) mocked_subprocess.check_call.assert_has_calls( test_data["expected_subprocess_calls"], any_order=True) self.__verify_fs(test_data["config_db"]["FEATURE"])
def test_sync_state_field(self, test_scenario_name, config_data, fs): """Tests the method `sync_state_field(...)` of `FeatureHandler` class. Args: test_secnario_name: A string indicates different testing scenario. config_data: A dictionary contains initial `CONFIG_DB` tables and expected results. Returns: Boolean value indicates whether test will pass or not. """ # add real path of sesscommon for database_config.json fs.add_real_paths(swsscommon_package.__path__) fs.create_dir(hostcfgd.FeatureHandler.SYSTEMD_SYSTEM_DIR) MockConfigDb.set_config_db(config_data['config_db']) feature_state_table_mock = mock.Mock() with mock.patch('hostcfgd.subprocess') as mocked_subprocess: popen_mock = mock.Mock() attrs = config_data['popen_attributes'] popen_mock.configure_mock(**attrs) mocked_subprocess.Popen.return_value = popen_mock device_config = {} device_config['DEVICE_METADATA'] = MockConfigDb.CONFIG_DB[ 'DEVICE_METADATA'] feature_handler = hostcfgd.FeatureHandler( MockConfigDb(), feature_state_table_mock, device_config) feature_table = MockConfigDb.CONFIG_DB['FEATURE'] feature_handler.sync_state_field(feature_table) is_any_difference = self.checks_config_table( MockConfigDb.get_config_db()['FEATURE'], config_data['expected_config_db']['FEATURE']) assert is_any_difference, "'FEATURE' table in 'CONFIG_DB' is modified unexpectedly!" feature_table_state_db_calls = self.get_state_db_set_calls( feature_table) self.checks_systemd_config_file( config_data['config_db']['FEATURE']) mocked_subprocess.check_call.assert_has_calls( config_data['enable_feature_subprocess_calls'], any_order=True) mocked_subprocess.check_call.assert_has_calls( config_data['daemon_reload_subprocess_call'], any_order=True) feature_state_table_mock.set.assert_has_calls( feature_table_state_db_calls) self.checks_systemd_config_file( config_data['config_db']['FEATURE'])
def test_hostcfgd_feature_handler(self, test_name, test_data, fs): """ Test feature config capability in the hostcfd Args: test_name(str): test name test_data(dict): test data which contains initial Config Db tables, and expected results Returns: None """ fs.add_real_paths( swsscommon_package.__path__ ) # add real path of swsscommon for database_config.json fs.create_dir(hostcfgd.FeatureHandler.SYSTEMD_SYSTEM_DIR) MockConfigDb.set_config_db(test_data['config_db']) feature_state_table_mock = mock.Mock() with mock.patch('hostcfgd.subprocess') as mocked_subprocess: popen_mock = mock.Mock() attrs = test_data['popen_attributes'] popen_mock.configure_mock(**attrs) mocked_subprocess.Popen.return_value = popen_mock # Initialize Feature Handler device_config = {} device_config['DEVICE_METADATA'] = MockConfigDb.CONFIG_DB[ 'DEVICE_METADATA'] feature_handler = hostcfgd.FeatureHandler( MockConfigDb(), feature_state_table_mock, device_config) # sync the state field and Handle Feature Updates features = MockConfigDb.CONFIG_DB['FEATURE'] feature_handler.sync_state_field(features) for key, fvs in features.items(): feature_handler.handle(key, 'SET', fvs) # Verify if the updates are properly updated assert self.__verify_table( MockConfigDb.get_config_db()['FEATURE'], feature_state_table_mock, test_data['expected_config_db']['FEATURE'] ), 'Test failed for test data: {0}'.format(test_data) mocked_subprocess.check_call.assert_has_calls( test_data['expected_subprocess_calls'], any_order=True) self.__verify_fs(test_data['config_db']['FEATURE'])