From d1b3620bd4baa32289e6e876b74cdbad429abacf Mon Sep 17 00:00:00 2001 From: SuvarnaMeenakshi Date: Mon, 1 Nov 2021 06:00:04 -0700 Subject: [PATCH 1/7] [port2alias]: Fix to get right number of return values from get_port_config. Modified to get ports from all namespaces and added unit test cases. Signed-off-by: SuvarnaMeenakshi --- scripts/port2alias | 19 ++++++++++++++++++- tests/port2alias_test.py | 18 +++++++++++++++++- 2 files changed, 35 insertions(+), 2 deletions(-) diff --git a/scripts/port2alias b/scripts/port2alias index 2063f50bef..5a4ff8dd79 100755 --- a/scripts/port2alias +++ b/scripts/port2alias @@ -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 = ['-', '_'] @@ -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)) diff --git a/tests/port2alias_test.py b/tests/port2alias_test.py index 03b017f968..e7e37064ad 100644 --- a/tests/port2alias_test.py +++ b/tests/port2alias_test.py @@ -1,9 +1,11 @@ import os import sys from unittest import TestCase +from unittest import mock +from mock import patch +from io import StringIO from utilities_common.general import load_module_from_source - # Load the file under test port2alias_path = os.path.join(os.path.dirname(__file__), '..', 'scripts', 'port2alias') port2alias = load_module_from_source('port2alias', port2alias_path) @@ -18,6 +20,12 @@ def setUp(self): "Ethernet_11": {"alias" : "fortyG0/11"}, } + @mock.patch('sys.stdout') + def test_main(self, mock_stdout): + with patch('sys.stdin', StringIO("Ethernet0")): + port2alias.main() + mock_stdout.write.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") @@ -42,3 +50,11 @@ 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): + @mock.patch('sys.stdout') + def test_main(self, mock_stdout): + with patch('sys.stdin', StringIO("Ethernet0")): + os.environ["UTILITIES_UNIT_TESTING"] = "2" + port2alias = load_module_from_source('port2alias', port2alias_path) + port2alias.main() + mock_stdout.write.assert_called_with("Ethernet1/1") From 676f7eb590d33cb39acf30ba96aec31a5d28f98d Mon Sep 17 00:00:00 2001 From: SuvarnaMeenakshi Date: Mon, 8 Nov 2021 15:20:47 -0800 Subject: [PATCH 2/7] Fix mock of stdout.write. Signed-off-by: SuvarnaMeenakshi --- tests/port2alias_test.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/tests/port2alias_test.py b/tests/port2alias_test.py index e7e37064ad..3b4753b3b3 100644 --- a/tests/port2alias_test.py +++ b/tests/port2alias_test.py @@ -20,11 +20,11 @@ def setUp(self): "Ethernet_11": {"alias" : "fortyG0/11"}, } - @mock.patch('sys.stdout') + @mock.patch('sys.stdout.write') def test_main(self, mock_stdout): with patch('sys.stdin', StringIO("Ethernet0")): port2alias.main() - mock_stdout.write.assert_called_with("etp1") + mock_stdout.assert_called_with("etp1") def test_translate_line_single_word(self): self.assertEqual(port2alias.translate_line("1", self.ports),"1") @@ -51,10 +51,10 @@ def test_translate_line_empty_ports(self): self.assertEqual(port2alias.translate_line("Ethernet1\n", {}),"Ethernet1\n") class TestPort2AliasNamespace(TestCase): - @mock.patch('sys.stdout') + @mock.patch('sys.stdout.write') def test_main(self, mock_stdout): with patch('sys.stdin', StringIO("Ethernet0")): os.environ["UTILITIES_UNIT_TESTING"] = "2" port2alias = load_module_from_source('port2alias', port2alias_path) port2alias.main() - mock_stdout.write.assert_called_with("Ethernet1/1") + mock_stdout.assert_called_with("Ethernet1/1") From 1cd898bcc8f60233a356f9bb2888a616ce200975 Mon Sep 17 00:00:00 2001 From: Suvarna Meenakshi Date: Wed, 24 Nov 2021 08:44:54 +0000 Subject: [PATCH 3/7] Add setup and teardown functions. Signed-off-by: Suvarna Meenakshi --- tests/port2alias_test.py | 52 +++++++++++++++++++++++----------------- 1 file changed, 30 insertions(+), 22 deletions(-) diff --git a/tests/port2alias_test.py b/tests/port2alias_test.py index 3b4753b3b3..3ae0f1f721 100644 --- a/tests/port2alias_test.py +++ b/tests/port2alias_test.py @@ -8,11 +8,12 @@ from utilities_common.general import load_module_from_source # Load the file under test port2alias_path = os.path.join(os.path.dirname(__file__), '..', 'scripts', 'port2alias') -port2alias = load_module_from_source('port2alias', port2alias_path) class TestPort2Alias(TestCase): - def setUp(self): + @classmethod + def setUpClass(cls): + self.self.port2alias = load_module_from_source('port2alias', port2alias_path) self.ports = { "Ethernet1": {"alias" : "fortyG0/1"}, "Ethernet2": {"alias" : "fortyG0/2"}, @@ -23,38 +24,45 @@ def setUp(self): @mock.patch('sys.stdout.write') def test_main(self, mock_stdout): with patch('sys.stdin', StringIO("Ethernet0")): - port2alias.main() + self.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") - self.assertEqual(port2alias.translate_line("Ethernet1", self.ports),"fortyG0/1") - self.assertEqual(port2alias.translate_line("Ethernet1\n", self.ports),"fortyG0/1\n") - self.assertEqual(port2alias.translate_line("Ethernet2\n", self.ports),"fortyG0/2\n") - self.assertEqual(port2alias.translate_line("Ethernet10\n", self.ports),"fortyG0/10\n") - self.assertEqual(port2alias.translate_line("Ethernet20\n", self.ports),"Ethernet20\n") + self.assertEqual(self.port2alias.translate_line("1", self.ports),"1") + self.assertEqual(self.port2alias.translate_line("1\n", self.ports),"1\n") + self.assertEqual(self.port2alias.translate_line("Ethernet1", self.ports),"fortyG0/1") + self.assertEqual(self.port2alias.translate_line("Ethernet1\n", self.ports),"fortyG0/1\n") + self.assertEqual(self.port2alias.translate_line("Ethernet2\n", self.ports),"fortyG0/2\n") + self.assertEqual(self.port2alias.translate_line("Ethernet10\n", self.ports),"fortyG0/10\n") + self.assertEqual(self.port2alias.translate_line("Ethernet20\n", self.ports),"Ethernet20\n") def test_translate_line_with_symbol(self): - self.assertEqual(port2alias.translate_line("Ethernet_11\n", self.ports),"fortyG0/11\n") - self.assertEqual(port2alias.translate_line("Ethernet1_1\n", self.ports),"Ethernet1_1\n") - self.assertEqual(port2alias.translate_line("Ethernet1-1\n", self.ports),"Ethernet1-1\n") - self.assertEqual(port2alias.translate_line("Ethernet1/1\n", self.ports),"fortyG0/1/1\n") - self.assertEqual(port2alias.translate_line("Ethernet1:1\n", self.ports),"fortyG0/1:1\n") - self.assertEqual(port2alias.translate_line("Ethernet1 U\n", self.ports),"fortyG0/1 U\n") + self.assertEqual(self.port2alias.translate_line("Ethernet_11\n", self.ports),"fortyG0/11\n") + self.assertEqual(self.port2alias.translate_line("Ethernet1_1\n", self.ports),"Ethernet1_1\n") + self.assertEqual(self.port2alias.translate_line("Ethernet1-1\n", self.ports),"Ethernet1-1\n") + self.assertEqual(self.port2alias.translate_line("Ethernet1/1\n", self.ports),"fortyG0/1/1\n") + self.assertEqual(self.port2alias.translate_line("Ethernet1:1\n", self.ports),"fortyG0/1:1\n") + self.assertEqual(self.port2alias.translate_line("Ethernet1 U\n", self.ports),"fortyG0/1 U\n") def test_translate_line_multiple_words(self): - self.assertEqual(port2alias.translate_line(" Ethernet1 Ethernet2 \n", self.ports)," fortyG0/1 fortyG0/2 \n") - self.assertEqual(port2alias.translate_line("Ethernet1,Ethernet1,Ethernet1,Ethernet1\n", self.ports),"fortyG0/1,fortyG0/1,fortyG0/1,fortyG0/1\n") + self.assertEqual(self.port2alias.translate_line(" Ethernet1 Ethernet2 \n", self.ports)," fortyG0/1 fortyG0/2 \n") + self.assertEqual(self.port2alias.translate_line("Ethernet1,Ethernet1,Ethernet1,Ethernet1\n", self.ports),"fortyG0/1,fortyG0/1,fortyG0/1,fortyG0/1\n") def test_translate_line_empty_ports(self): - self.assertEqual(port2alias.translate_line("Ethernet1\n", {}),"Ethernet1\n") + self.assertEqual(self.port2alias.translate_line("Ethernet1\n", {}),"Ethernet1\n") class TestPort2AliasNamespace(TestCase): + @classmethod + def setUpClass(cls): + os.environ["UTILITIES_UNIT_TESTING"] = "2" + self.port2alias = load_module_from_source('port2alias', port2alias_path) + @mock.patch('sys.stdout.write') def test_main(self, mock_stdout): with patch('sys.stdin', StringIO("Ethernet0")): - os.environ["UTILITIES_UNIT_TESTING"] = "2" - port2alias = load_module_from_source('port2alias', port2alias_path) - port2alias.main() + self.port2alias.main() mock_stdout.assert_called_with("Ethernet1/1") + + @classmethod + def tearDownClass(cls): + os.environ["UTILITIES_UNIT_TESTING"] = "0" From e220de4529a5935c37e3a0d34f454c5b7cebb4d2 Mon Sep 17 00:00:00 2001 From: Suvarna Meenakshi Date: Wed, 24 Nov 2021 16:48:28 +0000 Subject: [PATCH 4/7] Fix Typo. Signed-off-by: Suvarna Meenakshi --- tests/port2alias_test.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/port2alias_test.py b/tests/port2alias_test.py index 3ae0f1f721..e770444211 100644 --- a/tests/port2alias_test.py +++ b/tests/port2alias_test.py @@ -13,7 +13,7 @@ class TestPort2Alias(TestCase): @classmethod def setUpClass(cls): - self.self.port2alias = load_module_from_source('port2alias', port2alias_path) + self.port2alias = load_module_from_source('port2alias', port2alias_path) self.ports = { "Ethernet1": {"alias" : "fortyG0/1"}, "Ethernet2": {"alias" : "fortyG0/2"}, From 93c9427f0d78b9e2439209e23710d2356d7b1947 Mon Sep 17 00:00:00 2001 From: Suvarna Meenakshi Date: Wed, 24 Nov 2021 17:35:54 +0000 Subject: [PATCH 5/7] Fix setup and teardown functions. Signed-off-by: Suvarna Meenakshi --- tests/port2alias_test.py | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/tests/port2alias_test.py b/tests/port2alias_test.py index e770444211..a4b343cc46 100644 --- a/tests/port2alias_test.py +++ b/tests/port2alias_test.py @@ -12,7 +12,7 @@ class TestPort2Alias(TestCase): @classmethod - def setUpClass(cls): + def setUp(self): self.port2alias = load_module_from_source('port2alias', port2alias_path) self.ports = { "Ethernet1": {"alias" : "fortyG0/1"}, @@ -53,7 +53,7 @@ def test_translate_line_empty_ports(self): class TestPort2AliasNamespace(TestCase): @classmethod - def setUpClass(cls): + def setUp(self): os.environ["UTILITIES_UNIT_TESTING"] = "2" self.port2alias = load_module_from_source('port2alias', port2alias_path) @@ -63,6 +63,5 @@ def test_main(self, mock_stdout): self.port2alias.main() mock_stdout.assert_called_with("Ethernet1/1") - @classmethod - def tearDownClass(cls): + def tearDown(self): os.environ["UTILITIES_UNIT_TESTING"] = "0" From 99d838ea290efa58ce77eec3b11f30ced74c0262 Mon Sep 17 00:00:00 2001 From: Suvarna Meenakshi Date: Wed, 24 Nov 2021 22:32:34 +0000 Subject: [PATCH 6/7] Load namespace config. Signed-off-by: Suvarna Meenakshi --- tests/port2alias_test.py | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/tests/port2alias_test.py b/tests/port2alias_test.py index a4b343cc46..2b9f93e0ab 100644 --- a/tests/port2alias_test.py +++ b/tests/port2alias_test.py @@ -4,6 +4,7 @@ from unittest import mock from mock import patch from io import StringIO +import tests.mock_tables.dbconnector from utilities_common.general import load_module_from_source # Load the file under test @@ -11,7 +12,6 @@ class TestPort2Alias(TestCase): - @classmethod def setUp(self): self.port2alias = load_module_from_source('port2alias', port2alias_path) self.ports = { @@ -52,9 +52,9 @@ def test_translate_line_empty_ports(self): self.assertEqual(self.port2alias.translate_line("Ethernet1\n", {}),"Ethernet1\n") class TestPort2AliasNamespace(TestCase): - @classmethod def setUp(self): os.environ["UTILITIES_UNIT_TESTING"] = "2" + tests.mock_tables.dbconnector.load_namespace_config() self.port2alias = load_module_from_source('port2alias', port2alias_path) @mock.patch('sys.stdout.write') @@ -65,3 +65,4 @@ def test_main(self, mock_stdout): def tearDown(self): os.environ["UTILITIES_UNIT_TESTING"] = "0" + tests.mock_tables.dbconnector.clean_up_config() From 9ec4998db8af449b0f0f8fd7bb386428299d001f Mon Sep 17 00:00:00 2001 From: Suvarna Meenakshi Date: Mon, 29 Nov 2021 20:08:46 +0000 Subject: [PATCH 7/7] Load and clean up namespace config before after test cases. Signed-off-by: Suvarna Meenakshi --- tests/port2alias_test.py | 23 ++++++++++++++++++----- 1 file changed, 18 insertions(+), 5 deletions(-) diff --git a/tests/port2alias_test.py b/tests/port2alias_test.py index 2b9f93e0ab..cb3d8d5cac 100644 --- a/tests/port2alias_test.py +++ b/tests/port2alias_test.py @@ -5,6 +5,7 @@ from mock import patch from io import StringIO import tests.mock_tables.dbconnector +import importlib from utilities_common.general import load_module_from_source # Load the file under test @@ -52,9 +53,15 @@ def test_translate_line_empty_ports(self): self.assertEqual(self.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() + def setUp(self): - os.environ["UTILITIES_UNIT_TESTING"] = "2" - tests.mock_tables.dbconnector.load_namespace_config() self.port2alias = load_module_from_source('port2alias', port2alias_path) @mock.patch('sys.stdout.write') @@ -63,6 +70,12 @@ def test_main(self, mock_stdout): self.port2alias.main() mock_stdout.assert_called_with("Ethernet1/1") - def tearDown(self): - os.environ["UTILITIES_UNIT_TESTING"] = "0" - tests.mock_tables.dbconnector.clean_up_config() + @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()