From 5c9b21771a733944ff1dc8058250185fef62f2f7 Mon Sep 17 00:00:00 2001 From: Junchao-Mellanox <57339448+Junchao-Mellanox@users.noreply.github.com> Date: Mon, 5 Jun 2023 15:20:49 +0800 Subject: [PATCH] Fix issue: out of range sflow polling interval is accepted and stored in config_db (#2847) #### What I did Fixed issue: out of range sflow polling interval is accepted and stored in config_db. Reproduce step: ``` 1. Enable sflow feature: config feature state sflow enabled 2. Enable sflow itself: config sflow enable 3. Configure out of range polling interval: config sflow polling-interval 1. Error message is shown as expected 4. Save config: config save -y 5. Check "SFLOW" section inside config_db ``` As the interval is invalid, the expected behavior is that the interval is not saved to redis. But we see the invalid value was written to redis. #### How I did it Change `click.echo` to `ctx.fail` #### How to verify it 1. Manual test 2. Add a check in existing unit test case to cover the change --- config/main.py | 2 +- tests/sflow_test.py | 1 + 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/config/main.py b/config/main.py index aab334e712..450b9fc5c1 100644 --- a/config/main.py +++ b/config/main.py @@ -6631,7 +6631,7 @@ def polling_int(ctx, interval): """Set polling-interval for counter-sampling (0 to disable)""" if ADHOC_VALIDATION: if interval not in range(5, 301) and interval != 0: - click.echo("Polling interval must be between 5-300 (0 to disable)") + ctx.fail("Polling interval must be between 5-300 (0 to disable)") config_db = ValidatedConfigDBConnector(ctx.obj['db']) sflow_tbl = config_db.get_table('SFLOW') diff --git a/tests/sflow_test.py b/tests/sflow_test.py index da03ff396e..ecd622655e 100644 --- a/tests/sflow_test.py +++ b/tests/sflow_test.py @@ -237,6 +237,7 @@ def test_config_sflow_polling_interval(self): result = runner.invoke(config.config.commands["sflow"]. commands["polling-interval"], ["500"], obj=obj) print(result.exit_code, result.output) + assert result.exit_code != 0 assert "Polling interval must be between 5-300" in result.output # set to 20