Skip to content

Commit

Permalink
[20212][port2alias]: Fix to get right number of return values (#2188)
Browse files Browse the repository at this point in the history
What I did
Cherry-pick of #1906 to 202012 branch
Fix conflict also few changes done to cherry-pick:

get_port_config function takes asic id as argument in 2019/202012 branches
Keep load_source of port2alias as module, as is used in unit-test in 202012 branch
get_port_config was modified to return different set number of arguments in PR: sonic-net/sonic-buildimage#4222. Changes in this PR is:

To address the different set of return values
To get the ports from all namespaces
To add unit-test
Additional change done over the cherry-pick:
added setup_class method in test case to load single asic mock db, this was done in pfcwd_test teardown method in 201911 and master branches. It is not done in 202012 branch, hence added setup_class method.

How I did it
Modify port2alias to read ports from all namespaces and also add unit-test

How to verify it
Verified on single and multi-asic DUTs with 202012 image.
  • Loading branch information
SuvarnaMeenakshi authored Jun 13, 2022
1 parent b0d7062 commit 2fc40cd
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 2 deletions.
21 changes: 19 additions & 2 deletions scripts/port2alias
Original file line number Diff line number Diff line change
@@ -1,11 +1,25 @@
#!/usr/bin/env python3

import sys
import os
from io import StringIO

from portconfig import get_port_config
from sonic_py_common import device_info
from sonic_py_common import multi_asic

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

def translate_line(line, ports):
allowed_symbols = ['-', '_']
Expand All @@ -15,7 +29,7 @@ def translate_line(line, ports):
while end < len(line):
if line[end].isalnum() or line[end] in allowed_symbols:
pass
else:
else:
# End of a word
word = line[start:end]
if word in ports:
Expand All @@ -35,7 +49,10 @@ def translate_line(line, ports):

def main():
(platform, hwsku) = device_info.get_platform_and_hwsku()
(ports, _) = get_port_config(hwsku, platform)
ports = {}
for ns in multi_asic.get_namespace_list():
(ports_ns, _, _) = get_port_config(hwsku=hwsku, platform=platform, asic_name=ns)
ports.update(ports_ns)
for line in sys.stdin:
sys.stdout.write(translate_line(line, ports))

Expand Down
43 changes: 43 additions & 0 deletions tests/port2alias_test.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,25 @@
import sys
import os
from unittest import TestCase
from unittest import mock
from mock import patch
from io import StringIO
import tests.mock_tables.dbconnector
import importlib

import imp

port2alias = imp.load_source('port2alias', os.path.join(os.path.dirname(__file__), '..', 'scripts', 'port2alias'))

class TestPort2Alias(TestCase):
@classmethod
def setup_class(cls):
os.environ['UTILITIES_UNIT_TESTING'] = "0"
from .mock_tables import dbconnector
from .mock_tables import mock_single_asic
importlib.reload(mock_single_asic)
dbconnector.load_namespace_config()

def setUp(self):
self.ports = {
"Ethernet1": {"alias" : "fortyG0/1"},
Expand All @@ -15,6 +28,12 @@ def setUp(self):
"Ethernet_11": {"alias" : "fortyG0/11"},
}

@mock.patch('sys.stdout.write')
def test_main(self, mock_stdout):
with patch('sys.stdin', StringIO("Ethernet0")):
port2alias.main()
mock_stdout.assert_called_with("etp1")

def test_translate_line_single_word(self):
self.assertEqual(port2alias.translate_line("1", self.ports),"1")
self.assertEqual(port2alias.translate_line("1\n", self.ports),"1\n")
Expand All @@ -39,3 +58,27 @@ def test_translate_line_multiple_words(self):
def test_translate_line_empty_ports(self):
self.assertEqual(port2alias.translate_line("Ethernet1\n", {}),"Ethernet1\n")

class TestPort2AliasNamespace(TestCase):
@classmethod
def setup_class(cls):
os.environ['UTILITIES_UNIT_TESTING'] = "2"
from .mock_tables import dbconnector
from .mock_tables import mock_multi_asic
importlib.reload(mock_multi_asic)
dbconnector.load_namespace_config()

@mock.patch('sys.stdout.write')
def test_main(self, mock_stdout):
with patch('sys.stdin', StringIO("Ethernet0")):
port2alias.main()
mock_stdout.assert_called_with("Ethernet1/1")

@classmethod
def teardown_class(cls):
print("TEARDOWN")
os.environ['UTILITIES_UNIT_TESTING'] = "0"
# change back to single asic config
from .mock_tables import dbconnector
from .mock_tables import mock_single_asic
importlib.reload(mock_single_asic)
dbconnector.load_namespace_config()

0 comments on commit 2fc40cd

Please sign in to comment.