| ... | ... | @@ -1,4 +1,5 @@ |
| 1 | 1 | #!/usr/bin/env python3 |
| 2 | from contextlib import ExitStack |
| 2 | 3 | import json |
| 3 | 4 | import os |
| 4 | 5 | from pathlib import Path |
| ... | ... | @@ -75,5 +76,131 @@ class MainExport(unittest.TestCase): |
| 75 | 76 | deploy.upload(main=True) |
| 76 | 77 | |
| 77 | 78 | |
| 79 | class BuildSourceExport(unittest.TestCase): |
| 80 | def setUp(self): |
| 81 | stack = ExitStack() |
| 82 | self.addCleanup(stack.close) |
| 83 | root = Path(stack.enter_context(tempfile.TemporaryDirectory(prefix="studio-build-source-"))).resolve() |
| 84 | self.repo = root / "repo" |
| 85 | self.source = root / "source" |
| 86 | self.remote = root / "remote" |
| 87 | for repo in (self.repo, self.source): |
| 88 | subprocess.run(["jj", "git", "init", str(repo)], check=True, capture_output=True) |
| 89 | for name in release.SOURCES: |
| 90 | path = self.repo / name |
| 91 | if "." in name: |
| 92 | path.write_text("fixture") |
| 93 | else: |
| 94 | path.mkdir() |
| 95 | (path / "fixture").write_text("fixture") |
| 96 | (self.repo / "config/excluded-services.json").write_text("[]\n") |
| 97 | (self.source / "lib").mkdir() |
| 98 | (self.source / "lib/committed").write_text("committed library") |
| 99 | (self.source / "src").mkdir() |
| 100 | (self.source / "src/app.dockerfile").write_text("FROM committed\n") |
| 101 | (self.source / "run.sh").write_text("#!/bin/sh\necho committed\n") |
| 102 | (self.source / "run.sh").chmod(0o755) |
| 103 | subprocess.run(["jj", "describe", "-m", "Committed build source"], cwd=self.source, check=True, capture_output=True) |
| 104 | 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 |
| 105 | subprocess.run(["jj", "new"], cwd=self.source, check=True, capture_output=True) |
| 106 | (self.source / "run.sh").write_text("unfinished runner") |
| 107 | (self.source / "run.sh").chmod(0o644) |
| 108 | (self.source / "src/app.dockerfile").write_text("FROM unfinished\n") |
| 109 | (self.source / "lib/committed").unlink() |
| 110 | (self.source / "lib/new").write_text("uncommitted library") |
| 111 | self.manifest = self.repo / "service/app/build-source.json" |
| 112 | self.manifest.parent.mkdir() |
| 113 | self.spec = {"source": "../source", "revision": self.commit, |
| 114 | "dockerfile": "src/app.dockerfile", "include": ["run.sh", "lib"]} |
| 115 | self.manifest.write_text(json.dumps(self.spec)) |
| 116 | for args in [["describe", "-m", "Pinned build source"], ["bookmark", "set", "main"]]: |
| 117 | subprocess.run(["jj", *args], cwd=self.repo, check=True, capture_output=True) |
| 118 | original = subprocess.run |
| 119 | |
| 120 | def transport(argv, **kwargs): |
| 121 | if argv[0] == "ssh": |
| 122 | exists = argv[-1].startswith("test -d ") and Path(argv[-1][8:]).is_dir() |
| 123 | return subprocess.CompletedProcess(argv, 0 if exists else 1) |
| 124 | if argv[0] == "rsync" and "-e" in argv: |
| 125 | destination = Path(argv[-1].partition(":")[2]) |
| 126 | destination.mkdir(parents=True) |
| 127 | for name in argv[4:-1]: |
| 128 | source = Path(name) |
| 129 | if source.is_dir(): |
| 130 | shutil.copytree(source, destination / source.name) |
| 131 | else: |
| 132 | shutil.copy2(source, destination / source.name) |
| 133 | return subprocess.CompletedProcess(argv, 0) |
| 134 | return original(argv, **kwargs) |
| 135 | |
| 136 | stack.enter_context(patch.object(deploy, "REPO", self.repo)) |
| 137 | stack.enter_context(patch.object(deploy, "REMOTE", self.remote)) |
| 138 | stack.enter_context(patch.object(deploy, "ssh")) |
| 139 | stack.enter_context(patch.object(deploy.subprocess, "run", side_effect=transport)) |
| 140 | |
| 141 | def test_pinned_source_ignores_working_files_for_main_and_stage(self): |
| 142 | for main in (True, False): |
| 143 | with self.subTest(main=main): |
| 144 | context = self.remote / "releases" / deploy.upload(main=main) / "service/app/build" |
| 145 | self.assertEqual((context / "run.sh").read_text(), "#!/bin/sh\necho committed\n") |
| 146 | self.assertTrue(os.access(context / "run.sh", os.X_OK)) |
| 147 | self.assertEqual((context / "lib/committed").read_text(), "committed library") |
| 148 | self.assertFalse((context / "lib/new").exists()) |
| 149 | self.assertEqual((context / "Dockerfile").read_text(), "FROM committed\n") |
| 150 | self.assertFalse((context / "src/app.dockerfile").exists()) |
| 151 | |
| 152 | def test_unpinned_source_uses_working_files(self): |
| 153 | del self.spec["revision"] |
| 154 | self.manifest.write_text(json.dumps(self.spec)) |
| 155 | context = self.remote / "releases" / deploy.upload() / "service/app/build" |
| 156 | self.assertEqual((context / "run.sh").read_text(), "unfinished runner") |
| 157 | self.assertFalse(os.access(context / "run.sh", os.X_OK)) |
| 158 | self.assertFalse((context / "lib/committed").exists()) |
| 159 | self.assertEqual((context / "lib/new").read_text(), "uncommitted library") |
| 160 | self.assertEqual((context / "Dockerfile").read_text(), "FROM unfinished\n") |
| 161 | |
| 162 | def test_dockerfile_also_selected_by_include_is_retained(self): |
| 163 | self.manifest.write_text(json.dumps({**self.spec, "include": ["src"]})) |
| 164 | context = self.remote / "releases" / deploy.upload() / "service/app/build" |
| 165 | self.assertEqual((context / "src/app.dockerfile").read_text(), "FROM committed\n") |
| 166 | self.assertEqual((context / "Dockerfile").read_text(), "FROM committed\n") |
| 167 | |
| 168 | def test_invalid_revision_and_paths_are_rejected(self): |
| 169 | for revision in ("main", "f" * 39, "g" * 40, None): |
| 170 | with self.subTest(revision=revision): |
| 171 | self.manifest.write_text(json.dumps({**self.spec, "revision": revision})) |
| 172 | with self.assertRaisesRegex(ValueError, "invalid build source revision"): |
| 173 | deploy.upload() |
| 174 | for name in ("../outside", "/outside", "", "missing", "glob:*"): |
| 175 | with self.subTest(path=name): |
| 176 | self.manifest.write_text(json.dumps({**self.spec, "include": [name]})) |
| 177 | with self.assertRaisesRegex(ValueError, "build source path"): |
| 178 | deploy.upload() |
| 179 | self.manifest.write_text(json.dumps({**self.spec, "source": "../.."})) |
| 180 | with self.assertRaisesRegex(ValueError, "invalid build source"): |
| 181 | deploy.upload() |
| 182 | |
| 183 | def test_selected_symlink_is_rejected(self): |
| 184 | (self.source / "link").symlink_to("run.sh") |
| 185 | subprocess.run(["jj", "describe", "-m", "Source with symlink"], cwd=self.source, check=True, capture_output=True) |
| 186 | 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 |
| 187 | self.manifest.write_text(json.dumps({**self.spec, "revision": commit, "include": ["link"]})) |
| 188 | with self.assertRaisesRegex(ValueError, "unsupported revision file: link"): |
| 189 | deploy.upload() |
| 190 | |
| 191 | def test_selected_conflict_is_rejected(self): |
| 192 | commits = [] |
| 193 | for content in ("left", "right"): |
| 194 | subprocess.run(["jj", "new", self.commit], cwd=self.source, check=True, capture_output=True) |
| 195 | (self.source / "run.sh").write_text(content) |
| 196 | subprocess.run(["jj", "describe", "-m", content], cwd=self.source, check=True, capture_output=True) |
| 197 | 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) |
| 198 | subprocess.run(["jj", "new", *commits], cwd=self.source, check=True, capture_output=True) |
| 199 | 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 |
| 200 | self.manifest.write_text(json.dumps({**self.spec, "revision": commit})) |
| 201 | with self.assertRaisesRegex(ValueError, "unsupported revision file: run.sh"): |
| 202 | deploy.upload() |
| 203 | |
| 204 | |
| 78 | 205 | if __name__ == "__main__": |
| 79 | 206 | unittest.main() |