Skip to content

Commit

Permalink
Add unit test for show interfaces status / intfutil status
Browse files Browse the repository at this point in the history
Signed-off-by: Wenda Ni <[email protected]>
  • Loading branch information
wendani committed Nov 4, 2019
1 parent 2dd02bc commit f0faf8f
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 4 deletions.
20 changes: 16 additions & 4 deletions scripts/intfutil
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,18 @@ from tabulate import tabulate
from natsort import natsorted
from swsssdk import ConfigDBConnector
from pprint import pprint

import os

# mock the redis for unit test purposes #
try:
if os.environ["UTILITIES_UNIT_TESTING"] == "1":
modules_path = os.path.join(os.path.dirname(__file__), "..")
tests_path = os.path.join(modules_path, "sonic-utilities-tests")
sys.path.insert(0, modules_path)
sys.path.insert(0, tests_path)
import mock_tables.dbconnector
except KeyError:
pass

# ========================== Common interface-utils logic ==========================

Expand Down Expand Up @@ -114,9 +125,10 @@ def appl_db_sub_intf_keys_get(appl_db, sub_intf_list, sub_intf_name):
if sub_intf_name is None:
appl_db_sub_intf_keys = []
appl_db_intf_keys = appl_db.keys(appl_db.APPL_DB, "INTF_TABLE:*")
for appl_db_intf_key in appl_db_intf_keys:
if re.split(':', appl_db_intf_key, maxsplit=1)[-1].strip() in sub_intf_list:
appl_db_sub_intf_keys.append(appl_db_intf_key)
if appl_db_intf_keys is not None:
for appl_db_intf_key in appl_db_intf_keys:
if re.split(':', appl_db_intf_key, maxsplit=1)[-1].strip() in sub_intf_list:
appl_db_sub_intf_keys.append(appl_db_intf_key)
elif sub_intf_name in sub_intf_list:
appl_db_sub_intf_keys = appl_db.keys(appl_db.APPL_DB, "INTF_TABLE:%s" % sub_intf_name)
else:
Expand Down
48 changes: 48 additions & 0 deletions sonic-utilities-tests/intfutil_test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
import os
import sys
from click.testing import CliRunner
from unittest import TestCase
import subprocess

import show.main as show

root_path = os.path.dirname(os.path.abspath(__file__))
modules_path = os.path.dirname(root_path)
scripts_path = os.path.join(modules_path, "scripts")

class TestIntfutil(TestCase):
@classmethod
def setup_class(cls):
print("SETUP")
os.environ["PATH"] += os.pathsep + scripts_path
os.environ["UTILITIES_UNIT_TESTING"] = "1"

def setUp(self):
self.runner = CliRunner()

# Test 'show interfaces status' / 'intfutil status'
def test_intf_status(self):
# Test 'show interfaces status'
result = self.runner.invoke(show.cli.commands["interfaces"].commands["status"], [])
print >> sys.stderr, result.output
expected_output = (
"Interface Lanes Speed MTU Alias Vlan Oper Admin Type Asym PFC\n"
"----------- ------- ------- ----- --------- ------ ------ ------- --------------- ----------\n"
" Ethernet0 0 25G 9100 Ethernet0 routed down up QSFP28 or later off"
)
self.assertEqual(result.output.strip(), expected_output)

# Test 'intfutil status'
output = subprocess.check_output('intfutil status', stderr=subprocess.STDOUT, shell=True)
print >> sys.stderr, output
self.assertEqual(output.strip(), expected_output)
assert(0)

def test_intf_status_verbose(self):
result = self.runner.invoke(show.cli.commands["interfaces"].commands["status"], ["--verbose"])

@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 f0faf8f

Please sign in to comment.