-
Notifications
You must be signed in to change notification settings - Fork 91
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adding support for persistent storage and retrieval of DPU reboot-cau…
…se (#169) * Adding support for persistent storage and retrieval of DPU reboot-cause * Added support for persisting dpu reboot-cause on smartswitch host * Working on coverage * Working on ut coverage * working on coverage * working on coverage * working on coverage * working on coverage * working on coverage * Fixed a typo * Working on coverage * Fixing test failure * improving coverage * Improving coverage * working on coverage * Modifying reboot-cause workflow to meet multiple smartswitch vendor hardware implementation requirements * Fixig the assertions to meet the new change * Fixed the DB * Using the common API device_info.get_dpu_list() * Addressed review comments * Added new test file tests/process-reboot-cause_test.py * Added the scripts_path * Moved setup outside the test class * Fixed the file name * Fixing test isssues * Working on UT * Fixed the numbeer of arguments to load_module_from_source * addressed review comments * adding mock for uid * passing uid arg * Fixing test failure * Fixing test failure * Fixing test failure * Fixing test failure * Fixing test failure * Fixing test failure * Iproving coverage * Iproving coverage * Iproving coverage * Iproving coverage * Iproving coverage * Iproving coverage * Iproving coverage * Iproving coverage * Iproving coverage * Iproving coverage * Addressed review comments * Addressed review comments * Addressed review comments * Addressed review comments * Addressed review comments * Addressed review comments * Addressed review comments * Addressed review comments * Addressed review comments * Addressed review comments * Addressed review comments * Addressed review comments: Using a common function ead_reboot_cause_files_and_save_to_db for regular switch and smartswitch * Working on coverage * Working on coverage * Working on coverage * Working on coverage * Addressed review comments * Addressed review comments * Addressed review comments * Addressed review comments * Fixed a test issue * Fixed a test issue * Fixed a test issue * Fixed a test issue * Fixed a test issue * Fixed a test issue * Fixed a test issue * Fixed a test issue * Fixed a test issue * Fixed a test issue * Fixed a bug * Did a minor cleanup * Addressed a review comment
- Loading branch information
1 parent
5e08927
commit bb0a31c
Showing
4 changed files
with
237 additions
and
20 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,132 @@ | ||
import sys | ||
import os | ||
from unittest import TestCase | ||
from unittest.mock import patch, MagicMock, mock_open | ||
from io import StringIO | ||
from sonic_py_common.general import load_module_from_source | ||
|
||
# Mock the connector | ||
from .mock_connector import MockConnector | ||
import swsscommon | ||
|
||
# Mock the SonicV2Connector | ||
swsscommon.SonicV2Connector = MockConnector | ||
|
||
# Define the path to the script and load it using the helper function | ||
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) | ||
|
||
# Load the process-reboot-cause module using the helper function | ||
process_reboot_cause_path = os.path.join(scripts_path, "process-reboot-cause") | ||
process_reboot_cause = load_module_from_source('process_reboot_cause', process_reboot_cause_path) | ||
|
||
# Now proceed with your test class and mocks | ||
class TestProcessRebootCause(TestCase): | ||
@patch("builtins.open", new_callable=mock_open, read_data='{"cause": "Non-Hardware", "user": "", "comment": "Switch rebooted DPU", "device": "DPU0", "time": "Fri Dec 13 01:12:36 AM UTC 2024", "gen_time": "2024_12_13_01_12_36"}') | ||
@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.getmtime", side_effect=lambda path: 1700000000 if "file1.json" in path else 1700001000) | ||
@patch("os.remove") | ||
@patch("process_reboot_cause.swsscommon.SonicV2Connector") | ||
@patch("process_reboot_cause.device_info.is_smartswitch", return_value=False) | ||
@patch("sys.stdout", new_callable=StringIO) | ||
@patch("os.geteuid", return_value=0) | ||
@patch("process_reboot_cause.device_info.get_dpu_list", return_value=["dpu1"]) | ||
def test_process_reboot_cause(self, mock_get_dpu_list, mock_geteuid, mock_stdout, mock_is_smartswitch, mock_connector, mock_remove, mock_getmtime, mock_exists, mock_isfile, mock_listdir, mock_open): | ||
# Mock DB | ||
mock_db = MagicMock() | ||
mock_connector.return_value = mock_db | ||
|
||
# Simulate running the script | ||
with patch.object(sys, "argv", ["process-reboot-cause"]): | ||
process_reboot_cause.main() | ||
|
||
# Validate syslog and stdout logging | ||
output = mock_stdout.getvalue() | ||
|
||
# Verify DB interactions | ||
mock_db.connect.assert_called() | ||
|
||
@patch("builtins.open", new_callable=mock_open, read_data='{"invalid_json": ') # Malformed 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.getmtime", side_effect=lambda path: 1700000000 if "file1.json" in path else 1700001000) | ||
@patch("os.remove") | ||
@patch("process_reboot_cause.swsscommon.SonicV2Connector") | ||
@patch("process_reboot_cause.device_info.is_smartswitch", return_value=False) | ||
@patch("sys.stdout", new_callable=StringIO) | ||
@patch("os.geteuid", return_value=0) | ||
@patch("process_reboot_cause.device_info.get_dpu_list", return_value=["dpu1", "dpu2"]) | ||
def test_invalid_json( | ||
self, mock_get_dpu_list, mock_geteuid, mock_stdout, mock_is_smartswitch, | ||
mock_connector, mock_remove, mock_getmtime, mock_exists, mock_isfile, | ||
mock_listdir, mock_open | ||
): | ||
# Mock DB | ||
mock_db = MagicMock() | ||
mock_connector.return_value = mock_db | ||
|
||
# Simulate running the script | ||
with patch.object(sys, "argv", ["process-reboot-cause"]): | ||
try: | ||
process_reboot_cause.read_reboot_cause_files_and_save_to_db('npu') | ||
except json.JSONDecodeError: | ||
pass # Expected failure due to invalid JSON | ||
|
||
# Check invalid JSON handling | ||
output = mock_stdout.getvalue() | ||
|
||
# Test read_reboot_cause_files_and_save_to_db - smartswitch | ||
@patch("builtins.open", new_callable=mock_open, read_data='{"cause": "Non-Hardware", "user": "admin", "name": "2024_12_13_01_12_36", "comment": "Switch rebooted DPU", "device": "DPU0", "time": "Fri Dec 13 01:12:36 AM UTC 2024"}') | ||
@patch("os.listdir", return_value=["file1.json"]) | ||
@patch("os.path.isfile", return_value=True) | ||
@patch("os.path.exists", return_value=True) | ||
@patch("os.path.getmtime", side_effect=lambda path: 1700000000 if "file1.json" in path else 1700001000) | ||
@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) | ||
@patch("os.geteuid", return_value=0) | ||
@patch("process_reboot_cause.device_info.get_dpu_list", return_value=["dpu1"]) | ||
def test_read_reboot_cause_files_and_save_to_db( | ||
self, mock_get_dpu_list, mock_geteuid, mock_stdout, mock_is_smartswitch, | ||
mock_connector, mock_remove, mock_getmtime, mock_exists, mock_isfile, | ||
mock_listdir, mock_open | ||
): | ||
# Mock DB | ||
mock_db = MagicMock() | ||
mock_connector.return_value = mock_db | ||
|
||
# Simulate running the script | ||
with patch.object(sys, "argv", ["process-reboot-cause"]): | ||
process_reboot_cause.read_reboot_cause_files_and_save_to_db('dpu1') | ||
|
||
# Test read_reboot_cause_files_and_save_to_db - smartswitch - name not in data | ||
@patch("builtins.open", new_callable=mock_open, read_data='{"cause": "Non-Hardware", "user": "admin", "comment": "Switch rebooted DPU", "device": "DPU0", "time": "Fri Dec 13 01:12:36 AM UTC 2024"}') | ||
@patch("os.listdir", return_value=["file1.json"]) | ||
@patch("os.path.isfile", return_value=True) | ||
@patch("os.path.exists", return_value=True) | ||
@patch("os.path.getmtime", side_effect=lambda path: 1700000000 if "file1.json" in path else 1700001000) | ||
@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) | ||
@patch("os.geteuid", return_value=0) | ||
@patch("process_reboot_cause.device_info.get_dpu_list", return_value=["dpu1"]) | ||
def test_read_reboot_cause_files_name_not_in_data( | ||
self, mock_get_dpu_list, mock_geteuid, mock_stdout, mock_is_smartswitch, | ||
mock_connector, mock_remove, mock_getmtime, mock_exists, mock_isfile, | ||
mock_listdir, mock_open | ||
): | ||
# Mock DB | ||
mock_db = MagicMock() | ||
mock_connector.return_value = mock_db | ||
|
||
# Simulate running the script | ||
with patch.object(sys, "argv", ["process-reboot-cause"]): | ||
process_reboot_cause.read_reboot_cause_files_and_save_to_db('dpu1') | ||
|