From 90abc0716e5369624655c228496805d0e849f351 Mon Sep 17 00:00:00 2001 From: ganglv <88995770+ganglyu@users.noreply.github.com> Date: Thu, 12 May 2022 12:32:28 +0800 Subject: [PATCH] [config reload] Fix invalid rstrip. (#2157) What I did 'rstrip' behavior is wrong, if unit is 'gnmi.timer', the result is 'gn', and 'gnmi' is expected. How I did it Use 're.sub' to replace 'rstrip'. How to verify it Run unit test for sonic-utilities. Signed-off-by: Gang Lv ganglv@microsoft.com --- 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 61ce7751b7..924364ba11 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(re.sub('\.timer$', '', unit, 1)) return services diff --git a/tests/config_test.py b/tests/config_test.py index 87865334a2..5263b8e54d 100644 --- a/tests/config_test.py +++ b/tests/config_test.py @@ -89,6 +89,22 @@ def mock_run_command_side_effect(*args, **kwargs): 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')) + + if kwargs.get('return_cmd'): + if command == "systemctl list-dependencies --plain sonic-delayed.target | sed '1d'": + return 'gnmi.timer' + elif command == "systemctl list-dependencies --plain sonic.target | sed '1d'": + return 'swss' + elif command == "systemctl is-enabled gnmi.timer": + return 'enabled' + else: + return '' + # Load sonic-cfggen from source since /usr/local/bin/sonic-cfggen does not have .py extension. sonic_cfggen = load_module_from_source('sonic_cfggen', '/usr/local/bin/sonic-cfggen') @@ -168,6 +184,22 @@ def test_load_minigraph(self, get_cmd_module, setup_single_broadcom_asic): 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 + def test_load_minigraph_with_port_config_bad_format(self, get_cmd_module, setup_single_broadcom_asic): with mock.patch( "utilities_common.cli.run_command",