Skip to content

Commit 3297b7a

Browse files
author
Praveen Chaudhary
authored
[sflow_test.py]: Fix show sflow display. (sonic-net#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
1 parent 37f131e commit 3297b7a

File tree

4 files changed

+99
-8
lines changed

4 files changed

+99
-8
lines changed

show/main.py

+6-8
Original file line numberDiff line numberDiff line change
@@ -1714,20 +1714,18 @@ def policer(policer_name, verbose):
17141714
@click.pass_context
17151715
def sflow(ctx):
17161716
"""Show sFlow related information"""
1717-
config_db = ConfigDBConnector()
1718-
config_db.connect()
1719-
ctx.obj = {'db': config_db}
17201717
if ctx.invoked_subcommand is None:
1721-
show_sflow_global(config_db)
1718+
db = Db()
1719+
show_sflow_global(db.cfgdb)
17221720

17231721
#
17241722
# 'sflow command ("show sflow interface ...")
17251723
#
17261724
@sflow.command('interface')
1727-
@click.pass_context
1728-
def sflow_interface(ctx):
1725+
@clicommon.pass_db
1726+
def sflow_interface(db):
17291727
"""Show sFlow interface information"""
1730-
show_sflow_interface(ctx.obj['db'])
1728+
show_sflow_interface(db.cfgdb)
17311729

17321730
def sflow_appDB_connect():
17331731
db = SonicV2Connector(host='127.0.0.1')
@@ -1789,7 +1787,7 @@ def show_sflow_global(config_db):
17891787
click.echo("\n {} Collectors configured:".format(len(sflow_info)))
17901788
for collector_name in sorted(sflow_info.keys()):
17911789
click.echo(" Name: {}".format(collector_name).ljust(30) +
1792-
"IP addr: {}".format(sflow_info[collector_name]['collector_ip']).ljust(20) +
1790+
"IP addr: {} ".format(sflow_info[collector_name]['collector_ip']).ljust(25) +
17931791
"UDP port: {}".format(sflow_info[collector_name]['collector_port']))
17941792

17951793

tests/mock_tables/appl_db.json

+16
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,20 @@
11
{
2+
"SFLOW_SESSION_TABLE:Ethernet0": {
3+
"admin_state": "up",
4+
"sample_rate": "2500"
5+
},
6+
"SFLOW_SESSION_TABLE:Ethernet4": {
7+
"admin_state": "up",
8+
"sample_rate": "1000"
9+
},
10+
"SFLOW_SESSION_TABLE:Ethernet112": {
11+
"admin_state": "up",
12+
"sample_rate": "1000"
13+
},
14+
"SFLOW_SESSION_TABLE:Ethernet116": {
15+
"admin_state": "up",
16+
"sample_rate": "5000"
17+
},
218
"PORT_TABLE:Ethernet0": {
319
"index": "0",
420
"lanes": "0",

tests/mock_tables/config_db.json

+13
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,17 @@
11
{
2+
"SFLOW|global": {
3+
"admin_state": "up",
4+
"agent_id": "eth0",
5+
"polling_interval": "0"
6+
},
7+
"SFLOW_COLLECTOR|prod": {
8+
"collector_ip": "fe80::6e82:6aff:fe1e:cd8e",
9+
"collector_port": "6343"
10+
},
11+
"SFLOW_COLLECTOR|ser5": {
12+
"collector_ip": "172.21.35.15",
13+
"collector_port": "6343"
14+
},
215
"BREAKOUT_CFG|Ethernet0": {
316
"brkout_mode": "4x25G[10G]"
417
},

tests/sflow_test.py

+64
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
import os
2+
import sys
3+
import pytest
4+
from click.testing import CliRunner
5+
from utilities_common.db import Db
6+
7+
import show.main as show
8+
import mock_tables.dbconnector
9+
10+
# Expected output for 'show sflow'
11+
show_sflow_output = ''+ \
12+
"""
13+
sFlow Global Information:
14+
sFlow Admin State: up
15+
sFlow Polling Interval: 0
16+
sFlow AgentID: eth0
17+
18+
2 Collectors configured:
19+
Name: prod IP addr: fe80::6e82:6aff:fe1e:cd8e UDP port: 6343
20+
Name: ser5 IP addr: 172.21.35.15 UDP port: 6343
21+
"""
22+
23+
# Expected output for 'show sflow interface'
24+
show_sflow_intf_output = ''+ \
25+
"""
26+
sFlow interface configurations
27+
+-------------+---------------+-----------------+
28+
| Interface | Admin State | Sampling Rate |
29+
+=============+===============+=================+
30+
| Ethernet0 | up | 2500 |
31+
+-------------+---------------+-----------------+
32+
| Ethernet4 | up | 1000 |
33+
+-------------+---------------+-----------------+
34+
| Ethernet112 | up | 1000 |
35+
+-------------+---------------+-----------------+
36+
| Ethernet116 | up | 5000 |
37+
+-------------+---------------+-----------------+
38+
"""
39+
40+
class TestShowSflow(object):
41+
@classmethod
42+
def setup_class(cls):
43+
print("SETUP")
44+
os.environ["UTILITIES_UNIT_TESTING"] = "1"
45+
46+
def test_show_sflow(self):
47+
runner = CliRunner()
48+
result = runner.invoke(show.cli.commands["sflow"], [], obj=Db())
49+
print(sys.stderr, result.output)
50+
assert result.exit_code == 0
51+
assert result.output == show_sflow_output
52+
53+
def test_show_sflow_intf(self):
54+
runner = CliRunner()
55+
result = runner.invoke(show.cli.commands["sflow"].commands["interface"], [], obj=Db())
56+
print(sys.stderr, result.output)
57+
assert result.exit_code == 0
58+
assert result.output == show_sflow_intf_output
59+
60+
@classmethod
61+
def teardown_class(cls):
62+
print("TEARDOWN")
63+
os.environ["PATH"] = os.pathsep.join(os.environ["PATH"].split(os.pathsep)[:-1])
64+
os.environ["UTILITIES_UNIT_TESTING"] = "0"

0 commit comments

Comments
 (0)