From 1850ff749c51d58c33771b4c4c9b9587f6f8d619 Mon Sep 17 00:00:00 2001 From: Ramesh Raghupathy Date: Tue, 12 Nov 2024 19:42:42 -0800 Subject: [PATCH] Trying a different approach --- sonic-chassisd/tests/mock_platform.py | 10 ++++++++ sonic-chassisd/tests/test_chassisd.py | 35 +++++++++++---------------- 2 files changed, 24 insertions(+), 21 deletions(-) diff --git a/sonic-chassisd/tests/mock_platform.py b/sonic-chassisd/tests/mock_platform.py index 62e3ae179..7fdb8d384 100644 --- a/sonic-chassisd/tests/mock_platform.py +++ b/sonic-chassisd/tests/mock_platform.py @@ -177,6 +177,16 @@ def get_dataplane_state(self): def get_controlplane_state(self): raise NotImplementedError + def _is_first_boot(self, module): + """Checks if the reboot-cause file indicates a first boot.""" + file_path = os.path.join("path_to_reboot_cause_dir", module.lower(), "reboot-cause.txt") + try: + with open(file_path, 'r') as f: + content = f.read().strip() + return content == "First boot" + except FileNotFoundError: + return False + class MockDpuChassis: def get_dpu_id(self): diff --git a/sonic-chassisd/tests/test_chassisd.py b/sonic-chassisd/tests/test_chassisd.py index 2212f438d..3c8105327 100644 --- a/sonic-chassisd/tests/test_chassisd.py +++ b/sonic-chassisd/tests/test_chassisd.py @@ -433,30 +433,23 @@ def test_smartswitch_configupdater_check_admin_state(): assert module.get_admin_state() == admin_state -# Define the mock_open function with the safety check -def safe_mock_open(*args, **kwargs): - if args and args[0] == PLATFORM_ENV_CONF_FILE: - return mock_open(read_data="dummy=1\nlinecard_reboot_timeout=240\n")(*args, **kwargs) - # Fallback to the real open for any other file - return builtin_open(*args, **kwargs) - + @mock.patch("os.path.join", return_value="/mocked/path/to/reboot-cause.txt") + def test_is_first_boot_file_found_first_boot(self, mock_join): + chassis = MockSmartSwitchChassis() + module = "DPU0" -def test_dpu_is_first_boot_true(): - """Test when _is_first_boot returns True and should be called once with 'DPU0'.""" + with mock.patch("builtins.open", mock.mock_open(read_data="First boot")): + result = chassis._is_first_boot(module) + self.assertTrue(result) - # Mock the SmartSwitchChassis and Module objects - chassis = MockSmartSwitchChassis() - module = MockModule(0, "DPU0", "DPU Module 0", ModuleBase.MODULE_TYPE_DPU, -1, "TS1000101") - module.set_oper_status(ModuleBase.MODULE_STATUS_PRESENT) - chassis.module_list.append(module) - - # Patch the open function within the context of this test - with mock.patch("builtins.open", new=safe_mock_open): - # Code to invoke _is_first_boot and test its behavior - is_first_boot = chassis._is_first_boot("DPU0") + def test_is_first_boot_file_not_found(self): + chassis = MockSmartSwitchChassis() + module = "DPU0" - # Check that _is_first_boot returned True as expected - assert is_first_boot is True, "Expected _is_first_boot to return True for 'DPU0'" + with mock.patch("builtins.open", mock.mock_open()) as mock_file: + mock_file.side_effect = FileNotFoundError + result = chassis._is_first_boot(module) + self.assertFalse(result) def test_platform_json_file_exists_and_valid():