| 1 | #!/usr/bin/env python3 |
| 2 | from contextlib import ExitStack |
| 3 | import json |
| 4 | import os |
| 5 | from pathlib import Path |
| 6 | import shutil |
| 7 | import subprocess |
| 8 | import tempfile |
| 9 | import unittest |
| 10 | from unittest.mock import patch |
| 11 | |
| 12 | import deploy |
| 13 | import release |
| 14 | |
| 15 | |
| 16 | class HostStage(unittest.TestCase): |
| 17 | def test_prepares_working_host_without_publishing_or_deploying_services(self): |
| 18 | with patch("sys.argv", ["deploy.py", "stage", "host"]), patch.object( |
| 19 | deploy, "upload", return_value="0123456789abcdef", |
| 20 | ) as upload, patch.object(deploy, "run_logged") as run: |
| 21 | deploy.main() |
| 22 | upload.assert_called_once_with() |
| 23 | run.assert_called_once_with("stage", "host", "python3 /opt/snowglobe/releases/0123456789abcdef/tools/release.py prepare-boot 0123456789abcdef") |
| 24 | |
| 25 | |
| 26 | class MainExport(unittest.TestCase): |
| 27 | def test_production_exports_main_while_staging_exports_working_files(self): |
| 28 | with tempfile.TemporaryDirectory(prefix="snowglobe-main-export-") as temporary: |
| 29 | root = Path(temporary) |
| 30 | repo = root / "repo" |
| 31 | repo.mkdir() |
| 32 | remote = root / "remote" |
| 33 | subprocess.run(["jj", "git", "init", str(repo)], check=True, capture_output=True) |
| 34 | for name in release.SOURCES: |
| 35 | path = repo / name |
| 36 | if "." in name: |
| 37 | path.write_text("fixture") |
| 38 | else: |
| 39 | path.mkdir() |
| 40 | (path / "fixture").write_text("committed") |
| 41 | (repo / "service/app").mkdir() |
| 42 | (repo / "service/app/service.pkl").write_text("service definition") |
| 43 | (repo / "service/app/worker.pkl").write_text("grouped definition") |
| 44 | executable = repo / "tools/fixture" |
| 45 | executable.chmod(0o755) |
| 46 | subprocess.run(["jj", "file", "track", "--include-ignored", "AGENTS.md"], cwd=repo, check=True, capture_output=True) |
| 47 | for args in [["describe", "-m", "Test main"], ["bookmark", "set", "main"]]: |
| 48 | subprocess.run(["jj", *args], cwd=repo, check=True, capture_output=True) |
| 49 | commit = subprocess.run(["jj", "--ignore-working-copy", "log", "-r", "main", "--no-graph", "-T", "commit_id"], cwd=repo, check=True, capture_output=True, text=True).stdout |
| 50 | subprocess.run(["jj", "new"], cwd=repo, check=True, capture_output=True) |
| 51 | (repo / "service/fixture").write_text("unfinished") |
| 52 | (repo / "service/new").write_text("new working file") |
| 53 | generated = repo / "guest/build/fixture" |
| 54 | generated.parent.mkdir(parents=True) |
| 55 | digest = release.tree_digest(repo) |
| 56 | generated.write_bytes(b"compiled fixture") |
| 57 | self.assertEqual(release.tree_digest(repo), digest) |
| 58 | original = subprocess.run |
| 59 | |
| 60 | def transport(argv, **kwargs): |
| 61 | if argv[0] == "ssh": |
| 62 | return subprocess.CompletedProcess(argv, 1) |
| 63 | if argv[0] == "rsync" and "-e" in argv: |
| 64 | destination = Path(argv[-1].partition(":")[2]) |
| 65 | destination.mkdir(parents=True) |
| 66 | for name in argv[4:-1]: |
| 67 | source = Path(name) |
| 68 | if source.is_dir(): |
| 69 | shutil.copytree(source, destination / source.name) |
| 70 | else: |
| 71 | shutil.copy2(source, destination / source.name) |
| 72 | return subprocess.CompletedProcess(argv, 0) |
| 73 | return original(argv, **kwargs) |
| 74 | |
| 75 | with patch.object(deploy, "REPO", repo), patch.object(deploy, "REMOTE", remote), patch.object(deploy, "ssh"), patch.object(deploy.subprocess, "run", side_effect=transport): |
| 76 | main = remote / "releases" / deploy.upload(main=True) |
| 77 | stage = remote / "releases" / deploy.upload() |
| 78 | self.assertEqual((main / "service/fixture").read_text(), "committed") |
| 79 | self.assertFalse((main / "service/new").exists()) |
| 80 | self.assertTrue(os.access(main / "tools/fixture", os.X_OK)) |
| 81 | self.assertEqual(json.loads((main / ".snowglobe-release.json").read_text())["main"], {"commit": commit, "description": "Test main\n"}) |
| 82 | self.assertEqual((stage / "service/fixture").read_text(), "unfinished") |
| 83 | self.assertEqual((stage / "service/new").read_text(), "new working file") |
| 84 | self.assertFalse((stage / generated.relative_to(repo)).exists()) |
| 85 | self.assertNotIn("main", json.loads((stage / ".snowglobe-release.json").read_text())) |
| 86 | for exported in (main, stage): |
| 87 | self.assertEqual((exported / "service/app/service.pkl").read_text(), "service definition") |
| 88 | self.assertEqual((exported / "service/app/worker.pkl").read_text(), "grouped definition") |
| 89 | subprocess.run(["jj", "bookmark", "set", "main"], cwd=repo, check=True, capture_output=True) |
| 90 | with self.assertRaisesRegex(ValueError, "commit description"): |
| 91 | deploy.upload(main=True) |
| 92 | |
| 93 | |
| 94 | class BuildSourceExport(unittest.TestCase): |
| 95 | def setUp(self): |
| 96 | stack = ExitStack() |
| 97 | self.addCleanup(stack.close) |
| 98 | root = Path(stack.enter_context(tempfile.TemporaryDirectory(prefix="snowglobe-build-source-"))).resolve() |
| 99 | self.repo = root / "repo" |
| 100 | self.source = root / "source" |
| 101 | self.remote = root / "remote" |
| 102 | for repo in (self.repo, self.source): |
| 103 | subprocess.run(["jj", "git", "init", str(repo)], check=True, capture_output=True) |
| 104 | for name in release.SOURCES: |
| 105 | path = self.repo / name |
| 106 | if "." in name: |
| 107 | path.write_text("fixture") |
| 108 | else: |
| 109 | path.mkdir() |
| 110 | (path / "fixture").write_text("fixture") |
| 111 | (self.source / "lib").mkdir() |
| 112 | (self.source / "lib/committed").write_text("committed library") |
| 113 | (self.source / "src").mkdir() |
| 114 | (self.source / "src/app.dockerfile").write_text("FROM committed\n") |
| 115 | (self.source / "run.sh").write_text("#!/bin/sh\necho committed\n") |
| 116 | (self.source / "run.sh").chmod(0o755) |
| 117 | subprocess.run(["jj", "describe", "-m", "Committed build source"], cwd=self.source, check=True, capture_output=True) |
| 118 | self.commit = subprocess.run(["jj", "--ignore-working-copy", "log", "-r", "@", "--no-graph", "-T", "commit_id"], cwd=self.source, check=True, capture_output=True, text=True).stdout |
| 119 | subprocess.run(["jj", "new"], cwd=self.source, check=True, capture_output=True) |
| 120 | (self.source / "run.sh").write_text("unfinished runner") |
| 121 | (self.source / "run.sh").chmod(0o644) |
| 122 | (self.source / "src/app.dockerfile").write_text("FROM unfinished\n") |
| 123 | (self.source / "lib/committed").unlink() |
| 124 | (self.source / "lib/new").write_text("uncommitted library") |
| 125 | self.manifest = self.repo / "service/app/build-source.json" |
| 126 | self.manifest.parent.mkdir() |
| 127 | self.spec = {"source": "../source", "revision": self.commit, |
| 128 | "dockerfile": "src/app.dockerfile", "include": ["run.sh", "lib"]} |
| 129 | self.manifest.write_text(json.dumps(self.spec)) |
| 130 | subprocess.run(["jj", "file", "track", "--include-ignored", "AGENTS.md"], cwd=self.repo, check=True, capture_output=True) |
| 131 | for args in [["describe", "-m", "Pinned build source"], ["bookmark", "set", "main"]]: |
| 132 | subprocess.run(["jj", *args], cwd=self.repo, check=True, capture_output=True) |
| 133 | original = subprocess.run |
| 134 | |
| 135 | def transport(argv, **kwargs): |
| 136 | if argv[0] == "ssh": |
| 137 | exists = argv[-1].startswith("test -d ") and Path(argv[-1][8:]).is_dir() |
| 138 | return subprocess.CompletedProcess(argv, 0 if exists else 1) |
| 139 | if argv[0] == "rsync" and "-e" in argv: |
| 140 | destination = Path(argv[-1].partition(":")[2]) |
| 141 | destination.mkdir(parents=True) |
| 142 | for name in argv[4:-1]: |
| 143 | source = Path(name) |
| 144 | if source.is_dir(): |
| 145 | shutil.copytree(source, destination / source.name) |
| 146 | else: |
| 147 | shutil.copy2(source, destination / source.name) |
| 148 | return subprocess.CompletedProcess(argv, 0) |
| 149 | return original(argv, **kwargs) |
| 150 | |
| 151 | stack.enter_context(patch.object(deploy, "REPO", self.repo)) |
| 152 | stack.enter_context(patch.object(deploy, "REMOTE", self.remote)) |
| 153 | stack.enter_context(patch.object(deploy, "ssh")) |
| 154 | stack.enter_context(patch.object(deploy.subprocess, "run", side_effect=transport)) |
| 155 | |
| 156 | def test_pinned_source_ignores_working_files_for_main_and_stage(self): |
| 157 | for main in (True, False): |
| 158 | with self.subTest(main=main): |
| 159 | context = self.remote / "releases" / deploy.upload(main=main) / "service/app/build" |
| 160 | self.assertEqual((context / "run.sh").read_text(), "#!/bin/sh\necho committed\n") |
| 161 | self.assertTrue(os.access(context / "run.sh", os.X_OK)) |
| 162 | self.assertEqual((context / "lib/committed").read_text(), "committed library") |
| 163 | self.assertFalse((context / "lib/new").exists()) |
| 164 | self.assertEqual((context / "Dockerfile").read_text(), "FROM committed\n") |
| 165 | self.assertFalse((context / "src/app.dockerfile").exists()) |
| 166 | |
| 167 | def test_composed_sources_export_pinned_files_under_distinct_destinations(self): |
| 168 | self.manifest.write_text(json.dumps({"sources": [self.spec, { |
| 169 | "source": "../source", "revision": self.commit, |
| 170 | "destination": "panel", "include": ["run.sh", "lib"], |
| 171 | }]})) |
| 172 | context = self.remote / "releases" / deploy.upload() / "service/app/build" |
| 173 | self.assertEqual((context / "Dockerfile").read_text(), "FROM committed\n") |
| 174 | self.assertEqual((context / "panel/lib/committed").read_text(), "committed library") |
| 175 | self.assertFalse((context / "panel/lib/new").exists()) |
| 176 | for target in ("../escape", "/absolute", "."): |
| 177 | self.manifest.write_text(json.dumps({"sources": [self.spec, { |
| 178 | "source": "../source", "revision": self.commit, |
| 179 | "destination": target, "include": ["run.sh"], |
| 180 | }]})) |
| 181 | with self.assertRaisesRegex(ValueError, "invalid build source destination"): |
| 182 | deploy.upload() |
| 183 | |
| 184 | def test_unpinned_source_uses_working_files(self): |
| 185 | del self.spec["revision"] |
| 186 | self.manifest.write_text(json.dumps(self.spec)) |
| 187 | context = self.remote / "releases" / deploy.upload() / "service/app/build" |
| 188 | self.assertEqual((context / "run.sh").read_text(), "unfinished runner") |
| 189 | self.assertFalse(os.access(context / "run.sh", os.X_OK)) |
| 190 | self.assertFalse((context / "lib/committed").exists()) |
| 191 | self.assertEqual((context / "lib/new").read_text(), "uncommitted library") |
| 192 | self.assertEqual((context / "Dockerfile").read_text(), "FROM unfinished\n") |
| 193 | |
| 194 | def test_local_dockerfile_uses_exported_main_or_working_stage(self): |
| 195 | dockerfile = self.manifest.parent / "Dockerfile" |
| 196 | dockerfile.write_text("FROM reviewed\n") |
| 197 | source = {key: value for key, value in self.spec.items() if key != "dockerfile"} |
| 198 | self.manifest.write_text(json.dumps({"dockerfile": "Dockerfile", "sources": [source]})) |
| 199 | subprocess.run(["jj", "describe", "-m", "Reviewed container recipe"], cwd=self.repo, check=True, capture_output=True) |
| 200 | subprocess.run(["jj", "bookmark", "set", "main"], cwd=self.repo, check=True, capture_output=True) |
| 201 | dockerfile.write_text("FROM unfinished\n") |
| 202 | for main, expected in ((True, "FROM reviewed\n"), (False, "FROM unfinished\n")): |
| 203 | context = self.remote / "releases" / deploy.upload(main=main) / "service/app/build" |
| 204 | self.assertEqual((context / "Dockerfile").read_text(), expected) |
| 205 | self.assertEqual((context / "run.sh").read_text(), "#!/bin/sh\necho committed\n") |
| 206 | for name in ("../outside", "/absolute", "."): |
| 207 | self.manifest.write_text(json.dumps({"dockerfile": name, "sources": [source]})) |
| 208 | with self.assertRaisesRegex(ValueError, "invalid build context Dockerfile"): |
| 209 | deploy.upload() |
| 210 | |
| 211 | def test_dockerfile_also_selected_by_include_is_retained(self): |
| 212 | self.manifest.write_text(json.dumps({**self.spec, "include": ["src"]})) |
| 213 | context = self.remote / "releases" / deploy.upload() / "service/app/build" |
| 214 | self.assertEqual((context / "src/app.dockerfile").read_text(), "FROM committed\n") |
| 215 | self.assertEqual((context / "Dockerfile").read_text(), "FROM committed\n") |
| 216 | |
| 217 | def test_invalid_revision_and_paths_are_rejected(self): |
| 218 | for revision in ("main", "f" * 39, "g" * 40, None): |
| 219 | with self.subTest(revision=revision): |
| 220 | self.manifest.write_text(json.dumps({**self.spec, "revision": revision})) |
| 221 | with self.assertRaisesRegex(ValueError, "invalid build source revision"): |
| 222 | deploy.upload() |
| 223 | for name in ("../outside", "/outside", "", "missing", "glob:*"): |
| 224 | with self.subTest(path=name): |
| 225 | self.manifest.write_text(json.dumps({**self.spec, "include": [name]})) |
| 226 | with self.assertRaisesRegex(ValueError, "build source path"): |
| 227 | deploy.upload() |
| 228 | self.manifest.write_text(json.dumps({**self.spec, "source": "../.."})) |
| 229 | with self.assertRaisesRegex(ValueError, "invalid build source"): |
| 230 | deploy.upload() |
| 231 | |
| 232 | def test_selected_symlink_is_rejected(self): |
| 233 | (self.source / "link").symlink_to("run.sh") |
| 234 | subprocess.run(["jj", "describe", "-m", "Source with symlink"], cwd=self.source, check=True, capture_output=True) |
| 235 | commit = subprocess.run(["jj", "--ignore-working-copy", "log", "-r", "@", "--no-graph", "-T", "commit_id"], cwd=self.source, check=True, capture_output=True, text=True).stdout |
| 236 | self.manifest.write_text(json.dumps({**self.spec, "revision": commit, "include": ["link"]})) |
| 237 | with self.assertRaisesRegex(ValueError, "unsupported revision file: link"): |
| 238 | deploy.upload() |
| 239 | |
| 240 | def test_selected_conflict_is_rejected(self): |
| 241 | commits = [] |
| 242 | for content in ("left", "right"): |
| 243 | subprocess.run(["jj", "new", self.commit], cwd=self.source, check=True, capture_output=True) |
| 244 | (self.source / "run.sh").write_text(content) |
| 245 | subprocess.run(["jj", "describe", "-m", content], cwd=self.source, check=True, capture_output=True) |
| 246 | commits.append(subprocess.run(["jj", "--ignore-working-copy", "log", "-r", "@", "--no-graph", "-T", "commit_id"], cwd=self.source, check=True, capture_output=True, text=True).stdout) |
| 247 | subprocess.run(["jj", "new", *commits], cwd=self.source, check=True, capture_output=True) |
| 248 | commit = subprocess.run(["jj", "--ignore-working-copy", "log", "-r", "@", "--no-graph", "-T", "commit_id"], cwd=self.source, check=True, capture_output=True, text=True).stdout |
| 249 | self.manifest.write_text(json.dumps({**self.spec, "revision": commit})) |
| 250 | with self.assertRaisesRegex(ValueError, "unsupported revision file: run.sh"): |
| 251 | deploy.upload() |
| 252 | |
| 253 | |
| 254 | if __name__ == "__main__": |
| 255 | unittest.main() |