diff --git a/scripts/hostcfgd b/scripts/hostcfgd index 12f2fd1b..923f7724 100644 --- a/scripts/hostcfgd +++ b/scripts/hostcfgd @@ -1728,6 +1728,7 @@ class HostConfigDaemon: def callback(table, key, data): if data is None: op = "DEL" + data = {} else: op = "SET" return func(key, op, data) diff --git a/tests/common/mock_configdb.py b/tests/common/mock_configdb.py index 982a7f8c..d6c3c055 100644 --- a/tests/common/mock_configdb.py +++ b/tests/common/mock_configdb.py @@ -59,6 +59,9 @@ def get_table(self, table_name): def subscribe(self, table_name, callback): self.handlers[table_name] = callback + def publish(self, table_name, key, op, data): + self.handlers[table_name](key, op, data) + def listen(self, init_data_handler=None): for e in MockConfigDb.event_queue: self.handlers[e[0]](e[0], e[1], self.get_entry(e[0], e[1])) diff --git a/tests/hostcfgd/hostcfgd_tacacs_test.py b/tests/hostcfgd/hostcfgd_tacacs_test.py index 0577e61f..1364afb3 100644 --- a/tests/hostcfgd/hostcfgd_tacacs_test.py +++ b/tests/hostcfgd/hostcfgd_tacacs_test.py @@ -192,3 +192,43 @@ def test_hostcfgd_sshd_not_empty(self, test_name, test_data): ] mocked_syslog.assert_has_calls(expected) + @parameterized.expand(HOSTCFGD_TEST_TACACS_VECTOR) + def test_hostcfgd_delete_config_table(self, test_name, test_data): + """ + Test hostcfd delete config table check + + Args: + test_name(str): test name + test_data(dict): test data which contains initial Config Db tables, and expected results + + Returns: + None + """ + config_name = "config_db_local" + op_path = output_path + "/" + test_name + "_" + config_name + sop_path = sample_output_path + "/" + test_name + "_" + config_name + host_config_daemon = self.mock_hostcfgd(test_data, config_name, op_path, sop_path) + host_config_daemon.register_callbacks() + + # render with delete config table + original_syslog = hostcfgd.syslog + with mock.patch('hostcfgd.syslog.syslog') as mocked_syslog: + mocked_syslog.LOG_INFO = original_syslog.LOG_INFO + mocked_syslog.LOG_ERR = original_syslog.LOG_ERR + + # simulate subscribe callback + try: + host_config_daemon.__dict__['config_db'].publish('AAA', 'authorization', 'DEL', None) + except TypeError as e: + assert False + + # check sys log + expected = [ + mock.call(mocked_syslog.LOG_INFO, "file size check pass: {} size is (2139) bytes".format(hostcfgd.ETC_PAMD_SSHD)), + mock.call(mocked_syslog.LOG_INFO, "file size check pass: {} size is (4951) bytes".format(hostcfgd.ETC_PAMD_LOGIN)), + mock.call(mocked_syslog.LOG_INFO, "Found audisp-tacplus PID: "), + mock.call(mocked_syslog.LOG_INFO, "cmd - ['service', 'aaastatsd', 'stop']"), + mock.call(mocked_syslog.LOG_ERR, "['service', 'aaastatsd', 'stop'] - failed: return code - 1, output:\nNone"), + mock.call(mocked_syslog.LOG_INFO, "AAA Update: key: DEL, op: DEL, data: {}") + ] + mocked_syslog.assert_has_calls(expected)