Skip to content

Commit d007fbe

Browse files
ayurkivayurkiv-nvda
ayurkiv
authored andcommitted
Add UT for show/clear
1 parent 7ec47e2 commit d007fbe

File tree

5 files changed

+152
-3
lines changed

5 files changed

+152
-3
lines changed

clear/main.py

+3
Original file line numberDiff line numberDiff line change
@@ -224,6 +224,9 @@ def drop():
224224
@drop.command('counters')
225225
def clear_pg_counters():
226226
"""Clear priority-group dropped packets counter """
227+
228+
if os.geteuid() != 0 and os.environ.get("UTILITIES_UNIT_TESTING", "0") != "2":
229+
exit("Root privileges are required for this operation")
227230
command = 'pg-drop -c clear'
228231
run_command(command)
229232

doc/Command-Reference.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -6095,7 +6095,7 @@ This command displays the user watermark for the queues (Egress shared pool occu
60956095
**show priority-group**
60966096
60976097
This command displays:
6098-
1) the user watermark or persistent-watermark for the Ingress "headroom" or "shared pool occupancy" per priority-group for all ports.
6098+
1) The user watermark or persistent-watermark for the Ingress "headroom" or "shared pool occupancy" per priority-group for all ports.
60996099
2) Dropped packets per priority-group for all ports
61006100
61016101
- Usage:

scripts/pg-drop

+2-2
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,10 @@
77
#####################################################################
88
import _pickle as pickle
99
import argparse
10-
import json
1110
import os
1211
import sys
1312
from collections import OrderedDict
1413

15-
from swsscommon.swsscommon import SonicV2Connector
1614
from natsort import natsorted
1715
from tabulate import tabulate
1816

@@ -28,6 +26,8 @@ try:
2826
except KeyError:
2927
pass
3028

29+
from swsscommon.swsscommon import SonicV2Connector
30+
3131
STATUS_NA = 'N/A'
3232

3333
COUNTER_TABLE_PREFIX = "COUNTERS:"

tests/mock_tables/counters_db.json

