refactor(commit): introduce Freezer class hierarchy across backends
Adds a Freezer ABC (backend/freeze.py) that encapsulates the
stop-commit-mark-preserved flow for all backends, following the same
pattern as BottleBackend. Each backend gets its own Freezer subclass:
DockerFreezer — docker commit
MacosContainerFreezer — container export + image rebuild; prompts
to stop if the container is running
SmolmachinesFreezer — smolvm pack create --from-vm
The base class owns write_committed_image, mark_preserved, and the
resume hint. Subclasses implement _freeze() and optionally override
_export_hint() for migration instructions.
Freezer.commit(agent, bottle) is the primary entry point for use
within a live launch context. Freezer.commit_slug(slug) is a
convenience wrapper for cmd_commit, which no longer branches on
backend names itself.
get_freezer(backend_name) is the factory, analogous to
get_bottle_backend(). CommitCancelled is raised by MacosContainerFreezer
when the user declines the stop prompt; cmd_commit catches it and
returns 0.
This commit is contained in:
+40
-179
@@ -7,15 +7,10 @@ import unittest
|
||||
from pathlib import Path
|
||||
from unittest.mock import MagicMock, patch
|
||||
|
||||
from bot_bottle.cli.commit import (
|
||||
cmd_commit,
|
||||
_agent_container_name,
|
||||
_committed_image_tag,
|
||||
_committed_smolmachine_artifact,
|
||||
_committed_smolmachine_output,
|
||||
)
|
||||
from bot_bottle.cli.commit import cmd_commit
|
||||
from bot_bottle import supervise
|
||||
from bot_bottle import bottle_state
|
||||
from bot_bottle.backend.freeze import CommitCancelled
|
||||
|
||||
|
||||
class _FakeHomeMixin:
|
||||
@@ -34,32 +29,8 @@ class _FakeHomeMixin:
|
||||
self._tmp.cleanup()
|
||||
|
||||
|
||||
class TestCommitHelpers(unittest.TestCase):
|
||||
def test_committed_image_tag(self):
|
||||
self.assertEqual(
|
||||
"bot-bottle-committed-dev-abc12:latest",
|
||||
_committed_image_tag("dev-abc12"),
|
||||
)
|
||||
|
||||
def test_agent_container_name(self):
|
||||
self.assertEqual(
|
||||
"bot-bottle-dev-abc12",
|
||||
_agent_container_name("dev-abc12"),
|
||||
)
|
||||
|
||||
def test_committed_smolmachine_paths(self):
|
||||
output = _committed_smolmachine_output("dev-abc12")
|
||||
artifact = _committed_smolmachine_artifact("dev-abc12")
|
||||
self.assertTrue(str(output).endswith(
|
||||
"/.bot-bottle/state/dev-abc12/committed-smolmachine"
|
||||
))
|
||||
self.assertTrue(str(artifact).endswith(
|
||||
"/.bot-bottle/state/dev-abc12/committed-smolmachine.smolmachine"
|
||||
))
|
||||
|
||||
|
||||
class TestCmdCommitSlugArg(_FakeHomeMixin, unittest.TestCase):
|
||||
"""cmd_commit with an explicit slug bypasses the TUI picker."""
|
||||
"""cmd_commit with an explicit slug delegates to get_freezer."""
|
||||
|
||||
def setUp(self):
|
||||
self._setup_fake_home()
|
||||
@@ -67,184 +38,74 @@ class TestCmdCommitSlugArg(_FakeHomeMixin, unittest.TestCase):
|
||||
def tearDown(self):
|
||||
self._teardown_fake_home()
|
||||
|
||||
def _write_meta(self, slug: str, backend: str) -> None:
|
||||
bottle_state.write_metadata(bottle_state.BottleMetadata(
|
||||
identity=slug, agent_name="dev", cwd="", copy_cwd=False,
|
||||
started_at="t", backend=backend,
|
||||
))
|
||||
|
||||
def test_commits_docker_bottle(self):
|
||||
slug = "dev-abc12"
|
||||
# Write metadata saying this is a docker bottle.
|
||||
bottle_state.write_metadata(bottle_state.BottleMetadata(
|
||||
identity=slug, agent_name="dev", cwd="", copy_cwd=False,
|
||||
started_at="t", backend="docker",
|
||||
))
|
||||
self._write_meta(slug, "docker")
|
||||
|
||||
with patch(
|
||||
"bot_bottle.cli.commit.docker_commit_container",
|
||||
) as mock_commit, patch(
|
||||
"bot_bottle.cli.commit.info",
|
||||
):
|
||||
with patch("bot_bottle.cli.commit.get_freezer") as mock_gf:
|
||||
mock_freezer = MagicMock()
|
||||
mock_gf.return_value = mock_freezer
|
||||
rc = cmd_commit([slug])
|
||||
|
||||
self.assertEqual(0, rc)
|
||||
mock_commit.assert_called_once_with(
|
||||
f"bot-bottle-{slug}",
|
||||
f"bot-bottle-committed-{slug}:latest",
|
||||
)
|
||||
mock_gf.assert_called_once_with("docker")
|
||||
mock_freezer.commit_slug.assert_called_once_with(slug)
|
||||
|
||||
def test_writes_committed_image_to_state(self):
|
||||
def test_empty_backend_passed_to_get_freezer(self):
|
||||
"""Old state dirs without a backend field pass '' to get_freezer."""
|
||||
slug = "dev-abc12"
|
||||
bottle_state.write_metadata(bottle_state.BottleMetadata(
|
||||
identity=slug, agent_name="dev", cwd="", copy_cwd=False,
|
||||
started_at="t", backend="docker",
|
||||
))
|
||||
self._write_meta(slug, "")
|
||||
|
||||
with patch("bot_bottle.cli.commit.docker_commit_container"), \
|
||||
patch("bot_bottle.cli.commit.info"):
|
||||
cmd_commit([slug])
|
||||
|
||||
self.assertEqual(
|
||||
f"bot-bottle-committed-{slug}:latest",
|
||||
bottle_state.read_committed_image(slug),
|
||||
)
|
||||
|
||||
def test_marks_bottle_preserved(self):
|
||||
slug = "dev-abc12"
|
||||
bottle_state.write_metadata(bottle_state.BottleMetadata(
|
||||
identity=slug, agent_name="dev", cwd="", copy_cwd=False,
|
||||
started_at="t", backend="docker",
|
||||
))
|
||||
|
||||
with patch("bot_bottle.cli.commit.docker_commit_container"), \
|
||||
patch("bot_bottle.cli.commit.info"):
|
||||
cmd_commit([slug])
|
||||
|
||||
self.assertTrue(bottle_state.is_preserved(slug))
|
||||
|
||||
def test_empty_backend_treated_as_docker(self):
|
||||
"""Old state dirs without a backend field should be treated as docker."""
|
||||
slug = "dev-abc12"
|
||||
bottle_state.write_metadata(bottle_state.BottleMetadata(
|
||||
identity=slug, agent_name="dev", cwd="", copy_cwd=False,
|
||||
started_at="t", backend="",
|
||||
))
|
||||
|
||||
with patch("bot_bottle.cli.commit.docker_commit_container") as mock_commit, \
|
||||
patch("bot_bottle.cli.commit.info"):
|
||||
with patch("bot_bottle.cli.commit.get_freezer") as mock_gf:
|
||||
mock_freezer = MagicMock()
|
||||
mock_gf.return_value = mock_freezer
|
||||
rc = cmd_commit([slug])
|
||||
|
||||
self.assertEqual(0, rc)
|
||||
mock_commit.assert_called_once()
|
||||
mock_gf.assert_called_once_with("")
|
||||
|
||||
def test_commits_macos_container_bottle(self):
|
||||
slug = "dev-abc12"
|
||||
bottle_state.write_metadata(bottle_state.BottleMetadata(
|
||||
identity=slug, agent_name="dev", cwd="", copy_cwd=False,
|
||||
started_at="t", backend="macos-container",
|
||||
))
|
||||
self._write_meta(slug, "macos-container")
|
||||
|
||||
with patch(
|
||||
"bot_bottle.cli.commit.macos_commit_container",
|
||||
) as mock_commit, patch(
|
||||
"bot_bottle.cli.commit.info",
|
||||
), patch(
|
||||
"bot_bottle.cli.commit.macos_container_is_running", return_value=False,
|
||||
):
|
||||
with patch("bot_bottle.cli.commit.get_freezer") as mock_gf:
|
||||
mock_freezer = MagicMock()
|
||||
mock_gf.return_value = mock_freezer
|
||||
rc = cmd_commit([slug])
|
||||
|
||||
self.assertEqual(0, rc)
|
||||
mock_commit.assert_called_once_with(
|
||||
f"bot-bottle-{slug}",
|
||||
f"bot-bottle-committed-{slug}:latest",
|
||||
)
|
||||
mock_gf.assert_called_once_with("macos-container")
|
||||
mock_freezer.commit_slug.assert_called_once_with(slug)
|
||||
|
||||
def test_running_macos_container_stops_then_commits_on_yes(self):
|
||||
def test_commits_smolmachines_bottle(self):
|
||||
slug = "dev-abc12"
|
||||
bottle_state.write_metadata(bottle_state.BottleMetadata(
|
||||
identity=slug, agent_name="dev", cwd="", copy_cwd=False,
|
||||
started_at="t", backend="macos-container",
|
||||
))
|
||||
self._write_meta(slug, "smolmachines")
|
||||
|
||||
with patch(
|
||||
"bot_bottle.cli.commit.macos_container_is_running", return_value=True,
|
||||
), patch(
|
||||
"bot_bottle.cli.commit.read_tty_line", return_value="y",
|
||||
), patch(
|
||||
"bot_bottle.cli.commit.macos_stop_container",
|
||||
) as mock_stop, patch(
|
||||
"bot_bottle.cli.commit.macos_commit_container",
|
||||
) as mock_commit, patch(
|
||||
"bot_bottle.cli.commit.info",
|
||||
):
|
||||
with patch("bot_bottle.cli.commit.get_freezer") as mock_gf:
|
||||
mock_freezer = MagicMock()
|
||||
mock_gf.return_value = mock_freezer
|
||||
rc = cmd_commit([slug])
|
||||
|
||||
self.assertEqual(0, rc)
|
||||
mock_stop.assert_called_once_with(f"bot-bottle-{slug}")
|
||||
mock_commit.assert_called_once_with(
|
||||
f"bot-bottle-{slug}",
|
||||
f"bot-bottle-committed-{slug}:latest",
|
||||
)
|
||||
mock_gf.assert_called_once_with("smolmachines")
|
||||
|
||||
def test_running_macos_container_aborts_on_no(self):
|
||||
def test_returns_zero_on_commit_cancelled(self):
|
||||
slug = "dev-abc12"
|
||||
bottle_state.write_metadata(bottle_state.BottleMetadata(
|
||||
identity=slug, agent_name="dev", cwd="", copy_cwd=False,
|
||||
started_at="t", backend="macos-container",
|
||||
))
|
||||
self._write_meta(slug, "macos-container")
|
||||
|
||||
with patch(
|
||||
"bot_bottle.cli.commit.macos_container_is_running", return_value=True,
|
||||
), patch(
|
||||
"bot_bottle.cli.commit.read_tty_line", return_value="n",
|
||||
), patch(
|
||||
"bot_bottle.cli.commit.macos_stop_container",
|
||||
) as mock_stop, patch(
|
||||
"bot_bottle.cli.commit.macos_commit_container",
|
||||
) as mock_commit, patch(
|
||||
"bot_bottle.cli.commit.info",
|
||||
):
|
||||
with patch("bot_bottle.cli.commit.get_freezer") as mock_gf:
|
||||
mock_freezer = MagicMock()
|
||||
mock_freezer.commit_slug.side_effect = CommitCancelled
|
||||
mock_gf.return_value = mock_freezer
|
||||
rc = cmd_commit([slug])
|
||||
|
||||
self.assertEqual(0, rc)
|
||||
mock_stop.assert_not_called()
|
||||
mock_commit.assert_not_called()
|
||||
|
||||
|
||||
class TestCmdCommitSmolmachinesBackend(_FakeHomeMixin, unittest.TestCase):
|
||||
def setUp(self):
|
||||
self._setup_fake_home()
|
||||
|
||||
def tearDown(self):
|
||||
self._teardown_fake_home()
|
||||
|
||||
def test_packs_smolmachines_bottle(self):
|
||||
slug = "dev-abc12"
|
||||
bottle_state.write_metadata(bottle_state.BottleMetadata(
|
||||
identity=slug, agent_name="dev", cwd="", copy_cwd=False,
|
||||
started_at="t", backend="smolmachines",
|
||||
))
|
||||
|
||||
with patch(
|
||||
"bot_bottle.cli.commit.pack_create_from_vm",
|
||||
) as mock_pack, patch(
|
||||
"bot_bottle.cli.commit.info",
|
||||
):
|
||||
rc = cmd_commit([slug])
|
||||
|
||||
self.assertEqual(0, rc)
|
||||
mock_pack.assert_called_once_with(
|
||||
f"bot-bottle-{slug}",
|
||||
_committed_smolmachine_output(slug),
|
||||
)
|
||||
self.assertEqual(
|
||||
str(_committed_smolmachine_artifact(slug)),
|
||||
bottle_state.read_committed_image(slug),
|
||||
)
|
||||
self.assertTrue(bottle_state.is_preserved(slug))
|
||||
|
||||
|
||||
class TestCmdCommitUnsupportedBackend(_FakeHomeMixin, unittest.TestCase):
|
||||
def setUp(self):
|
||||
self._setup_fake_home()
|
||||
|
||||
def tearDown(self):
|
||||
self._teardown_fake_home()
|
||||
|
||||
|
||||
class TestCmdCommitNoActiveBottles(_FakeHomeMixin, unittest.TestCase):
|
||||
|
||||
Reference in New Issue
Block a user