From 3297b7aa233b84f1946e736d32810a9c6de6df7e Mon Sep 17 00:00:00 2001 From: Praveen Chaudhary Date: Sat, 15 Aug 2020 12:44:50 -0700 Subject: [PATCH] [sflow_test.py]: Fix show sflow display. (#1054) Changes: -- Display ipv4 address with left adjust of 25 width and with space before UDP. -- IPv6 address will be displayed as is. -- Add test data to appl_db.json and config_db.json -- add test file sflow_test.py -- use pass_db decorator for sflow_interface. -- since sflow needs ctx, create use Db() in function. Signed-off-by: Praveen Chaudhary pchaudhary@linkedin.com --- show/main.py | 14 +++---- tests/mock_tables/appl_db.json | 16 ++++++++ tests/mock_tables/config_db.json | 13 +++++++ tests/sflow_test.py | 64 ++++++++++++++++++++++++++++++++ 4 files changed, 99 insertions(+), 8 deletions(-) create mode 100644 tests/sflow_test.py diff --git a/show/main.py b/show/main.py index 30e1bed6f2c0..712ae674a468 100755 --- a/show/main.py +++ b/show/main.py @@ -1714,20 +1714,18 @@ def policer(policer_name, verbose): @click.pass_context def sflow(ctx): """Show sFlow related information""" - config_db = ConfigDBConnector() - config_db.connect() - ctx.obj = {'db': config_db} if ctx.invoked_subcommand is None: - show_sflow_global(config_db) + db = Db() + show_sflow_global(db.cfgdb) # # 'sflow command ("show sflow interface ...") # @sflow.command('interface') -@click.pass_context -def sflow_interface(ctx): +@clicommon.pass_db +def sflow_interface(db): """Show sFlow interface information""" - show_sflow_interface(ctx.obj['db']) + show_sflow_interface(db.cfgdb) def sflow_appDB_connect(): db = SonicV2Connector(host='127.0.0.1') @@ -1789,7 +1787,7 @@ def show_sflow_global(config_db): click.echo("\n {} Collectors configured:".format(len(sflow_info))) for collector_name in sorted(sflow_info.keys()): click.echo(" Name: {}".format(collector_name).ljust(30) + - "IP addr: {}".format(sflow_info[collector_name]['collector_ip']).ljust(20) + + "IP addr: {} ".format(sflow_info[collector_name]['collector_ip']).ljust(25) + "UDP port: {}".format(sflow_info[collector_name]['collector_port'])) diff --git a/tests/mock_tables/appl_db.json b/tests/mock_tables/appl_db.json index f851712caad1..f64914760bd8 100644 --- a/tests/mock_tables/appl_db.json +++ b/tests/mock_tables/appl_db.json @@ -1,4 +1,20 @@ { + "SFLOW_SESSION_TABLE:Ethernet0": { + "admin_state": "up", + "sample_rate": "2500" + }, + "SFLOW_SESSION_TABLE:Ethernet4": { + "admin_state": "up", + "sample_rate": "1000" + }, + "SFLOW_SESSION_TABLE:Ethernet112": { + "admin_state": "up", + "sample_rate": "1000" + }, + "SFLOW_SESSION_TABLE:Ethernet116": { + "admin_state": "up", + "sample_rate": "5000" + }, "PORT_TABLE:Ethernet0": { "index": "0", "lanes": "0", diff --git a/tests/mock_tables/config_db.json b/tests/mock_tables/config_db.json index 51bd7fb683ac..c06949321679 100644 --- a/tests/mock_tables/config_db.json +++ b/tests/mock_tables/config_db.json @@ -1,4 +1,17 @@ { + "SFLOW|global": { + "admin_state": "up", + "agent_id": "eth0", + "polling_interval": "0" + }, + "SFLOW_COLLECTOR|prod": { + "collector_ip": "fe80::6e82:6aff:fe1e:cd8e", + "collector_port": "6343" + }, + "SFLOW_COLLECTOR|ser5": { + "collector_ip": "172.21.35.15", + "collector_port": "6343" + }, "BREAKOUT_CFG|Ethernet0": { "brkout_mode": "4x25G[10G]" }, diff --git a/tests/sflow_test.py b/tests/sflow_test.py new file mode 100644 index 000000000000..e54c195b3dda --- /dev/null +++ b/tests/sflow_test.py @@ -0,0 +1,64 @@ +import os +import sys +import pytest +from click.testing import CliRunner +from utilities_common.db import Db + +import show.main as show +import mock_tables.dbconnector + +# Expected output for 'show sflow' +show_sflow_output = ''+ \ +""" +sFlow Global Information: + sFlow Admin State: up + sFlow Polling Interval: 0 + sFlow AgentID: eth0 + + 2 Collectors configured: + Name: prod IP addr: fe80::6e82:6aff:fe1e:cd8e UDP port: 6343 + Name: ser5 IP addr: 172.21.35.15 UDP port: 6343 +""" + +# Expected output for 'show sflow interface' +show_sflow_intf_output = ''+ \ +""" +sFlow interface configurations ++-------------+---------------+-----------------+ +| Interface | Admin State | Sampling Rate | ++=============+===============+=================+ +| Ethernet0 | up | 2500 | ++-------------+---------------+-----------------+ +| Ethernet4 | up | 1000 | ++-------------+---------------+-----------------+ +| Ethernet112 | up | 1000 | ++-------------+---------------+-----------------+ +| Ethernet116 | up | 5000 | ++-------------+---------------+-----------------+ +""" + +class TestShowSflow(object): + @classmethod + def setup_class(cls): + print("SETUP") + os.environ["UTILITIES_UNIT_TESTING"] = "1" + + def test_show_sflow(self): + runner = CliRunner() + result = runner.invoke(show.cli.commands["sflow"], [], obj=Db()) + print(sys.stderr, result.output) + assert result.exit_code == 0 + assert result.output == show_sflow_output + + def test_show_sflow_intf(self): + runner = CliRunner() + result = runner.invoke(show.cli.commands["sflow"].commands["interface"], [], obj=Db()) + print(sys.stderr, result.output) + assert result.exit_code == 0 + assert result.output == show_sflow_intf_output + + @classmethod + def teardown_class(cls): + print("TEARDOWN") + os.environ["PATH"] = os.pathsep.join(os.environ["PATH"].split(os.pathsep)[:-1]) + os.environ["UTILITIES_UNIT_TESTING"] = "0"