|
| 1 | +# Copyright 2026 The Kubernetes Authors. |
| 2 | +# |
| 3 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +# you may not use this file except in compliance with the License. |
| 5 | +# You may obtain a copy of the License at |
| 6 | +# |
| 7 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +# |
| 9 | +# Unless required by applicable law or agreed to in writing, software |
| 10 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +# See the License for the specific language governing permissions and |
| 13 | +# limitations under the License. |
| 14 | + |
| 15 | +import unittest |
| 16 | +import os |
| 17 | +import logging |
| 18 | +from unittest.mock import MagicMock, patch |
| 19 | +from k8s_agent_sandbox.gke_extensions.podsnapshot_client import ( |
| 20 | + PodSnapshotSandboxClient, |
| 21 | +) |
| 22 | +from k8s_agent_sandbox.constants import ( |
| 23 | + PODSNAPSHOT_API_KIND, |
| 24 | + PODSNAPSHOT_API_GROUP, |
| 25 | + PODSNAPSHOT_API_VERSION, |
| 26 | +) |
| 27 | + |
| 28 | +from kubernetes.client import ApiException |
| 29 | + |
| 30 | +from kubernetes import config |
| 31 | + |
| 32 | +logger = logging.getLogger(__name__) |
| 33 | + |
| 34 | + |
| 35 | +class TestPodSnapshotSandboxClient(unittest.TestCase): |
| 36 | + |
| 37 | + @patch("kubernetes.config") |
| 38 | + def setUp(self, mock_config): |
| 39 | + logger.info("Setting up TestPodSnapshotSandboxClient...") |
| 40 | + # Mock kubernetes config loading |
| 41 | + mock_config.load_incluster_config.side_effect = config.ConfigException( |
| 42 | + "Not in cluster" |
| 43 | + ) |
| 44 | + mock_config.load_kube_config.return_value = None |
| 45 | + |
| 46 | + # Create client without patching super, as it's tested separately |
| 47 | + with patch.object( |
| 48 | + PodSnapshotSandboxClient, "_check_snapshot_crd_installed", return_value=True |
| 49 | + ): |
| 50 | + self.client = PodSnapshotSandboxClient("test-template") |
| 51 | + |
| 52 | + # Mock the kubernetes APIs on the client instance |
| 53 | + self.client.custom_objects_api = MagicMock() |
| 54 | + self.client.core_v1_api = MagicMock() |
| 55 | + |
| 56 | + logger.info("Finished setting up TestPodSnapshotSandboxClient.") |
| 57 | + |
| 58 | + def test_init(self): |
| 59 | + """Test initialization of PodSnapshotSandboxClient.""" |
| 60 | + logger.info("Starting test_init...") |
| 61 | + with patch( |
| 62 | + "k8s_agent_sandbox.sandbox_client.SandboxClient.__init__", return_value=None |
| 63 | + ) as mock_super: |
| 64 | + with patch.object( |
| 65 | + PodSnapshotSandboxClient, |
| 66 | + "_check_snapshot_crd_installed", |
| 67 | + return_value=True, |
| 68 | + ): |
| 69 | + client = PodSnapshotSandboxClient("test-template") |
| 70 | + mock_super.assert_called_once_with("test-template") |
| 71 | + self.assertFalse(client.snapshot_crd_installed) |
| 72 | + logger.info("Finished test_init.") |
| 73 | + |
| 74 | + def test_check_snapshot_crd_installed_success(self): |
| 75 | + """Test _check_snapshot_crd_installed success scenarios (Check CRD Existence).""" |
| 76 | + logger.info("TEST: CRD Existence Success") |
| 77 | + mock_resource_list = MagicMock() |
| 78 | + mock_resource = MagicMock() |
| 79 | + mock_resource.kind = PODSNAPSHOT_API_KIND |
| 80 | + mock_resource_list.resources = [mock_resource] |
| 81 | + self.client.custom_objects_api.get_api_resources.return_value = ( |
| 82 | + mock_resource_list |
| 83 | + ) |
| 84 | + |
| 85 | + self.client.snapshot_crd_installed = False |
| 86 | + self.assertTrue(self.client._check_snapshot_crd_installed()) |
| 87 | + self.client.custom_objects_api.get_api_resources.assert_called_with( |
| 88 | + group=PODSNAPSHOT_API_GROUP, version=PODSNAPSHOT_API_VERSION |
| 89 | + ) |
| 90 | + |
| 91 | + def test_check_snapshot_crd_installed_failures(self): |
| 92 | + """Test _check_snapshot_crd_installed failure scenarios.""" |
| 93 | + |
| 94 | + # 1. No CRDs found |
| 95 | + self.client.custom_objects_api.get_api_resources.return_value = None |
| 96 | + self.client.snapshot_crd_installed = False |
| 97 | + self.assertFalse(self.client._check_snapshot_crd_installed()) |
| 98 | + |
| 99 | + # 2. CRD Kind mismatch |
| 100 | + mock_resource_list = MagicMock() |
| 101 | + mock_resource = MagicMock() |
| 102 | + mock_resource.kind = "SomeOtherKind" |
| 103 | + mock_resource_list.resources = [mock_resource] |
| 104 | + self.client.custom_objects_api.get_api_resources.return_value = ( |
| 105 | + mock_resource_list |
| 106 | + ) |
| 107 | + self.client.snapshot_crd_installed = False |
| 108 | + self.assertFalse(self.client._check_snapshot_crd_installed()) |
| 109 | + |
| 110 | + # 3. 404 on CRD check |
| 111 | + self.client.custom_objects_api.get_api_resources.side_effect = ApiException( |
| 112 | + status=404 |
| 113 | + ) |
| 114 | + self.client.snapshot_crd_installed = False |
| 115 | + self.assertFalse(self.client._check_snapshot_crd_installed()) |
| 116 | + |
| 117 | + # 4. 403 on CRD check |
| 118 | + self.client.custom_objects_api.get_api_resources.side_effect = ApiException( |
| 119 | + status=403 |
| 120 | + ) |
| 121 | + self.client.snapshot_crd_installed = False |
| 122 | + self.assertFalse(self.client._check_snapshot_crd_installed()) |
| 123 | + |
| 124 | + def test_check_snapshot_crd_installed_exceptions(self): |
| 125 | + """Test API exceptions during snapshot readiness checks.""" |
| 126 | + |
| 127 | + # 1. 500 on CRD Check |
| 128 | + self.client.custom_objects_api.get_api_resources.side_effect = ApiException( |
| 129 | + status=500 |
| 130 | + ) |
| 131 | + self.client.snapshot_crd_installed = False |
| 132 | + with self.assertRaises(ApiException): |
| 133 | + self.client._check_snapshot_crd_installed() |
| 134 | + |
| 135 | + def test_enter_exit(self): |
| 136 | + """Test context manager __enter__ implementation.""" |
| 137 | + # Success path |
| 138 | + self.client.snapshot_crd_installed = False |
| 139 | + with patch.object( |
| 140 | + self.client, "_check_snapshot_crd_installed", return_value=True |
| 141 | + ) as mock_ready: |
| 142 | + with patch( |
| 143 | + "k8s_agent_sandbox.sandbox_client.SandboxClient.__enter__" |
| 144 | + ) as mock_super_enter: |
| 145 | + result = self.client.__enter__() |
| 146 | + self.assertEqual(result, self.client) |
| 147 | + mock_ready.assert_called_once() |
| 148 | + mock_super_enter.assert_called_once() |
| 149 | + self.assertTrue(self.client.snapshot_crd_installed) |
| 150 | + |
| 151 | + # Failure path: Controller not ready (return False) |
| 152 | + self.client.snapshot_crd_installed = False |
| 153 | + with patch.object( |
| 154 | + self.client, "_check_snapshot_crd_installed", return_value=False |
| 155 | + ) as mock_ready: |
| 156 | + with patch.object(self.client, "__exit__") as mock_exit: |
| 157 | + with self.assertRaises(RuntimeError) as context: |
| 158 | + self.client.__enter__() |
| 159 | + self.assertIn( |
| 160 | + "Pod Snapshot Controller is not ready", |
| 161 | + str(context.exception), |
| 162 | + ) |
| 163 | + mock_exit.assert_called_once_with(None, None, None) |
| 164 | + |
| 165 | + # Failure path: Exception during check |
| 166 | + self.client.snapshot_crd_installed = False |
| 167 | + with patch.object( |
| 168 | + self.client, |
| 169 | + "_check_snapshot_crd_installed", |
| 170 | + side_effect=ValueError("Test error"), |
| 171 | + ) as mock_ready: |
| 172 | + with patch.object(self.client, "__exit__") as mock_exit: |
| 173 | + with self.assertRaises(RuntimeError) as context: |
| 174 | + self.client.__enter__() |
| 175 | + self.assertIn( |
| 176 | + "Failed to initialize PodSnapshotSandboxClient", |
| 177 | + str(context.exception), |
| 178 | + ) |
| 179 | + mock_exit.assert_called_once_with(None, None, None) |
| 180 | + |
| 181 | + # Test Exit |
| 182 | + with patch( |
| 183 | + "k8s_agent_sandbox.sandbox_client.SandboxClient.__exit__" |
| 184 | + ) as mock_super_exit: |
| 185 | + exc_val = ValueError("test") |
| 186 | + self.client.__exit__(ValueError, exc_val, None) |
| 187 | + mock_super_exit.assert_called_once_with(ValueError, exc_val, None) |
| 188 | + |
| 189 | + def test_check_snapshot_crd_installed_already_ready(self): |
| 190 | + """Test early return if snapshot controller is already ready.""" |
| 191 | + self.client.snapshot_crd_installed = True |
| 192 | + result = self.client._check_snapshot_crd_installed() |
| 193 | + self.assertTrue(result) |
| 194 | + self.client.custom_objects_api.get_api_resources.assert_not_called() |
| 195 | + |
| 196 | + |
| 197 | +if __name__ == "__main__": |
| 198 | + unittest.main() |
0 commit comments