|
| 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