From e26aeed67933342406be5c9e7b4e8ca0f2836eed Mon Sep 17 00:00:00 2001 From: shi-su <67605788+shi-su@users.noreply.github.com> Date: Tue, 22 Sep 2020 21:02:50 -0700 Subject: [PATCH] Add CLI for configuring synchronous mode (#1094) * Add CLI for configuring synchronous mode * Integrate switch for sync mode as a config CLI * Add unit test --- config/main.py | 25 +++++++++++++++++++++++ tests/synchronous_mode_test.py | 37 ++++++++++++++++++++++++++++++++++ 2 files changed, 62 insertions(+) create mode 100644 tests/synchronous_mode_test.py diff --git a/config/main.py b/config/main.py index 0dc0d71b8c8a..d9672b61549b 100755 --- a/config/main.py +++ b/config/main.py @@ -1238,6 +1238,31 @@ def hostname(new_hostname): raise click.echo("Please note loaded setting will be lost after system reboot. To preserve setting, run `config save`.") +# +# 'synchronous_mode' command ('config synchronous_mode ...') +# +@config.command('synchronous_mode') +@click.argument('sync_mode', metavar='', required=True) +def synchronous_mode(sync_mode): + """ Enable or disable synchronous mode between orchagent and syncd \n + swss restart required to apply the configuration \n + Options to restart swss and apply the configuration: \n + 1. config save -y \n + config reload -y \n + 2. systemctl restart swss + """ + + if sync_mode == 'enable' or sync_mode == 'disable': + config_db = ConfigDBConnector() + config_db.connect() + config_db.mod_entry('DEVICE_METADATA' , 'localhost', {"synchronous_mode" : sync_mode}) + click.echo("""Wrote %s synchronous mode into CONFIG_DB, swss restart required to apply the configuration: \n + Option 1. config save -y \n + config reload -y \n + Option 2. systemctl restart swss""" % sync_mode) + else: + raise click.BadParameter("Error: Invalid argument %s, expect either enable or disable" % sync_mode) + # # 'portchannel' group ('config portchannel ...') # diff --git a/tests/synchronous_mode_test.py b/tests/synchronous_mode_test.py new file mode 100644 index 000000000000..05f341710c1c --- /dev/null +++ b/tests/synchronous_mode_test.py @@ -0,0 +1,37 @@ +from click.testing import CliRunner +import config.main as config + +class TestSynchronousMode(object): + @classmethod + def setup_class(cls): + print("SETUP") + + def __check_result(self, result_msg, mode): + if mode == "enable" or mode == "disable": + expected_msg = """Wrote %s synchronous mode into CONFIG_DB, swss restart required to apply the configuration: \n + Option 1. config save -y \n + config reload -y \n + Option 2. systemctl restart swss""" % mode + else: + expected_msg = "Error: Invalid argument %s, expect either enable or disable" % mode + + return expected_msg in result_msg + + + def test_synchronous_mode(self): + runner = CliRunner() + + result = runner.invoke(config.config.commands["synchronous_mode"], ["enable"]) + print(result.output) + assert result.exit_code == 0 + assert self.__check_result(result.output, "enable") + + result = runner.invoke(config.config.commands["synchronous_mode"], ["disable"]) + print(result.output) + assert result.exit_code == 0 + assert self.__check_result(result.output, "disable") + + result = runner.invoke(config.config.commands["synchronous_mode"], ["invalid-input"]) + print(result.output) + assert result.exit_code != 0 + assert self.__check_result(result.output, "invalid-input")