Files
computer-playbook/tests/unit/python/cli/administration/deploy/test_container.py
T
kevinveenbirkenbachandClaude Opus 5.5 c9e7f950d9 Merge branch 'feature/updater' into feature/store-i18n
68 conflicts, resolved per hunk; resolutions agreed with the Updater
session were applied as agreed:

- svc-db-postgres (superuser password script and core) and
  sys-svc-rdbms realign_dedicated_db: updater's argv/stdin redesign with
  expand_argument_vars: false, nested into store-i18n's block structure.
- svc-db-redis 01_init: store-i18n's ACL password on stdin.
- sys-svc-proxy forwarded.conf.j2: store-i18n's handover-aware file,
  without the forwarded_base include (Host and X-Forwarded-Proto would
  be sent twice); forwarded_base serves the inline consumers, e.g.
  web-app-seaweedfs nginx.conf.j2.
- basic.conf.j2 and web-app-openproject proxy.conf.j2: issuer redirect,
  then handover, else updater's location/sso_acl.conf.j2; the
  server-level webserver_extra_configuration hook stays.
- web-app-keycloak: updater's admin-REST settings step and renumbered
  update steps; update/01_client.yml removed; store-i18n's design step
  and LDAP block kept; a73435cc9d arrived through rename detection.
- web-app-nextcloud OpenProject provisioning: updater's rewrite, all
  credentials on the Ruby runner's stdin.
- web-app-semaphore: store-i18n's canonical web root (a1d7835863).
- Role READMEs: no generated Cosmos/Architecture/Quick Setup/Credits
  sections; desk-* paths resolved to the dsk-* renames.

Integration fixes for what merged cleanly but did not fit together:

- web-app-matrix ansible vars: drop updater's second
  matrix_client_element_configuration_extension_json, which silently
  overrode store-i18n's design-aware value.
- url.origin instead of a stripped url.base in 19 places store-i18n
  added since 44aaa68924, same lookup targets (the double-slash lint).
- Package batch (18ea75049e, cb4d340de9): installs the deploy-start
  batch covers are removed (caffeine, cursor, blender, npm in dsk-gnt-*,
  with shared references), the rest carry batch: false and a nocheck
  with the verified reason (PyPI fallback, before the batch, vendor
  repository, configured flavor only, addon gate); dev-npm references
  the root npm id.
- sys-token-store write_many.yml: store-i18n's change-gated block and
  IS_SWARM_MODE.
- web-app-openproject: IS_COMPOSE_MODE in 02_migrate/run.yml; the
  seeded-admin lock (ccbf8f168d) ported into 04_admin/provision.yml and
  covered by the admin-state digest.
- Bucket policy (3b4ac40df5 and follow-ups) moved to web-svc-seaweedfs,
  where store-i18n's object store lives; the S3 admin secret travels on
  stdin instead of a task-level environment.
- act workflow.sh: no Exception-labelled comment.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-29 02:28:16 +01:00

245 lines
7.7 KiB
Python

