From 1e7c2e2dd17c24a9282e4a770eaf2702b52a449d Mon Sep 17 00:00:00 2001 From: Joe LeVeque Date: Fri, 31 Jul 2020 23:52:26 +0000 Subject: [PATCH 01/15] [tests] Add unit tests for 'show platform ...' commands --- .../show/show_platform_tests.py | 47 +++++++++++++++++++ 1 file changed, 47 insertions(+) create mode 100644 sonic-utilities-tests/show/show_platform_tests.py diff --git a/sonic-utilities-tests/show/show_platform_tests.py b/sonic-utilities-tests/show/show_platform_tests.py new file mode 100644 index 0000000000..afac4423eb --- /dev/null +++ b/sonic-utilities-tests/show/show_platform_tests.py @@ -0,0 +1,47 @@ +import os +import sys +import textwrap + +from click.testing import CliRunner +from unittest import TestCase, mock + +test_path = os.path.dirname(os.path.abspath(__file__)) +modules_path = os.path.dirname(test_path) +sys.path.insert(0, test_path) +sys.path.insert(0, modules_path) + +import show.main as show + + +TEST_PLATFORM = "x86_64-mlnx_msn2700-r0" +TEST_HWSKU = "Mellanox-SN2700" +TEST_ASIC_TYPE = "mellanox" + + +class TestShowPlatform(TestCase): + @classmethod + def setup_class(cls): + print("SETUP") + os.environ["UTILITIES_UNIT_TESTING"] = "1" + + def setUp(self): + self.runner = CliRunner() + + # Test 'show platform summary' + def test_summary(self): + expected_output = """\ + Platform: {} + HwSKU: {} + ASIC: {} + """.format(TEST_PLATFORM, TEST_HWSKU, TEST_ASIC_TYPE) + + with mock.patch("show.get_hw_info_dict", + return_value={"platform": TEST_PLATFORM, "hwsku": TEST_HWSKU, "asic_type": TEST_ASIC_TYPE}): + result = self.runner.invoke(show.cli.commands["platform"].commands["summary"], []) + assert result.output == textwrap.dedent(expected_output) + + @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" From 817f7427476b9bcc901462366bf7f35aec48d4ef Mon Sep 17 00:00:00 2001 From: Joe LeVeque Date: Sat, 1 Aug 2020 00:54:47 +0000 Subject: [PATCH 02/15] Add __init__.py to show/ dir --- sonic-utilities-tests/show/__init__.py | 0 1 file changed, 0 insertions(+), 0 deletions(-) create mode 100644 sonic-utilities-tests/show/__init__.py diff --git a/sonic-utilities-tests/show/__init__.py b/sonic-utilities-tests/show/__init__.py new file mode 100644 index 0000000000..e69de29bb2 From fc9bf089d66c28d5bce0492bc34c77ca43694cb0 Mon Sep 17 00:00:00 2001 From: Joe LeVeque Date: Sat, 1 Aug 2020 01:06:37 +0000 Subject: [PATCH 03/15] Move file out of subdir --- sonic-utilities-tests/show/__init__.py | 0 sonic-utilities-tests/{show => }/show_platform_tests.py | 0 2 files changed, 0 insertions(+), 0 deletions(-) delete mode 100644 sonic-utilities-tests/show/__init__.py rename sonic-utilities-tests/{show => }/show_platform_tests.py (100%) diff --git a/sonic-utilities-tests/show/__init__.py b/sonic-utilities-tests/show/__init__.py deleted file mode 100644 index e69de29bb2..0000000000 diff --git a/sonic-utilities-tests/show/show_platform_tests.py b/sonic-utilities-tests/show_platform_tests.py similarity index 100% rename from sonic-utilities-tests/show/show_platform_tests.py rename to sonic-utilities-tests/show_platform_tests.py From 01a8d74e99d3482c2ed7478268a2f861a4e5de30 Mon Sep 17 00:00:00 2001 From: Joe LeVeque Date: Sat, 1 Aug 2020 01:15:55 +0000 Subject: [PATCH 04/15] Move file back and rename properly so pytest collects it --- .../{show_platform_tests.py => show/show_platform_test.py} | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename sonic-utilities-tests/{show_platform_tests.py => show/show_platform_test.py} (100%) diff --git a/sonic-utilities-tests/show_platform_tests.py b/sonic-utilities-tests/show/show_platform_test.py similarity index 100% rename from sonic-utilities-tests/show_platform_tests.py rename to sonic-utilities-tests/show/show_platform_test.py From 061f793f18113bfdde6135504a6584513207f460 Mon Sep 17 00:00:00 2001 From: Joe LeVeque Date: Sat, 1 Aug 2020 01:44:34 +0000 Subject: [PATCH 05/15] Add __init__.py back in hopes it helps pytest to collect the dir --- sonic-utilities-tests/show/__init__.py | 0 1 file changed, 0 insertions(+), 0 deletions(-) create mode 100644 sonic-utilities-tests/show/__init__.py diff --git a/sonic-utilities-tests/show/__init__.py b/sonic-utilities-tests/show/__init__.py new file mode 100644 index 0000000000..e69de29bb2 From a78c197e263591f9f2a1ee349a937070687427bd Mon Sep 17 00:00:00 2001 From: Joe LeVeque Date: Sat, 1 Aug 2020 01:56:24 +0000 Subject: [PATCH 06/15] Move file back under test root --- sonic-utilities-tests/show/__init__.py | 0 sonic-utilities-tests/{show => }/show_platform_test.py | 0 2 files changed, 0 insertions(+), 0 deletions(-) delete mode 100644 sonic-utilities-tests/show/__init__.py rename sonic-utilities-tests/{show => }/show_platform_test.py (100%) diff --git a/sonic-utilities-tests/show/__init__.py b/sonic-utilities-tests/show/__init__.py deleted file mode 100644 index e69de29bb2..0000000000 diff --git a/sonic-utilities-tests/show/show_platform_test.py b/sonic-utilities-tests/show_platform_test.py similarity index 100% rename from sonic-utilities-tests/show/show_platform_test.py rename to sonic-utilities-tests/show_platform_test.py From 9662380ed02af201a8f98e4de7163498ccfb62ff Mon Sep 17 00:00:00 2001 From: Joe LeVeque Date: Sat, 1 Aug 2020 02:01:01 +0000 Subject: [PATCH 07/15] Don't import mock from unittest --- sonic-utilities-tests/show_platform_test.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/sonic-utilities-tests/show_platform_test.py b/sonic-utilities-tests/show_platform_test.py index afac4423eb..d806e8b1db 100644 --- a/sonic-utilities-tests/show_platform_test.py +++ b/sonic-utilities-tests/show_platform_test.py @@ -2,8 +2,9 @@ import sys import textwrap +import mock from click.testing import CliRunner -from unittest import TestCase, mock +from unittest import TestCase test_path = os.path.dirname(os.path.abspath(__file__)) modules_path = os.path.dirname(test_path) From 1ded7ba4fd0a57b2c74fd7f1a33d096ee286f017 Mon Sep 17 00:00:00 2001 From: Joe LeVeque Date: Sat, 1 Aug 2020 02:23:45 +0000 Subject: [PATCH 08/15] function in is main --- sonic-utilities-tests/show_platform_test.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/sonic-utilities-tests/show_platform_test.py b/sonic-utilities-tests/show_platform_test.py index d806e8b1db..8e88499691 100644 --- a/sonic-utilities-tests/show_platform_test.py +++ b/sonic-utilities-tests/show_platform_test.py @@ -36,7 +36,7 @@ def test_summary(self): ASIC: {} """.format(TEST_PLATFORM, TEST_HWSKU, TEST_ASIC_TYPE) - with mock.patch("show.get_hw_info_dict", + with mock.patch("show.main.get_hw_info_dict", return_value={"platform": TEST_PLATFORM, "hwsku": TEST_HWSKU, "asic_type": TEST_ASIC_TYPE}): result = self.runner.invoke(show.cli.commands["platform"].commands["summary"], []) assert result.output == textwrap.dedent(expected_output) From 8dba5ec0347c1db31d514321a61e39433bc67642 Mon Sep 17 00:00:00 2001 From: Joe LeVeque Date: Sat, 1 Aug 2020 03:05:58 +0000 Subject: [PATCH 09/15] Add comment regarding other subcommands --- sonic-utilities-tests/show_platform_test.py | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/sonic-utilities-tests/show_platform_test.py b/sonic-utilities-tests/show_platform_test.py index 8e88499691..a3021ee56c 100644 --- a/sonic-utilities-tests/show_platform_test.py +++ b/sonic-utilities-tests/show_platform_test.py @@ -19,6 +19,21 @@ TEST_ASIC_TYPE = "mellanox" +""" + Note: The following 'show platform' commands simply call other SONiC + CLI utilities, so the unit tests for the other utilities will cover + testing their functionality: + + show platform fan + show platform firmware + show platform mlnx + show platform psustatus + show platform ssdhealth + show platform summary + show platform syseeprom + show platform temperature +""" + class TestShowPlatform(TestCase): @classmethod def setup_class(cls): From bba2a82e047daecc79e32b63932cf4d659d21f7a Mon Sep 17 00:00:00 2001 From: Joe LeVeque Date: Sat, 1 Aug 2020 03:09:29 +0000 Subject: [PATCH 10/15] Tweak/fix comment --- sonic-utilities-tests/show_platform_test.py | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/sonic-utilities-tests/show_platform_test.py b/sonic-utilities-tests/show_platform_test.py index a3021ee56c..3e3867a6d0 100644 --- a/sonic-utilities-tests/show_platform_test.py +++ b/sonic-utilities-tests/show_platform_test.py @@ -21,15 +21,14 @@ """ Note: The following 'show platform' commands simply call other SONiC - CLI utilities, so the unit tests for the other utilities will cover - testing their functionality: + CLI utilities, so the unit tests for the other utilities are expected + to cover testing their functionality: show platform fan show platform firmware show platform mlnx show platform psustatus show platform ssdhealth - show platform summary show platform syseeprom show platform temperature """ From 3b219b031905897df87787303493ba2aa4891ad4 Mon Sep 17 00:00:00 2001 From: Joe LeVeque Date: Sun, 2 Aug 2020 00:53:32 +0000 Subject: [PATCH 11/15] Remove 'test_suite' from setup.py, as I don't believe it's necessary if using pytest --- setup.py | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/setup.py b/setup.py index bece679708..dd465f04de 100644 --- a/setup.py +++ b/setup.py @@ -175,6 +175,5 @@ 'Programming Language :: Python :: 2.7', 'Topic :: Utilities', ], - keywords='sonic SONiC utilities command line cli CLI', - test_suite='setup.get_test_suite' + keywords='sonic SONiC utilities command line cli CLI' ) From 1f22f92fc4a2e9d030caf46959c182130e28a90c Mon Sep 17 00:00:00 2001 From: Joe LeVeque Date: Sun, 2 Aug 2020 01:01:40 +0000 Subject: [PATCH 12/15] Revert "Remove 'test_suite' from setup.py, as I don't believe it's necessary if using pytest" This reverts commit ea3fd8ac5ce836025755d48f32af952a39665f7f. --- setup.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/setup.py b/setup.py index dd465f04de..bece679708 100644 --- a/setup.py +++ b/setup.py @@ -175,5 +175,6 @@ 'Programming Language :: Python :: 2.7', 'Topic :: Utilities', ], - keywords='sonic SONiC utilities command line cli CLI' + keywords='sonic SONiC utilities command line cli CLI', + test_suite='setup.get_test_suite' ) From c31df0dc4908cd06e911de7612b6c71f3880ee17 Mon Sep 17 00:00:00 2001 From: Joe LeVeque Date: Mon, 3 Aug 2020 01:01:04 +0000 Subject: [PATCH 13/15] Remove setUp method, as it is not part of pytest --- sonic-utilities-tests/show_platform_test.py | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/sonic-utilities-tests/show_platform_test.py b/sonic-utilities-tests/show_platform_test.py index 3e3867a6d0..d988a6e652 100644 --- a/sonic-utilities-tests/show_platform_test.py +++ b/sonic-utilities-tests/show_platform_test.py @@ -39,9 +39,6 @@ def setup_class(cls): print("SETUP") os.environ["UTILITIES_UNIT_TESTING"] = "1" - def setUp(self): - self.runner = CliRunner() - # Test 'show platform summary' def test_summary(self): expected_output = """\ @@ -52,7 +49,8 @@ def test_summary(self): with mock.patch("show.main.get_hw_info_dict", return_value={"platform": TEST_PLATFORM, "hwsku": TEST_HWSKU, "asic_type": TEST_ASIC_TYPE}): - result = self.runner.invoke(show.cli.commands["platform"].commands["summary"], []) + runner = CliRunner() + result = runner.invoke(show.cli.commands["platform"].commands["summary"], []) assert result.output == textwrap.dedent(expected_output) @classmethod From 154cc747251cba03c546b2f5eed208b322d4efd9 Mon Sep 17 00:00:00 2001 From: Joe LeVeque Date: Mon, 3 Aug 2020 06:37:24 +0000 Subject: [PATCH 14/15] 'sonic-utilities-tests' dir name has changed to 'tests' --- {sonic-utilities-tests => tests}/show_platform_test.py | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename {sonic-utilities-tests => tests}/show_platform_test.py (100%) diff --git a/sonic-utilities-tests/show_platform_test.py b/tests/show_platform_test.py similarity index 100% rename from sonic-utilities-tests/show_platform_test.py rename to tests/show_platform_test.py From 768f5a6b9c16c95b5f75b1f4a8a0c7509ebc64ef Mon Sep 17 00:00:00 2001 From: Joe LeVeque Date: Mon, 3 Aug 2020 20:19:57 +0000 Subject: [PATCH 15/15] Remove dependency on unittest --- tests/show_platform_test.py | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/tests/show_platform_test.py b/tests/show_platform_test.py index d988a6e652..028a90fb69 100644 --- a/tests/show_platform_test.py +++ b/tests/show_platform_test.py @@ -4,11 +4,9 @@ import mock from click.testing import CliRunner -from unittest import TestCase test_path = os.path.dirname(os.path.abspath(__file__)) modules_path = os.path.dirname(test_path) -sys.path.insert(0, test_path) sys.path.insert(0, modules_path) import show.main as show @@ -33,7 +31,7 @@ show platform temperature """ -class TestShowPlatform(TestCase): +class TestShowPlatform(object): @classmethod def setup_class(cls): print("SETUP")