8ae6561b33
lint / lint (push) Failing after 2m7s
Importing backend.docker.util previously triggered eager loading of all three backend packages (~76 modules) because backend/__init__.py imported DockerBottleBackend, FirecrackerBottleBackend, and MacosContainerBottleBackend at module scope. This made the module prohibitively expensive to import from the orchestrator layer and elsewhere. The three backend imports are now deferred into _get_backends(), which loads all three on first call and caches the result in the module-level _BACKENDS variable (initially None). Module-level __getattr__ exposes backend classes and freeze symbols lazily for existing import/patch sites. backend/docker/util.py raw subprocess.run(["docker", ...]) calls are replaced with the shared run_docker primitive from docker_cmd, eliminating the duplication between the backend and orchestrator implementations. _silent_run() is removed; image_exists() is inlined directly onto run_docker. The commit_container test is updated to patch run_docker instead of subprocess.run.
72 lines
2.3 KiB
Python
72 lines
2.3 KiB
Python
"""Unit: commit_container helper in
|
|
bot_bottle.backend.docker.util (PRD 0023 chunk 4c additions).
|
|
|
|
Tests mock `subprocess.run` and assert on argv shape + parsing.
|
|
The actual docker round-trip is covered by the chunk 4c
|
|
integration smoke."""
|
|
|
|
from __future__ import annotations
|
|
|
|
import subprocess
|
|
import unittest
|
|
from unittest.mock import patch
|
|
|
|
from bot_bottle.backend.docker import util as docker_mod
|
|
|
|
|
|
def _ok(stdout: str = "", stderr: str = "") -> subprocess.CompletedProcess: # type: ignore
|
|
return subprocess.CompletedProcess(
|
|
args=[], returncode=0, stdout=stdout, stderr=stderr,
|
|
)
|
|
|
|
|
|
def _fail(stderr: str = "boom") -> subprocess.CompletedProcess: # type: ignore
|
|
return subprocess.CompletedProcess(
|
|
args=[], returncode=1, stdout="", stderr=stderr,
|
|
)
|
|
|
|
|
|
class TestCommitContainer(unittest.TestCase):
|
|
def test_runs_docker_commit(self):
|
|
with patch.object(
|
|
docker_mod, "run_docker", return_value=_ok(),
|
|
) as run, patch.object(docker_mod, "info"):
|
|
docker_mod.commit_container(
|
|
"bot-bottle-dev-abc12",
|
|
"bot-bottle-committed-dev-abc12:latest",
|
|
)
|
|
argv = run.call_args.args[0]
|
|
self.assertEqual(
|
|
[
|
|
"docker", "commit",
|
|
"bot-bottle-dev-abc12",
|
|
"bot-bottle-committed-dev-abc12:latest",
|
|
],
|
|
argv,
|
|
)
|
|
|
|
def test_dies_on_docker_commit_failure(self):
|
|
with patch.object(
|
|
docker_mod, "run_docker", return_value=_fail("No such container"),
|
|
), patch.object(
|
|
docker_mod, "die", side_effect=SystemExit("die"),
|
|
) as die:
|
|
with self.assertRaises(SystemExit):
|
|
docker_mod.commit_container("missing-container", "some:tag")
|
|
die.assert_called_once()
|
|
self.assertIn("missing-container", die.call_args.args[0])
|
|
|
|
def test_die_message_includes_image_tag(self):
|
|
with patch.object(
|
|
docker_mod, "run_docker", return_value=_fail("boom"),
|
|
), patch.object(
|
|
docker_mod, "die", side_effect=SystemExit("die"),
|
|
) as die:
|
|
with self.assertRaises(SystemExit):
|
|
docker_mod.commit_container("ctr", "my-tag:v1")
|
|
self.assertIn("my-tag:v1", die.call_args.args[0])
|
|
|
|
|
|
if __name__ == "__main__":
|
|
unittest.main()
|