import os
import subprocess
import sys
import unittest
import unittest.mock
from cli.administration.deploy.container import command as deploy_container
class TestEnsureImage(unittest.TestCase):
@unittest.mock.patch("subprocess.run")
def test_ensure_image_skips_build_when_image_exists(self, mock_run):
"""
If the image already exists, ensure_image should only call
`docker image inspect` and NOT run `docker build`.
"""
mock_run.return_value = subprocess.CompletedProcess(
args=["docker", "image", "inspect", "infinito:latest"],
returncode=0,
stdout="",
stderr="",
)
deploy_container.ensure_image("infinito:latest")
self.assertEqual(mock_run.call_count, 1)
cmd = mock_run.call_args_list[0].args[0]
self.assertEqual(cmd[:3], ["docker", "image", "inspect"])
self.assertFalse(
any(
call.args[0][:2] == ["docker", "build"]
for call in mock_run.call_args_list
),
"docker build should not run when the image already exists",
)
@unittest.mock.patch("subprocess.run")
def test_ensure_image_builds_when_missing(self, mock_run):
"""
If the image does not exist, ensure_image should call
`docker image inspect` first and then `docker build`.
"""
calls: list[list[str]] = []
def _side_effect(cmd, *args, **kwargs):
calls.append(cmd)
if cmd[:3] == ["docker", "image", "inspect"]:
return subprocess.CompletedProcess(
args=cmd,
returncode=1,
stdout="",
stderr="missing",
)
if cmd[:2] == ["docker", "build"]:
return subprocess.CompletedProcess(
args=cmd,
returncode=0,
stdout="",
stderr="",
)
return subprocess.CompletedProcess(
args=cmd,
returncode=0,
stdout="",
stderr="",
)
mock_run.side_effect = _side_effect
deploy_container.ensure_image("infinito:latest")
self.assertTrue(
any(c[:3] == ["docker", "image", "inspect"] for c in calls),
"Expected 'docker image inspect' to be called",
)
self.assertTrue(
any(c[:2] == ["docker", "build"] for c in calls),
"Expected 'docker build' to run when image is missing",
)
class TestRunInContainer(unittest.TestCase):
@unittest.mock.patch("subprocess.run")
@unittest.mock.patch(
"cli.administration.deploy.container.command.docker_exec",
return_value=subprocess.CompletedProcess(args=[], returncode=0),
)
@unittest.mock.patch(
"cli.administration.deploy.container.command.start_ci_container",
return_value="infinito-ci-test",
)
def test_generated_inventory_keeps_the_placed_custom_roles(
self, _start, mock_docker_exec, _run
):
deploy_container.run_in_container(
image="infinito:latest",
build=False,
rebuild=False,
no_cache=False,
inventory_dir="/tmp/inventory",
inventory_args=[],
deploy_args=["--debug"],
)
deploy_cmd = mock_docker_exec.call_args_list[-1].args[1]
self.assertIn("cli.administration.deploy.dedicated", deploy_cmd)
self.assertIn("--skip-pull", deploy_cmd)
self.assertEqual("--debug", deploy_cmd[-1])
class TestMain(unittest.TestCase):
@unittest.mock.patch("cli.administration.deploy.container.command.run_in_container")
def test_main_requires_forwarded_args(self, mock_run_in_container):
"""
In 'run' mode, main() must return 1 and not call run_in_container()
if no deploy arguments are provided after the first '--'.
"""
argv = [
"cli.administration.deploy.container.command",
"run",
"--image",
"myimage:latest",
"--build",
"--inventory-dir",
"/tmp/inventory",
"--",
]
with unittest.mock.patch.object(sys, "argv", argv):
rc = deploy_container.main()
self.assertEqual(rc, 1)
mock_run_in_container.assert_not_called()
@unittest.mock.patch("cli.administration.deploy.container.command.run_in_container")
def test_main_passes_arguments_to_run_in_container(self, mock_run_in_container):
"""
Ensure that main() correctly splits container args vs inventory/deploy
args and passes them to run_in_container(). Also ensure inventory_dir
is forwarded.
"""
argv = [
"cli.administration.deploy.container",
"run",
"--image",
"myimage:latest",
"--build",
"--inventory-dir",
"/tmp/inventory",
"--",
"--exclude",
"foo,bar",
"--",
"--debug",
]
with unittest.mock.patch.object(sys, "argv", argv):
rc = deploy_container.main()
self.assertEqual(rc, 0)
mock_run_in_container.assert_called_once()
kwargs = mock_run_in_container.call_args.kwargs
self.assertEqual(kwargs["image"], "myimage:latest")
self.assertTrue(kwargs["build"])
self.assertFalse(kwargs["rebuild"])
self.assertFalse(kwargs["no_cache"])
self.assertIsNone(kwargs["name"])
self.assertEqual(kwargs["inventory_dir"], "/tmp/inventory")
self.assertEqual(
kwargs["inventory_args"],
["--exclude", "foo,bar"],
)
self.assertEqual(
kwargs["deploy_args"],
["--debug"],
)
@unittest.mock.patch("cli.administration.deploy.container.command.run_in_container")
def test_main_requires_inventory_dir_when_env_missing(self, mock_run_in_container):
"""
main() must fail if neither --inventory-dir is provided nor INFINITO_INVENTORY_DIR env is set.
"""
argv = [
"cli.administration.deploy.container",
"run",
"--image",
"myimage:latest",
"--build",
"--",
"--exclude",
"foo,bar",
]
with (
unittest.mock.patch.dict(os.environ, {}, clear=True),
unittest.mock.patch.object(sys, "argv", argv),
self.assertRaises(SystemExit),
):
deploy_container.main()
mock_run_in_container.assert_not_called()
@unittest.mock.patch("cli.administration.deploy.container.command.run_in_container")
def test_main_uses_inventory_dir_from_env_when_present(self, mock_run_in_container):
"""
If INFINITO_INVENTORY_DIR env is set, --inventory-dir should not be required and the env value
must be forwarded to run_in_container().
"""
argv = [
"cli.administration.deploy.container",
"run",
"--image",
"myimage:latest",
"--build",
"--",
"--exclude",
"foo,bar",
]
with (
unittest.mock.patch.dict(
os.environ, {"INFINITO_INVENTORY_DIR": "/env/inventory"}, clear=True
),
unittest.mock.patch.object(sys, "argv", argv),
):
rc = deploy_container.main()
self.assertEqual(rc, 0)
mock_run_in_container.assert_called_once()
kwargs = mock_run_in_container.call_args.kwargs
self.assertEqual(kwargs["inventory_dir"], "/env/inventory")
if __name__ == "__main__":
unittest.main()