From 654d4f7b550da69e873971ea2f5e21d8a98ee8d4 Mon Sep 17 00:00:00 2001 From: Miryam Schwartz Date: Sun, 8 Dec 2024 09:05:32 +0200 Subject: [PATCH 01/14] test --- .../ufm_log_analyzer_ci_workflow.yml | 23 +++++++++++++++++++ .../unit_tests/test_log_analyzer.py | 5 ++++ 2 files changed, 28 insertions(+) create mode 100644 plugins/ufm_log_analyzer_plugin/unit_tests/test_log_analyzer.py diff --git a/.github/workflows/ufm_log_analyzer_ci_workflow.yml b/.github/workflows/ufm_log_analyzer_ci_workflow.yml index 8a73689b..a2964038 100644 --- a/.github/workflows/ufm_log_analyzer_ci_workflow.yml +++ b/.github/workflows/ufm_log_analyzer_ci_workflow.yml @@ -53,3 +53,26 @@ jobs: pip install ruff==0.7.3 ruff format --diff --check src/loganalyze + + pytest: + runs-on: ubuntu-latest + + steps: + - name: Checkout code + uses: actions/checkout@v4 + + - name: Set up Python + uses: actions/setup-python@v4 + with: + python-version: 3.9 + + - name: Run Pytest + run: | + SCRIPT_DIR="plugins/ufm_log_analyzer_plugin" + # Set PYTHONPATH to include src directory and two levels up for utils + PYTHONPATH="$(realpath $SCRIPT_DIR/src):$(realpath $SCRIPT_DIR/../../)" + export PYTHONPATH + + cd $SCRIPT_DIR + + pytest --maxfail=1 --disable-warnings -q # Runs pytest with limited output \ No newline at end of file diff --git a/plugins/ufm_log_analyzer_plugin/unit_tests/test_log_analyzer.py b/plugins/ufm_log_analyzer_plugin/unit_tests/test_log_analyzer.py new file mode 100644 index 00000000..fd3fd1bb --- /dev/null +++ b/plugins/ufm_log_analyzer_plugin/unit_tests/test_log_analyzer.py @@ -0,0 +1,5 @@ +def test_succeed(): + assert 2 + 2 == 4 # This will pass + +def test_fail(): + assert 2 + 2 == 5 # This will fail From 2364f244cfcd6292a651adddf64f44b152d3a53b Mon Sep 17 00:00:00 2001 From: Miryam Schwartz Date: Sun, 8 Dec 2024 09:09:16 +0200 Subject: [PATCH 02/14] add pytest to requirments --- .../ufm_log_analyzer_plugin/src/loganalyze/requirements.txt | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/plugins/ufm_log_analyzer_plugin/src/loganalyze/requirements.txt b/plugins/ufm_log_analyzer_plugin/src/loganalyze/requirements.txt index de5ba89b..7ffd2820 100644 --- a/plugins/ufm_log_analyzer_plugin/src/loganalyze/requirements.txt +++ b/plugins/ufm_log_analyzer_plugin/src/loganalyze/requirements.txt @@ -3,4 +3,5 @@ numpy matplotlib IPython fpdf2 -tabulate \ No newline at end of file +tabulate +pytest \ No newline at end of file From 14aeb94eb871a8fb909f1d1e816a23b4ac61cf8b Mon Sep 17 00:00:00 2001 From: Miryam Schwartz Date: Sun, 8 Dec 2024 09:14:02 +0200 Subject: [PATCH 03/14] test --- .github/workflows/ufm_log_analyzer_ci_workflow.yml | 2 ++ .../ufm_log_analyzer_plugin/src/loganalyze/requirements.txt | 3 +-- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/.github/workflows/ufm_log_analyzer_ci_workflow.yml b/.github/workflows/ufm_log_analyzer_ci_workflow.yml index a2964038..e5f63340 100644 --- a/.github/workflows/ufm_log_analyzer_ci_workflow.yml +++ b/.github/workflows/ufm_log_analyzer_ci_workflow.yml @@ -75,4 +75,6 @@ jobs: cd $SCRIPT_DIR + pip install pytest + pytest --maxfail=1 --disable-warnings -q # Runs pytest with limited output \ No newline at end of file diff --git a/plugins/ufm_log_analyzer_plugin/src/loganalyze/requirements.txt b/plugins/ufm_log_analyzer_plugin/src/loganalyze/requirements.txt index 7ffd2820..de5ba89b 100644 --- a/plugins/ufm_log_analyzer_plugin/src/loganalyze/requirements.txt +++ b/plugins/ufm_log_analyzer_plugin/src/loganalyze/requirements.txt @@ -3,5 +3,4 @@ numpy matplotlib IPython fpdf2 -tabulate -pytest \ No newline at end of file +tabulate \ No newline at end of file From 07d3343b7756d8a9e65000aa9a8613630300b9e6 Mon Sep 17 00:00:00 2001 From: Miryam Schwartz Date: Sun, 8 Dec 2024 10:54:11 +0200 Subject: [PATCH 04/14] add tests --- .../loganalyze/log_analyzers/base_analyzer.py | 6 +++- .../unit_tests/__init__.py | 0 .../unit_tests/test_ibdiagnet_log_analyzer.py | 30 +++++++++++++++++ .../unit_tests/test_log_analyzer.py | 5 --- .../unit_tests/test_ufm_top_analyzer.py | 33 +++++++++++++++++++ 5 files changed, 68 insertions(+), 6 deletions(-) create mode 100644 plugins/ufm_log_analyzer_plugin/unit_tests/__init__.py create mode 100644 plugins/ufm_log_analyzer_plugin/unit_tests/test_ibdiagnet_log_analyzer.py delete mode 100644 plugins/ufm_log_analyzer_plugin/unit_tests/test_log_analyzer.py create mode 100644 plugins/ufm_log_analyzer_plugin/unit_tests/test_ufm_top_analyzer.py diff --git a/plugins/ufm_log_analyzer_plugin/src/loganalyze/log_analyzers/base_analyzer.py b/plugins/ufm_log_analyzer_plugin/src/loganalyze/log_analyzers/base_analyzer.py index a95ca640..82ef67c8 100644 --- a/plugins/ufm_log_analyzer_plugin/src/loganalyze/log_analyzers/base_analyzer.py +++ b/plugins/ufm_log_analyzer_plugin/src/loganalyze/log_analyzers/base_analyzer.py @@ -185,7 +185,11 @@ def __init__( ): super().__init__(dest_image_path) dataframes = [pd.read_csv(ufm_log) for ufm_log in logs_csvs] - df = pd.concat(dataframes, ignore_index=True) + if dataframes: + df = pd.concat(dataframes, ignore_index=True) + else: + df = pd.DataFrame() # Return an empty DataFrame if dataframes is empty + if sort_timestamp: df[DataConstants.TIMESTAMP] = pd.to_datetime(df[DataConstants.TIMESTAMP]) max_timestamp = df[DataConstants.TIMESTAMP].max() diff --git a/plugins/ufm_log_analyzer_plugin/unit_tests/__init__.py b/plugins/ufm_log_analyzer_plugin/unit_tests/__init__.py new file mode 100644 index 00000000..e69de29b diff --git a/plugins/ufm_log_analyzer_plugin/unit_tests/test_ibdiagnet_log_analyzer.py b/plugins/ufm_log_analyzer_plugin/unit_tests/test_ibdiagnet_log_analyzer.py new file mode 100644 index 00000000..7054f7e4 --- /dev/null +++ b/plugins/ufm_log_analyzer_plugin/unit_tests/test_ibdiagnet_log_analyzer.py @@ -0,0 +1,30 @@ +import unittest +import sys +import os + +sys.path.append(os.getcwd()) +sys.path.append("/".join(os.getcwd().split("/")[:-1])) +sys.path.append("/".join(os.getcwd().split("/")[:-1]) + "/src") + +from loganalyze.log_analyzers.ibdiagnet_log_analyzer import IBDIAGNETLogAnalyzer + + +class TestIBDIAGNETLogAnalyzer(unittest.TestCase): + def setUp(self): + # Example initialization with dummy arguments + self.logs_csvs = [] + self.hours = 24 + self.dest_image_path = "/dummy/path" + self.analyzer = IBDIAGNETLogAnalyzer(self.logs_csvs, self.hours, self.dest_image_path) + + def test_get_fabric_size(self): + # Mock the _log_data_sorted attribute + expected_fabric_size = {"switch_count": 10, "link_count": 50} # Example data + self.analyzer._log_data_sorted = expected_fabric_size # pylint: disable=protected-access + + # Call the method and check the result + result = self.analyzer.get_fabric_size() + self.assertEqual(result, expected_fabric_size) + +if __name__ == "__main__": + unittest.main() diff --git a/plugins/ufm_log_analyzer_plugin/unit_tests/test_log_analyzer.py b/plugins/ufm_log_analyzer_plugin/unit_tests/test_log_analyzer.py deleted file mode 100644 index fd3fd1bb..00000000 --- a/plugins/ufm_log_analyzer_plugin/unit_tests/test_log_analyzer.py +++ /dev/null @@ -1,5 +0,0 @@ -def test_succeed(): - assert 2 + 2 == 4 # This will pass - -def test_fail(): - assert 2 + 2 == 5 # This will fail diff --git a/plugins/ufm_log_analyzer_plugin/unit_tests/test_ufm_top_analyzer.py b/plugins/ufm_log_analyzer_plugin/unit_tests/test_ufm_top_analyzer.py new file mode 100644 index 00000000..a67e98df --- /dev/null +++ b/plugins/ufm_log_analyzer_plugin/unit_tests/test_ufm_top_analyzer.py @@ -0,0 +1,33 @@ +import unittest +import sys +import os + +sys.path.append(os.getcwd()) +sys.path.append("/".join(os.getcwd().split("/")[:-1])) +sys.path.append("/".join(os.getcwd().split("/")[:-1]) + "/src") + +from loganalyze.log_analyzers.ufm_top_analyzer import UFMTopAnalyzer + +class TestUFMTopAnalyzer(unittest.TestCase): + def setUp(self): + self.analyzer = UFMTopAnalyzer() + + def test_add_analyzer(self): + mock_analyzer_1 = "Analyzer1" + mock_analyzer_2 = "Analyzer2" + + # Initially, the list should be empty + self.assertEqual(len(self.analyzer._analyzers), 0) # pylint: disable=protected-access + + # Add first analyzer and check the length + self.analyzer.add_analyzer(mock_analyzer_1) + self.assertEqual(len(self.analyzer._analyzers), 1) # pylint: disable=protected-access + self.assertIn(mock_analyzer_1, self.analyzer._analyzers) # pylint: disable=protected-access + + # Add second analyzer and check the updated length + self.analyzer.add_analyzer(mock_analyzer_2) + self.assertEqual(len(self.analyzer._analyzers), 2) # pylint: disable=protected-access + self.assertIn(mock_analyzer_2, self.analyzer._analyzers) # pylint: disable=protected-access + +if __name__ == "__main__": + unittest.main() From 9f78a5806a3adcdd42885a3754b157608a9cd667 Mon Sep 17 00:00:00 2001 From: Miryam Schwartz Date: Sun, 8 Dec 2024 10:57:11 +0200 Subject: [PATCH 05/14] add insall requierments to workflow --- .github/workflows/ufm_log_analyzer_ci_workflow.yml | 1 + 1 file changed, 1 insertion(+) diff --git a/.github/workflows/ufm_log_analyzer_ci_workflow.yml b/.github/workflows/ufm_log_analyzer_ci_workflow.yml index e5f63340..ea4354bb 100644 --- a/.github/workflows/ufm_log_analyzer_ci_workflow.yml +++ b/.github/workflows/ufm_log_analyzer_ci_workflow.yml @@ -75,6 +75,7 @@ jobs: cd $SCRIPT_DIR + pip install -r src/loganalyze/requirements.txt pip install pytest pytest --maxfail=1 --disable-warnings -q # Runs pytest with limited output \ No newline at end of file From b079038dac7b3bcc08ec96ae86fdbf0de3b8f1bb Mon Sep 17 00:00:00 2001 From: Miryam Schwartz Date: Sun, 8 Dec 2024 11:08:38 +0200 Subject: [PATCH 06/14] add copyright --- .../unit_tests/test_ibdiagnet_log_analyzer.py | 16 ++++++++++++++++ .../unit_tests/test_ufm_top_analyzer.py | 16 ++++++++++++++++ 2 files changed, 32 insertions(+) diff --git a/plugins/ufm_log_analyzer_plugin/unit_tests/test_ibdiagnet_log_analyzer.py b/plugins/ufm_log_analyzer_plugin/unit_tests/test_ibdiagnet_log_analyzer.py index 7054f7e4..3fbe9070 100644 --- a/plugins/ufm_log_analyzer_plugin/unit_tests/test_ibdiagnet_log_analyzer.py +++ b/plugins/ufm_log_analyzer_plugin/unit_tests/test_ibdiagnet_log_analyzer.py @@ -1,3 +1,19 @@ +""" +@copyright: + Copyright © 2013-2024 NVIDIA CORPORATION & AFFILIATES. ALL RIGHTS RESERVED. + + This software product is a proprietary product of Nvidia Corporation and its affiliates + (the "Company") and all right, title, and interest in and to the + software product, including all associated intellectual property rights, + are and shall remain exclusively with the Company. + + This software product is governed by the End User License Agreement + provided with the software product. + +@author: Miryam Schwartz +@date: Dec 08, 2024 +""" + import unittest import sys import os diff --git a/plugins/ufm_log_analyzer_plugin/unit_tests/test_ufm_top_analyzer.py b/plugins/ufm_log_analyzer_plugin/unit_tests/test_ufm_top_analyzer.py index a67e98df..ce6a8deb 100644 --- a/plugins/ufm_log_analyzer_plugin/unit_tests/test_ufm_top_analyzer.py +++ b/plugins/ufm_log_analyzer_plugin/unit_tests/test_ufm_top_analyzer.py @@ -1,3 +1,19 @@ +""" +@copyright: + Copyright © 2013-2024 NVIDIA CORPORATION & AFFILIATES. ALL RIGHTS RESERVED. + + This software product is a proprietary product of Nvidia Corporation and its affiliates + (the "Company") and all right, title, and interest in and to the + software product, including all associated intellectual property rights, + are and shall remain exclusively with the Company. + + This software product is governed by the End User License Agreement + provided with the software product. + +@author: Miryam Schwartz +@date: Dec 08, 2024 +""" + import unittest import sys import os From c5539b7de541dc6c7cb299b483d0805c826745d9 Mon Sep 17 00:00:00 2001 From: Miryam Schwartz Date: Sun, 8 Dec 2024 11:42:08 +0200 Subject: [PATCH 07/14] fix comments --- .github/workflows/ufm_log_analyzer_ci_workflow.yml | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/.github/workflows/ufm_log_analyzer_ci_workflow.yml b/.github/workflows/ufm_log_analyzer_ci_workflow.yml index ea4354bb..8d5015c0 100644 --- a/.github/workflows/ufm_log_analyzer_ci_workflow.yml +++ b/.github/workflows/ufm_log_analyzer_ci_workflow.yml @@ -8,7 +8,7 @@ on: jobs: pylint: - runs-on: ubuntu-latest + runs-on: ubuntu-22.04 steps: - name: Checkout code @@ -34,7 +34,7 @@ jobs: pylint --rcfile=src/loganalyze/.pylintrc src/loganalyze ruff: - runs-on: ubuntu-latest + runs-on: ubuntu-22.04 steps: - name: Checkout code @@ -55,7 +55,7 @@ jobs: ruff format --diff --check src/loganalyze pytest: - runs-on: ubuntu-latest + runs-on: ubuntu-22.04 steps: - name: Checkout code @@ -78,4 +78,4 @@ jobs: pip install -r src/loganalyze/requirements.txt pip install pytest - pytest --maxfail=1 --disable-warnings -q # Runs pytest with limited output \ No newline at end of file + pytest \ No newline at end of file From 042d4c8fa8b3f7c76f8ea5991f3de29ad7ad8fd2 Mon Sep 17 00:00:00 2001 From: Miryam Schwartz Date: Sun, 8 Dec 2024 15:19:20 +0200 Subject: [PATCH 08/14] fix comments --- .../unit_tests/test_ibdiagnet_log_analyzer.py | 55 ++++++++--------- .../unit_tests/test_ufm_top_analyzer.py | 60 +++++++++---------- 2 files changed, 53 insertions(+), 62 deletions(-) diff --git a/plugins/ufm_log_analyzer_plugin/unit_tests/test_ibdiagnet_log_analyzer.py b/plugins/ufm_log_analyzer_plugin/unit_tests/test_ibdiagnet_log_analyzer.py index 3fbe9070..026f7447 100644 --- a/plugins/ufm_log_analyzer_plugin/unit_tests/test_ibdiagnet_log_analyzer.py +++ b/plugins/ufm_log_analyzer_plugin/unit_tests/test_ibdiagnet_log_analyzer.py @@ -1,20 +1,18 @@ -""" -@copyright: - Copyright © 2013-2024 NVIDIA CORPORATION & AFFILIATES. ALL RIGHTS RESERVED. +# @copyright: +# Copyright © 2013-2024 NVIDIA CORPORATION & AFFILIATES. ALL RIGHTS RESERVED. - This software product is a proprietary product of Nvidia Corporation and its affiliates - (the "Company") and all right, title, and interest in and to the - software product, including all associated intellectual property rights, - are and shall remain exclusively with the Company. +# This software product is a proprietary product of Nvidia Corporation and its affiliates +# (the "Company") and all right, title, and interest in and to the +# software product, including all associated intellectual property rights, +# are and shall remain exclusively with the Company. - This software product is governed by the End User License Agreement - provided with the software product. +# This software product is governed by the End User License Agreement +# provided with the software product. -@author: Miryam Schwartz -@date: Dec 08, 2024 -""" +# @author: Miryam Schwartz +# @date: Dec 08, 2024 -import unittest +import pytest import sys import os @@ -25,22 +23,19 @@ from loganalyze.log_analyzers.ibdiagnet_log_analyzer import IBDIAGNETLogAnalyzer -class TestIBDIAGNETLogAnalyzer(unittest.TestCase): - def setUp(self): - # Example initialization with dummy arguments - self.logs_csvs = [] - self.hours = 24 - self.dest_image_path = "/dummy/path" - self.analyzer = IBDIAGNETLogAnalyzer(self.logs_csvs, self.hours, self.dest_image_path) +@pytest.fixture +def analyzer(): + # Fixture to initialize the analyzer object + logs_csvs = [] + hours = 24 + dest_image_path = "/dummy/path" + return IBDIAGNETLogAnalyzer(logs_csvs, hours, dest_image_path) - def test_get_fabric_size(self): - # Mock the _log_data_sorted attribute - expected_fabric_size = {"switch_count": 10, "link_count": 50} # Example data - self.analyzer._log_data_sorted = expected_fabric_size # pylint: disable=protected-access +def test_get_fabric_size(analyzer): + # Mock the _log_data_sorted attribute + expected_fabric_size = {"switch_count": 10, "link_count": 50} # Example data + analyzer._log_data_sorted = expected_fabric_size # pylint: disable=protected-access - # Call the method and check the result - result = self.analyzer.get_fabric_size() - self.assertEqual(result, expected_fabric_size) - -if __name__ == "__main__": - unittest.main() + # Call the method and check the result + result = analyzer.get_fabric_size() + assert result == expected_fabric_size \ No newline at end of file diff --git a/plugins/ufm_log_analyzer_plugin/unit_tests/test_ufm_top_analyzer.py b/plugins/ufm_log_analyzer_plugin/unit_tests/test_ufm_top_analyzer.py index ce6a8deb..6cebb7cb 100644 --- a/plugins/ufm_log_analyzer_plugin/unit_tests/test_ufm_top_analyzer.py +++ b/plugins/ufm_log_analyzer_plugin/unit_tests/test_ufm_top_analyzer.py @@ -1,20 +1,18 @@ -""" -@copyright: - Copyright © 2013-2024 NVIDIA CORPORATION & AFFILIATES. ALL RIGHTS RESERVED. +# @copyright: +# Copyright © 2013-2024 NVIDIA CORPORATION & AFFILIATES. ALL RIGHTS RESERVED. - This software product is a proprietary product of Nvidia Corporation and its affiliates - (the "Company") and all right, title, and interest in and to the - software product, including all associated intellectual property rights, - are and shall remain exclusively with the Company. +# This software product is a proprietary product of Nvidia Corporation and its affiliates +# (the "Company") and all right, title, and interest in and to the +# software product, including all associated intellectual property rights, +# are and shall remain exclusively with the Company. - This software product is governed by the End User License Agreement - provided with the software product. +# This software product is governed by the End User License Agreement +# provided with the software product. -@author: Miryam Schwartz -@date: Dec 08, 2024 -""" +# @author: Miryam Schwartz +# @date: Dec 08, 2024 -import unittest +import pytest import sys import os @@ -24,26 +22,24 @@ from loganalyze.log_analyzers.ufm_top_analyzer import UFMTopAnalyzer -class TestUFMTopAnalyzer(unittest.TestCase): - def setUp(self): - self.analyzer = UFMTopAnalyzer() +@pytest.fixture +def analyzer(): + # Fixture to initialize the analyzer object + return UFMTopAnalyzer() - def test_add_analyzer(self): - mock_analyzer_1 = "Analyzer1" - mock_analyzer_2 = "Analyzer2" +def test_add_analyzer(analyzer): + mock_analyzer_1 = "Analyzer1" + mock_analyzer_2 = "Analyzer2" - # Initially, the list should be empty - self.assertEqual(len(self.analyzer._analyzers), 0) # pylint: disable=protected-access + # Initially, the list should be empty + assert len(analyzer._analyzers) == 0 # pylint: disable=protected-access - # Add first analyzer and check the length - self.analyzer.add_analyzer(mock_analyzer_1) - self.assertEqual(len(self.analyzer._analyzers), 1) # pylint: disable=protected-access - self.assertIn(mock_analyzer_1, self.analyzer._analyzers) # pylint: disable=protected-access + # Add first analyzer and check the length + analyzer.add_analyzer(mock_analyzer_1) + assert len(analyzer._analyzers) == 1 # pylint: disable=protected-access + assert mock_analyzer_1 in analyzer._analyzers # pylint: disable=protected-access - # Add second analyzer and check the updated length - self.analyzer.add_analyzer(mock_analyzer_2) - self.assertEqual(len(self.analyzer._analyzers), 2) # pylint: disable=protected-access - self.assertIn(mock_analyzer_2, self.analyzer._analyzers) # pylint: disable=protected-access - -if __name__ == "__main__": - unittest.main() + # Add second analyzer and check the updated length + analyzer.add_analyzer(mock_analyzer_2) + assert len(analyzer._analyzers) == 2 # pylint: disable=protected-access + assert mock_analyzer_2 in analyzer._analyzers # pylint: disable=protected-access From 49bf32572491fa0f4fb1085402cb54c31f561add Mon Sep 17 00:00:00 2001 From: Miryam Schwartz Date: Tue, 10 Dec 2024 11:00:58 +0200 Subject: [PATCH 09/14] fix comments --- .github/workflows/ufm_log_analyzer_ci_workflow.yml | 2 +- .../ufm_log_analyzer_plugin/src/loganalyze/.pylintrc | 5 ++++- .../unit_tests/test_ibdiagnet_log_analyzer.py | 4 ++-- .../unit_tests/test_ufm_top_analyzer.py | 10 +++++----- 4 files changed, 12 insertions(+), 9 deletions(-) diff --git a/.github/workflows/ufm_log_analyzer_ci_workflow.yml b/.github/workflows/ufm_log_analyzer_ci_workflow.yml index 8d5015c0..543ef8ef 100644 --- a/.github/workflows/ufm_log_analyzer_ci_workflow.yml +++ b/.github/workflows/ufm_log_analyzer_ci_workflow.yml @@ -78,4 +78,4 @@ jobs: pip install -r src/loganalyze/requirements.txt pip install pytest - pytest \ No newline at end of file + pytest /unit_tests \ No newline at end of file diff --git a/plugins/ufm_log_analyzer_plugin/src/loganalyze/.pylintrc b/plugins/ufm_log_analyzer_plugin/src/loganalyze/.pylintrc index 1f8ebad5..fe4d56ae 100644 --- a/plugins/ufm_log_analyzer_plugin/src/loganalyze/.pylintrc +++ b/plugins/ufm_log_analyzer_plugin/src/loganalyze/.pylintrc @@ -8,4 +8,7 @@ disable=missing-function-docstring, [DESIGN] max-locals=20 -max-args=8 \ No newline at end of file +max-args=8 + +[unit_tests/*] +disable=protected-access \ No newline at end of file diff --git a/plugins/ufm_log_analyzer_plugin/unit_tests/test_ibdiagnet_log_analyzer.py b/plugins/ufm_log_analyzer_plugin/unit_tests/test_ibdiagnet_log_analyzer.py index 026f7447..bc157d26 100644 --- a/plugins/ufm_log_analyzer_plugin/unit_tests/test_ibdiagnet_log_analyzer.py +++ b/plugins/ufm_log_analyzer_plugin/unit_tests/test_ibdiagnet_log_analyzer.py @@ -34,8 +34,8 @@ def analyzer(): def test_get_fabric_size(analyzer): # Mock the _log_data_sorted attribute expected_fabric_size = {"switch_count": 10, "link_count": 50} # Example data - analyzer._log_data_sorted = expected_fabric_size # pylint: disable=protected-access + analyzer._log_data_sorted = expected_fabric_size # Call the method and check the result result = analyzer.get_fabric_size() - assert result == expected_fabric_size \ No newline at end of file + assert result == expected_fabric_size, "get_fabric_size should return _log_data_sorted" \ No newline at end of file diff --git a/plugins/ufm_log_analyzer_plugin/unit_tests/test_ufm_top_analyzer.py b/plugins/ufm_log_analyzer_plugin/unit_tests/test_ufm_top_analyzer.py index 6cebb7cb..0a589a7e 100644 --- a/plugins/ufm_log_analyzer_plugin/unit_tests/test_ufm_top_analyzer.py +++ b/plugins/ufm_log_analyzer_plugin/unit_tests/test_ufm_top_analyzer.py @@ -32,14 +32,14 @@ def test_add_analyzer(analyzer): mock_analyzer_2 = "Analyzer2" # Initially, the list should be empty - assert len(analyzer._analyzers) == 0 # pylint: disable=protected-access + assert len(analyzer._analyzers) == 0 # Add first analyzer and check the length analyzer.add_analyzer(mock_analyzer_1) - assert len(analyzer._analyzers) == 1 # pylint: disable=protected-access - assert mock_analyzer_1 in analyzer._analyzers # pylint: disable=protected-access + assert len(analyzer._analyzers) == 1 + assert mock_analyzer_1 in analyzer._analyzers # Add second analyzer and check the updated length analyzer.add_analyzer(mock_analyzer_2) - assert len(analyzer._analyzers) == 2 # pylint: disable=protected-access - assert mock_analyzer_2 in analyzer._analyzers # pylint: disable=protected-access + assert len(analyzer._analyzers) == 2 + assert mock_analyzer_2 in analyzer._analyzers From b972bce5f243051e6df04cd99e141a0ffc008b6f Mon Sep 17 00:00:00 2001 From: Miryam Schwartz Date: Tue, 10 Dec 2024 11:05:56 +0200 Subject: [PATCH 10/14] fix pytest path --- .github/workflows/ufm_log_analyzer_ci_workflow.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/ufm_log_analyzer_ci_workflow.yml b/.github/workflows/ufm_log_analyzer_ci_workflow.yml index 543ef8ef..6e3d62ea 100644 --- a/.github/workflows/ufm_log_analyzer_ci_workflow.yml +++ b/.github/workflows/ufm_log_analyzer_ci_workflow.yml @@ -78,4 +78,4 @@ jobs: pip install -r src/loganalyze/requirements.txt pip install pytest - pytest /unit_tests \ No newline at end of file + pytest unit_tests \ No newline at end of file From 74c6af01c43d10e05cf2cb85852d6c9f72e84510 Mon Sep 17 00:00:00 2001 From: Miryam Schwartz Date: Tue, 10 Dec 2024 15:42:46 +0200 Subject: [PATCH 11/14] fix comments --- .../ufm_log_analyzer_ci_workflow.yml | 2 +- plugins/ufm_log_analyzer_plugin/README.md | 13 +++++++ .../unit_tests/test_ibdiagnet_log_analyzer.py | 38 ++++++++++--------- .../unit_tests/test_ufm_top_analyzer.py | 6 --- 4 files changed, 35 insertions(+), 24 deletions(-) diff --git a/.github/workflows/ufm_log_analyzer_ci_workflow.yml b/.github/workflows/ufm_log_analyzer_ci_workflow.yml index 6e3d62ea..dacf3bb1 100644 --- a/.github/workflows/ufm_log_analyzer_ci_workflow.yml +++ b/.github/workflows/ufm_log_analyzer_ci_workflow.yml @@ -1,7 +1,7 @@ name: Ufm log analyzer CI Workflow on: - push: + pull_request: paths: - 'plugins/ufm_log_analyzer_plugin/**' - '.github/workflows/ufm_log_analyzer_ci_workflow.yml' diff --git a/plugins/ufm_log_analyzer_plugin/README.md b/plugins/ufm_log_analyzer_plugin/README.md index 05694bae..7fedaa98 100644 --- a/plugins/ufm_log_analyzer_plugin/README.md +++ b/plugins/ufm_log_analyzer_plugin/README.md @@ -75,4 +75,17 @@ This logic will show links that: ![Tool flow](img/loganalzer.png) +## Testing +There is a folder named `unit_tests`, this folder contains some unit tests, to run the tests follow these steps: + +1. Make sure you have `pytest` installed. If not, you can install it using `pip`: + + ```bash + pip install pytest +2. Navigate to the root directory of the log analyzer project: + ```bash + cd plugins/ufm_log_analyzer_plugin +3. Run `pytest` to execute the tests: + ```bash + pytest unit_tests \ No newline at end of file diff --git a/plugins/ufm_log_analyzer_plugin/unit_tests/test_ibdiagnet_log_analyzer.py b/plugins/ufm_log_analyzer_plugin/unit_tests/test_ibdiagnet_log_analyzer.py index bc157d26..4cef5cc9 100644 --- a/plugins/ufm_log_analyzer_plugin/unit_tests/test_ibdiagnet_log_analyzer.py +++ b/plugins/ufm_log_analyzer_plugin/unit_tests/test_ibdiagnet_log_analyzer.py @@ -12,30 +12,34 @@ # @author: Miryam Schwartz # @date: Dec 08, 2024 -import pytest -import sys -import os -sys.path.append(os.getcwd()) -sys.path.append("/".join(os.getcwd().split("/")[:-1])) -sys.path.append("/".join(os.getcwd().split("/")[:-1]) + "/src") + +import pytest +from unittest.mock import MagicMock from loganalyze.log_analyzers.ibdiagnet_log_analyzer import IBDIAGNETLogAnalyzer @pytest.fixture -def analyzer(): - # Fixture to initialize the analyzer object - logs_csvs = [] - hours = 24 - dest_image_path = "/dummy/path" - return IBDIAGNETLogAnalyzer(logs_csvs, hours, dest_image_path) - -def test_get_fabric_size(analyzer): +def analyzer(fabric_size_data): + # Mock the constructor of IBDIAGNETLogAnalyzer + mock_analyzer = MagicMock(spec=IBDIAGNETLogAnalyzer) + # Mock the _log_data_sorted attribute - expected_fabric_size = {"switch_count": 10, "link_count": 50} # Example data - analyzer._log_data_sorted = expected_fabric_size + mock_analyzer._log_data_sorted = fabric_size_data + + # Mock the get_fabric_size method to return the _log_data_sorted attribute + mock_analyzer.get_fabric_size.return_value = fabric_size_data + + # Return the mocked analyzer instance + return mock_analyzer + +@pytest.fixture +def fabric_size_data(): + # Shared mock data + return {"switch_count": 10, "link_count": 50} +def test_get_fabric_size(analyzer, fabric_size_data): # Call the method and check the result result = analyzer.get_fabric_size() - assert result == expected_fabric_size, "get_fabric_size should return _log_data_sorted" \ No newline at end of file + assert result == fabric_size_data, "get_fabric_size should return _log_data_sorted" \ No newline at end of file diff --git a/plugins/ufm_log_analyzer_plugin/unit_tests/test_ufm_top_analyzer.py b/plugins/ufm_log_analyzer_plugin/unit_tests/test_ufm_top_analyzer.py index 0a589a7e..3ce677da 100644 --- a/plugins/ufm_log_analyzer_plugin/unit_tests/test_ufm_top_analyzer.py +++ b/plugins/ufm_log_analyzer_plugin/unit_tests/test_ufm_top_analyzer.py @@ -13,12 +13,6 @@ # @date: Dec 08, 2024 import pytest -import sys -import os - -sys.path.append(os.getcwd()) -sys.path.append("/".join(os.getcwd().split("/")[:-1])) -sys.path.append("/".join(os.getcwd().split("/")[:-1]) + "/src") from loganalyze.log_analyzers.ufm_top_analyzer import UFMTopAnalyzer From 72703ae20f73d630bcd7e8f1db024a762c62278d Mon Sep 17 00:00:00 2001 From: Miryam Schwartz Date: Wed, 11 Dec 2024 14:50:27 +0200 Subject: [PATCH 12/14] fix comments --- .../ufm_log_analyzer_ci_workflow.yml | 2 +- .../unit_tests/conftest.py | 19 ++++++++++++ .../unit_tests/test_ibdiagnet_log_analyzer.py | 29 +++++++------------ 3 files changed, 31 insertions(+), 19 deletions(-) create mode 100644 plugins/ufm_log_analyzer_plugin/unit_tests/conftest.py diff --git a/.github/workflows/ufm_log_analyzer_ci_workflow.yml b/.github/workflows/ufm_log_analyzer_ci_workflow.yml index dacf3bb1..d487d96b 100644 --- a/.github/workflows/ufm_log_analyzer_ci_workflow.yml +++ b/.github/workflows/ufm_log_analyzer_ci_workflow.yml @@ -76,6 +76,6 @@ jobs: cd $SCRIPT_DIR pip install -r src/loganalyze/requirements.txt - pip install pytest + pip install pytest==8.3.4 pytest unit_tests \ No newline at end of file diff --git a/plugins/ufm_log_analyzer_plugin/unit_tests/conftest.py b/plugins/ufm_log_analyzer_plugin/unit_tests/conftest.py new file mode 100644 index 00000000..cef7aee0 --- /dev/null +++ b/plugins/ufm_log_analyzer_plugin/unit_tests/conftest.py @@ -0,0 +1,19 @@ +# @copyright: +# Copyright © 2013-2024 NVIDIA CORPORATION & AFFILIATES. ALL RIGHTS RESERVED. + +# This software product is a proprietary product of Nvidia Corporation and its affiliates +# (the "Company") and all right, title, and interest in and to the +# software product, including all associated intellectual property rights, +# are and shall remain exclusively with the Company. + +# This software product is governed by the End User License Agreement +# provided with the software product. + +# @author: Miryam Schwartz +# @date: Dec 08, 2024 + +import sys +import os + +sys.path.append(os.getcwd() + "/src") # Add the src directory containing loganalyze +sys.path.append("/".join(os.getcwd().split("/")[:-2])) # Add the root project directory diff --git a/plugins/ufm_log_analyzer_plugin/unit_tests/test_ibdiagnet_log_analyzer.py b/plugins/ufm_log_analyzer_plugin/unit_tests/test_ibdiagnet_log_analyzer.py index 4cef5cc9..42294404 100644 --- a/plugins/ufm_log_analyzer_plugin/unit_tests/test_ibdiagnet_log_analyzer.py +++ b/plugins/ufm_log_analyzer_plugin/unit_tests/test_ibdiagnet_log_analyzer.py @@ -12,34 +12,27 @@ # @author: Miryam Schwartz # @date: Dec 08, 2024 - - import pytest -from unittest.mock import MagicMock from loganalyze.log_analyzers.ibdiagnet_log_analyzer import IBDIAGNETLogAnalyzer - -@pytest.fixture -def analyzer(fabric_size_data): - # Mock the constructor of IBDIAGNETLogAnalyzer - mock_analyzer = MagicMock(spec=IBDIAGNETLogAnalyzer) - - # Mock the _log_data_sorted attribute - mock_analyzer._log_data_sorted = fabric_size_data - - # Mock the get_fabric_size method to return the _log_data_sorted attribute - mock_analyzer.get_fabric_size.return_value = fabric_size_data - - # Return the mocked analyzer instance - return mock_analyzer +# Define a test-specific subclass +class TestIBDIAGNETLogAnalyzer(IBDIAGNETLogAnalyzer): + def __init__(self, fabric_size_data): + # Do not call the parent constructor, set up only what's needed for the test + self._log_data_sorted = fabric_size_data @pytest.fixture def fabric_size_data(): # Shared mock data return {"switch_count": 10, "link_count": 50} +@pytest.fixture +def analyzer(fabric_size_data): + # Return an instance of the test-specific subclass + return TestIBDIAGNETLogAnalyzer(fabric_size_data) + def test_get_fabric_size(analyzer, fabric_size_data): # Call the method and check the result result = analyzer.get_fabric_size() - assert result == fabric_size_data, "get_fabric_size should return _log_data_sorted" \ No newline at end of file + assert result == fabric_size_data, "get_fabric_size should return _log_data_sorted" From c81244875c195013a73df16b609d755abcbdef44 Mon Sep 17 00:00:00 2001 From: Miryam Schwartz Date: Thu, 12 Dec 2024 08:29:49 +0200 Subject: [PATCH 13/14] fix comments --- plugins/ufm_log_analyzer_plugin/README.md | 4 ++-- .../unit_tests/test_ibdiagnet_log_analyzer.py | 2 -- 2 files changed, 2 insertions(+), 4 deletions(-) diff --git a/plugins/ufm_log_analyzer_plugin/README.md b/plugins/ufm_log_analyzer_plugin/README.md index 7fedaa98..114a52b5 100644 --- a/plugins/ufm_log_analyzer_plugin/README.md +++ b/plugins/ufm_log_analyzer_plugin/README.md @@ -79,8 +79,8 @@ This logic will show links that: There is a folder named `unit_tests`, this folder contains some unit tests, to run the tests follow these steps: -1. Make sure you have `pytest` installed. If not, you can install it using `pip`: - +1. Inside the project virtual environment, make sure you have `pytest` installed. + If not, you can install it using `pip`: ```bash pip install pytest 2. Navigate to the root directory of the log analyzer project: diff --git a/plugins/ufm_log_analyzer_plugin/unit_tests/test_ibdiagnet_log_analyzer.py b/plugins/ufm_log_analyzer_plugin/unit_tests/test_ibdiagnet_log_analyzer.py index 42294404..ac4f3ee4 100644 --- a/plugins/ufm_log_analyzer_plugin/unit_tests/test_ibdiagnet_log_analyzer.py +++ b/plugins/ufm_log_analyzer_plugin/unit_tests/test_ibdiagnet_log_analyzer.py @@ -29,10 +29,8 @@ def fabric_size_data(): @pytest.fixture def analyzer(fabric_size_data): - # Return an instance of the test-specific subclass return TestIBDIAGNETLogAnalyzer(fabric_size_data) def test_get_fabric_size(analyzer, fabric_size_data): - # Call the method and check the result result = analyzer.get_fabric_size() assert result == fabric_size_data, "get_fabric_size should return _log_data_sorted" From e0654f2ad512f8c99b579ad6f8e20dc4e7ab0492 Mon Sep 17 00:00:00 2001 From: Miryam Schwartz Date: Thu, 12 Dec 2024 09:02:46 +0200 Subject: [PATCH 14/14] fix comments --- plugins/ufm_log_analyzer_plugin/README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/plugins/ufm_log_analyzer_plugin/README.md b/plugins/ufm_log_analyzer_plugin/README.md index 114a52b5..dead48d5 100644 --- a/plugins/ufm_log_analyzer_plugin/README.md +++ b/plugins/ufm_log_analyzer_plugin/README.md @@ -82,7 +82,7 @@ There is a folder named `unit_tests`, this folder contains some unit tests, to r 1. Inside the project virtual environment, make sure you have `pytest` installed. If not, you can install it using `pip`: ```bash - pip install pytest + python3 -m pip install pytest 2. Navigate to the root directory of the log analyzer project: ```bash cd plugins/ufm_log_analyzer_plugin