Skip to content

Commit

Permalink
[sflow_test.py]: Fix show sflow display. (sonic-net#1054)
Browse files Browse the repository at this point in the history
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
  • Loading branch information
Praveen Chaudhary authored Aug 15, 2020
1 parent 37f131e commit 3297b7a
Show file tree
Hide file tree
Showing 4 changed files with 99 additions and 8 deletions.
14 changes: 6 additions & 8 deletions show/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand Down Expand Up @@ -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']))


Expand Down
16 changes: 16 additions & 0 deletions tests/mock_tables/appl_db.json
Original file line number Diff line number Diff line change
@@ -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",
Expand Down
13 changes: 13 additions & 0 deletions tests/mock_tables/config_db.json
Original file line number Diff line number Diff line change
@@ -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]"
},
Expand Down
64 changes: 64 additions & 0 deletions tests/sflow_test.py
Original file line number Diff line number Diff line change
@@ -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"

0 comments on commit 3297b7a

Please sign in to comment.