Skip to content

Commit

Permalink
Trying a different approach
Browse files Browse the repository at this point in the history
  • Loading branch information
rameshraghupathy committed Nov 13, 2024
1 parent 334a473 commit 1850ff7
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 21 deletions.
10 changes: 10 additions & 0 deletions sonic-chassisd/tests/mock_platform.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down
35 changes: 14 additions & 21 deletions sonic-chassisd/tests/test_chassisd.py
Original file line number Diff line number Diff line change
Expand Up @@ -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():
Expand Down

0 comments on commit 1850ff7

Please sign in to comment.