+74
Original file line numberDiff line numberDiff line change
@@ -294,6 +294,80 @@
294294
"SAI_SWITCH_STAT_OUT_DROP_REASON_RANGE_BASE": "1000",
295295
"SAI_SWITCH_STAT_OUT_CONFIGURED_DROP_REASONS_1_DROPPED_PKTS": "0"
296296
},
297+
298+
"COUNTERS:oid:0x1a00000000034f": {
299+
"SAI_INGRESS_PRIORITY_GROUP_STAT_DROPPED_PACKETS" : "800"
300+
},
301+
"COUNTERS:oid:0x1a000000000350" : {
302+
"SAI_INGRESS_PRIORITY_GROUP_STAT_DROPPED_PACKETS" : "801"
303+
},
304+
"COUNTERS:oid:0x1a000000000351" : {
305+
"SAI_INGRESS_PRIORITY_GROUP_STAT_DROPPED_PACKETS" : "802"
306+
},
307+
"COUNTERS:oid:0x1a000000000352" : {
308+
"SAI_INGRESS_PRIORITY_GROUP_STAT_DROPPED_PACKETS" : "803"
309+
},
310+
"COUNTERS:oid:0x1a000000000353" : {
311+
"SAI_INGRESS_PRIORITY_GROUP_STAT_DROPPED_PACKETS" : "804"
312+
},
313+
"COUNTERS:oid:0x1a000000000354" : {
314+
"SAI_INGRESS_PRIORITY_GROUP_STAT_DROPPED_PACKETS" : "805"
315+
},
316+
"COUNTERS:oid:0x1a000000000355" : {
317+
"SAI_INGRESS_PRIORITY_GROUP_STAT_DROPPED_PACKETS" : "806"
318+
},
319+
"COUNTERS:oid:0x1a000000000356" : {
320+
"SAI_INGRESS_PRIORITY_GROUP_STAT_DROPPED_PACKETS" : "807"
321+
},
322+
"COUNTERS:oid:0x1a000000000377" : {
323+
"SAI_INGRESS_PRIORITY_GROUP_STAT_DROPPED_PACKETS" : "400"
324+
},
325+
"COUNTERS:oid:0x1a000000000378" : {
326+
"SAI_INGRESS_PRIORITY_GROUP_STAT_DROPPED_PACKETS" : "401"
327+
},
328+
"COUNTERS:oid:0x1a000000000379" : {
329+
"SAI_INGRESS_PRIORITY_GROUP_STAT_DROPPED_PACKETS" : "402"
330+
},
331+
"COUNTERS:oid:0x1a00000000037a" : {
332+
"SAI_INGRESS_PRIORITY_GROUP_STAT_DROPPED_PACKETS" : "403"
333+
},
334+
"COUNTERS:oid:0x1a00000000037b" : {
335+
"SAI_INGRESS_PRIORITY_GROUP_STAT_DROPPED_PACKETS" : "404"
336+
},
337+
"COUNTERS:oid:0x1a00000000037c" : {
338+
"SAI_INGRESS_PRIORITY_GROUP_STAT_DROPPED_PACKETS" : "405"
339+
},
340+
"COUNTERS:oid:0x1a00000000037d" : {
341+
"SAI_INGRESS_PRIORITY_GROUP_STAT_DROPPED_PACKETS" : "406"
342+
},
343+
"COUNTERS:oid:0x1a00000000037e" : {
344+
"SAI_INGRESS_PRIORITY_GROUP_STAT_DROPPED_PACKETS" : "407"
345+
},
346+
"COUNTERS:oid:0x1a00000000039f" : {
347+
"SAI_INGRESS_PRIORITY_GROUP_STAT_DROPPED_PACKETS" : "100"
348+
},
349+
"COUNTERS:oid:0x1a0000000003a0" : {
350+
"SAI_INGRESS_PRIORITY_GROUP_STAT_DROPPED_PACKETS" : "101"
351+
},
352+
"COUNTERS:oid:0x1a0000000003a1" : {
353+
"SAI_INGRESS_PRIORITY_GROUP_STAT_DROPPED_PACKETS" : "102"
354+
},
355+
"COUNTERS:oid:0x1a0000000003a2" : {
356+
"SAI_INGRESS_PRIORITY_GROUP_STAT_DROPPED_PACKETS" : "103"
357+
},
358+
"COUNTERS:oid:0x1a0000000003a3" : {
359+
"SAI_INGRESS_PRIORITY_GROUP_STAT_DROPPED_PACKETS" : "104"
360+
},
361+
"COUNTERS:oid:0x1a0000000003a4" : {
362+
"SAI_INGRESS_PRIORITY_GROUP_STAT_DROPPED_PACKETS" : "105"
363+
},
364+
"COUNTERS:oid:0x1a0000000003a5" : {
365+
"SAI_INGRESS_PRIORITY_GROUP_STAT_DROPPED_PACKETS" : "106"
366+
},
367+
"COUNTERS:oid:0x1a0000000003a6" : {
368+
"SAI_INGRESS_PRIORITY_GROUP_STAT_DROPPED_PACKETS" : "107"
369+
},
370+
297371
"COUNTERS_PORT_NAME_MAP": {
298372
"Ethernet0": "oid:0x1000000000012",
299373
"Ethernet4": "oid:0x1000000000013",

tests/pgdropstat_test.py

+72
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
import os
2+
import sys
3+
4+
import show.main as show
5+
import clear.main as clear
6+
7+
from click.testing import CliRunner
8+
9+
test_path = os.path.dirname(os.path.abspath(__file__))
10+
modules_path = os.path.dirname(test_path)
11+
scripts_path = os.path.join(modules_path, "scripts")
12+
sys.path.insert(0, test_path)
13+
sys.path.insert(0, modules_path)
14+
15+
16+
show_pg_dropped_packet_stat="""\
17+
Ingress PG dropped packets:
18+
Port PG0 PG1 PG2 PG3 PG4 PG5 PG6 PG7
19+
--------- ----- ----- ----- ----- ----- ----- ----- -----
20+
Ethernet0 800 801 802 803 804 805 806 807
21+
Ethernet4 400 401 402 403 404 405 406 407
22+
Ethernet8 100 101 102 103 104 105 106 107
23+
"""
24+
25+
show_cleared_pg_dropped_packet_stat="""\
26+
Ingress PG dropped packets:
27+
Port PG0 PG1 PG2 PG3 PG4 PG5 PG6 PG7
28+
--------- ----- ----- ----- ----- ----- ----- ----- -----
29+
Ethernet0 0 0 0 0 0 0 0 0
30+
Ethernet4 0 0 0 0 0 0 0 0
31+
Ethernet8 0 0 0 0 0 0 0 0
32+
"""
33+
34+
class TestPgDropstat(object):
35+
@classmethod
36+
def setup_class(cls):
37+
os.environ["PATH"] += os.pathsep + scripts_path
38+
os.environ['UTILITIES_UNIT_TESTING'] = "2"
39+
print("SETUP")
40+
41+
def test_show_pg_drop_show(self):
42+
self.executor(clear_before_show = False)
43+
44+
def test_show_pg_drop_clear(self):
45+
self.executor(clear_before_show = True)
46+
47+
def executor(self, clear_before_show):
48+
runner = CliRunner()
49+
show_output = show_pg_dropped_packet_stat
50+
51+
# Clear stats
52+
if clear_before_show:
53+
result = runner.invoke(clear.cli.commands["priority-group"].commands["drop"].commands["counters"], [])
54+
assert result.exit_code == 0
55+
show_output = show_cleared_pg_dropped_packet_stat
56+
57+
result = runner.invoke(show.cli.commands["priority-group"].commands["drop"].commands["counters"], [])
58+
59+
print(result.exit_code)
60+
print(result.output)
61+
62+
assert result.exit_code == 0
63+
assert result.output == show_output
64+
65+
@classmethod
66+
def teardown_class(cls):
67+
os.environ["PATH"] = os.pathsep.join(os.environ["PATH"].split(os.pathsep)[:-1])
68+
os.environ['UTILITIES_UNIT_TESTING'] = "0"
69+
dropstat_dir_prefix = '/tmp/dropstat'
70+
dir_path = "{}-{}/".format(dropstat_dir_prefix, os.getuid())
71+
os.system("rm -rf {}".format(dir_path))
72+
print("TEARDOWN")

0 commit comments

Comments
 (0)