Skip to content

Commit 04c5628

Browse files
committed
Address review comments, fix mounting of the cidfile
1 parent f09b279 commit 04c5628

File tree

5 files changed

+39
-22
lines changed

5 files changed

+39
-22
lines changed

supervisor/bootstrap.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -226,9 +226,9 @@ def initialize_system(coresys: CoreSys) -> None:
226226
)
227227
config.path_addon_configs.mkdir()
228228

229-
if not config.path_cidfiles.is_dir():
230-
_LOGGER.debug("Creating Docker cidfiles folder at '%s'", config.path_cidfiles)
231-
config.path_cidfiles.mkdir()
229+
if not config.path_cid_files.is_dir():
230+
_LOGGER.debug("Creating Docker cidfiles folder at '%s'", config.path_cid_files)
231+
config.path_cid_files.mkdir()
232232

233233

234234
def warning_handler(message, category, filename, lineno, file=None, line=None):

supervisor/config.py

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@
5454
EMERGENCY_DATA = PurePath("emergency")
5555
ADDON_CONFIGS = PurePath("addon_configs")
5656
CORE_BACKUP_DATA = PurePath("core/backup")
57-
CIDFILES = PurePath("cidfiles")
57+
CID_FILES = PurePath("cid_files")
5858

5959
DEFAULT_BOOT_TIME = datetime.fromtimestamp(0, UTC).isoformat()
6060

@@ -401,9 +401,14 @@ def path_extern_media(self) -> PurePath:
401401
return PurePath(self.path_extern_supervisor, MEDIA_DATA)
402402

403403
@property
404-
def path_cidfiles(self) -> Path:
404+
def path_cid_files(self) -> Path:
405405
"""Return CID files folder."""
406-
return self.path_supervisor / CIDFILES
406+
return self.path_supervisor / CID_FILES
407+
408+
@property
409+
def path_extern_cid_files(self) -> PurePath:
410+
"""Return CID files folder."""
411+
return PurePath(self.path_extern_supervisor, CID_FILES)
407412

408413
@property
409414
def addons_repositories(self) -> list[str]:

