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