Upload files to "scripts"
This commit is contained in:
1 parent
54082d8c6e
commit
dd346932d4
1 file changed
+92
@@ -0,0 +1,92 @@
|
|||||||
|
#!/usr/bin/env python3
|
||||||
|
"""Validate Compose offline with synthetic settings; never start the stack."""
|
||||||
|
import json
|
||||||
|
import os
|
||||||
|
from pathlib import Path
|
||||||
|
import subprocess
|
||||||
|
import tempfile
|
||||||
|
import unittest
|
||||||
|
|
||||||
|
ROOT = Path(__file__).resolve().parents[1]
|
||||||
|
COMPOSE = os.environ.get("COMPOSE_BINARY", "docker-compose")
|
||||||
|
|
||||||
|
|
||||||
|
class ComposeTests(unittest.TestCase):
|
||||||
|
@classmethod
|
||||||
|
def setUpClass(cls):
|
||||||
|
cls.temp = tempfile.TemporaryDirectory()
|
||||||
|
cls.addClassCleanup(cls.temp.cleanup)
|
||||||
|
cls.directory = Path(cls.temp.name)
|
||||||
|
# Copy only the configuration, never the committed .env or host settings.
|
||||||
|
(cls.directory / "compose.yaml").write_bytes((ROOT / "compose.yaml").read_bytes())
|
||||||
|
(cls.directory / "empty.env").write_text("")
|
||||||
|
|
||||||
|
def render(self, overrides=None, missing=None):
|
||||||
|
env = {"PATH": os.defpath, "HOME": str(self.directory),
|
||||||
|
"COMPOSE_DISABLE_ENV_FILE": "1",
|
||||||
|
"DOCKER_HOST": "unix:///nonexistent-ci-docker.sock",
|
||||||
|
"NPM_DB_PASSWORD": "ci-only-not-a-secret",
|
||||||
|
"NPM_DB_ROOT_PASSWORD": "ci-only-root-not-a-secret"}
|
||||||
|
env.update(overrides or {})
|
||||||
|
if missing:
|
||||||
|
env.pop(missing)
|
||||||
|
result = subprocess.run(
|
||||||
|
[COMPOSE, "--project-directory", str(self.directory),
|
||||||
|
"--env-file", str(self.directory / "empty.env"),
|
||||||
|
"-f", str(self.directory / "compose.yaml"), "config", "--format", "json"],
|
||||||
|
env=env, capture_output=True, text=True, timeout=30)
|
||||||
|
if missing:
|
||||||
|
self.assertNotEqual(result.returncode, 0)
|
||||||
|
self.assertIn(missing, result.stderr)
|
||||||
|
return
|
||||||
|
self.assertEqual(result.returncode, 0, result.stderr)
|
||||||
|
return json.loads(result.stdout)
|
||||||
|
|
||||||
|
def test_default_stack_and_persistence(self):
|
||||||
|
model = self.render()
|
||||||
|
services = model["services"]
|
||||||
|
self.assertEqual(set(services), {"wireguard-ui", "gitea", "nginx-proxy-manager", "npm-db"})
|
||||||
|
for name, service in services.items():
|
||||||
|
with self.subTest(service=name):
|
||||||
|
self.assertTrue(service["image"])
|
||||||
|
self.assertIn("apps", service["networks"])
|
||||||
|
self.assertTrue(any(v["type"] == "volume" for v in service["volumes"]))
|
||||||
|
self.assertNotIn("ports", services["npm-db"])
|
||||||
|
self.assertEqual(services["nginx-proxy-manager"]["depends_on"]["npm-db"]["condition"], "service_healthy")
|
||||||
|
self.assertIn("--innodb_initialized", services["npm-db"]["healthcheck"]["test"])
|
||||||
|
|
||||||
|
def test_wireguard_security_and_ports(self):
|
||||||
|
service = self.render()["services"]["wireguard-ui"]
|
||||||
|
ui = next(p for p in service["ports"] if p["target"] == 51821)
|
||||||
|
vpn = next(p for p in service["ports"] if p["target"] == 51820)
|
||||||
|
self.assertEqual(ui["host_ip"], "127.0.0.1")
|
||||||
|
self.assertEqual(vpn["protocol"], "udp")
|
||||||
|
self.assertEqual(service["environment"]["INSECURE"], "false")
|
||||||
|
self.assertIn("@sha256:", service["image"])
|
||||||
|
|
||||||
|
def test_custom_settings_and_shared_database_credentials(self):
|
||||||
|
services = self.render({"WG_PORT": "51830", "WG_UI_PORT": "51831",
|
||||||
|
"GITEA_HTTP_PORT": "3100", "GITEA_SSH_PORT": "2223",
|
||||||
|
"GITEA_DOMAIN": "git.ci.invalid",
|
||||||
|
"GITEA_ROOT_URL": "https://git.ci.invalid/",
|
||||||
|
"NPM_DB_NAME": "ci_db", "NPM_DB_USER": "ci_user"})["services"]
|
||||||
|
for name, target, expected in [("wireguard-ui", 51820, "51830"),
|
||||||
|
("wireguard-ui", 51821, "51831"),
|
||||||
|
("gitea", 3000, "3100"), ("gitea", 22, "2223")]:
|
||||||
|
self.assertEqual(next(p["published"] for p in services[name]["ports"] if p["target"] == target), expected)
|
||||||
|
self.assertEqual(services["gitea"]["environment"]["GITEA__server__ROOT_URL"], "https://git.ci.invalid/")
|
||||||
|
proxy = services["nginx-proxy-manager"]["environment"]
|
||||||
|
database = services["npm-db"]["environment"]
|
||||||
|
for suffix in ("NAME", "USER", "PASSWORD"):
|
||||||
|
db_key = "DATABASE" if suffix == "NAME" else suffix
|
||||||
|
self.assertEqual(proxy["DB_MYSQL_" + suffix], database["MARIADB_" + db_key])
|
||||||
|
|
||||||
|
def test_database_password_is_required(self):
|
||||||
|
self.render(missing="NPM_DB_PASSWORD")
|
||||||
|
|
||||||
|
def test_root_password_is_required(self):
|
||||||
|
self.render(missing="NPM_DB_ROOT_PASSWORD")
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
unittest.main(verbosity=2)
|
||||||
Reference in new issue
Block a user