supervisor/docker/manager.py

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -324,17 +324,24 @@ def run(
324324
# Setup cidfile and bind mount it
325325
cidfile_path = None
326326
if name:
327-
cidfile_path = self.coresys.config.path_cidfiles / f"{name}.cid"
327+
cidfile_path = self.coresys.config.path_cid_files / f"{name}.cid"
328328

329329
# Remove the file if it exists e.g. as a leftover from unclean shutdown
330330
if cidfile_path.is_file():
331331
with suppress(OSError):
332332
cidfile_path.unlink(missing_ok=True)
333333

334+
extern_cidfile_path = (
335+
self.coresys.config.path_extern_cid_files / f"{name}.cid"
336+
)
337+
334338
# Bind mount to /run/cid in container
335339
if "volumes" not in kwargs:
336340
kwargs["volumes"] = {}
337-
kwargs["volumes"]["/run/cid"] = {"bind": str(cidfile_path), "mode": "ro"}
341+
kwargs["volumes"][str(extern_cidfile_path)] = {
342+
"bind": "/run/cid",
343+
"mode": "ro",
344+
}
338345

339346
# Create container
340347
try:
@@ -581,7 +588,7 @@ def stop_container(
581588
_LOGGER.info("Cleaning %s application", name)
582589
docker_container.remove(force=True, v=True)
583590

584-
cidfile_path = self.coresys.config.path_cidfiles / f"{name}.cid"
591+
cidfile_path = self.coresys.config.path_cid_files / f"{name}.cid"
585592
with suppress(OSError):
586593
cidfile_path.unlink(missing_ok=True)
587594

tests/conftest.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -488,7 +488,7 @@ async def tmp_supervisor_data(coresys: CoreSys, tmp_path: Path) -> Path:
488488
coresys.config.path_addon_configs.mkdir(parents=True)
489489
coresys.config.path_ssl.mkdir()
490490
coresys.config.path_core_backup.mkdir(parents=True)
491-
coresys.config.path_cidfiles.mkdir()
491+
coresys.config.path_cid_files.mkdir()
492492
yield tmp_path
493493

494494

tests/docker/test_manager.py

Lines changed: 17 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
import pytest
88
from requests import RequestException
99

10-
from supervisor.config import CIDFILES
10+
from supervisor.coresys import CoreSys
1111
from supervisor.docker.manager import CommandReturn, DockerAPI
1212
from supervisor.exceptions import DockerError
1313

@@ -138,14 +138,17 @@ async def test_run_command_custom_stdout_stderr(docker: DockerAPI):
138138
assert result.output == b"output"
139139

140140

141-
async def test_run_container_with_cidfile(docker: DockerAPI, tmp_supervisor_data):
141+
async def test_run_container_with_cidfile(
142+
coresys: CoreSys, docker: DockerAPI, path_extern, tmp_supervisor_data
143+
):
142144
"""Test container creation with cidfile and bind mount."""
143145
# Mock container
144146
mock_container = MagicMock()
145147
mock_container.id = "test_container_id_12345"
146148

147149
container_name = "test_container"
148-
cidfile_path = tmp_supervisor_data / CIDFILES / f"{container_name}.cid"
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"
149152

150153
docker.docker.containers.run.return_value = mock_container
151154

@@ -166,9 +169,9 @@ async def test_run_container_with_cidfile(docker: DockerAPI, tmp_supervisor_data
166169
kwargs = create_mock.call_args[1]
167170

168171
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+
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"
172175

173176
# Verify container start was called
174177
mock_container.start.assert_called_once()
@@ -181,15 +184,15 @@ async def test_run_container_with_cidfile(docker: DockerAPI, tmp_supervisor_data
181184

182185

183186
async def test_run_container_with_leftover_cidfile(
184-
docker: DockerAPI, tmp_supervisor_data
187+
coresys: CoreSys, docker: DockerAPI, path_extern, tmp_supervisor_data
185188
):
186189
"""Test container creation removes leftover cidfile before creating new one."""
187190
# Mock container
188191
mock_container = MagicMock()
189192
mock_container.id = "test_container_id_new"
190193

191194
container_name = "test_container"
192-
cidfile_path = tmp_supervisor_data / "cidfiles" / f"{container_name}.cid"
195+
cidfile_path = coresys.config.path_cid_files / f"{container_name}.cid"
193196

194197
# Create a leftover cidfile
195198
cidfile_path.touch()
@@ -217,15 +220,15 @@ async def test_run_container_with_leftover_cidfile(
217220

218221

219222
async def test_stop_container_with_cidfile_cleanup(
220-
docker: DockerAPI, tmp_supervisor_data
223+
coresys: CoreSys, docker: DockerAPI, path_extern, tmp_supervisor_data
221224
):
222225
"""Test container stop with cidfile cleanup."""
223226
# Mock container
224227
mock_container = MagicMock()
225228
mock_container.status = "running"
226229

227230
container_name = "test_container"
228-
cidfile_path = tmp_supervisor_data / "cidfiles" / f"{container_name}.cid"
231+
cidfile_path = coresys.config.path_cid_files / f"{container_name}.cid"
229232

230233
# Create a cidfile
231234
cidfile_path.touch()
@@ -273,14 +276,16 @@ async def test_stop_container_without_removal_no_cidfile_cleanup(docker: DockerA
273276
mock_unlink.assert_not_called()
274277

275278

276-
async def test_cidfile_cleanup_handles_oserror(docker: DockerAPI, tmp_supervisor_data):
279+
async def test_cidfile_cleanup_handles_oserror(
280+
coresys: CoreSys, docker: DockerAPI, path_extern, tmp_supervisor_data
281+
):
277282
"""Test that cidfile cleanup handles OSError gracefully."""
278283
# Mock container
279284
mock_container = MagicMock()
280285
mock_container.status = "running"
281286

282287
container_name = "test_container"
283-
cidfile_path = tmp_supervisor_data / "cidfiles" / f"{container_name}.cid"
288+
cidfile_path = coresys.config.path_cid_files / f"{container_name}.cid"
284289

285290
# Create a cidfile
286291
cidfile_path.touch()

0 commit comments

Comments
 (0)