package main import ( "bytes" "context" "errors" "os" "path/filepath" "runtime" "strings" "testing" "amadan.net/oficina/equipo/internal/release" "amadan.net/oficina/equipo/internal/release/renderertest" "amadan.net/oficina/equipo/internal/runner" "amadan.net/oficina/equipo/internal/runner/render" "amadan.net/oficina/equipo/internal/wire" ) // fixtureCall is one fixture render verify-installed asked for. type fixtureCall struct { cfg render.LaunchConfig scratch string } // installedRenderer installs a small renderer tree beside the runner in // dir, links its pin into this build, and stands in for the fixture // render (a real one needs Chromium: TestRendererFromARealRelease). func installedRenderer(t *testing.T, dir string) (string, *[]fixtureCall, *error) { t.Helper() archive, pin := renderertest.Archive(renderertest.Small()) if runtime.GOOS == "linux" { if err := os.Chmod(dir, 0o700); err != nil { t.Fatal(err) } if _, err := release.InstallRenderer(dir, bytes.NewReader(archive), pin); err != nil { t.Fatal(err) } } oldPin, oldRender := rendererPin, fixtureRender calls, fail := new([]fixtureCall), new(error) rendererPin = func() string { return pin } fixtureRender = func(_ context.Context, cfg render.LaunchConfig, scratch string) error { *calls = append(*calls, fixtureCall{cfg, scratch}) return *fail } t.Cleanup(func() { rendererPin, fixtureRender = oldPin, oldRender }) return pin, calls, fail } // verify-installed holds the renderer beside the runner to the pin linked // into this build and renders the fixture page with it, through the // verified bubblewrap and the runner's own binary: a tree missing or // changed, no pin, or a fixture render that fails, refuses. func TestVerifyInstalledHoldsTheRendererBesideTheRunner(t *testing.T) { if runtime.GOOS != "linux" || runtime.GOARCH != "amd64" { t.Skip("the renderer is Linux amd64's") } m, cfg, _ := installedFixture(t) dir := filepath.Dir(cfg.Executable) pin, calls, fail := installedRenderer(t, dir) tree := filepath.Join(dir, "renderer", pin[:12]) if err := verifyInstalled(m, cfg); err != nil { t.Fatalf("the release's own renderer refused: %v", err) } want := fixtureCall{render.LaunchConfig{Bwrap: cfg.Bwrap(), Binary: cfg.Executable, Tree: tree, Pin: pin}, filepath.Join(dir, "renderer")} if len(*calls) != 1 || (*calls)[0] != want { t.Fatalf("fixture renders %+v, want one %+v", *calls, want) } *fail = errors.New("the capture is not the fixture page") if verifyInstalled(m, cfg) == nil { t.Fatal("verified with a failed fixture render") } *fail = nil rendererPin = func() string { return "" } if err := verifyInstalled(m, cfg); release.RendererHealth(err) != wire.ToolUnpinned { t.Fatalf("a build with no renderer pin verified: %v", err) } rendererPin = func() string { return pin } conf := filepath.Join(tree, "usr/share/fonts.conf") if err := os.WriteFile(conf, []byte("changed"), 0o644); err != nil { t.Fatal(err) } *calls = nil if err := verifyInstalled(m, cfg); release.RendererHealth(err) != wire.ToolMismatch || len(*calls) != 0 { t.Fatalf("a changed tree verified or rendered: %v %d", err, len(*calls)) } if err := os.RemoveAll(tree); err != nil { t.Fatal(err) } if err := verifyInstalled(m, cfg); release.RendererHealth(err) != wire.ToolMissing || len(*calls) != 0 { t.Fatalf("no tree verified or rendered: %v %d", err, len(*calls)) } } // A Mac, or a Linux arm64 machine, has no renderer: verify-installed // neither looks for a tree nor renders, with no tree and no pin. func TestVerifyInstalledNeedsNoRendererOffLinuxAmd64(t *testing.T) { dir := t.TempDir() cfg := runner.Config{Executable: filepath.Join(dir, "equipo-runner")} oldPin, oldRender := rendererPin, fixtureRender t.Cleanup(func() { rendererPin, fixtureRender = oldPin, oldRender }) rendererPin = func() string { return "" } fixtureRender = func(context.Context, render.LaunchConfig, string) error { t.Error("a fixture render off Linux amd64") return errors.New("no renderer here") } for _, p := range [][2]string{{"darwin", "arm64"}, {"linux", "arm64"}} { if err := verifyRenderer(cfg, p[0], p[1]); err != nil { t.Fatalf("%s/%s: %v", p[0], p[1], err) } } if err := verifyRenderer(cfg, "linux", "amd64"); err == nil { t.Fatal("Linux amd64 verified with no renderer") } if names, _ := os.ReadDir(dir); len(names) != 0 { t.Fatalf("wrote %v", names) } } // A Mac install fetches no renderer and unpacks none. func TestInstallMacFetchesNoRenderer(t *testing.T) { x := newMacRig(t) if code, _, errout := x.install(); code != 0 { t.Fatalf("code %d: %s", code, errout) } for _, url := range x.fetched { if strings.Contains(url, "renderer") { t.Fatalf("a Mac fetched %s", url) } } if _, err := os.Stat(filepath.Join(x.home, ".local/lib/equipo-runner/renderer")); !os.IsNotExist(err) { t.Fatalf("a Mac has a renderer: %v", err) } } // equipo-runner install-renderer unpacks the release's archive // beside the runner's own executable, the tree for the pin linked into // it; another archive, a relative path, or a machine that is not Linux // amd64, installs nothing. func TestInstallRendererCommand(t *testing.T) { if runtime.GOOS != "linux" { t.Skip("the renderer is Linux's") } archive, pin := renderertest.Archive(renderertest.Small()) other, _ := renderertest.Archive(renderertest.Small()[:4]) scratch := t.TempDir() path, otherPath := filepath.Join(scratch, "renderer.tar.gz"), filepath.Join(scratch, "other.tar.gz") for p, b := range map[string][]byte{path: archive, otherPath: other} { if err := os.WriteFile(p, b, 0o600); err != nil { t.Fatal(err) } } runnerDir := func() string { dir := filepath.Join(t.TempDir(), "releases", "v1") if err := os.MkdirAll(dir, 0o700); err != nil { t.Fatal(err) } return dir } dir := runnerDir() tree, err := installRendererOn("linux", "amd64", dir, path, pin) if err != nil || tree != filepath.Join(dir, "renderer", pin[:12]) || release.VerifyRenderer(tree, pin) != nil { t.Fatalf("install-renderer: %s %v", tree, err) } for name, run := range map[string]func(dir string) error{ "another archive": func(dir string) error { _, err := installRendererOn("linux", "amd64", dir, otherPath, pin); return err }, "relative path": func(dir string) error { _, err := installRendererOn("linux", "amd64", dir, "renderer.tar.gz", pin) return err }, "no archive": func(dir string) error { _, err := installRendererOn("linux", "amd64", dir, path+".missing", pin) return err }, "no pin": func(dir string) error { _, err := installRendererOn("linux", "amd64", dir, path, ""); return err }, "a Mac": func(dir string) error { _, err := installRendererOn("darwin", "arm64", dir, path, pin); return err }, "Linux arm64": func(dir string) error { _, err := installRendererOn("linux", "arm64", dir, path, pin); return err }, } { dir := runnerDir() if run(dir) == nil { t.Fatalf("installed with %s", name) } if _, err := os.Stat(filepath.Join(dir, "renderer", pin[:12])); !os.IsNotExist(err) { t.Fatalf("a tree with %s: %v", name, err) } } // The command itself: one absolute archive path, the tree printed. var out, errout bytes.Buffer if code := installRendererCommand([]string{"renderer.tar.gz"}, &out, &errout); code != 2 { t.Fatalf("relative path: %d", code) } if code := installRendererCommand(nil, &out, &errout); code != 2 { t.Fatalf("no path: %d", code) } } // renderer-asset --json prints the asset linked into the build; with none // (a test or Mac build) it prints nothing and exits 1, and any other // arguments are a usage error. func TestRendererAssetCommand(t *testing.T) { var out, errout bytes.Buffer if code := rendererAssetCommand([]string{"--json"}, &out, &errout); code != 1 || out.Len() != 0 { t.Fatalf("none linked: %d %q", code, out.String()) } sum := strings.Repeat("ab", 32) old := linkedRendererAsset linkedRendererAsset = func() (release.RendererAsset, bool) { return release.RendererAsset{Pin: strings.Repeat("cd", 32), SHA256: sum, Size: 42, Tag: "renderer-" + sum[:12]}, true } defer func() { linkedRendererAsset = old }() out.Reset() if code := rendererAssetCommand([]string{"--json"}, &out, &errout); code != 0 || out.String() != `{"tag":"renderer-abababababab","pin":"`+strings.Repeat("cd", 32)+`","sha256":"`+sum+`","size":42}`+"\n" { t.Fatalf("linked: %d %q", code, out.String()) } for _, args := range [][]string{nil, {"--text"}, {"--json", "x"}} { if code := rendererAssetCommand(args, &out, &errout); code != 2 { t.Fatalf("%v: %d", args, code) } } }