|
| 1 | +import tempfile |
| 2 | +import os |
| 3 | +from unittest import mock |
| 4 | +from charon.pkgs.radas_signature_handler import sign_in_radas |
| 5 | + |
| 6 | +def test_sign_in_radas_normal_flow(self): |
| 7 | + with tempfile.TemporaryDirectory() as tmpdir: |
| 8 | + # Mock configuration |
| 9 | + mock_config = mock.MagicMock() |
| 10 | + mock_config.is_radas_enabled.return_value = True |
| 11 | + mock_radas_config = mock.MagicMock() |
| 12 | + mock_config.get_radas_config.return_value = mock_radas_config |
| 13 | + |
| 14 | + # Mock Container run to avoid real AMQP connection |
| 15 | + with mock.patch("charon.pkgs.radas_signature_handler.Container") as mock_container, \ |
| 16 | + mock.patch("charon.pkgs.radas_signature_handler.get_config", return_value=mock_config), \ |
| 17 | + mock.patch("charon.pkgs.radas_signature_handler.uuid.uuid4", return_value="mocked-uuid"): |
| 18 | + |
| 19 | + # Test parameters |
| 20 | + test_result_path = os.path.join(tmpdir, "results") |
| 21 | + os.makedirs(test_result_path) |
| 22 | + |
| 23 | + # Call the function |
| 24 | + sign_in_radas( |
| 25 | + repo_url="quay.io/test/repo", |
| 26 | + requester="test-user", |
| 27 | + sign_key="test-key", |
| 28 | + result_path=test_result_path, |
| 29 | + ignore_patterns=[], |
| 30 | + radas_config=mock_radas_config |
| 31 | + ) |
| 32 | + |
| 33 | + # Verify Container.run() was called twice (sender and receiver) |
| 34 | + self.assertEqual(mock_container.call_count, 2) |
| 35 | + |
| 36 | + # Verify request ID propagation |
| 37 | + receiver_call = mock_container.call_args_list[1] |
| 38 | + self.assertEqual(receiver_call.args[0].request_id, "mocked-uuid") |
| 39 | + |
| 40 | +def test_sign_in_radas_with_disabled_config(self): |
| 41 | + # Mock disabled configuration |
| 42 | + mock_config = mock.MagicMock() |
| 43 | + mock_config.is_radas_enabled.return_value = False |
| 44 | + |
| 45 | + with mock.patch("charon.pkgs.radas_signature_handler.get_config", return_value=mock_config), \ |
| 46 | + self.assertRaises(SystemExit): |
| 47 | + sign_in_radas( |
| 48 | + repo_url="quay.io/test/repo", |
| 49 | + requester="test-user", |
| 50 | + sign_key="test-key", |
| 51 | + result_path="/tmp/results", |
| 52 | + ignore_patterns=[], |
| 53 | + radas_config=mock.MagicMock() |
| 54 | + ) |
| 55 | + |
| 56 | +def test_sign_in_radas_connection_cleanup(self): |
| 57 | + # Mock configuration and connection objects |
| 58 | + mock_config = mock.MagicMock() |
| 59 | + mock_config.is_radas_enabled.return_value = True |
| 60 | + mock_radas_config = mock.MagicMock() |
| 61 | + |
| 62 | + with mock.patch("charon.pkgs.radas_signature_handler.Container") as mock_container, \ |
| 63 | + mock.patch("charon.pkgs.radas_signature_handler.get_config", return_value=mock_config): |
| 64 | + |
| 65 | + # Mock connection objects |
| 66 | + mock_sender_conn = mock.MagicMock() |
| 67 | + mock_listener_conn = mock.MagicMock() |
| 68 | + |
| 69 | + # Create test call |
| 70 | + def container_side_effect(*args, **kwargs): |
| 71 | + if args[0].__class__.__name__ == "RadasReceiver": |
| 72 | + args[0].conn = mock_listener_conn |
| 73 | + elif args[0].__class__.__name__ == "RadasSender": |
| 74 | + args[0].conn = mock_sender_conn |
| 75 | + return mock.MagicMock() |
| 76 | + |
| 77 | + mock_container.side_effect = container_side_effect |
| 78 | + |
| 79 | + sign_in_radas( |
| 80 | + repo_url="quay.io/test/repo", |
| 81 | + requester="test-user", |
| 82 | + sign_key="test-key", |
| 83 | + result_path="/tmp/results", |
| 84 | + ignore_patterns=[], |
| 85 | + radas_config=mock_radas_config |
| 86 | + ) |
| 87 | + |
| 88 | + # Verify connections are closed |
| 89 | + mock_sender_conn.close.assert_called_once() |
| 90 | + mock_listener_conn.close.assert_called_once() |
0 commit comments