PipelineManager 0.1: gemeinsame Docker-Werkzeuge und Strato-Deployment-Vertrag
PipelineManager Image / image (push) Successful in 2m44s

This commit is contained in:
Lars Stommer committed 2026-10-26 13:15:34 +01:00
commit a6e159d125
12 files changed
+807

No files matched your search

+213
View File
@@ -0,0 +1,213 @@
import copy
import hashlib
import io
import json
import os
from pathlib import Path
import subprocess
import sys
import tarfile
import tempfile
import unittest
from unittest.mock import patch
from manager.cli import PipelineError, deploy, digest, inspect_package, load_config, prepare, render_remote
class ManagerTests(unittest.TestCase):
def setUp(self):
self.temp = tempfile.TemporaryDirectory()
self.addCleanup(self.temp.cleanup)
self.root = Path(self.temp.name)
self.config = {
"schema": 1, "project": "static-demo", "manager": "0.1.0",
"build": {"prepare": [], "test": [[sys.executable, "-c", "pass"]], "finalize": [],
"exclude": ["node_modules"], "required": ["web/index.html"]},
"remote": {"public": "web", "shared": {}, "backup": [], "activate": []},
"targets": {"staging": {"path": "test.example.de", "url": "https://example.test", "enabled": True,
"branch": "develop"},
"production": {"path": "prod.example.de", "url": "https://www.example.test", "enabled": False,
"branch": "main"}},
}
self.revision = "a" * 40
def package(self, changes=None, extra=None):
archive = self.root / "release.tgz"
metadata = {"schema": 1, "project": "static-demo", "revision": self.revision, "tests": "passed"}
metadata.update(changes or {})
with tarfile.open(archive, "w:gz") as package:
for name, value in {"PIPELINE.json": json.dumps(metadata).encode(), "web/index.html": b"ok", **(extra or {})}.items():
member = tarfile.TarInfo(name)
member.size = len(value)
package.addfile(member, io.BytesIO(value))
self.checksum(archive)
return archive
def checksum(self, archive):
Path(str(archive) + ".sha256").write_text(digest(archive))
def repository(self):
project = self.root / "source"
project.mkdir()
subprocess.run(["git", "init", "-b", "develop", str(project)], check=True, capture_output=True)
(project / "web").mkdir()
(project / "web/index.html").write_text("Versioned page")
(project / ".env.example").write_text("Example only")
subprocess.run(["git", "-C", str(project), "add", "."], check=True, capture_output=True)
subprocess.run(["git", "-C", str(project), "-c", "user.name=Test", "-c", "user.email=test@example.test",
"commit", "-m", "Fixture"], check=True, capture_output=True)
return project
def test_static_application_builds_without_laravel_hooks(self):
project = self.repository()
archive = self.root / "prepared.tgz"
with patch.dict(os.environ, {"DEPLOY_SSH_KEY": "must-not-reach-build"}):
self.config["build"]["test"] = [[sys.executable, "-c", "import os; assert 'DEPLOY_SSH_KEY' not in os.environ"]]
revision = prepare(project, self.config, archive)
self.assertEqual(digest(archive), inspect_package(archive, self.config, revision))
with tarfile.open(archive) as package:
self.assertEqual(b"Versioned page", package.extractfile("web/index.html").read())
self.assertNotIn(".env.example", package.getnames())
def test_failed_test_prevents_package_creation(self):
project = self.repository()
archive = self.root / "failed.tgz"
self.config["build"]["test"] = [[sys.executable, "-c", "raise SystemExit(7)"]]
with self.assertRaises(subprocess.CalledProcessError):
prepare(project, self.config, archive)
self.assertFalse(archive.exists())
def test_prepare_uses_committed_source_not_uncommitted_edits(self):
project = self.repository()
(project / "web/index.html").write_text("Uncommitted edit")
archive = self.root / "committed.tgz"
prepare(project, self.config, archive)
with tarfile.open(archive) as package:
self.assertEqual(b"Versioned page", package.extractfile("web/index.html").read())
def test_modified_archive_fails_integrity_validation(self):
archive = self.package()
with archive.open("ab") as output:
output.write(b"altered")
with self.assertRaisesRegex(PipelineError, "Prüfsumme"):
inspect_package(archive, self.config, self.revision)
def test_another_project_or_commit_cannot_be_deployed(self):
for change in ({"project": "other"}, {"revision": "b" * 40}, {"tests": "failed"}):
with self.subTest(change=change):
archive = self.package(change)
with self.assertRaisesRegex(PipelineError, "Commit"):
inspect_package(archive, self.config, self.revision)
def test_archive_cannot_contain_secrets_or_traversal(self):
for name in (".env", "nested/.env.production", "../outside", "/absolute", ".git/config"):
with self.subTest(name=name):
archive = self.package(extra={name: b"fixture"})
with self.assertRaises(PipelineError):
inspect_package(archive, self.config, self.revision)
def test_archive_cannot_contain_symlinks(self):
archive = self.root / "link.tgz"
with tarfile.open(archive, "w:gz") as package:
member = tarfile.TarInfo("escape")
member.type = tarfile.SYMTYPE
member.linkname = "/outside"
package.addfile(member)
self.checksum(archive)
with self.assertRaisesRegex(PipelineError, "Links"):
inspect_package(archive, self.config, self.revision)
def test_application_required_files_are_enforced(self):
archive = self.package()
self.config["build"]["required"].append("missing.html")
with self.assertRaisesRegex(PipelineError, "Pflichtdateien"):
inspect_package(archive, self.config, self.revision)
def test_remote_hooks_are_application_supplied_and_arguments_remain_quoted(self):
self.config["remote"]["activate"] = [["@php", "artisan", "migrate", "--force"], ["printf", "$(secret)"]]
remote = render_remote(self.config)
self.assertIn('"$php" artisan migrate --force', remote)
self.assertIn("printf '$(secret)'", remote)
self.assertIn("public_path=web", remote)
def test_disabled_production_stops_before_any_git_or_remote_call(self):
with patch("manager.cli.git") as git_call:
with self.assertRaisesRegex(PipelineError, "gesperrt"):
deploy(self.root, self.config, "production", self.root / "missing", self.root)
git_call.assert_not_called()
def test_wrong_branch_stops_before_remote_access(self):
with patch.dict(os.environ, {"GITHUB_REF": "refs/heads/main"}):
with self.assertRaisesRegex(PipelineError, "Branch"):
deploy(self.root, self.config, "staging", self.root / "missing", self.root)
def test_stale_queued_job_cannot_replace_a_newer_version(self):
with patch.dict(os.environ, {"GITHUB_REF": "refs/heads/develop", "GITHUB_SHA": self.revision}):
with patch("manager.cli.git", side_effect=[self.revision, "b" * 40 + "\trefs/heads/develop"]):
with self.assertRaisesRegex(PipelineError, "Veralteter"):
deploy(self.root, self.config, "staging", self.root / "missing", self.root)
def run_deployment(self, failure=None, healthy=True):
archive = self.package()
events = []
class FakeTransport:
def __init__(self, *arguments):
pass
def call(self, command, *arguments, stdout=None):
events.append(command)
if command == failure:
raise PipelineError("Fixture failure")
if command == "backup":
with tarfile.open(fileobj=stdout, mode="w:gz") as saved:
item = tarfile.TarInfo("shared/.env")
item.size = 7
saved.addfile(item, io.BytesIO(b"fixture"))
def upload(self, *arguments):
events.append("upload")
self.events = events
with patch.dict(os.environ, {"GITHUB_REF": "refs/heads/develop", "GITHUB_SHA": self.revision,
"DEPLOY_SSH_KEY": "fixture", "DEPLOY_KNOWN_HOSTS": "fixture"}):
with patch("manager.cli.git", side_effect=[self.revision, self.revision + "\trefs/heads/develop"]):
with patch("manager.cli.health", return_value=healthy):
deploy(self.root, self.config, "staging", archive, self.root, FakeTransport)
return events
def test_backup_is_complete_before_upload_and_activation(self):
self.assertEqual(["lock", "backup", "cleanup", "upload", "receive", "activate", "cleanup", "unlock"], self.run_deployment())
backups = list((self.root / "static-demo/staging").glob("*.tar.gz"))
self.assertEqual(1, len(backups))
with tarfile.open(backups[0]) as backup:
self.assertIn("shared/.env", backup.getnames())
def test_backup_failure_prevents_upload_and_releases_lock(self):
with self.assertRaisesRegex(PipelineError, "Fixture"):
self.run_deployment(failure="backup")
self.assertEqual(["lock", "backup", "unlock"], self.events)
def test_existing_deployment_lock_is_never_removed_by_a_second_job(self):
with self.assertRaisesRegex(PipelineError, "Fixture"):
self.run_deployment(failure="lock")
self.assertEqual(["lock"], self.events)
def test_failed_health_check_rolls_back_before_releasing_lock(self):
with self.assertRaisesRegex(PipelineError, "Gesundheitscheck"):
self.run_deployment(healthy=False)
self.assertEqual(["rollback", "unlock"], self.events[-2:])
def test_configuration_rejects_unsafe_targets_and_incompatible_versions(self):
path = self.root / "pipeline.toml"
for directory in ("../outside", "/absolute", "x;command", "a//b"):
path.write_text(f'schema=1\nproject="demo"\nmanager="0.1.0"\n[build]\ntest=[["true"]]\n[targets.staging]\npath="{directory}"\nurl="https://example.test"\nenabled=true\n')
with self.subTest(directory=directory), self.assertRaises(PipelineError):
load_config(path)
path.write_text('schema=1\nproject="demo"\nmanager="9.0.0"\n')
with self.assertRaisesRegex(PipelineError, "Manager-Version"):
load_config(path)
if __name__ == "__main__":
unittest.main()