From bcba133e1cf8882c00ff6e6653e17521e3006046 Mon Sep 17 00:00:00 2001 From: Ramesh Raghupathy Date: Thu, 12 Dec 2024 12:03:59 -0800 Subject: [PATCH] Fixing test isssues --- tests/process-reboot-cause_test.py | 35 +++++++++++++++++++----------- 1 file changed, 22 insertions(+), 13 deletions(-) diff --git a/tests/process-reboot-cause_test.py b/tests/process-reboot-cause_test.py index ba9071d6..eb385c76 100644 --- a/tests/process-reboot-cause_test.py +++ b/tests/process-reboot-cause_test.py @@ -1,37 +1,47 @@ -import unittest import os import sys +import unittest from unittest.mock import patch, mock_open, MagicMock from io import StringIO +import importlib.util -# Setup paths and load the module +# Paths setup test_path = os.path.dirname(os.path.abspath(__file__)) modules_path = os.path.dirname(test_path) scripts_path = os.path.join(modules_path, "scripts") -sys.path.insert(0, modules_path) +process_reboot_cause_path = os.path.join(scripts_path, 'process-reboot-cause') + +# Check if the file exists (ensure mocks don’t interfere) +real_exists = os.path.exists +if not real_exists(process_reboot_cause_path): + raise FileNotFoundError(f"File not found: {process_reboot_cause_path}") def load_module_from_source(module_name, path): - import importlib.util + if not os.access(path, os.R_OK): + raise PermissionError(f"File is not readable: {path}") spec = importlib.util.spec_from_file_location(module_name, path) + if spec is None: + raise ImportError(f"Could not load spec for {module_name} from {path}") module = importlib.util.module_from_spec(spec) + if spec.loader is None: + raise ImportError(f"No loader found for module: {module_name}") spec.loader.exec_module(module) return module -process_reboot_cause_path = os.path.join(scripts_path, 'process-reboot-cause') +# Load the module process_reboot_cause = load_module_from_source('process_reboot_cause', process_reboot_cause_path) class TestProcessRebootCause(unittest.TestCase): - @patch("builtins.open", new_callable=mock_open, read_data='{"cause": "PowerLoss", "user": "admin", "time": "2024-12-10", "comment": "test"}') @patch("os.listdir", return_value=["file1.json", "file2.json"]) @patch("os.path.isfile", return_value=True) - @patch("os.path.exists", return_value=True) + @patch("os.path.exists", side_effect=lambda path: real_exists(path) or path.endswith('file1.json') or path.endswith('file2.json')) @patch("os.remove") @patch("process_reboot_cause.swsscommon.SonicV2Connector") @patch("process_reboot_cause.device_info.is_smartswitch", return_value=True) @patch("sys.stdout", new_callable=StringIO) def test_process_reboot_cause(self, mock_stdout, mock_is_smartswitch, mock_connector, mock_remove, mock_exists, mock_isfile, mock_listdir, mock_open): - # Mock the SonicV2Connector behavior + # Mock DB mock_db = MagicMock() mock_connector.return_value = mock_db @@ -51,12 +61,12 @@ def test_process_reboot_cause(self, mock_stdout, mock_is_smartswitch, mock_conne @patch("builtins.open", new_callable=mock_open, read_data='{"invalid_json}') @patch("os.listdir", return_value=["file1.json"]) @patch("os.path.isfile", return_value=True) - @patch("os.path.exists", return_value=True) + @patch("os.path.exists", side_effect=lambda path: real_exists(path) or path.endswith('file1.json')) @patch("process_reboot_cause.swsscommon.SonicV2Connector") @patch("process_reboot_cause.device_info.is_smartswitch", return_value=True) @patch("sys.stdout", new_callable=StringIO) def test_invalid_json(self, mock_stdout, mock_is_smartswitch, mock_connector, mock_exists, mock_isfile, mock_listdir, mock_open): - # Mock the SonicV2Connector behavior + # Mock DB mock_db = MagicMock() mock_connector.return_value = mock_db @@ -64,9 +74,8 @@ def test_invalid_json(self, mock_stdout, mock_is_smartswitch, mock_connector, mo with patch.object(sys, "argv", ["process-reboot-cause"]): process_reboot_cause.main() - # Check that invalid JSON does not break execution + # Check invalid JSON handling output = mock_stdout.getvalue() self.assertIn("Starting up...", output) - self.assertIn("Invalid JSON format", output) # Adjust as per your script's error messages - + self.assertIn("Invalid JSON format", output) # Adjust based on your script self.assertTrue(mock_connector.called) \ No newline at end of file