From 7a7305e98d71741b37ad3bb9350960bbc97b6a31 Mon Sep 17 00:00:00 2001 From: Stephen Sun <5379172+stephenxs@users.noreply.github.com> Date: Tue, 23 Jan 2024 07:43:42 +0800 Subject: [PATCH] Fix issue: out of range sflow polling interval is accepted and stored in config_db (#2847) (#3123) 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. Change `click.echo` to `ctx.fail` 1. Manual test 2. Add a check in existing unit test case to cover the change Co-authored-by: Junchao-Mellanox <57339448+Junchao-Mellanox@users.noreply.github.com> --- config/main.py | 2 +- tests/sflow_test.py | 7 +++++++ 2 files changed, 8 insertions(+), 1 deletion(-) diff --git a/config/main.py b/config/main.py index efb4ca41c2..9897e79ea3 100644 --- a/config/main.py +++ b/config/main.py @@ -6343,7 +6343,7 @@ def disable(ctx): def polling_int(ctx, interval): """Set polling-interval for counter-sampling (0 to disable)""" 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 = ctx.obj['db'] sflow_tbl = config_db.get_table('SFLOW') diff --git a/tests/sflow_test.py b/tests/sflow_test.py index ecb2782534..ad6c53462c 100644 --- a/tests/sflow_test.py +++ b/tests/sflow_test.py @@ -182,6 +182,13 @@ def test_config_sflow_polling_interval(self): runner = CliRunner() obj = {'db':db.cfgdb} + # set to 500 out of range + 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 result = runner.invoke(config.config.commands["sflow"]. commands["polling-interval"], ["20"], obj=obj)