Skip to content

Commit 90abc07

Browse files
authored
[config reload] Fix invalid rstrip. (sonic-net#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 [email protected]
1 parent fac1769 commit 90abc07

File tree

2 files changed

+33
-1
lines changed

2 files changed

+33
-1
lines changed

config/main.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -746,7 +746,7 @@ def _get_delayed_sonic_services():
746746
services = []
747747
for unit in timer:
748748
if state[timer.index(unit)] == "enabled":
749-
services.append(unit.rstrip(".timer"))
749+
services.append(re.sub('\.timer$', '', unit, 1))
750750
return services
751751

752752

tests/config_test.py

+32
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,22 @@ def mock_run_command_side_effect(*args, **kwargs):
8989
else:
9090
return ''
9191

92+
def mock_run_command_side_effect_gnmi(*args, **kwargs):
93+
command = args[0]
94+
95+
if kwargs.get('display_cmd'):
96+
click.echo(click.style("Running command: ", fg='cyan') + click.style(command, fg='green'))
97+
98+
if kwargs.get('return_cmd'):
99+
if command == "systemctl list-dependencies --plain sonic-delayed.target | sed '1d'":
100+
return 'gnmi.timer'
101+
elif command == "systemctl list-dependencies --plain sonic.target | sed '1d'":
102+
return 'swss'
103+
elif command == "systemctl is-enabled gnmi.timer":
104+
return 'enabled'
105+
else:
106+
return ''
107+
92108

93109
# Load sonic-cfggen from source since /usr/local/bin/sonic-cfggen does not have .py extension.
94110
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):
168184
mock_run_command.assert_any_call('systemctl reset-failed snmp')
169185
assert mock_run_command.call_count == 11
170186

187+
def test_load_minigraph_with_gnmi_timer(self, get_cmd_module, setup_single_broadcom_asic):
188+
with mock.patch("utilities_common.cli.run_command", mock.MagicMock(side_effect=mock_run_command_side_effect_gnmi)) as mock_run_command:
189+
(config, show) = get_cmd_module
190+
runner = CliRunner()
191+
result = runner.invoke(config.config.commands["load_minigraph"], ["-y"])
192+
print(result.exit_code)
193+
print(result.output)
194+
traceback.print_tb(result.exc_info[2])
195+
assert result.exit_code == 0
196+
assert "\n".join([l.rstrip() for l in result.output.split('\n')]) == load_minigraph_command_output
197+
# Verify "systemctl reset-failed" is called for services under sonic.target
198+
mock_run_command.assert_any_call('systemctl reset-failed swss')
199+
# Verify "systemctl reset-failed" is called for services under sonic-delayed.target
200+
mock_run_command.assert_any_call('systemctl reset-failed gnmi')
201+
assert mock_run_command.call_count == 11
202+
171203
def test_load_minigraph_with_port_config_bad_format(self, get_cmd_module, setup_single_broadcom_asic):
172204
with mock.patch(
173205
"utilities_common.cli.run_command",

0 commit comments

Comments
 (0)