From 5a7fc7578dce10c7e2ab3fe6eca42ef17f393f10 Mon Sep 17 00:00:00 2001 From: ganglyu Date: Tue, 10 May 2022 12:17:12 +0800 Subject: [PATCH 1/3] [config reload] Fix invalid rstrip. Signed-off-by: Gang Lv ganglv@microsoft.com --- config/main.py | 2 +- tests/config_test.py | 6 +++--- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/config/main.py b/config/main.py index f6ad1aa041..2f067b17b8 100644 --- a/config/main.py +++ b/config/main.py @@ -746,7 +746,7 @@ def _get_delayed_sonic_services(): services = [] for unit in timer: if state[timer.index(unit)] == "enabled": - services.append(unit.rstrip(".timer")) + services.append(unit.replace(".timer", "")) return services diff --git a/tests/config_test.py b/tests/config_test.py index 87865334a2..c1cf3ac1b2 100644 --- a/tests/config_test.py +++ b/tests/config_test.py @@ -81,10 +81,10 @@ def mock_run_command_side_effect(*args, **kwargs): if kwargs.get('return_cmd'): if command == "systemctl list-dependencies --plain sonic-delayed.target | sed '1d'": - return 'snmp.timer' + return 'gnmi.timer' elif command == "systemctl list-dependencies --plain sonic.target | sed '1d'": return 'swss' - elif command == "systemctl is-enabled snmp.timer": + elif command == "systemctl is-enabled gnmi.timer": return 'enabled' else: return '' @@ -165,7 +165,7 @@ def test_load_minigraph(self, get_cmd_module, setup_single_broadcom_asic): # Verify "systemctl reset-failed" is called for services under sonic.target mock_run_command.assert_any_call('systemctl reset-failed swss') # Verify "systemctl reset-failed" is called for services under sonic-delayed.target - mock_run_command.assert_any_call('systemctl reset-failed snmp') + mock_run_command.assert_any_call('systemctl reset-failed gnmi') assert mock_run_command.call_count == 11 def test_load_minigraph_with_port_config_bad_format(self, get_cmd_module, setup_single_broadcom_asic): From b4446088745702bea27a301680ac14e8d0675a39 Mon Sep 17 00:00:00 2001 From: ganglyu Date: Tue, 10 May 2022 14:09:28 +0800 Subject: [PATCH 2/3] Use re.sub instead, and add new unit test. --- config/main.py | 2 +- tests/config_test.py | 32 ++++++++++++++++++++++++++++++++ 2 files changed, 33 insertions(+), 1 deletion(-) diff --git a/config/main.py b/config/main.py index 2f067b17b8..dc513e4562 100644 --- a/config/main.py +++ b/config/main.py @@ -746,7 +746,7 @@ def _get_delayed_sonic_services(): services = [] for unit in timer: if state[timer.index(unit)] == "enabled": - services.append(unit.replace(".timer", "")) + services.append(re.sub('.timer$', '', unit, 1)) return services diff --git a/tests/config_test.py b/tests/config_test.py index c1cf3ac1b2..5263b8e54d 100644 --- a/tests/config_test.py +++ b/tests/config_test.py @@ -76,6 +76,22 @@ def mock_run_command_side_effect(*args, **kwargs): command = args[0] + if kwargs.get('display_cmd'): + click.echo(click.style("Running command: ", fg='cyan') + click.style(command, fg='green')) + + if kwargs.get('return_cmd'): + if command == "systemctl list-dependencies --plain sonic-delayed.target | sed '1d'": + return 'snmp.timer' + elif command == "systemctl list-dependencies --plain sonic.target | sed '1d'": + return 'swss' + elif command == "systemctl is-enabled snmp.timer": + return 'enabled' + else: + return '' + +def mock_run_command_side_effect_gnmi(*args, **kwargs): + command = args[0] + if kwargs.get('display_cmd'): click.echo(click.style("Running command: ", fg='cyan') + click.style(command, fg='green')) @@ -165,6 +181,22 @@ def test_load_minigraph(self, get_cmd_module, setup_single_broadcom_asic): # Verify "systemctl reset-failed" is called for services under sonic.target mock_run_command.assert_any_call('systemctl reset-failed swss') # Verify "systemctl reset-failed" is called for services under sonic-delayed.target + mock_run_command.assert_any_call('systemctl reset-failed snmp') + assert mock_run_command.call_count == 11 + + def test_load_minigraph_with_gnmi_timer(self, get_cmd_module, setup_single_broadcom_asic): + with mock.patch("utilities_common.cli.run_command", mock.MagicMock(side_effect=mock_run_command_side_effect_gnmi)) as mock_run_command: + (config, show) = get_cmd_module + runner = CliRunner() + result = runner.invoke(config.config.commands["load_minigraph"], ["-y"]) + print(result.exit_code) + print(result.output) + traceback.print_tb(result.exc_info[2]) + assert result.exit_code == 0 + assert "\n".join([l.rstrip() for l in result.output.split('\n')]) == load_minigraph_command_output + # Verify "systemctl reset-failed" is called for services under sonic.target + mock_run_command.assert_any_call('systemctl reset-failed swss') + # Verify "systemctl reset-failed" is called for services under sonic-delayed.target mock_run_command.assert_any_call('systemctl reset-failed gnmi') assert mock_run_command.call_count == 11 From 719046799e6e01b2f648822a4fb2714917a006d2 Mon Sep 17 00:00:00 2001 From: ganglyu Date: Wed, 11 May 2022 13:54:50 +0800 Subject: [PATCH 3/3] Fix pattern. --- config/main.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/config/main.py b/config/main.py index dc513e4562..d06694cc90 100644 --- a/config/main.py +++ b/config/main.py @@ -746,7 +746,7 @@ def _get_delayed_sonic_services(): services = [] for unit in timer: if state[timer.index(unit)] == "enabled": - services.append(re.sub('.timer$', '', unit, 1)) + services.append(re.sub('\.timer$', '', unit, 1)) return services