Skip to content

Commit b2dcd63

Browse files
committed
Add unit tests for reboot_helper.py
1 parent 01cf117 commit b2dcd63

File tree

3 files changed

+119
-5
lines changed

3 files changed

+119
-5
lines changed

scripts/reboot

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,6 @@ PLATFORM_UPDATE_REBOOT_CAUSE="platform_update_reboot_cause"
1111
REBOOT_CAUSE_FILE="/host/reboot-cause/reboot-cause.txt"
1212
PLATFORM_REBOOT_PRE_CHECK="platform_reboot_pre_check"
1313
REBOOT_TIME=$(date)
14-
PLATFORM_JSON_FILE="platform.json"
15-
PLATFORM_JSON_PATH="${DEVPATH}/${PLATFORM}/${PLATFORM_JSON_FILE}"
1614

1715
# Reboot immediately if we run the kdump capture kernel
1816
VMCORE_FILE=/proc/vmcore
@@ -39,6 +37,8 @@ EXIT_NEXT_IMAGE_NOT_EXISTS=4
3937
EXIT_SONIC_INSTALLER_VERIFY_REBOOT=21
4038
EXIT_PLATFORM_FW_AU_FAILURE=22
4139
PLATFORM_FWUTIL_AU_REBOOT_HANDLE="platform_fw_au_reboot_handle"
40+
PLATFORM_JSON_FILE="platform.json"
41+
PLATFORM_JSON_PATH="${DEVPATH}/${PLATFORM}/${PLATFORM_JSON_FILE}"
4242
REBOOT_SCRIPT_NAME=$(basename $0)
4343
REBOOT_TYPE="${REBOOT_SCRIPT_NAME}"
4444
TAG_LATEST=no
@@ -213,7 +213,7 @@ function get_reboot_status()
213213
{
214214
local dpu_ip=$1
215215
local port=$2
216-
reboot_status=$(gnoi_client -target ${dpu_ip}:${port} -logtostderr -insecure -rpc RebootStatus)
216+
reboot_status=$(docker exec -i gnmi gnoi_client -target ${dpu_ip}:${port} -logtostderr -insecure -rpc RebootStatus)
217217
if [ $? -ne 0 ] || [ -z "$reboot_status" ]; then
218218
echo "Error: Failed to send reboot status command to DPU ${DPU_NAME}"
219219
exit ${EXIT_ERROR}
@@ -259,7 +259,7 @@ function reboot_dpu_module()
259259
fi
260260

261261
# Issue GNOI client command to reboot the DPU
262-
gnoi_client -target ${dpu_ip}:${port} -logtostderr -insecure -rpc Reboot -jsonin '{"method":3}'
262+
docker exec -i gnmi gnoi_client -target ${dpu_ip}:${port} -logtostderr -insecure -rpc Reboot -jsonin '{"method":3}'
263263
if [ $? -ne 0 ]; then
264264
echo "Error: Failed to send reboot command to DPU ${DPU_NAME}"
265265
exit ${EXIT_ERROR}

scripts/reboot_helper.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,9 @@
44
#
55
# Utility helper for reboot within SONiC
66

7-
import sonic_platform
87
import sys
98
import syslog
9+
import sonic_platform
1010

1111
chk_log_level = syslog.LOG_ERR
1212

@@ -58,6 +58,9 @@ def reboot_module(module_name):
5858
log_err("Failed to load platform chassis")
5959
return False
6060

61+
if not platform_chassis.is_smartswitch():
62+
return False
63+
6164
# Iterate over the modules to find the one with the specified name
6265
try:
6366
# Use get_all_modules to retrieve all modules on the chassis

tests/test_reboot_helper.py

Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
1+
import os
2+
import sys
3+
import unittest
4+
from unittest.mock import patch, MagicMock
5+
6+
import pytest
7+
from utilities_common.general import load_module_from_source
8+
9+
test_path = os.path.dirname(os.path.abspath(__file__))
10+
modules_path = os.path.dirname(test_path)
11+
scripts_path = os.path.join(modules_path, "scripts")
12+
sys.path.insert(0, modules_path)
13+
14+
reboot_helper_path = os.path.join(scripts_path, 'reboot_helper.py')
15+
reboot_helper = load_module_from_source('reboot_helper', reboot_helper_path)
16+
17+
class TestRebootHelper(unittest.TestCase):
18+
19+
@patch('reboot_helper.syslog.syslog')
20+
def test_log_err(self, mock_syslog):
21+
reboot_helper.log_err('Test error message')
22+
mock_syslog.assert_called_with(reboot_helper.syslog.LOG_ERR, 'Test error message')
23+
24+
@patch('reboot_helper.syslog.syslog')
25+
def test_log_info(self, mock_syslog):
26+
reboot_helper.log_info('Test info message')
27+
mock_syslog.assert_called_with(reboot_helper.syslog.LOG_INFO, 'Test info message')
28+
29+
@patch('reboot_helper.syslog.syslog')
30+
def test_log_debug(self, mock_syslog):
31+
reboot_helper.log_debug('Test debug message')
32+
mock_syslog.assert_called_with(reboot_helper.syslog.LOG_DEBUG, 'Test debug message')
33+
34+
@patch('reboot_helper.sonic_platform.platform.Platform')
35+
def test_load_platform_chassis_success(self, mock_platform):
36+
mock_chassis = MagicMock()
37+
mock_platform.return_value.get_chassis.return_value = mock_chassis
38+
self.assertTrue(reboot_helper.load_platform_chassis())
39+
self.assertEqual(reboot_helper.platform_chassis, mock_chassis)
40+
41+
@patch('reboot_helper.sonic_platform.platform.Platform')
42+
def test_load_platform_chassis_failure(self, mock_platform):
43+
mock_platform.return_value.get_chassis.side_effect = Exception('Load failed')
44+
self.assertFalse(reboot_helper.load_platform_chassis())
45+
46+
@patch('reboot_helper.load_platform_chassis', return_value=True)
47+
@patch('reboot_helper.platform_chassis.get_all_modules')
48+
def test_reboot_module_success(self, mock_get_all_modules, mock_load_platform_chassis):
49+
mock_module = MagicMock()
50+
mock_module.get_name.return_value = 'test_module'
51+
mock_get_all_modules.return_value = [mock_module]
52+
self.assertTrue(reboot_helper.reboot_module('test_module'))
53+
mock_module.reboot.assert_called_once()
54+
55+
@patch('reboot_helper.load_platform_chassis', return_value=True)
56+
@patch('reboot_helper.platform_chassis.get_all_modules')
57+
def test_reboot_module_not_implemented_failure(self, mock_get_all_modules, mock_load_platform_chassis):
58+
mock_module = MagicMock()
59+
mock_module.get_name.return_value = 'test_module'
60+
mock_module.reboot.side_effect = NotImplementedError
61+
mock_get_all_modules.return_value = [mock_module]
62+
self.assertFalse(reboot_helper.reboot_module('test_module'))
63+
64+
@patch('reboot_helper.load_platform_chassis', return_value=True)
65+
@patch('reboot_helper.platform_chassis.get_all_modules')
66+
def test_reboot_module_general_exception(self, mock_get_all_modules, mock_load_platform_chassis):
67+
mock_module = MagicMock()
68+
mock_module.get_name.return_value = 'test_module'
69+
mock_module.reboot.side_effect = Exception('Reboot failed')
70+
mock_get_all_modules.return_value = [mock_module]
71+
self.assertFalse(reboot_helper.reboot_module('test_module'))
72+
73+
@patch('reboot_helper.load_platform_chassis', return_value=True)
74+
@patch('reboot_helper.platform_chassis.get_all_modules', side_effect=Exception('Failed to get modules'))
75+
def test_reboot_module_exception(self, mock_get_all_modules, mock_load_platform_chassis):
76+
self.assertFalse(reboot_helper.reboot_module('test_module'))
77+
78+
@patch('reboot_helper.load_platform_chassis', return_value=True)
79+
@patch('reboot_helper.platform_chassis.is_smartswitch', return_value=True)
80+
@patch('reboot_helper.platform_chassis.get_all_modules')
81+
def test_is_dpu_success(self, mock_get_all_modules, mock_is_smartswitch, mock_load_platform_chassis):
82+
mock_module = MagicMock()
83+
mock_module.get_name.return_value = 'DPU test_module'
84+
mock_get_all_modules.return_value = [mock_module]
85+
self.assertTrue(reboot_helper.is_dpu())
86+
87+
@patch('reboot_helper.load_platform_chassis', return_value=True)
88+
@patch('reboot_helper.platform_chassis.is_smartswitch', return_value=False)
89+
def test_is_dpu_failure(self, mock_is_smartswitch, mock_load_platform_chassis):
90+
self.assertFalse(reboot_helper.is_dpu())
91+
92+
@patch('reboot_helper.load_platform_chassis', return_value=False)
93+
def test_is_dpu_load_platform_chassis_failure(self, mock_load_platform_chassis):
94+
self.assertFalse(reboot_helper.is_dpu())
95+
96+
@patch('reboot_helper.load_platform_chassis', return_value=True)
97+
@patch('reboot_helper.platform_chassis.get_all_modules')
98+
def test_reboot_module_not_found(self, mock_get_all_modules, mock_load_platform_chassis):
99+
mock_get_all_modules.return_value = []
100+
self.assertFalse(reboot_helper.reboot_module('non_existent_module'))
101+
102+
@patch('reboot_helper.load_platform_chassis', return_value=True)
103+
@patch('reboot_helper.platform_chassis.get_all_modules')
104+
def test_reboot_module_name_mismatch(self, mock_get_all_modules, mock_load_platform_chassis):
105+
mock_module = MagicMock()
106+
mock_module.get_name.return_value = 'another_module'
107+
mock_get_all_modules.return_value = [mock_module]
108+
self.assertFalse(reboot_helper.reboot_module('test_module'))
109+
110+
if __name__ == '__main__':
111+
unittest.main()

0 commit comments

Comments
 (0)