Skip to content

Commit 2cd7893

Browse files
author
Praveen Chaudhary
authored
[sflow_test.py]: tests for config sflow commands. (sonic-net#1112)
* [sflow_test.py]: tests for config sflow commands. Changes: -- show sflow use ctx.obj['db'] instead of creating new instance, this is must for test, else config change will not be reflected. -- config SFLOW tests for config sflow <enable|disable> config sflow agent-id <add | del> config sflow collector add|del config sflow interface <enable|disable> config sflow interface sample-rate Signed-off-by: Praveen Chaudhary <[email protected]>
1 parent adb8941 commit 2cd7893

File tree

3 files changed

+213
-8
lines changed

3 files changed

+213
-8
lines changed

show/main.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -1592,11 +1592,11 @@ def policer(policer_name, verbose):
15921592
# 'sflow command ("show sflow ...")
15931593
#
15941594
@cli.group(invoke_without_command=True)
1595+
@clicommon.pass_db
15951596
@click.pass_context
1596-
def sflow(ctx):
1597+
def sflow(ctx, db):
15971598
"""Show sFlow related information"""
15981599
if ctx.invoked_subcommand is None:
1599-
db = Db()
16001600
show_sflow_global(db.cfgdb)
16011601

16021602
#

tests/mock_tables/config_db.json

-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
{
22
"SFLOW|global": {
33
"admin_state": "up",
4-
"agent_id": "eth0",
54
"polling_interval": "0"
65
},
76
"SFLOW_COLLECTOR|prod": {

tests/sflow_test.py

+211-5
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,23 @@
11
import os
22
import sys
33
import pytest
4+
import mock
5+
46
from click.testing import CliRunner
57
from utilities_common.db import Db
68

79
import show.main as show
8-
import mock_tables.dbconnector
10+
import config.main as config
11+
12+
config.asic_type = mock.MagicMock(return_value = "broadcom")
913

1014
# Expected output for 'show sflow'
1115
show_sflow_output = ''+ \
1216
"""
1317
sFlow Global Information:
1418
sFlow Admin State: up
1519
sFlow Polling Interval: 0
16-
sFlow AgentID: eth0
20+
sFlow AgentID: default
1721
1822
2 Collectors configured:
1923
Name: prod IP addr: fe80::6e82:6aff:fe1e:cd8e UDP port: 6343
@@ -46,17 +50,219 @@ def setup_class(cls):
4650
def test_show_sflow(self):
4751
runner = CliRunner()
4852
result = runner.invoke(show.cli.commands["sflow"], [], obj=Db())
49-
print(sys.stderr, result.output)
53+
print(result.exit_code, result.output)
5054
assert result.exit_code == 0
5155
assert result.output == show_sflow_output
5256

5357
def test_show_sflow_intf(self):
5458
runner = CliRunner()
55-
result = runner.invoke(show.cli.commands["sflow"].commands["interface"], [], obj=Db())
56-
print(sys.stderr, result.output)
59+
result = runner.invoke(show.cli.commands["sflow"].commands["interface"], \
60+
[], obj=Db())
61+
print(result.exit_code, result.output)
5762
assert result.exit_code == 0
5863
assert result.output == show_sflow_intf_output
5964

65+
def test_config_sflow_disable_enable(self):
66+
# config sflow <enable|disable>
67+
db = Db()
68+
runner = CliRunner()
69+
obj = {'db':db.cfgdb}
70+
71+
#disable
72+
result = runner.invoke(config.config.commands["sflow"].\
73+
commands["disable"], [], obj=obj)
74+
print(result.exit_code, result.output)
75+
assert result.exit_code == 0
76+
77+
# change the output
78+
global show_sflow_output
79+
show_sflow_output_local = show_sflow_output.replace(\
80+
'Admin State: up', \
81+
'Admin State: down')
82+
83+
# run show and check
84+
result = runner.invoke(show.cli.commands["sflow"], [], obj=db)
85+
print(result.exit_code, result.output, show_sflow_output_local)
86+
assert result.exit_code == 0
87+
assert result.output == show_sflow_output_local
88+
89+
#enable
90+
result = runner.invoke(config.config.commands["sflow"].\
91+
commands["enable"], [], obj=obj)
92+
print(result.exit_code, result.output)
93+
assert result.exit_code == 0
94+
95+
# run show and check
96+
result = runner.invoke(show.cli.commands["sflow"], [], obj=db)
97+
print(result.exit_code, result.output)
98+
assert result.exit_code == 0
99+
assert result.output == show_sflow_output
100+
101+
return
102+
103+
def test_config_sflow_agent_id(self):
104+
db = Db()
105+
runner = CliRunner()
106+
obj = {'db':db.cfgdb}
107+
108+
# mock netifaces.interface
109+
config.netifaces.interfaces = mock.MagicMock(return_value = "Ethernet0")
110+
111+
# set agent-id
112+
result = runner.invoke(config.config.commands["sflow"].\
113+
commands["agent-id"].commands["add"], ["Ethernet0"], obj=obj)
114+
print(result.exit_code, result.output)
115+
assert result.exit_code == 0
116+
117+
# change the output
118+
global show_sflow_output
119+
show_sflow_output_local = \
120+
show_sflow_output.replace('default', 'Ethernet0')
121+
122+
# run show and check
123+
result = runner.invoke(show.cli.commands["sflow"], [], obj=db)
124+
print(result.exit_code, result.output, show_sflow_output_local)
125+
assert result.exit_code == 0
126+
assert result.output == show_sflow_output_local
127+
128+
#del agent id
129+
result = runner.invoke(config.config.commands["sflow"].\
130+
commands["agent-id"].commands["del"], [], obj=obj)
131+
print(result.exit_code, result.output)
132+
assert result.exit_code == 0
133+
134+
# run show and check
135+
result = runner.invoke(show.cli.commands["sflow"], [], obj=db)
136+
print(result.exit_code, result.output)
137+
assert result.exit_code == 0
138+
assert result.output == show_sflow_output
139+
140+
return
141+
142+
def test_config_sflow_collector(self):
143+
db = Db()
144+
runner = CliRunner()
145+
obj = {'db':db.cfgdb}
146+
147+
# del a collector
148+
result = runner.invoke(config.config.commands["sflow"].\
149+
commands["collector"].commands["del"], ["prod"], obj=obj)
150+
print(result.exit_code, result.output)
151+
assert result.exit_code == 0
152+
153+
# change the output
154+
global show_sflow_output
155+
show_sflow_output_local = show_sflow_output.replace(\
156+
"2 Collectors configured:\n\
157+
Name: prod IP addr: fe80::6e82:6aff:fe1e:cd8e UDP port: 6343\n\
158+
Name: ser5 IP addr: 172.21.35.15 UDP port: 6343", \
159+
"1 Collectors configured:\n\
160+
Name: ser5 IP addr: 172.21.35.15 UDP port: 6343")
161+
162+
# run show and check
163+
result = runner.invoke(show.cli.commands["sflow"], [], obj=db)
164+
print(result.exit_code, result.output, show_sflow_output_local)
165+
assert result.exit_code == 0
166+
assert result.output == show_sflow_output_local
167+
168+
# add collector
169+
result = runner.invoke(config.config.commands["sflow"].\
170+
commands["collector"].commands["add"], \
171+
["prod", "fe80::6e82:6aff:fe1e:cd8e"], obj=obj)
172+
assert result.exit_code == 0
173+
174+
# run show and check
175+
result = runner.invoke(show.cli.commands["sflow"], [], obj=db)
176+
print(result.exit_code, result.output)
177+
assert result.exit_code == 0
178+
assert result.output == show_sflow_output
179+
180+
return
181+
182+
def test_config_sflow_polling_interval(self):
183+
db = Db()
184+
runner = CliRunner()
185+
obj = {'db':db.cfgdb}
186+
187+
# set to 20
188+
result = runner.invoke(config.config.commands["sflow"].\
189+
commands["polling-interval"], ["20"], obj=obj)
190+
print(result.exit_code, result.output)
191+
assert result.exit_code == 0
192+
193+
# change the expected output
194+
global show_sflow_output
195+
show_sflow_output_local = show_sflow_output.replace(\
196+
'sFlow Polling Interval: 0', \
197+
'sFlow Polling Interval: 20')
198+
199+
# run show and check
200+
result = runner.invoke(show.cli.commands["sflow"], [], obj=db)
201+
print(result.exit_code, result.output)
202+
assert result.exit_code == 0
203+
assert result.output == show_sflow_output_local
204+
205+
#reset to 0, no need to verify this one
206+
result = runner.invoke(config.config.commands["sflow"].\
207+
commands["polling-interval"], ["0"], obj=obj)
208+
print(result.exit_code, result.output)
209+
assert result.exit_code == 0
210+
211+
return
212+
213+
def test_config_sflow_intf_enable_disable(self):
214+
db = Db()
215+
runner = CliRunner()
216+
obj = {'db':db.cfgdb}
217+
218+
# mock interface_name_is_valid
219+
config.interface_name_is_valid = mock.MagicMock(return_value = True)
220+
221+
# intf enable
222+
result = runner.invoke(config.config.commands["sflow"].\
223+
commands["interface"].commands["enable"], ["Ethernet1"], obj=obj)
224+
print(result.exit_code, result.output)
225+
assert result.exit_code == 0
226+
227+
# we can not use 'show sflow interface', becasue 'show sflow interface'
228+
# gets data from appDB, we need to fetch data from configDB for verification
229+
sflowSession = db.cfgdb.get_table('SFLOW_SESSION')
230+
assert sflowSession["Ethernet1"]["admin_state"] == "up"
231+
232+
# intf disable
233+
result = runner.invoke(config.config.commands["sflow"].\
234+
commands["interface"].commands["disable"], ["Ethernet1"], obj=obj)
235+
print(result.exit_code, result.output)
236+
assert result.exit_code == 0
237+
238+
# verify in configDb
239+
sflowSession = db.cfgdb.get_table('SFLOW_SESSION')
240+
assert sflowSession["Ethernet1"]["admin_state"] == "down"
241+
242+
return
243+
244+
def test_config_sflow_intf_sample_rate(self):
245+
db = Db()
246+
runner = CliRunner()
247+
obj = {'db':db.cfgdb}
248+
249+
# mock interface_name_is_valid
250+
config.interface_name_is_valid = mock.MagicMock(return_value = True)
251+
252+
# set sample-rate to 2500
253+
result = runner.invoke(config.config.commands["sflow"].\
254+
commands["interface"].commands["sample-rate"], \
255+
["Ethernet2", "2500"], obj=obj)
256+
print(result.exit_code, result.output)
257+
assert result.exit_code == 0
258+
259+
# we can not use 'show sflow interface', becasue 'show sflow interface'
260+
# gets data from appDB, we need to fetch data from configDB for verification
261+
sflowSession = db.cfgdb.get_table('SFLOW_SESSION')
262+
assert sflowSession["Ethernet2"]["sample_rate"] == "2500"
263+
264+
return
265+
60266
@classmethod
61267
def teardown_class(cls):
62268
print("TEARDOWN")

0 commit comments

Comments
 (0)