141 lines
6.2 KiB
Python
141 lines
6.2 KiB
Python
"""Opt-in REAL Docker acceptance. Only uniquely named synthetic resources are touched."""
|
|
|
|
import os
|
|
import subprocess
|
|
import tempfile
|
|
import time
|
|
import unittest
|
|
import uuid
|
|
from pathlib import Path
|
|
|
|
from .support import ROOT, git_fixture
|
|
|
|
|
|
@unittest.skipUnless(
|
|
os.environ.get("FAM_TEST_DOCKER") == "1",
|
|
"Real Docker check: set FAM_TEST_DOCKER=1 on an authorized development Docker host",
|
|
)
|
|
class ContainerPersistenceTests(unittest.TestCase):
|
|
def test_container_restart_and_revision_recreation_preserve_volume(self):
|
|
name = "fam-mvp-test-" + uuid.uuid4().hex[:12]
|
|
volume, image = name + "-data", name + ":local"
|
|
|
|
def docker(*args, timeout=120):
|
|
result = subprocess.run(
|
|
["docker", *args], cwd=ROOT, text=True, capture_output=True, timeout=timeout
|
|
)
|
|
if result.returncode:
|
|
self.fail(result.stdout + result.stderr)
|
|
return result.stdout.strip()
|
|
|
|
# Do not silently skip an explicitly requested check if the daemon is unavailable.
|
|
docker("info", "--format", "{{.ServerVersion}}")
|
|
with tempfile.TemporaryDirectory(prefix="fam-container-") as directory:
|
|
directory = Path(directory)
|
|
repo, first, second = git_fixture(directory)
|
|
bundle = directory / "repository.bundle"
|
|
subprocess.run(
|
|
["git", "bundle", "create", str(bundle), "--all"],
|
|
cwd=repo,
|
|
check=True,
|
|
capture_output=True,
|
|
)
|
|
docker("build", "--tag", image, ".", timeout=600)
|
|
try:
|
|
docker("volume", "create", volume)
|
|
|
|
def start(revision):
|
|
docker(
|
|
"run",
|
|
"--detach",
|
|
"--name",
|
|
name,
|
|
"--security-opt",
|
|
"no-new-privileges:true",
|
|
"--env",
|
|
"GIT_REPO_URL=file:///fixtures/repository.bundle",
|
|
"--env",
|
|
"FAM_ALLOW_LOCAL_GIT=1",
|
|
"--env",
|
|
"GIT_REVISION=" + revision,
|
|
"--env",
|
|
"SEED_CATALOG=1",
|
|
"--env",
|
|
"DJANGO_COOKIE_SECURE=0",
|
|
"--mount",
|
|
"type=volume,source=" + volume + ",target=/data",
|
|
"--mount",
|
|
"type=bind,source="
|
|
+ str(bundle)
|
|
+ ",target=/fixtures/repository.bundle,readonly",
|
|
image,
|
|
)
|
|
|
|
def ready():
|
|
for _ in range(150):
|
|
state = docker(
|
|
"inspect",
|
|
"--format",
|
|
"{{.State.Status}} {{if .State.Health}}{{.State.Health.Status}}{{end}}",
|
|
name,
|
|
)
|
|
if state == "running healthy":
|
|
return
|
|
if state.startswith("exited"):
|
|
break
|
|
time.sleep(2)
|
|
self.fail("Synthetic container not healthy: " + docker("logs", name))
|
|
|
|
def manage(code):
|
|
return docker(
|
|
"exec",
|
|
"--user",
|
|
"fam",
|
|
name,
|
|
"/opt/fam/venv/bin/python",
|
|
"/opt/fam/app/manage.py",
|
|
"shell",
|
|
"-c",
|
|
code,
|
|
)
|
|
|
|
start(first)
|
|
ready()
|
|
manage(
|
|
"import secrets; from django.contrib.auth import get_user_model; from filament.models import Variant,InventoryItem,OrderItem; from filament.services import add_quantity; U=get_user_model(); a=U.objects.create_user('synthetic-one',password=secrets.token_urlsafe(24)); b=U.objects.create_user('synthetic-two',password=secrets.token_urlsafe(24)); v=Variant.objects.first(); add_quantity(InventoryItem,a,v,2); add_quantity(InventoryItem,a,v,1); add_quantity(OrderItem,a,v,2); add_quantity(OrderItem,b,v,1)"
|
|
)
|
|
check = "from filament.models import InventoryItem,OrderItem; from django.db.models import Sum; assert InventoryItem.objects.get().quantity==3; assert OrderItem.objects.aggregate(n=Sum('quantity'))['n']==3; print('PERSISTENCE_OK')"
|
|
self.assertIn("PERSISTENCE_OK", manage(check))
|
|
docker("restart", name)
|
|
ready()
|
|
self.assertIn("PERSISTENCE_OK", manage(check))
|
|
docker("rm", "--force", name)
|
|
start(second)
|
|
ready()
|
|
self.assertIn("PERSISTENCE_OK", manage(check))
|
|
self.assertEqual(
|
|
docker(
|
|
"exec",
|
|
"--user",
|
|
"fam",
|
|
name,
|
|
"git",
|
|
"-C",
|
|
"/opt/fam/app",
|
|
"rev-parse",
|
|
"HEAD",
|
|
),
|
|
second,
|
|
)
|
|
manage(
|
|
"from pathlib import Path; rows=[p for p in Path('/proc').glob('[0-9]*') if any(a.endswith(b'/gunicorn') for a in (p/'cmdline').read_bytes().split(bytes([0])))]; assert rows; assert all('Uid:\t10001\t10001\t10001\t10001' in (p/'status').read_text() for p in rows); print('UNPRIVILEGED_SERVER_OK')"
|
|
)
|
|
print(
|
|
"REAL Docker: image build, two accounts, container restart, revision recreation and named-volume persistence verified."
|
|
)
|
|
finally:
|
|
# Exact, random test resources only. No compose down, daemon cleanup or existing services.
|
|
subprocess.run(["docker", "rm", "--force", name], capture_output=True)
|
|
subprocess.run(["docker", "volume", "rm", volume], capture_output=True)
|
|
subprocess.run(["docker", "image", "rm", image], capture_output=True)
|