oficina / equipo Public

Clone
git clone https://amadan.net/oficina/equipo

Plain git — no account needed to clone.

Download

Download this file

1package release
2
3import (
4 "crypto/sha256"
5 "encoding/hex"
6 "encoding/json"
7 "fmt"
8 "os"
9 "os/exec"
10 "path/filepath"
11 "runtime"
12 "strings"
13 "testing"
14
15 "amadan.net/oficina/equipo/internal/release/renderertest"
16 "amadan.net/oficina/equipo/internal/testci"
17)
18
19// publishRig is a release kit, the repository it was built from (with a
20// bare origin), and stub curl, amadan and go: the forge is a directory of
21// published assets by tag, amadan release create copies into it and logs,
22// and go version -m reports the tag linked into the kit's runner.
23type publishRig struct {
24 t *testing.T
25 repo, kit, forge, log, commit string
26 archive, sources, tag, pinJSON string
27 origin string
28 env []string
29 linked rigAsset
30}
31
32// rigAsset is what the rig's kit runner says is linked into it; none
33// makes it say nothing and exit 1.
34type rigAsset struct {
35 tag, pin, sum, size string
36 none bool
37}
38
39// writeRunner writes the kit's stub runner for r.linked.
40func (r *publishRig) writeRunner() {
41 exit := ""
42 if r.linked.none {
43 exit = "exit 1\n"
44 }
45 body := fmt.Sprintf("#!/bin/sh\n[ \"$1 $2\" = \"renderer-asset --json\" ] || exit 2\n%sprintf '{\"tag\":\"%s\",\"pin\":\"%s\",\"sha256\":\"%s\",\"size\":%s}\\n'\n", exit, r.linked.tag, r.linked.pin, r.linked.sum, r.linked.size)
46 os.WriteFile(filepath.Join(r.kit, "equipo-runner"), []byte(body), 0o755)
47}
48
49// linking rewrites the kit's runner with edit applied to what it says.
50func linking(edit func(*rigAsset)) func(*publishRig) []string {
51 return func(r *publishRig) []string {
52 edit(&r.linked)
53 r.writeRunner()
54 return nil
55 }
56}
57
58func newPublishRig(t *testing.T) *publishRig {
59 t.Helper()
60 testci.Files(t, "../../scripts/publish-renderer.sh", "../../scripts/renderer-sources.py")
61 testci.Tools(t, "git", "python3", "sh", "bwrap")
62 r := &publishRig{t: t, repo: t.TempDir(), kit: t.TempDir(), forge: t.TempDir(), log: filepath.Join(t.TempDir(), "log")}
63 run := func(dir string, args ...string) string {
64 t.Helper()
65 cmd := exec.Command(args[0], args[1:]...)
66 cmd.Dir = dir
67 cmd.Env = append(os.Environ(), "GIT_AUTHOR_NAME=t", "GIT_AUTHOR_EMAIL=t@example.com", "GIT_COMMITTER_NAME=t", "GIT_COMMITTER_EMAIL=t@example.com", "GIT_CONFIG_GLOBAL=/dev/null")
68 out, err := cmd.CombinedOutput()
69 if err != nil {
70 t.Fatalf("%v: %v\n%s", args, err, out)
71 }
72 return strings.TrimSpace(string(out))
73 }
74 origin := t.TempDir()
75 run(origin, "git", "init", "-q", "--bare")
76 run(r.repo, "git", "init", "-q", "-b", "main")
77 r.pinJSON = `{"version":"1.0-1","amd64":[{"package":"chromium-headless-shell","version":"1.0-1","architecture":"amd64","url":"https://snapshot.debian.org/archive/debian/20990101T000000Z/pool/main/c/chromium/chromium-headless-shell_1.0-1_amd64.deb","sha256":"` + strings.Repeat("a", 64) + `","size":1}]}`
78 os.MkdirAll(filepath.Join(r.repo, "internal", "release"), 0o755)
79 os.WriteFile(filepath.Join(r.repo, "internal", "release", "renderer.json"), []byte(r.pinJSON), 0o644)
80 run(r.repo, "git", "add", "-A")
81 run(r.repo, "git", "commit", "-q", "-m", "pin")
82 run(r.repo, "git", "remote", "add", "origin", origin)
83 r.commit = run(r.repo, "git", "rev-parse", "HEAD")
84 // A later commit: the kit is not the current checkout.
85 run(r.repo, "git", "commit", "-q", "--allow-empty", "-m", "later")
86
87 archive, pin := renderertest.Archive(renderertest.Small())
88 r.archive = string(archive)
89 r.sources = "The page renderer: Chromium headless shell 1.0-1, from Debian.\nChromium source: https://snapshot.debian.org/package/chromium/1.0-1/\n\n" +
90 "chromium-headless-shell 1.0-1 amd64 https://snapshot.debian.org/archive/debian/20990101T000000Z/pool/main/c/chromium/chromium-headless-shell_1.0-1_amd64.deb https://snapshot.debian.org/package/chromium/1.0-1/\n"
91 sum := sha256.Sum256([]byte(r.archive))
92 r.tag = "renderer-" + hex.EncodeToString(sum[:])[:12]
93 archiveSum := hex.EncodeToString(sum[:])
94 manifest := fmt.Sprintf(`{"targets":{"linux/amd64":{"renderer_sha256":%q,"renderer_archive_sha256":%q,"renderer_archive_size":%d}}}`, pin, archiveSum, len(archive))
95 for name, body := range map[string]string{"renderer.tar.gz": r.archive, "SOURCES": r.sources, "install-manifest.json": manifest, "COMMIT": r.commit + "\n"} {
96 os.WriteFile(filepath.Join(r.kit, name), []byte(body), 0o644)
97 }
98 bin := t.TempDir()
99 stub := func(name, body string) { os.WriteFile(filepath.Join(bin, name), []byte("#!/bin/sh\n"+body), 0o755) }
100 // curl: the last argument is the URL; --output names where to write;
101 // prints the status as --write-out asks.
102 stub("curl", `out=; for a; do [ "$prev" = --output ] && out=$a; prev=$a; url=$a; done
103echo "curl $*" >> `+r.log+`
104path=`+r.forge+`/${url#https://amadan.net/oficina/equipo/-/releases/}
105if [ -f "$path" ]; then cp "$path" "$out"; printf 200; else : > "$out"; printf 404; fi
106`)
107 // amadan release create -repo R -notes-file N TAG FILE...
108 stub("amadan", `echo "amadan $*" >> `+r.log+`
109tag=$7; shift 7; mkdir -p `+r.forge+`/$tag; for f; do cp "$f" `+r.forge+`/$tag/; done
110`)
111 // The kit's runner answers renderer-asset --json with what is linked
112 // into it (the script runs it sandboxed with an empty environment, so
113 // each case writes its own).
114 r.linked = rigAsset{tag: r.tag, pin: pin, sum: archiveSum, size: fmt.Sprint(len(archive))}
115 r.writeRunner()
116 r.env = append(os.Environ(), "PATH="+bin+string(os.PathListSeparator)+os.Getenv("PATH"), "GIT_CONFIG_GLOBAL=/dev/null", "GIT_AUTHOR_NAME=t", "GIT_AUTHOR_EMAIL=t@example.com", "GIT_COMMITTER_NAME=t", "GIT_COMMITTER_EMAIL=t@example.com")
117 r.origin = origin
118 return r
119}
120
121func (r *publishRig) run(args ...string) (string, error) {
122 script, _ := filepath.Abs("../../scripts/publish-renderer.sh")
123 cmd := exec.Command("bash", append([]string{script}, args...)...)
124 cmd.Dir = r.repo
125 cmd.Env = r.env
126 out, err := cmd.CombinedOutput()
127 return string(out), err
128}
129
130func (r *publishRig) logged() string { b, _ := os.ReadFile(r.log); return string(b) }
131
132// publish puts the kit's archive and SOURCES on the forge under the tag
133// linked into its runner, tagging the kit's commit (not the checkout),
134// then downloads both in full and compares them. A second publish only
135// checks; check mode downloads into a directory.
136func TestPublishRenderer(t *testing.T) {
137 r := newPublishRig(t)
138 if out, err := r.run("publish", r.kit); err != nil {
139 t.Fatalf("publish: %v\n%s", err, out)
140 }
141 if !strings.Contains(r.logged(), "amadan release create -repo oficina/equipo -notes-file ") || !strings.Contains(r.logged(), r.tag+" "+filepath.Join(r.kit, "renderer.tar.gz")+" "+filepath.Join(r.kit, "SOURCES")) {
142 t.Fatalf("no release created:\n%s", r.logged())
143 }
144 out, err := exec.Command("git", "--git-dir", r.origin, "rev-parse", r.tag+"^{commit}").Output()
145 if err != nil || strings.TrimSpace(string(out)) != r.commit {
146 t.Fatalf("the forge's tag names %q, not the kit's commit %s: %v", out, r.commit, err)
147 }
148 for _, name := range []string{"renderer.tar.gz", "SOURCES"} {
149 if b, _ := os.ReadFile(filepath.Join(r.forge, r.tag, name)); string(b) == "" {
150 t.Fatalf("%s not published", name)
151 }
152 }
153 os.Remove(r.log)
154 if out, err := r.run("publish", r.kit); err != nil || strings.Contains(r.logged(), "amadan release") {
155 t.Fatalf("second publish: %v\n%s\n%s", err, out, r.logged())
156 }
157 dir := filepath.Join(t.TempDir(), "downloaded")
158 if out, err := r.run("check", r.kit, dir); err != nil {
159 t.Fatalf("check: %v\n%s", err, out)
160 }
161 if b, _ := os.ReadFile(filepath.Join(dir, "renderer.tar.gz")); string(b) != r.archive {
162 t.Fatalf("check downloaded %q", b)
163 }
164 if !strings.Contains(r.logged(), "curl --silent") {
165 t.Fatalf("curl's arguments not logged:\n%s", r.logged())
166 }
167 if strings.Contains(r.logged(), " -I ") || strings.Contains(r.logged(), "--head") || strings.Contains(r.logged(), "--range") || strings.Contains(r.logged(), " -r ") {
168 t.Fatalf("a HEAD or ranged request:\n%s", r.logged())
169 }
170}
171
172// A published asset that differs from the kit, a tag that is not the
173// archive's, SOURCES that are not the pinned packages', and check with
174// nothing published: refused, and nothing is created.
175func TestPublishRendererRefuses(t *testing.T) {
176 for name, c := range map[string]struct {
177 set func(r *publishRig) []string
178 args func(r *publishRig) []string
179 }{
180 "a different published archive": {func(r *publishRig) []string {
181 os.MkdirAll(filepath.Join(r.forge, r.tag), 0o755)
182 os.WriteFile(filepath.Join(r.forge, r.tag, "renderer.tar.gz"), []byte("other bytes"), 0o644)
183 os.WriteFile(filepath.Join(r.forge, r.tag, "SOURCES"), []byte(r.sources), 0o644)
184 return nil
185 }, nil},
186 "a tag naming another archive": {linking(func(a *rigAsset) { a.tag = "renderer-000000000000" }), nil},
187 "no linked tag": {linking(func(a *rigAsset) { a.tag = "" }), nil},
188 "no asset linked": {linking(func(a *rigAsset) { a.none = true }), nil},
189 "another linked size": {linking(func(a *rigAsset) { a.size = "1" }), nil},
190 "another linked digest": {linking(func(a *rigAsset) { a.sum = strings.Repeat("0", 64) }), nil},
191 "another linked pin": {linking(func(a *rigAsset) { a.pin = strings.Repeat("0", 64) }), nil},
192 // The runner runs with no home, no network and no environment: one
193 // that tries to read the publisher's files or a credential in its
194 // environment finds nothing.
195 // Nor can it find the publisher's environment through the
196 // sandbox's first process (/proc/1/environ).
197 "a runner reading the publisher's environment": {func(r *publishRig) []string {
198 r.writeRunner()
199 good, _ := os.ReadFile(filepath.Join(r.kit, "equipo-runner"))
200 body := strings.Replace(string(good), "printf", "tr '\\0' '\\n' < /proc/1/environ | grep -q '^PUBLISHER_CANARY=leaked$' || exit 1\nprintf", 1)
201 os.WriteFile(filepath.Join(r.kit, "equipo-runner"), []byte(body), 0o755)
202 return []string{"PUBLISHER_CANARY=leaked"}
203 }, nil},
204 "a runner reading the home directory": {func(r *publishRig) []string {
205 r.writeRunner()
206 good, _ := os.ReadFile(filepath.Join(r.kit, "equipo-runner"))
207 // It answers properly only where it can read the kit.
208 body := strings.Replace(string(good), "printf", "cat \""+filepath.Join(r.kit, "COMMIT")+"\" >/dev/null || exit 1\nprintf", 1)
209 os.WriteFile(filepath.Join(r.kit, "equipo-runner"), []byte(body), 0o755)
210 return nil
211 }, nil},
212 "a manifest recording another size": {func(r *publishRig) []string {
213 raw, _ := os.ReadFile(filepath.Join(r.kit, "install-manifest.json"))
214 os.WriteFile(filepath.Join(r.kit, "install-manifest.json"), []byte(strings.Replace(string(raw), `"renderer_archive_size":`, `"renderer_archive_size":1`, 1)), 0o644)
215 return nil
216 }, nil},
217 "SOURCES not the pin's": {func(r *publishRig) []string {
218 os.WriteFile(filepath.Join(r.kit, "SOURCES"), []byte(strings.Replace(r.sources, "1.0-1 amd64", "9.9-9 amd64", 1)), 0o644)
219 return nil
220 }, nil},
221 "check with nothing published": {func(r *publishRig) []string { return nil }, func(r *publishRig) []string { return []string{"check", r.kit, t.TempDir()} }},
222 } {
223 t.Run(name, func(t *testing.T) {
224 r := newPublishRig(t)
225 r.env = append(r.env, c.set(r)...)
226 args := []string{"publish", r.kit}
227 if c.args != nil {
228 args = c.args(r)
229 }
230 if out, err := r.run(args...); err == nil {
231 t.Fatalf("passed:\n%s", out)
232 }
233 if strings.Contains(r.logged(), "amadan release create") {
234 t.Fatalf("created a release:\n%s", r.logged())
235 }
236 })
237 }
238}
239
240// releaseRunnerCommand is the shell command make release-runner runs to
241// build the Linux runner into kit, as make -n prints it: -trimpath and
242// the real -ldflags. Only its target is this machine's (the CI box is
243// arm64), so the test can run what it builds.
244func releaseRunnerCommand(t *testing.T, kit string) string {
245 t.Helper()
246 cmd := exec.Command("make", "-n", "--no-print-directory", "release-runner", "RUNNER_RELEASE_DIR="+kit)
247 cmd.Dir = "../.."
248 out, err := cmd.Output()
249 if err != nil {
250 t.Fatalf("make -n release-runner: %v", err)
251 }
252 text := string(out)
253 start := strings.Index(text, "CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build -trimpath \\\n")
254 end := strings.Index(text[max(start, 0):], "-o "+kit+"/equipo-runner ./cmd/equipo-runner\n")
255 if start < 0 || end < 0 {
256 t.Fatalf("no Linux runner build in make -n release-runner:\n%s", text)
257 }
258 build := text[start : start+end+len("-o "+kit+"/equipo-runner ./cmd/equipo-runner")]
259 if !strings.Contains(build, "-trimpath") || !strings.Contains(build, "internal/release.rendererTag=") {
260 t.Fatalf("the release build has changed shape: %s", build)
261 }
262 return strings.Replace(build, "GOOS=linux GOARCH=amd64", "GOOS="+runtime.GOOS+" GOARCH="+runtime.GOARCH, 1)
263}
264
265// The release tooling reads what is linked into a runner built exactly as
266// make release-runner builds it (-trimpath keeps no -ldflags in the build
267// information, go.dev/issue/52372): the runner's renderer-asset --json
268// names the kit's archive, and publish-renderer.sh check passes against
269// it. A check that read build information would refuse here.
270func TestPublishRendererReadsAReleaseBuiltRunner(t *testing.T) {
271 testci.Files(t, "../../Makefile")
272 testci.Tools(t, "git", "tar", "bwrap")
273 // The runner it builds, and everything it is built from.
274 testci.GoPackages(t, "../..", nil, "", "./cmd/equipo-runner")
275 r := newPublishRig(t)
276 for _, name := range []string{"gitleaks", "equipo-tool-proxy"} {
277 os.WriteFile(filepath.Join(r.kit, name), []byte("fixture "+name), 0o755)
278 }
279 os.Remove(filepath.Join(r.kit, "equipo-runner"))
280 build := exec.Command("sh", "-c", releaseRunnerCommand(t, r.kit))
281 build.Dir = "../.."
282 if out, err := build.CombinedOutput(); err != nil {
283 t.Fatalf("build: %v\n%s", err, out)
284 }
285 if info, err := exec.Command("go", "version", "-m", filepath.Join(r.kit, "equipo-runner")).Output(); err != nil || strings.Contains(string(info), "rendererTag") {
286 t.Fatalf("the build information holds the ldflags after all (%v): the test proves nothing", err)
287 }
288 out, err := exec.Command(filepath.Join(r.kit, "equipo-runner"), "renderer-asset", "--json").Output()
289 if err != nil {
290 t.Fatalf("renderer-asset: %v", err)
291 }
292 var got struct {
293 Tag, Pin, SHA256 string
294 Size int64
295 }
296 if err = json.Unmarshal(out, &got); err != nil || got.Tag != r.tag || got.SHA256 != fmt.Sprintf("%x", sha256.Sum256([]byte(r.archive))) || got.Size != int64(len(r.archive)) || got.Pin == "" {
297 t.Fatalf("renderer-asset %s: %v", out, err)
298 }
299 os.MkdirAll(filepath.Join(r.forge, r.tag), 0o755)
300 for _, name := range []string{"renderer.tar.gz", "SOURCES"} {
301 b, _ := os.ReadFile(filepath.Join(r.kit, name))
302 os.WriteFile(filepath.Join(r.forge, r.tag, name), b, 0o644)
303 }
304 if out, err := r.run("check", r.kit, t.TempDir()); err != nil {
305 t.Fatalf("check against a release-built runner: %v\n%s", err, out)
306 }
307}
308