|
1 | 1 | """Test Docker manager.""" |
2 | 2 |
|
3 | | -from unittest.mock import MagicMock |
| 3 | +import asyncio |
| 4 | +from unittest.mock import MagicMock, patch |
4 | 5 |
|
5 | 6 | from docker.errors import DockerException |
6 | 7 | import pytest |
7 | 8 | from requests import RequestException |
8 | 9 |
|
| 10 | +from supervisor.config import CIDFILES |
9 | 11 | from supervisor.docker.manager import CommandReturn, DockerAPI |
10 | 12 | from supervisor.exceptions import DockerError |
11 | 13 |
|
@@ -134,3 +136,168 @@ async def test_run_command_custom_stdout_stderr(docker: DockerAPI): |
134 | 136 | # Verify the result |
135 | 137 | assert result.exit_code == 0 |
136 | 138 | assert result.output == b"output" |
| 139 | + |
| 140 | + |
| 141 | +async def test_run_container_with_cidfile(docker: DockerAPI, tmp_supervisor_data): |
| 142 | + """Test container creation with cidfile and bind mount.""" |
| 143 | + # Mock container |
| 144 | + mock_container = MagicMock() |
| 145 | + mock_container.id = "test_container_id_12345" |
| 146 | + |
| 147 | + container_name = "test_container" |
| 148 | + cidfile_path = tmp_supervisor_data / CIDFILES / f"{container_name}.cid" |
| 149 | + |
| 150 | + docker.docker.containers.run.return_value = mock_container |
| 151 | + |
| 152 | + # Mock container creation |
| 153 | + with patch.object( |
| 154 | + docker.containers, "create", return_value=mock_container |
| 155 | + ) as create_mock: |
| 156 | + # Execute run with a container name |
| 157 | + loop = asyncio.get_event_loop() |
| 158 | + result = await loop.run_in_executor( |
| 159 | + None, |
| 160 | + lambda kwrgs: docker.run(**kwrgs), |
| 161 | + {"image": "test_image", "tag": "latest", "name": container_name}, |
| 162 | + ) |
| 163 | + |
| 164 | + # Check the container creation parameters |
| 165 | + create_mock.assert_called_once() |
| 166 | + kwargs = create_mock.call_args[1] |
| 167 | + |
| 168 | + assert "volumes" in kwargs |
| 169 | + assert "/run/cid" in kwargs["volumes"] |
| 170 | + assert kwargs["volumes"]["/run/cid"]["bind"] == str(cidfile_path) |
| 171 | + assert kwargs["volumes"]["/run/cid"]["mode"] == "ro" |
| 172 | + |
| 173 | + # Verify container start was called |
| 174 | + mock_container.start.assert_called_once() |
| 175 | + |
| 176 | + # Verify cidfile was written with container ID |
| 177 | + assert cidfile_path.exists() |
| 178 | + assert cidfile_path.read_text() == mock_container.id |
| 179 | + |
| 180 | + assert result == mock_container |
| 181 | + |
| 182 | + |
| 183 | +async def test_run_container_with_leftover_cidfile( |
| 184 | + docker: DockerAPI, tmp_supervisor_data |
| 185 | +): |
| 186 | + """Test container creation removes leftover cidfile before creating new one.""" |
| 187 | + # Mock container |
| 188 | + mock_container = MagicMock() |
| 189 | + mock_container.id = "test_container_id_new" |
| 190 | + |
| 191 | + container_name = "test_container" |
| 192 | + cidfile_path = tmp_supervisor_data / "cidfiles" / f"{container_name}.cid" |
| 193 | + |
| 194 | + # Create a leftover cidfile |
| 195 | + cidfile_path.touch() |
| 196 | + |
| 197 | + # Mock container creation |
| 198 | + with patch.object( |
| 199 | + docker.containers, "create", return_value=mock_container |
| 200 | + ) as create_mock: |
| 201 | + # Execute run with a container name |
| 202 | + loop = asyncio.get_event_loop() |
| 203 | + result = await loop.run_in_executor( |
| 204 | + None, |
| 205 | + lambda kwrgs: docker.run(**kwrgs), |
| 206 | + {"image": "test_image", "tag": "latest", "name": container_name}, |
| 207 | + ) |
| 208 | + |
| 209 | + # Verify container was created |
| 210 | + create_mock.assert_called_once() |
| 211 | + |
| 212 | + # Verify new cidfile was written with container ID |
| 213 | + assert cidfile_path.exists() |
| 214 | + assert cidfile_path.read_text() == mock_container.id |
| 215 | + |
| 216 | + assert result == mock_container |
| 217 | + |
| 218 | + |
| 219 | +async def test_stop_container_with_cidfile_cleanup( |
| 220 | + docker: DockerAPI, tmp_supervisor_data |
| 221 | +): |
| 222 | + """Test container stop with cidfile cleanup.""" |
| 223 | + # Mock container |
| 224 | + mock_container = MagicMock() |
| 225 | + mock_container.status = "running" |
| 226 | + |
| 227 | + container_name = "test_container" |
| 228 | + cidfile_path = tmp_supervisor_data / "cidfiles" / f"{container_name}.cid" |
| 229 | + |
| 230 | + # Create a cidfile |
| 231 | + cidfile_path.touch() |
| 232 | + |
| 233 | + # Mock the containers.get method and cidfile cleanup |
| 234 | + with ( |
| 235 | + patch.object(docker.containers, "get", return_value=mock_container), |
| 236 | + ): |
| 237 | + # Call stop_container with remove_container=True |
| 238 | + loop = asyncio.get_event_loop() |
| 239 | + await loop.run_in_executor( |
| 240 | + None, |
| 241 | + lambda kwrgs: docker.stop_container(**kwrgs), |
| 242 | + {"timeout": 10, "remove_container": True, "name": container_name}, |
| 243 | + ) |
| 244 | + |
| 245 | + # Verify container operations |
| 246 | + mock_container.stop.assert_called_once_with(timeout=10) |
| 247 | + mock_container.remove.assert_called_once_with(force=True, v=True) |
| 248 | + |
| 249 | + assert not cidfile_path.exists() |
| 250 | + |
| 251 | + |
| 252 | +async def test_stop_container_without_removal_no_cidfile_cleanup(docker: DockerAPI): |
| 253 | + """Test container stop without removal doesn't clean up cidfile.""" |
| 254 | + # Mock container |
| 255 | + mock_container = MagicMock() |
| 256 | + mock_container.status = "running" |
| 257 | + |
| 258 | + container_name = "test_container" |
| 259 | + |
| 260 | + # Mock the containers.get method and cidfile cleanup |
| 261 | + with ( |
| 262 | + patch.object(docker.containers, "get", return_value=mock_container), |
| 263 | + patch("pathlib.Path.unlink") as mock_unlink, |
| 264 | + ): |
| 265 | + # Call stop_container with remove_container=False |
| 266 | + docker.stop_container(container_name, timeout=10, remove_container=False) |
| 267 | + |
| 268 | + # Verify container operations |
| 269 | + mock_container.stop.assert_called_once_with(timeout=10) |
| 270 | + mock_container.remove.assert_not_called() |
| 271 | + |
| 272 | + # Verify cidfile cleanup was NOT called |
| 273 | + mock_unlink.assert_not_called() |
| 274 | + |
| 275 | + |
| 276 | +async def test_cidfile_cleanup_handles_oserror(docker: DockerAPI, tmp_supervisor_data): |
| 277 | + """Test that cidfile cleanup handles OSError gracefully.""" |
| 278 | + # Mock container |
| 279 | + mock_container = MagicMock() |
| 280 | + mock_container.status = "running" |
| 281 | + |
| 282 | + container_name = "test_container" |
| 283 | + cidfile_path = tmp_supervisor_data / "cidfiles" / f"{container_name}.cid" |
| 284 | + |
| 285 | + # Create a cidfile |
| 286 | + cidfile_path.touch() |
| 287 | + |
| 288 | + # Mock the containers.get method and cidfile cleanup to raise OSError |
| 289 | + with ( |
| 290 | + patch.object(docker.containers, "get", return_value=mock_container), |
| 291 | + patch( |
| 292 | + "pathlib.Path.unlink", side_effect=OSError("File not found") |
| 293 | + ) as mock_unlink, |
| 294 | + ): |
| 295 | + # Call stop_container - should not raise exception |
| 296 | + docker.stop_container(container_name, timeout=10, remove_container=True) |
| 297 | + |
| 298 | + # Verify container operations completed |
| 299 | + mock_container.stop.assert_called_once_with(timeout=10) |
| 300 | + mock_container.remove.assert_called_once_with(force=True, v=True) |
| 301 | + |
| 302 | + # Verify cidfile cleanup was attempted |
| 303 | + mock_unlink.assert_called_once_with(missing_ok=True) |
0 commit comments