Skip to content

Commit

Permalink
Fix issue: out of range sflow polling interval is accepted and stored…
Browse files Browse the repository at this point in the history
… in config_db (sonic-net#2847)

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
  • Loading branch information
Junchao-Mellanox authored and stephenxs committed Jan 15, 2024
1 parent f5f9a85 commit 59018c3
Show file tree
Hide file tree
Showing 2 changed files with 8 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 @@ -6308,7 +6308,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')
Expand Down
7 changes: 7 additions & 0 deletions tests/sflow_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit 59018c3

Please sign in to comment.