Files
computer-playbook/tests/unit/cli/deploy/development/test_exec.py
T
kevinveenbirkenbachandClaude Opus 4.7 3e31630d58 fix(ci/deploy)+refactor(cli/deploy/development): single-SPOT INFINITO_DISTRO/INFINITO_CONTAINER
Two related improvements anchored on the same principle: the env var
INFINITO_DISTRO is the single source of truth for the development distro,
and INFINITO_CONTAINER is purely derived from it.

1. Fix the matrix-deploy purge failure (CI run 24942172018, job 73038526513,
   web-app-wordpress @ fedora):
   scripts/meta/env/defaults.sh eager-exported INFINITO_CONTAINER once
   inside its load-once guard; ci/all.sh's iteration loop re-exported
   INFINITO_DISTRO per round but the cached INFINITO_CONTAINER stayed at
   the initial-default infinito_nexus_debian, so _purge_app_entities()
   between rounds tried to docker exec into a container from the wrong
   distro and failed with "No such container".

   Move the INFINITO_CONTAINER derivation OUTSIDE the load-once guard so
   every re-source picks up a CONTAINER name in lock-step with the current
   INFINITO_DISTRO. ci/all.sh now re-sources defaults.sh after each
   export INFINITO_DISTRO=\$distro to trigger that re-derivation. defaults.sh
   stays the single spot that owns the formula.

2. Retire the --distro CLI arg from cli.deploy.development {up,down,stop,
   restart,init,deploy,exec,logs,build}. The arg duplicated INFINITO_DISTRO
   (its default already read from the env), creating a hidden second source
   of truth that drifted from the env across matrix iterations. Replace it
   with cli.deploy.development.common.resolve_distro() which reads
   INFINITO_DISTRO strictly (raises SystemExit on unset/invalid), and
   simplify make_compose() to take no args. All bash callers
   (scripts/tests/deploy/{ci,local}/**, Makefile restart target) drop the
   redundant --distro \"\${INFINITO_DISTRO}\" pass-through.

Verified: make test passes (1553 unit + 168 integration). The mechanism of
the WP fix is verified by a synthetic distro-cycling test of defaults.sh
re-derivation; the original CI multi-distro repro can only be confirmed
end-to-end by the next CI push (local single-distro deploys do not exercise
the stale-env path).

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
2026-04-26 12:21:34 +02:00

154 lines
5.1 KiB
Python

from __future__ import annotations
import argparse
import os
import unittest
import unittest.mock
from cli.deploy.development import exec as exec_cmd
def _args(
*,
distro: str = "debian",
cmd: list[str] | None = None,
env: list[str] | None = None,
) -> argparse.Namespace:
return argparse.Namespace(
distro=distro,
cmd=cmd if cmd is not None else [],
env=env if env is not None else [],
)
class TestDevelopmentExec(unittest.TestCase):
def test_handler_forwards_services_disabled(self):
compose = unittest.mock.Mock()
compose.exec.return_value.returncode = 0
with unittest.mock.patch(
"cli.deploy.development.exec.make_compose",
return_value=compose,
):
with unittest.mock.patch.dict(
os.environ, {"SERVICES_DISABLED": "email"}, clear=False
):
rc = exec_cmd.handler(_args(cmd=["--", "sh", "-lc", "true"]))
self.assertEqual(rc, 0)
compose.exec.assert_called_once_with(
["sh", "-lc", "true"],
check=False,
extra_env={"SERVICES_DISABLED": "email"},
)
def test_handler_omits_extra_env_when_services_disabled_unset(self):
compose = unittest.mock.Mock()
compose.exec.return_value.returncode = 0
env = {k: v for k, v in os.environ.items() if k != "SERVICES_DISABLED"}
with unittest.mock.patch(
"cli.deploy.development.exec.make_compose",
return_value=compose,
):
with unittest.mock.patch.dict(os.environ, env, clear=True):
rc = exec_cmd.handler(_args(cmd=["echo", "hi"]))
self.assertEqual(rc, 0)
compose.exec.assert_called_once_with(
["echo", "hi"], check=False, extra_env=None
)
def test_handler_requires_command(self):
with self.assertRaises(SystemExit):
exec_cmd.handler(_args(cmd=[]))
def test_env_pairs_are_injected_into_extra_env(self):
compose = unittest.mock.Mock()
compose.exec.return_value.returncode = 0
env = {k: v for k, v in os.environ.items() if k != "SERVICES_DISABLED"}
with unittest.mock.patch(
"cli.deploy.development.exec.make_compose",
return_value=compose,
):
with unittest.mock.patch.dict(os.environ, env, clear=True):
rc = exec_cmd.handler(
_args(
cmd=["bash", "/opt/helper.sh"],
env=["INVENTORY_FILE=/srv/inv/devices.yml", "APPS=web-app-foo"],
)
)
self.assertEqual(rc, 0)
compose.exec.assert_called_once_with(
["bash", "/opt/helper.sh"],
check=False,
extra_env={
"INVENTORY_FILE": "/srv/inv/devices.yml",
"APPS": "web-app-foo",
},
)
def test_env_pair_with_equals_in_value_is_preserved(self):
compose = unittest.mock.Mock()
compose.exec.return_value.returncode = 0
env = {k: v for k, v in os.environ.items() if k != "SERVICES_DISABLED"}
with unittest.mock.patch(
"cli.deploy.development.exec.make_compose",
return_value=compose,
):
with unittest.mock.patch.dict(os.environ, env, clear=True):
exec_cmd.handler(_args(cmd=["true"], env=["EXTRA_VARS=a=1 b=2"]))
# Only the first '=' splits key from value; everything after stays intact.
compose.exec.assert_called_once_with(
["true"],
check=False,
extra_env={"EXTRA_VARS": "a=1 b=2"},
)
def test_env_pair_overrides_implicit_services_disabled(self):
compose = unittest.mock.Mock()
compose.exec.return_value.returncode = 0
with unittest.mock.patch(
"cli.deploy.development.exec.make_compose",
return_value=compose,
):
with unittest.mock.patch.dict(
os.environ, {"SERVICES_DISABLED": "from-env"}, clear=False
):
exec_cmd.handler(
_args(cmd=["true"], env=["SERVICES_DISABLED=from-cli"])
)
compose.exec.assert_called_once_with(
["true"],
check=False,
extra_env={"SERVICES_DISABLED": "from-cli"},
)
def test_env_pair_without_equals_is_rejected(self):
compose = unittest.mock.Mock()
with unittest.mock.patch(
"cli.deploy.development.exec.make_compose",
return_value=compose,
):
with self.assertRaisesRegex(SystemExit, "expects KEY=VALUE"):
exec_cmd.handler(_args(cmd=["true"], env=["BARE"]))
def test_env_pair_with_empty_key_is_rejected(self):
compose = unittest.mock.Mock()
with unittest.mock.patch(
"cli.deploy.development.exec.make_compose",
return_value=compose,
):
with self.assertRaisesRegex(SystemExit, "KEY is empty"):
exec_cmd.handler(_args(cmd=["true"], env=["=value"]))
if __name__ == "__main__":
unittest.main()