|
4 | 4 | import sys |
5 | 5 | import os |
6 | 6 |
|
| 7 | +# Mock redis module (available in SONiC runtime, not in test environment) |
| 8 | +sys.modules['redis'] = MagicMock() |
| 9 | + |
7 | 10 | sys.path.insert(0, os.path.abspath(os.path.join(os.path.dirname(__file__), '..', 'scripts'))) |
8 | 11 |
|
9 | 12 | import gnoi_shutdown_daemon |
@@ -390,5 +393,81 @@ def test_sonic_platform_import_mock(self): |
390 | 393 | mock_platform_class.assert_called_once() |
391 | 394 | mock_platform_instance.get_chassis.assert_called_once() |
392 | 395 |
|
| 396 | + def test_is_tcp_open_success(self): |
| 397 | + """Test is_tcp_open when connection succeeds.""" |
| 398 | + with patch('gnoi_shutdown_daemon.socket.create_connection') as mock_socket: |
| 399 | + mock_socket.return_value.__enter__ = MagicMock() |
| 400 | + mock_socket.return_value.__exit__ = MagicMock() |
| 401 | + result = gnoi_shutdown_daemon.is_tcp_open("10.0.0.1", 8080, timeout=1.0) |
| 402 | + self.assertTrue(result) |
| 403 | + mock_socket.assert_called_once_with(("10.0.0.1", 8080), 1.0) |
| 404 | + |
| 405 | + def test_is_tcp_open_failure(self): |
| 406 | + """Test is_tcp_open when connection fails.""" |
| 407 | + with patch('gnoi_shutdown_daemon.socket.create_connection', side_effect=OSError("Connection refused")): |
| 408 | + result = gnoi_shutdown_daemon.is_tcp_open("10.0.0.1", 8080, timeout=1.0) |
| 409 | + self.assertFalse(result) |
| 410 | + |
| 411 | + def test_get_dpu_ip_with_string_ips(self): |
| 412 | + """Test get_dpu_ip when ips is a string instead of list.""" |
| 413 | + mock_config = MagicMock() |
| 414 | + mock_config.get_entry.return_value = {"ips": "10.0.0.5"} |
| 415 | + |
| 416 | + ip = gnoi_shutdown_daemon.get_dpu_ip(mock_config, "DPU1") |
| 417 | + self.assertEqual(ip, "10.0.0.5") |
| 418 | + |
| 419 | + def test_get_dpu_ip_empty_entry(self): |
| 420 | + """Test get_dpu_ip when entry is empty.""" |
| 421 | + mock_config = MagicMock() |
| 422 | + mock_config.get_entry.return_value = {} |
| 423 | + |
| 424 | + ip = gnoi_shutdown_daemon.get_dpu_ip(mock_config, "DPU1") |
| 425 | + self.assertIsNone(ip) |
| 426 | + |
| 427 | + def test_get_dpu_ip_no_ips_field(self): |
| 428 | + """Test get_dpu_ip when entry has no ips field.""" |
| 429 | + mock_config = MagicMock() |
| 430 | + mock_config.get_entry.return_value = {"other_field": "value"} |
| 431 | + |
| 432 | + ip = gnoi_shutdown_daemon.get_dpu_ip(mock_config, "DPU1") |
| 433 | + self.assertIsNone(ip) |
| 434 | + |
| 435 | + def test_get_dpu_ip_exception(self): |
| 436 | + """Test get_dpu_ip when exception occurs.""" |
| 437 | + mock_config = MagicMock() |
| 438 | + mock_config.get_entry.side_effect = Exception("Database error") |
| 439 | + |
| 440 | + ip = gnoi_shutdown_daemon.get_dpu_ip(mock_config, "DPU1") |
| 441 | + self.assertIsNone(ip) |
| 442 | + |
| 443 | + def test_get_dpu_gnmi_port_exception(self): |
| 444 | + """Test get_dpu_gnmi_port when exception occurs.""" |
| 445 | + mock_config = MagicMock() |
| 446 | + mock_config.get_entry.side_effect = Exception("Database error") |
| 447 | + |
| 448 | + port = gnoi_shutdown_daemon.get_dpu_gnmi_port(mock_config, "DPU1") |
| 449 | + self.assertEqual(port, "8080") |
| 450 | + |
| 451 | + def test_send_reboot_command_success(self): |
| 452 | + """Test successful _send_reboot_command.""" |
| 453 | + with patch('gnoi_shutdown_daemon.execute_gnoi_command', return_value=(0, "success", "")): |
| 454 | + handler = gnoi_shutdown_daemon.GnoiRebootHandler(MagicMock(), MagicMock(), MagicMock()) |
| 455 | + result = handler._send_reboot_command("DPU0", "10.0.0.1", "8080") |
| 456 | + self.assertTrue(result) |
| 457 | + |
| 458 | + def test_set_gnoi_shutdown_complete_flag_success(self): |
| 459 | + """Test successful setting of gnoi_shutdown_complete flag.""" |
| 460 | + mock_db = MagicMock() |
| 461 | + mock_table = MagicMock() |
| 462 | + |
| 463 | + with patch('gnoi_shutdown_daemon.swsscommon.Table', return_value=mock_table): |
| 464 | + handler = gnoi_shutdown_daemon.GnoiRebootHandler(mock_db, MagicMock(), MagicMock()) |
| 465 | + handler._set_gnoi_shutdown_complete_flag("DPU0", True) |
| 466 | + |
| 467 | + # Verify the flag was set correctly |
| 468 | + mock_table.set.assert_called_once() |
| 469 | + call_args = mock_table.set.call_args |
| 470 | + self.assertEqual(call_args[0][0], "DPU0") |
| 471 | + |
393 | 472 | if __name__ == '__main__': |
394 | 473 | unittest.main() |
0 commit comments