|
4 | 4 | import unittest |
5 | 5 | from unittest.mock import patch, mock_open, Mock |
6 | 6 | from utilities_common.general import load_module_from_source |
7 | | - |
8 | 7 | from sonic_installer.common import IMAGE_PREFIX |
| 8 | +import argparse |
9 | 9 |
|
10 | 10 | TESTS_DIR_PATH = os.path.dirname(os.path.abspath(__file__)) |
11 | 11 | UTILITY_DIR_PATH = os.path.dirname(TESTS_DIR_PATH) |
|
23 | 23 | sonic_kdump_config = load_module_from_source("sonic_kdump_config", sonic_kdump_config_path) |
24 | 24 |
|
25 | 25 |
|
| 26 | +class TestRemoteFlag(unittest.TestCase): |
| 27 | + def setUp(self): |
| 28 | + # Create a new ArgumentParser for each test |
| 29 | + self.parser = argparse.ArgumentParser(description="kdump configuration and status tool") |
| 30 | + self.parser.add_argument('--remote', action='store_true', default=False, |
| 31 | + help='Enable the Kdump remote SSH mechanism') |
| 32 | + |
| 33 | + def test_remote_flag_provided(self): |
| 34 | + """Test that the --remote flag sets the remote attribute to True.""" |
| 35 | + with patch.object(sys, 'argv', ['script.py', '--remote']): |
| 36 | + args = self.parser.parse_args() |
| 37 | + self.assertTrue(args.remote) |
| 38 | + |
| 39 | + def test_remote_flag_not_provided(self): |
| 40 | + """Test that the --remote flag defaults to False when not provided.""" |
| 41 | + with patch.object(sys, 'argv', ['script.py']): |
| 42 | + args = self.parser.parse_args() |
| 43 | + self.assertFalse(args.remote) |
| 44 | + |
| 45 | + def test_remote_flag_with_value(self): |
| 46 | + """Test that providing a value to the --remote flag raises an error.""" |
| 47 | + with patch.object(sys, 'argv', ['script.py', '--remote', 'some_value']): |
| 48 | + with self.assertRaises(SystemExit): |
| 49 | + self.parser.parse_args() |
| 50 | + |
| 51 | + |
26 | 52 | class TestSonicKdumpConfig(unittest.TestCase): |
27 | 53 | @classmethod |
28 | 54 | def setup_class(cls): |
@@ -246,7 +272,7 @@ def test_cmd_kdump_ssh_string_none(self, mock_print, mock_read, mock_run): |
246 | 272 | def test_cmd_kdump_ssh_string_update(self, mock_print, mock_write, mock_read, mock_run): |
247 | 273 | # Mock read_ssh_string to return the current SSH string |
248 | 274 | mock_read.return_value = 'old_ssh_string' |
249 | | - # Call the function with a new SSH string |
| 275 | + # Call the function with a new SSH string to configure |
250 | 276 | sonic_kdump_config.cmd_kdump_ssh_string(verbose=True, ssh_string='new_ssh_string') |
251 | 277 |
|
252 | 278 | # Check that write_ssh_string was called with the new SSH string |
@@ -302,6 +328,63 @@ def test_cmd_kdump_disable(self, mock_path_exist, mock_num_dumps, mock_memory, |
302 | 328 | return_result = sonic_kdump_config.cmd_kdump_disable(True) |
303 | 329 | assert return_result == False |
304 | 330 |
|
| 331 | + @patch("sonic_kdump_config.read_num_dumps") |
| 332 | + @patch("sonic_kdump_config.run_command") |
| 333 | + def test_write_num_dumps(self, mock_run_cmd, mock_read_num_dumps): |
| 334 | + # Success case: correct write and verification |
| 335 | + mock_run_cmd.side_effect = [(0, [], None)] |
| 336 | + mock_read_num_dumps.return_value = 5 |
| 337 | + sonic_kdump_config.write_num_dumps(5) |
| 338 | + |
| 339 | + # Case where run_command returns wrong type |
| 340 | + mock_run_cmd.side_effect = [(0, (), None)] |
| 341 | + mock_read_num_dumps.return_value = 5 |
| 342 | + with self.assertRaises(SystemExit) as sys_exit: |
| 343 | + sonic_kdump_config.write_num_dumps(5) |
| 344 | + self.assertEqual(sys_exit.exception.code, 1) |
| 345 | + |
| 346 | + # Case where line is non-empty |
| 347 | + mock_run_cmd.side_effect = [(0, ["Some output"], None)] |
| 348 | + mock_read_num_dumps.return_value = 5 |
| 349 | + with self.assertRaises(SystemExit) as sys_exit: |
| 350 | + sonic_kdump_config.write_num_dumps(5) |
| 351 | + self.assertEqual(sys_exit.exception.code, 1) |
| 352 | + |
| 353 | + # Case where read_num_dumps does not match input |
| 354 | + mock_run_cmd.side_effect = [(0, [], None)] |
| 355 | + mock_read_num_dumps.return_value = 4 |
| 356 | + with self.assertRaises(SystemExit) as sys_exit: |
| 357 | + sonic_kdump_config.write_num_dumps(5) |
| 358 | + self.assertEqual(sys_exit.exception.code, 1) |
| 359 | + |
| 360 | + # Case where run_command fails |
| 361 | + mock_run_cmd.side_effect = [(1, [], "Error")] |
| 362 | + mock_read_num_dumps.return_value = 5 |
| 363 | + with self.assertRaises(SystemExit) as sys_exit: |
| 364 | + sonic_kdump_config.write_num_dumps(5) |
| 365 | + self.assertEqual(sys_exit.exception.code, 1) |
| 366 | + |
| 367 | + # Case where lines contain non-integer |
| 368 | + mock_run_cmd.side_effect = [(0, ["NotInteger"], None)] |
| 369 | + mock_read_num_dumps.return_value = 5 |
| 370 | + with self.assertRaises(SystemExit) as sys_exit: |
| 371 | + sonic_kdump_config.write_num_dumps(5) |
| 372 | + self.assertEqual(sys_exit.exception.code, 1) |
| 373 | + |
| 374 | + # Edge case: empty string in output but matches value |
| 375 | + mock_run_cmd.side_effect = [(0, [""], None)] |
| 376 | + mock_read_num_dumps.return_value = 5 |
| 377 | + with self.assertRaises(SystemExit) as sys_exit: |
| 378 | + sonic_kdump_config.write_num_dumps(5) |
| 379 | + self.assertEqual(sys_exit.exception.code, 1) |
| 380 | + |
| 381 | + # Edge case: read_num_dumps returns matching value but run_command fails |
| 382 | + mock_run_cmd.side_effect = [(2, [], None)] |
| 383 | + mock_read_num_dumps.return_value = 5 |
| 384 | + with self.assertRaises(SystemExit) as sys_exit: |
| 385 | + sonic_kdump_config.write_num_dumps(5) |
| 386 | + self.assertEqual(sys_exit.exception.code, 1) |
| 387 | + |
305 | 388 | @patch("sonic_kdump_config.get_bootloader") |
306 | 389 | def test_get_image(self, mock_get_bootloader): |
307 | 390 | """Tests the function `get_current_image() and get_next_image()` in script `sonic-kdump-config.py`. |
|
0 commit comments