Skip to content

Commit 34d2c8c

Browse files
authored
[counterpoll] make the 'eni' command functional only on the DPU (#3679)
- What I did To make the 'eni' command only appear in the list of commands when the 'switch_type' is 'dpu'. This way the "counterpoll --help" output shows only what is supported on the system. Current output (contains "eni" while the switch_type is not "dpu"): - How I did it Added a register_dynamic_commands() to dynamically add the 'eni' command. Updated the unit tests. - How to verify it Run the counterpoll_test.py test Signed-off-by: Yakiv Huryk <yhuryk@nvidia.com>
1 parent 2f8508f commit 34d2c8c

File tree

2 files changed

+32
-6
lines changed

2 files changed

+32
-6
lines changed

counterpoll/main.py

+25-4
Original file line numberDiff line numberDiff line change
@@ -397,15 +397,12 @@ def disable(ctx):
397397

398398

399399
# ENI counter commands
400-
@cli.group()
400+
@click.group()
401401
@click.pass_context
402402
def eni(ctx):
403403
""" ENI counter commands """
404404
ctx.obj = ConfigDBConnector()
405405
ctx.obj.connect()
406-
if not is_dpu(ctx.obj):
407-
click.echo("ENI counters are not supported on non DPU platforms")
408-
exit(1)
409406

410407

411408
@eni.command(name='interval')
@@ -534,3 +531,27 @@ def disable(filename):
534531
def delay(filename):
535532
""" Delay counters in config_db file """
536533
_update_config_db_flex_counter_table("delay", filename)
534+
535+
536+
"""
537+
The list of dynamic commands that are added on a specific condition.
538+
Format:
539+
(click group/command, callback function)
540+
"""
541+
dynamic_commands = [
542+
(eni, is_dpu)
543+
]
544+
545+
546+
def register_dynamic_commands(cmds):
547+
"""
548+
Dynamically register commands based on condition callback.
549+
"""
550+
db = ConfigDBConnector()
551+
db.connect()
552+
for cmd, cb in cmds:
553+
if cb(db):
554+
cli.add_command(cmd)
555+
556+
557+
register_dynamic_commands(dynamic_commands)

tests/counterpoll_test.py

+7-2
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import pytest
55
import mock
66
import sys
7+
import importlib
78
from click.testing import CliRunner
89
from shutil import copyfile
910
from utilities_common.db import Db
@@ -248,13 +249,15 @@ def test_update_route_counter_interval(self):
248249
def test_update_eni_status(self, status):
249250
runner = CliRunner()
250251
result = runner.invoke(counterpoll.cli, ["eni", status])
251-
assert result.exit_code == 1
252-
assert result.output == "ENI counters are not supported on non DPU platforms\n"
252+
assert 'No such command "eni"' in result.output
253+
assert result.exit_code == 2
253254

254255
@pytest.mark.parametrize("status", ["disable", "enable"])
255256
@mock.patch('counterpoll.main.device_info.get_platform_info')
256257
def test_update_eni_status_dpu(self, mock_get_platform_info, status):
257258
mock_get_platform_info.return_value = {'switch_type': 'dpu'}
259+
importlib.reload(counterpoll)
260+
258261
runner = CliRunner()
259262
db = Db()
260263

@@ -267,6 +270,8 @@ def test_update_eni_status_dpu(self, mock_get_platform_info, status):
267270
@mock.patch('counterpoll.main.device_info.get_platform_info')
268271
def test_update_eni_interval(self, mock_get_platform_info):
269272
mock_get_platform_info.return_value = {'switch_type': 'dpu'}
273+
importlib.reload(counterpoll)
274+
270275
runner = CliRunner()
271276
db = Db()
272277
test_interval = "2000"

0 commit comments

Comments
 (0)