fix: run Fil-A-Management from persistent Alpine bootstrap
This commit is contained in:
1 parent
1127e7ecf4
commit
c41f4295a4
15 files changed
+952
-379
No files matched your search
+94
-5
@@ -1,4 +1,6 @@
|
||||
import os
|
||||
import secrets
|
||||
import shutil
|
||||
import subprocess
|
||||
import tempfile
|
||||
import unittest
|
||||
@@ -8,6 +10,10 @@ from .support import ROOT, clean_environment, git_fixture
|
||||
|
||||
|
||||
class BootstrapTests(unittest.TestCase):
|
||||
@unittest.skipIf(
|
||||
os.geteuid() == 0,
|
||||
"Local standalone bootstrap runs as a non-root developer; use test_container for the real root-to-PUID bootstrap",
|
||||
)
|
||||
def test_real_git_start_tag_restart_and_commit_switch_preserve_database(self):
|
||||
start = ROOT / "start.sh"
|
||||
self.assertTrue(start.is_file(), "Git-basierter Start fehlt")
|
||||
@@ -21,13 +27,25 @@ class BootstrapTests(unittest.TestCase):
|
||||
check=True,
|
||||
capture_output=True,
|
||||
)
|
||||
persistent_app = directory / "persistent-app"
|
||||
persistent_app.mkdir()
|
||||
standalone_start = persistent_app / "start.sh"
|
||||
shutil.copy2(start, standalone_start)
|
||||
original_start = standalone_start.read_bytes()
|
||||
token = secrets.token_urlsafe(24)
|
||||
token_file = directory / "test-token"
|
||||
token_file.write_text(token)
|
||||
token_file.chmod(0o600)
|
||||
env = clean_environment() | {
|
||||
"GIT_REPO_URL": bundle.as_uri(),
|
||||
"GIT_REVISION": "mvp-test",
|
||||
"FAM_ALLOW_LOCAL_GIT": "1",
|
||||
"FAM_RUNTIME_DIR": str(directory / "runtime"),
|
||||
"FAM_RUNTIME_DIR": str(persistent_app / "runtime"),
|
||||
"FAM_DATA_DIR": str(directory / "data"),
|
||||
"FAM_GIT_HELPERS": str(ROOT / "docker"),
|
||||
"PUID": str(os.getuid() or 99),
|
||||
"PGID": str(os.getgid() or 100),
|
||||
"GITEA_TOKEN_FILE": str(token_file),
|
||||
"GITEA_USERNAME": "synthetic-gitea-user",
|
||||
"SEED_CATALOG": "1",
|
||||
"DJANGO_COOKIE_SECURE": "0",
|
||||
"FAM_TEST_PASSWORD": secrets.token_urlsafe(24),
|
||||
@@ -35,7 +53,7 @@ class BootstrapTests(unittest.TestCase):
|
||||
|
||||
def boot(code):
|
||||
result = subprocess.run(
|
||||
["sh", str(start), "python", "manage.py", "shell", "-c", code],
|
||||
["sh", str(standalone_start), "python", "manage.py", "shell", "-c", code],
|
||||
env=env,
|
||||
capture_output=True,
|
||||
text=True,
|
||||
@@ -48,11 +66,24 @@ class BootstrapTests(unittest.TestCase):
|
||||
boot(
|
||||
"import os; from django.contrib.auth import get_user_model; from filament.models import Variant,InventoryItem,OrderItem,Printer; from filament.services import add_quantity; U=get_user_model(); a=U.objects.create_user('synthetic-one',password=os.environ['FAM_TEST_PASSWORD']); b=U.objects.create_user('synthetic-two',password=os.environ['FAM_TEST_PASSWORD']); v=Variant.objects.filter(spool_type='spool').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); Printer.objects.create(owner=a,manufacturer='Test',model='Synthetic'); print('CREATED_SYNTHETIC_DATA')"
|
||||
)
|
||||
# A copied start.sh alone must provide the bootstrap helpers; no Dockerfile COPY.
|
||||
helper = persistent_app / "runtime/bootstrap/git-askpass"
|
||||
self.assertTrue(helper.is_file(), "Standalone-Start erzeugt Git-Helper nicht")
|
||||
username = subprocess.check_output(
|
||||
[str(helper), "Username for HTTPS:"], env=env, text=True
|
||||
).strip()
|
||||
value = subprocess.check_output(
|
||||
[str(helper), "Password for HTTPS:"], env=env, text=True
|
||||
).strip()
|
||||
self.assertEqual(username, "synthetic-gitea-user")
|
||||
self.assertEqual(value, token)
|
||||
secret = (directory / "data/secret.key").read_bytes()
|
||||
verify = "from filament.models import InventoryItem,OrderItem,Printer; from django.db.models import Sum; print('PERSISTENCE='+str((InventoryItem.objects.get().quantity,OrderItem.objects.aggregate(n=Sum('quantity'))['n'],Printer.objects.count())))"
|
||||
self.assertIn("PERSISTENCE=(3, 3, 1)", boot(verify))
|
||||
self.assertEqual(
|
||||
subprocess.check_output(["git", "rev-parse", "HEAD"], cwd=directory / "runtime/app")
|
||||
subprocess.check_output(
|
||||
["git", "rev-parse", "HEAD"], cwd=persistent_app / "runtime/app"
|
||||
)
|
||||
.decode()
|
||||
.strip(),
|
||||
first,
|
||||
@@ -60,7 +91,9 @@ class BootstrapTests(unittest.TestCase):
|
||||
env["GIT_REVISION"] = second
|
||||
self.assertIn("PERSISTENCE=(3, 3, 1)", boot(verify))
|
||||
self.assertEqual(
|
||||
subprocess.check_output(["git", "rev-parse", "HEAD"], cwd=directory / "runtime/app")
|
||||
subprocess.check_output(
|
||||
["git", "rev-parse", "HEAD"], cwd=persistent_app / "runtime/app"
|
||||
)
|
||||
.decode()
|
||||
.strip(),
|
||||
second,
|
||||
@@ -74,10 +107,66 @@ class BootstrapTests(unittest.TestCase):
|
||||
+ "')"
|
||||
)
|
||||
self.assertTrue((directory / "data/snapshot.sqlite3").is_file())
|
||||
self.assertEqual(standalone_start.read_bytes(), original_start)
|
||||
print(
|
||||
"Bootstrap: tag, repeated start, commit, branch, persistent data and backup verified."
|
||||
)
|
||||
|
||||
def test_invalid_runtime_ids_fail_before_touching_appdata(self):
|
||||
for field, value in (
|
||||
("PUID", "0"),
|
||||
("PGID", "0"),
|
||||
("PUID", "-1"),
|
||||
("PGID", "4294967295"),
|
||||
("PUID", "root"),
|
||||
):
|
||||
with (
|
||||
self.subTest(field=field, value=value),
|
||||
tempfile.TemporaryDirectory(prefix="fam-id-guard-") as directory,
|
||||
):
|
||||
directory = Path(directory)
|
||||
env = clean_environment() | {
|
||||
"PUID": str(os.getuid() or 99),
|
||||
"PGID": str(os.getgid() or 100),
|
||||
"GIT_REPO_URL": "https://example.invalid/repository.git",
|
||||
"FAM_RUNTIME_DIR": str(directory / "runtime"),
|
||||
"FAM_DATA_DIR": str(directory / "data"),
|
||||
field: value,
|
||||
}
|
||||
result = subprocess.run(
|
||||
["sh", str(ROOT / "start.sh")],
|
||||
env=env,
|
||||
capture_output=True,
|
||||
text=True,
|
||||
timeout=15,
|
||||
)
|
||||
self.assertNotEqual(result.returncode, 0)
|
||||
self.assertIn(field + " muss", result.stderr)
|
||||
self.assertFalse((directory / "data").exists())
|
||||
|
||||
def test_start_script_inside_checkout_is_rejected_before_cleanup(self):
|
||||
with tempfile.TemporaryDirectory(prefix="fam-start-guard-") as directory:
|
||||
directory = Path(directory)
|
||||
runtime = directory / "runtime"
|
||||
(runtime / "app").mkdir(parents=True)
|
||||
script = runtime / "app/start.sh"
|
||||
shutil.copy2(ROOT / "start.sh", script)
|
||||
before = script.read_bytes()
|
||||
env = clean_environment() | {
|
||||
"PUID": str(os.getuid() or 99),
|
||||
"PGID": str(os.getgid() or 100),
|
||||
"GIT_REPO_URL": "https://example.invalid/repository.git",
|
||||
"FAM_RUNTIME_DIR": str(runtime),
|
||||
"FAM_DATA_DIR": str(directory / "data"),
|
||||
}
|
||||
result = subprocess.run(
|
||||
["sh", str(script)], env=env, capture_output=True, text=True, timeout=15
|
||||
)
|
||||
self.assertNotEqual(result.returncode, 0)
|
||||
self.assertIn("persistente start.sh", result.stderr)
|
||||
self.assertEqual(script.read_bytes(), before)
|
||||
self.assertFalse((directory / "data").exists())
|
||||
|
||||
def test_credential_bearing_urls_fail_without_printing_credentials(self):
|
||||
self.assertTrue((ROOT / "start.sh").is_file())
|
||||
marker = secrets.token_hex(16)
|
||||
|
||||
Reference in new issue
Block a user