-
Notifications
You must be signed in to change notification settings - Fork 86
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Adding support for persistent storage and retrieval of DPU reboot-cause #169
Open
rameshraghupathy
wants to merge
55
commits into
sonic-net:master
Choose a base branch
from
rameshraghupathy:master
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+240
−6
Open
Changes from all commits
Commits
Show all changes
55 commits
Select commit
Hold shift + click to select a range
be55f8e
Adding support for persistent storage and retrieval of DPU reboot-cause
rameshraghupathy 3cd7b67
Added support for persisting dpu reboot-cause on smartswitch host
rameshraghupathy 0e47f97
Working on coverage
rameshraghupathy 1feeb9f
Working on ut coverage
rameshraghupathy 210dd14
working on coverage
rameshraghupathy 4c0fa72
working on coverage
rameshraghupathy 807a267
working on coverage
rameshraghupathy 766a677
working on coverage
rameshraghupathy 00496b5
working on coverage
rameshraghupathy b0b89c4
Fixed a typo
rameshraghupathy 667ec45
Working on coverage
rameshraghupathy 97ff55c
Fixing test failure
rameshraghupathy 0cca074
improving coverage
rameshraghupathy d97d228
Improving coverage
rameshraghupathy 1d0650f
working on coverage
rameshraghupathy 17345aa
Modifying reboot-cause workflow to meet multiple smartswitch vendor
rameshraghupathy 093bf00
Fixig the assertions to meet the new change
rameshraghupathy c28e29c
Fixed the DB
rameshraghupathy 887897d
Using the common API device_info.get_dpu_list()
rameshraghupathy ede90c0
Addressed review comments
rameshraghupathy 6b6b6b9
Added new test file tests/process-reboot-cause_test.py
rameshraghupathy e44cd1f
Added the scripts_path
rameshraghupathy a98c06d
Moved setup outside the test class
rameshraghupathy 6d21142
Fixed the file name
rameshraghupathy bcba133
Fixing test isssues
rameshraghupathy dffedc8
Working on UT
rameshraghupathy fefff1a
Fixed the numbeer of arguments to load_module_from_source
rameshraghupathy 3293749
addressed review comments
rameshraghupathy b61c2d9
adding mock for uid
rameshraghupathy 0e4d23a
passing uid arg
rameshraghupathy decce6c
Fixing test failure
rameshraghupathy 499d551
Fixing test failure
rameshraghupathy e9187a0
Fixing test failure
rameshraghupathy 82fc7fd
Fixing test failure
rameshraghupathy 15a70fa
Fixing test failure
rameshraghupathy 7a0d3d8
Fixing test failure
rameshraghupathy 1a55a59
Iproving coverage
rameshraghupathy 74c38ae
Iproving coverage
rameshraghupathy 2dfbc33
Iproving coverage
rameshraghupathy 0702dee
Iproving coverage
rameshraghupathy ddf4541
Iproving coverage
rameshraghupathy 3e2114f
Iproving coverage
rameshraghupathy 0be0072
Iproving coverage
rameshraghupathy 93a1dff
Iproving coverage
rameshraghupathy 79ed240
Iproving coverage
rameshraghupathy 6d41638
Iproving coverage
rameshraghupathy d6c4f94
Addressed review comments
rameshraghupathy 3bae343
Addressed review comments
rameshraghupathy c48efd9
Addressed review comments
rameshraghupathy 31aba69
Addressed review comments
rameshraghupathy a19e2ff
Addressed review comments
rameshraghupathy 6ad5432
Addressed review comments
rameshraghupathy 95dff75
Addressed review comments
rameshraghupathy 5f9859a
Addressed review comments
rameshraghupathy 67a1f07
Addressed review comments
rameshraghupathy File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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,115 @@ | ||
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": "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", side_effect=lambda path: 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) | ||
@patch("os.geteuid", return_value=0) | ||
def test_process_reboot_cause(self, mock_geteuid, mock_stdout, mock_is_smartswitch, mock_connector, mock_remove, 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}') | ||
@patch("os.listdir", return_value=["file1.json"]) | ||
@patch("os.path.isfile", return_value=True) | ||
@patch("os.path.exists", side_effect=lambda path: 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) | ||
@patch("os.geteuid", return_value=0) | ||
def test_invalid_json(self, mock_geteuid, mock_stdout, mock_is_smartswitch, mock_connector, 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() | ||
|
||
# Check invalid JSON handling | ||
output = mock_stdout.getvalue() | ||
self.assertTrue(mock_connector.called) | ||
|
||
# Test get_sorted_reboot_cause_files | ||
@patch("process_reboot_cause.os.listdir") | ||
@patch("process_reboot_cause.os.path.getmtime") | ||
def test_get_sorted_reboot_cause_files_success(self, mock_getmtime, mock_listdir): | ||
# Setup mock data | ||
mock_listdir.return_value = ["file1.txt", "file2.txt", "file3.txt"] | ||
mock_getmtime.side_effect = [100, 200, 50] # Mock modification times | ||
|
||
# Call the function | ||
result = process_reboot_cause.get_sorted_reboot_cause_files("/mock/dpu_history") | ||
|
||
# Assert the files are sorted by modification time in descending order | ||
self.assertEqual(result, [ | ||
"/mock/dpu_history/file2.txt", | ||
"/mock/dpu_history/file1.txt", | ||
"/mock/dpu_history/file3.txt" | ||
]) | ||
|
||
@patch("process_reboot_cause.os.listdir") | ||
def test_get_sorted_reboot_cause_files_error(self, mock_listdir): | ||
# Simulate an exception during file listing | ||
mock_listdir.side_effect = Exception("Mocked error") | ||
|
||
# Call the function and check the result | ||
result = process_reboot_cause.get_sorted_reboot_cause_files("/mock/dpu_history") | ||
self.assertEqual(result, []) | ||
|
||
# Test update_dpu_reboot_cause_to_chassis_state_db | ||
@patch("builtins.open", new_callable=mock_open, read_data='{"cause": "Non-Hardware", "comment": "Switch rebooted DPU", "device": "DPU0", "time": "Fri Dec 13 01:12:36 AM UTC 2024", "name": "2024_12_13_01_12_36"}') | ||
@patch("process_reboot_cause.device_info.get_dpu_list", return_value=["dpu1", "dpu2"]) | ||
@patch("os.path.isfile", return_value=True) | ||
@patch("process_reboot_cause.get_sorted_reboot_cause_files") | ||
@patch("process_reboot_cause.os.listdir", return_value=["2024_12_13_01_12_36_reboot_cause.txt", "2024_12_14_01_11_46_reboot_cause.txt"]) | ||
@patch("process_reboot_cause.swsscommon.SonicV2Connector") | ||
def test_update_dpu_reboot_cause_to_chassis_state_db_update(self, mock_connector, mock_listdir, mock_get_sorted_files, mock_isfile, mock_get_dpu_list, mock_open): | ||
# Setup mocks | ||
mock_get_sorted_files.return_value = ["/mock/dpu_history/2024_12_13_01_12_36_reboot_cause.txt"] | ||
|
||
# Mock the database connection | ||
mock_db = MagicMock() | ||
mock_connector.return_value = mock_db | ||
|
||
# Call the function that reads the file and updates the DB | ||
process_reboot_cause.update_dpu_reboot_cause_to_chassis_state_db() |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@rameshraghupathy How do we handle a case where NPU comes late so that DPU to NPU mid plane is not UP by the time this process starts?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@prgeor As shown in the HLD the NPU-chassisd will fetch the reboot-cause from the DPU and persist it.