oficina / equipo Public

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

Plain git — no account needed to clone.

Download

Download this file

1package main
2
3import (
4 "bytes"
5 "context"
6 "errors"
7 "os"
8 "path/filepath"
9 "runtime"
10 "strings"
11 "testing"
12
13 "amadan.net/oficina/equipo/internal/release"
14 "amadan.net/oficina/equipo/internal/release/renderertest"
15 "amadan.net/oficina/equipo/internal/runner"
16 "amadan.net/oficina/equipo/internal/runner/render"
17 "amadan.net/oficina/equipo/internal/wire"
18)
19
20// fixtureCall is one fixture render verify-installed asked for.
21type fixtureCall struct {
22 cfg render.LaunchConfig
23 scratch string
24}
25
26// installedRenderer installs a small renderer tree beside the runner in
27// dir, links its pin into this build, and stands in for the fixture
28// render (a real one needs Chromium: TestRendererFromARealRelease).
29func installedRenderer(t *testing.T, dir string) (string, *[]fixtureCall, *error) {
30 t.Helper()
31 archive, pin := renderertest.Archive(renderertest.Small())
32 if runtime.GOOS == "linux" {
33 if err := os.Chmod(dir, 0o700); err != nil {
34 t.Fatal(err)
35 }
36 if _, err := release.InstallRenderer(dir, bytes.NewReader(archive), pin); err != nil {
37 t.Fatal(err)
38 }
39 }
40 oldPin, oldRender := rendererPin, fixtureRender
41 calls, fail := new([]fixtureCall), new(error)
42 rendererPin = func() string { return pin }
43 fixtureRender = func(_ context.Context, cfg render.LaunchConfig, scratch string) error {
44 *calls = append(*calls, fixtureCall{cfg, scratch})
45 return *fail
46 }
47 t.Cleanup(func() { rendererPin, fixtureRender = oldPin, oldRender })
48 return pin, calls, fail
49}
50
51// verify-installed holds the renderer beside the runner to the pin linked
52// into this build and renders the fixture page with it, through the
53// verified bubblewrap and the runner's own binary: a tree missing or
54// changed, no pin, or a fixture render that fails, refuses.
55func TestVerifyInstalledHoldsTheRendererBesideTheRunner(t *testing.T) {
56 if runtime.GOOS != "linux" || runtime.GOARCH != "amd64" {
57 t.Skip("the renderer is Linux amd64's")
58 }
59 m, cfg, _ := installedFixture(t)
60 dir := filepath.Dir(cfg.Executable)
61 pin, calls, fail := installedRenderer(t, dir)
62 tree := filepath.Join(dir, "renderer", pin[:12])
63 if err := verifyInstalled(m, cfg); err != nil {
64 t.Fatalf("the release's own renderer refused: %v", err)
65 }
66 want := fixtureCall{render.LaunchConfig{Bwrap: cfg.Bwrap(), Binary: cfg.Executable, Tree: tree, Pin: pin}, filepath.Join(dir, "renderer")}
67 if len(*calls) != 1 || (*calls)[0] != want {
68 t.Fatalf("fixture renders %+v, want one %+v", *calls, want)
69 }
70 *fail = errors.New("the capture is not the fixture page")
71 if verifyInstalled(m, cfg) == nil {
72 t.Fatal("verified with a failed fixture render")
73 }
74 *fail = nil
75 rendererPin = func() string { return "" }
76 if err := verifyInstalled(m, cfg); release.RendererHealth(err) != wire.ToolUnpinned {
77 t.Fatalf("a build with no renderer pin verified: %v", err)
78 }
79 rendererPin = func() string { return pin }
80 conf := filepath.Join(tree, "usr/share/fonts.conf")
81 if err := os.WriteFile(conf, []byte("changed"), 0o644); err != nil {
82 t.Fatal(err)
83 }
84 *calls = nil
85 if err := verifyInstalled(m, cfg); release.RendererHealth(err) != wire.ToolMismatch || len(*calls) != 0 {
86 t.Fatalf("a changed tree verified or rendered: %v %d", err, len(*calls))
87 }
88 if err := os.RemoveAll(tree); err != nil {
89 t.Fatal(err)
90 }
91 if err := verifyInstalled(m, cfg); release.RendererHealth(err) != wire.ToolMissing || len(*calls) != 0 {
92 t.Fatalf("no tree verified or rendered: %v %d", err, len(*calls))
93 }
94}
95
96// A Mac, or a Linux arm64 machine, has no renderer: verify-installed
97// neither looks for a tree nor renders, with no tree and no pin.
98func TestVerifyInstalledNeedsNoRendererOffLinuxAmd64(t *testing.T) {
99 dir := t.TempDir()
100 cfg := runner.Config{Executable: filepath.Join(dir, "equipo-runner")}
101 oldPin, oldRender := rendererPin, fixtureRender
102 t.Cleanup(func() { rendererPin, fixtureRender = oldPin, oldRender })
103 rendererPin = func() string { return "" }
104 fixtureRender = func(context.Context, render.LaunchConfig, string) error {
105 t.Error("a fixture render off Linux amd64")
106 return errors.New("no renderer here")
107 }
108 for _, p := range [][2]string{{"darwin", "arm64"}, {"linux", "arm64"}} {
109 if err := verifyRenderer(cfg, p[0], p[1]); err != nil {
110 t.Fatalf("%s/%s: %v", p[0], p[1], err)
111 }
112 }
113 if err := verifyRenderer(cfg, "linux", "amd64"); err == nil {
114 t.Fatal("Linux amd64 verified with no renderer")
115 }
116 if names, _ := os.ReadDir(dir); len(names) != 0 {
117 t.Fatalf("wrote %v", names)
118 }
119}
120
121// A Mac install fetches no renderer and unpacks none.
122func TestInstallMacFetchesNoRenderer(t *testing.T) {
123 x := newMacRig(t)
124 if code, _, errout := x.install(); code != 0 {
125 t.Fatalf("code %d: %s", code, errout)
126 }
127 for _, url := range x.fetched {
128 if strings.Contains(url, "renderer") {
129 t.Fatalf("a Mac fetched %s", url)
130 }
131 }
132 if _, err := os.Stat(filepath.Join(x.home, ".local/lib/equipo-runner/renderer")); !os.IsNotExist(err) {
133 t.Fatalf("a Mac has a renderer: %v", err)
134 }
135}
136
137// equipo-runner install-renderer <archive> unpacks the release's archive
138// beside the runner's own executable, the tree for the pin linked into
139// it; another archive, a relative path, or a machine that is not Linux
140// amd64, installs nothing.
141func TestInstallRendererCommand(t *testing.T) {
142 if runtime.GOOS != "linux" {
143 t.Skip("the renderer is Linux's")
144 }
145 archive, pin := renderertest.Archive(renderertest.Small())
146 other, _ := renderertest.Archive(renderertest.Small()[:4])
147 scratch := t.TempDir()
148 path, otherPath := filepath.Join(scratch, "renderer.tar.gz"), filepath.Join(scratch, "other.tar.gz")
149 for p, b := range map[string][]byte{path: archive, otherPath: other} {
150 if err := os.WriteFile(p, b, 0o600); err != nil {
151 t.Fatal(err)
152 }
153 }
154 runnerDir := func() string {
155 dir := filepath.Join(t.TempDir(), "releases", "v1")
156 if err := os.MkdirAll(dir, 0o700); err != nil {
157 t.Fatal(err)
158 }
159 return dir
160 }
161 dir := runnerDir()
162 tree, err := installRendererOn("linux", "amd64", dir, path, pin)
163 if err != nil || tree != filepath.Join(dir, "renderer", pin[:12]) || release.VerifyRenderer(tree, pin) != nil {
164 t.Fatalf("install-renderer: %s %v", tree, err)
165 }
166 for name, run := range map[string]func(dir string) error{
167 "another archive": func(dir string) error { _, err := installRendererOn("linux", "amd64", dir, otherPath, pin); return err },
168 "relative path": func(dir string) error {
169 _, err := installRendererOn("linux", "amd64", dir, "renderer.tar.gz", pin)
170 return err
171 },
172 "no archive": func(dir string) error {
173 _, err := installRendererOn("linux", "amd64", dir, path+".missing", pin)
174 return err
175 },
176 "no pin": func(dir string) error { _, err := installRendererOn("linux", "amd64", dir, path, ""); return err },
177 "a Mac": func(dir string) error { _, err := installRendererOn("darwin", "arm64", dir, path, pin); return err },
178 "Linux arm64": func(dir string) error { _, err := installRendererOn("linux", "arm64", dir, path, pin); return err },
179 } {
180 dir := runnerDir()
181 if run(dir) == nil {
182 t.Fatalf("installed with %s", name)
183 }
184 if _, err := os.Stat(filepath.Join(dir, "renderer", pin[:12])); !os.IsNotExist(err) {
185 t.Fatalf("a tree with %s: %v", name, err)
186 }
187 }
188 // The command itself: one absolute archive path, the tree printed.
189 var out, errout bytes.Buffer
190 if code := installRendererCommand([]string{"renderer.tar.gz"}, &out, &errout); code != 2 {
191 t.Fatalf("relative path: %d", code)
192 }
193 if code := installRendererCommand(nil, &out, &errout); code != 2 {
194 t.Fatalf("no path: %d", code)
195 }
196}
197
198// renderer-asset --json prints the asset linked into the build; with none
199// (a test or Mac build) it prints nothing and exits 1, and any other
200// arguments are a usage error.
201func TestRendererAssetCommand(t *testing.T) {
202 var out, errout bytes.Buffer
203 if code := rendererAssetCommand([]string{"--json"}, &out, &errout); code != 1 || out.Len() != 0 {
204 t.Fatalf("none linked: %d %q", code, out.String())
205 }
206 sum := strings.Repeat("ab", 32)
207 old := linkedRendererAsset
208 linkedRendererAsset = func() (release.RendererAsset, bool) {
209 return release.RendererAsset{Pin: strings.Repeat("cd", 32), SHA256: sum, Size: 42, Tag: "renderer-" + sum[:12]}, true
210 }
211 defer func() { linkedRendererAsset = old }()
212 out.Reset()
213 if code := rendererAssetCommand([]string{"--json"}, &out, &errout); code != 0 || out.String() != `{"tag":"renderer-abababababab","pin":"`+strings.Repeat("cd", 32)+`","sha256":"`+sum+`","size":42}`+"\n" {
214 t.Fatalf("linked: %d %q", code, out.String())
215 }
216 for _, args := range [][]string{nil, {"--text"}, {"--json", "x"}} {
217 if code := rendererAssetCommand(args, &out, &errout); code != 2 {
218 t.Fatalf("%v: %d", args, code)
219 }
220 }
221}
222