test(smolmachines): cover sidecar vm launch paths
This commit is contained in:
@@ -9,6 +9,7 @@ from __future__ import annotations
|
||||
import subprocess
|
||||
import tempfile
|
||||
import unittest
|
||||
from contextlib import ExitStack
|
||||
from dataclasses import replace
|
||||
from pathlib import Path
|
||||
from unittest.mock import MagicMock, patch
|
||||
@@ -27,6 +28,7 @@ from bot_bottle.backend.smolmachines.bottle_plan import (
|
||||
SmolmachinesBottlePlan,
|
||||
)
|
||||
from bot_bottle.backend.smolmachines import launch as _launch
|
||||
from bot_bottle.backend.smolmachines import port_forward as _forward
|
||||
from bot_bottle.backend.smolmachines.launch import _bundle_launch_spec
|
||||
from bot_bottle.backend.util import AGENT_CA_PATH
|
||||
from bot_bottle.egress import EgressPlan, EgressRoute
|
||||
@@ -510,6 +512,113 @@ class TestProxyHost(unittest.TestCase):
|
||||
self.assertEqual("127.0.0.16", result)
|
||||
|
||||
|
||||
class TestLaunchResourceWiring(unittest.TestCase):
|
||||
def test_allocate_resources_uses_loopback_alias_and_bundle_name(self):
|
||||
plan = _plan()
|
||||
with patch(
|
||||
"bot_bottle.backend.smolmachines.launch._loopback.ensure_pool",
|
||||
) as ensure_pool, patch(
|
||||
"bot_bottle.backend.smolmachines.launch._loopback.allocate",
|
||||
return_value="127.0.0.16",
|
||||
) as allocate:
|
||||
loopback_ip, network = _launch._allocate_resources(plan, ExitStack())
|
||||
|
||||
ensure_pool.assert_called_once_with()
|
||||
allocate.assert_called_once_with("demo-abc12")
|
||||
self.assertEqual("127.0.0.16", loopback_ip)
|
||||
self.assertEqual("bot-bottle-bundle-demo-abc12", network)
|
||||
|
||||
def test_start_bundle_wraps_raw_vm_ports_with_forwarder_ports(self):
|
||||
plan = _plan(supervise=True)
|
||||
plan = replace(
|
||||
plan,
|
||||
git_gate_plan=replace(
|
||||
plan.git_gate_plan,
|
||||
upstreams=(GitGateUpstream(
|
||||
name="bot-bottle",
|
||||
upstream_url="ssh://git@host/repo.git",
|
||||
upstream_host="host",
|
||||
upstream_port="22",
|
||||
identity_file="/tmp/key",
|
||||
known_host_key="",
|
||||
),),
|
||||
),
|
||||
)
|
||||
stack = MagicMock(spec=ExitStack)
|
||||
raw_launch = MagicMock(raw_ports={9099: 55001, 9420: 55002, 9100: 55003})
|
||||
handle = _forward.ForwarderHandle(
|
||||
process=MagicMock(),
|
||||
forwards=(
|
||||
_forward.ForwardSpec("egress", "127.0.0.16", 65001, "127.0.0.1", 55001),
|
||||
_forward.ForwardSpec("git-http", "127.0.0.16", 65002, "127.0.0.1", 55002),
|
||||
_forward.ForwardSpec("supervise", "127.0.0.16", 65003, "127.0.0.1", 55003),
|
||||
),
|
||||
)
|
||||
|
||||
with patch(
|
||||
"bot_bottle.backend.smolmachines.launch._provision_git_gate_keys",
|
||||
return_value=plan,
|
||||
), patch(
|
||||
"bot_bottle.backend.smolmachines.launch._ensure_smolmachine",
|
||||
return_value=Path("/cache/sidecar.smolmachine"),
|
||||
) as ensure, patch(
|
||||
"bot_bottle.backend.smolmachines.launch._bundle.start_bundle_vm",
|
||||
return_value=raw_launch,
|
||||
) as start_vm, patch(
|
||||
"bot_bottle.backend.smolmachines.launch._forward.start_forwarder",
|
||||
return_value=handle,
|
||||
) as start_forwarder:
|
||||
stamped = _launch._start_bundle(plan, "net", "127.0.0.16", stack)
|
||||
|
||||
ensure.assert_called_once()
|
||||
start_vm.assert_called_once()
|
||||
self.assertEqual(Path("/cache/sidecar.smolmachine"), start_vm.call_args.kwargs["from_path"])
|
||||
specs = start_forwarder.call_args.args[0]
|
||||
self.assertEqual(
|
||||
(
|
||||
_forward.ForwardSpec("egress", "127.0.0.16", 0, "127.0.0.1", 55001),
|
||||
_forward.ForwardSpec("git-http", "127.0.0.16", 0, "127.0.0.1", 55002),
|
||||
_forward.ForwardSpec("supervise", "127.0.0.16", 0, "127.0.0.1", 55003),
|
||||
),
|
||||
specs,
|
||||
)
|
||||
self.assertEqual("http://127.0.0.16:65001", stamped.agent_proxy_url)
|
||||
self.assertEqual("127.0.0.16:65002", stamped.agent_git_gate_host)
|
||||
self.assertEqual("http://127.0.0.16:65003/", stamped.agent_supervise_url)
|
||||
self.assertEqual(2, stack.callback.call_count)
|
||||
|
||||
def test_init_vm_repairs_ownership_then_waits_for_exec(self):
|
||||
plan = _plan()
|
||||
with patch(
|
||||
"bot_bottle.backend.smolmachines.launch._smolvm.machine_exec",
|
||||
) as machine_exec, patch(
|
||||
"bot_bottle.backend.smolmachines.launch._smolvm.wait_exec_ready",
|
||||
) as wait_exec_ready:
|
||||
_launch._init_vm(plan)
|
||||
|
||||
machine_exec.assert_called_once()
|
||||
argv = machine_exec.call_args.args[1]
|
||||
self.assertEqual(["sh", "-c"], argv[:2])
|
||||
self.assertIn("chown -R node:node /home/node", argv[2])
|
||||
wait_exec_ready.assert_called_once_with(plan.machine_name)
|
||||
|
||||
|
||||
class TestPortLabels(unittest.TestCase):
|
||||
def test_known_and_dynamic_port_labels_round_trip(self):
|
||||
for port, label in (
|
||||
(9099, "egress"),
|
||||
(9420, "git-http"),
|
||||
(9100, "supervise"),
|
||||
(12345, "port-12345"),
|
||||
):
|
||||
self.assertEqual(label, _launch._label_for_port(port))
|
||||
self.assertEqual(port, _launch._port_for_label(label))
|
||||
|
||||
def test_unknown_label_is_rejected(self):
|
||||
with self.assertRaises(ValueError):
|
||||
_launch._port_for_label("sidecar")
|
||||
|
||||
|
||||
class TestDiscoverUrls(unittest.TestCase):
|
||||
"""_discover_urls stamps git-gate host + supervise URL into the plan."""
|
||||
|
||||
|
||||
Reference in New Issue
Block a user