Skip to content

Commit

Permalink
[config reload] Fix invalid rstrip. (#2157)
Browse files Browse the repository at this point in the history
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
  • Loading branch information
ganglyu authored May 12, 2022
1 parent fac1769 commit 90abc07
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 1 deletion.
2 changes: 1 addition & 1 deletion config/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -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


Expand Down
32 changes: 32 additions & 0 deletions tests/config_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand Down Expand Up @@ -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",
Expand Down

0 comments on commit 90abc07

Please sign in to comment.