| 1 | package thin |
| 2 | |
| 3 | import ( |
| 4 | "context" |
| 5 | "crypto/sha256" |
| 6 | "encoding/json" |
| 7 | "fmt" |
| 8 | "os" |
| 9 | "path/filepath" |
| 10 | "strings" |
| 11 | "testing" |
| 12 | |
| 13 | "amadan.net/oficina/equipo/internal/release" |
| 14 | "amadan.net/oficina/equipo/internal/runner" |
| 15 | "amadan.net/oficina/equipo/internal/runner/render" |
| 16 | "amadan.net/oficina/equipo/internal/wire" |
| 17 | ) |
| 18 | |
| 19 | // TestToolHealthReportsTheScannerPin (stuck tasks, protocol 30): the |
| 20 | // capability report says whether the preview scanner is installed and is |
| 21 | // the pinned build, as the scan itself checks it: one fixed state, never |
| 22 | // its path or digest. |
| 23 | func TestToolHealthReportsTheScannerPin(t *testing.T) { |
| 24 | dir := t.TempDir() |
| 25 | binary := filepath.Join(dir, "gitleaks") |
| 26 | if err := os.WriteFile(binary, []byte("#!/bin/sh\nexit 0\n"), 0o700); err != nil { |
| 27 | t.Fatal(err) |
| 28 | } |
| 29 | raw, _ := os.ReadFile(binary) |
| 30 | pin := fmt.Sprintf("%x", sha256.Sum256(raw)) |
| 31 | other := fmt.Sprintf("%x", sha256.Sum256([]byte("another build"))) |
| 32 | old := previewScannerTool |
| 33 | t.Cleanup(func() { previewScannerTool = old }) |
| 34 | report := func(path, want string) wire.Capabilities { |
| 35 | previewScannerTool = func(runner.Config) (string, string) { return path, want } |
| 36 | return linuxCapabilities(runner.Config{}, func() *wire.PreviewHost { return nil }, nil, nil, nil)(context.Background()) |
| 37 | } |
| 38 | for _, c := range []struct{ path, pin, want string }{ |
| 39 | {binary, pin, wire.ToolHealthy}, |
| 40 | {binary, other, wire.ToolMismatch}, |
| 41 | {filepath.Join(dir, "absent"), pin, wire.ToolMissing}, |
| 42 | {dir, pin, wire.ToolMissing}, |
| 43 | {binary, "", wire.ToolUnpinned}, |
| 44 | } { |
| 45 | caps := report(c.path, c.pin) |
| 46 | if caps.Tools == nil || caps.Tools.Scanner != c.want || caps.Tools.Renderer != "" { |
| 47 | t.Fatalf("%s pinned %q: %+v", c.path, c.pin, caps.Tools) |
| 48 | } |
| 49 | if err := wire.ValidCapabilities(caps); err != nil { |
| 50 | t.Fatalf("report out of bounds: %v", err) |
| 51 | } |
| 52 | out, _ := json.Marshal(caps) |
| 53 | if strings.Contains(string(out), dir) || strings.Contains(string(out), pin) || strings.Contains(string(out), "gitleaks") { |
| 54 | t.Fatalf("the report names the tool's path or pin: %s", out) |
| 55 | } |
| 56 | } |
| 57 | // Not executable is not installed: the scan would refuse it. |
| 58 | if err := os.Chmod(binary, 0o600); err != nil { |
| 59 | t.Fatal(err) |
| 60 | } |
| 61 | if caps := report(binary, pin); caps.Tools.Scanner != wire.ToolMismatch && caps.Tools.Scanner != wire.ToolMissing { |
| 62 | t.Fatalf("a scanner the scan refuses reported %q", caps.Tools.Scanner) |
| 63 | } |
| 64 | } |
| 65 | |
| 66 | // TestHandshakeHelloWithholdsToolHealth: the first hello goes out before |
| 67 | // the instance says its protocol, so it never carries the tool health an |
| 68 | // older instance's strict decoder refuses; after a protocol 30 welcome |
| 69 | // the next hello, sent at once, does; at 29 none ever does. |
| 70 | func TestHandshakeHelloWithholdsToolHealth(t *testing.T) { |
| 71 | hello := func() wire.Hello { |
| 72 | return wire.Hello{Repos: []string{"amadan.net/oficina/sample"}, CreateIn: []string{}, Catalog: []wire.RepoInfo{}, Connections: []wire.HeldConnection{}, Versions: map[string]string{"runner": "test", "claude": "2.1.312"}, |
| 73 | Capabilities: &wire.Capabilities{Tags: []string{wire.TagLinux}, Simulators: []wire.Simulator{}, Tools: &wire.ToolHealth{Scanner: wire.ToolMissing}}} |
| 74 | } |
| 75 | for _, protocol := range []int{wire.ProtocolPinnedRelease, wire.ProtocolStuckTasks} { |
| 76 | inst := newFakeInstance(t) |
| 77 | link, _, _, _ := startLinkWith(t, inst.url, &stubCoordinator{}, hello) |
| 78 | c := <-inst.conns |
| 79 | _, raw := read(t, c, wire.FrameHello) |
| 80 | if strings.Contains(string(raw), `"tools"`) || !strings.Contains(string(raw), `"linux"`) { |
| 81 | t.Fatalf("handshake hello: %s", raw) |
| 82 | } |
| 83 | welcome, _ := json.Marshal(wire.Frame{ID: "w-t", Type: wire.FrameWelcome, Data: json.RawMessage(`{"generation":3,"runner_id":"RUNNER1","protocol":` + itoa(int64(protocol)) + `,"role":"primary"}`)}) |
| 84 | write(t, c, welcome) |
| 85 | read(t, c, wire.FrameSync) |
| 86 | if protocol < wire.ProtocolStuckTasks { |
| 87 | link.HelloSoon() |
| 88 | } |
| 89 | _, raw = read(t, c, wire.FrameHello) |
| 90 | if got := strings.Contains(string(raw), `"tools":{"scanner":"missing"}`); got != (protocol >= wire.ProtocolStuckTasks) { |
| 91 | t.Fatalf("protocol %d hello: %s", protocol, raw) |
| 92 | } |
| 93 | } |
| 94 | } |
| 95 | |
| 96 | // TestToolHealthReportsTheRenderer (task 8): on Linux amd64 the |
| 97 | // capability report carries the page renderer's health, ok only once the |
| 98 | // tree verified and rendered the fixture page; a runner without one (a |
| 99 | // Mac, Linux arm64) sends no renderer field at all. |
| 100 | func TestToolHealthReportsTheRenderer(t *testing.T) { |
| 101 | for _, p := range [][2]string{{"darwin", "arm64"}, {"linux", "arm64"}} { |
| 102 | if h := rendererHealth(runner.Config{Executable: "/opt/equipo/runner"}, p[0], p[1], strings.Repeat("a", 64), nil); h != nil { |
| 103 | t.Fatalf("%s/%s has a renderer: %+v", p[0], p[1], h) |
| 104 | } |
| 105 | } |
| 106 | h := rendererHealth(runner.Config{Executable: "/opt/equipo/runner", BwrapPath: "/usr/bin/bwrap"}, "linux", "amd64", strings.Repeat("a", 64), nil) |
| 107 | if h == nil || h.Config.Tree != "/opt/equipo/renderer/aaaaaaaaaaaa" || h.Config.Binary != "/opt/equipo/runner" || h.Config.Bwrap != "/usr/bin/bwrap" || h.Scratch != "/opt/equipo/renderer" { |
| 108 | t.Fatalf("linux/amd64 renderer: %+v", h) |
| 109 | } |
| 110 | caps := func(h *render.Health) wire.Capabilities { |
| 111 | return linuxCapabilities(runner.Config{}, func() *wire.PreviewHost { return nil }, nil, nil, h)(context.Background()) |
| 112 | } |
| 113 | none := caps(nil) |
| 114 | if out, _ := json.Marshal(none); none.Tools == nil || none.Tools.Renderer != "" || strings.Contains(string(out), "renderer") { |
| 115 | t.Fatalf("no renderer reported: %s", out) |
| 116 | } |
| 117 | tree := filepath.Join(t.TempDir(), "renderer", "aaaaaaaaaaaa") |
| 118 | if err := os.MkdirAll(tree, 0o755); err != nil { |
| 119 | t.Fatal(err) |
| 120 | } |
| 121 | for _, c := range []struct { |
| 122 | verify, fixture error |
| 123 | want string |
| 124 | }{ |
| 125 | {nil, nil, wire.ToolHealthy}, |
| 126 | {nil, fmt.Errorf("no namespaces"), wire.ToolMissing}, |
| 127 | {&release.RendererError{Health: wire.ToolMismatch, Reason: "tampered"}, nil, wire.ToolMismatch}, |
| 128 | {&release.RendererError{Health: wire.ToolUnpinned, Reason: "no pin"}, nil, wire.ToolUnpinned}, |
| 129 | {&release.RendererError{Health: wire.ToolMissing, Reason: "absent"}, nil, wire.ToolMissing}, |
| 130 | } { |
| 131 | h := &render.Health{Config: render.LaunchConfig{Tree: tree}, Verify: func(string, string) error { return c.verify }, Fixture: func(context.Context, render.LaunchConfig, string) error { return c.fixture }} |
| 132 | h.Start(context.Background()) |
| 133 | _, _, _ = h.Ready(context.Background()) |
| 134 | got := caps(h) |
| 135 | if got.Tools == nil || got.Tools.Renderer != c.want || wire.ValidCapabilities(got) != nil { |
| 136 | t.Fatalf("verify %v, fixture %v: %+v", c.verify, c.fixture, got.Tools) |
| 137 | } |
| 138 | if out, _ := json.Marshal(got); strings.Contains(string(out), tree) { |
| 139 | t.Fatalf("the report names the tree: %s", out) |
| 140 | } |
| 141 | } |
| 142 | } |
| 143 | |
| 144 | // TestStatusSaysABadRendererNeedsTheRunnerUpdated: status's description |
| 145 | // tells the coordinator what runner.tools.renderer means and that a bad |
| 146 | // renderer needs the runner updated, never another try. |
| 147 | func TestStatusSaysABadRendererNeedsTheRunnerUpdated(t *testing.T) { |
| 148 | for _, tool := range converseTools { |
| 149 | m := tool.(map[string]any) |
| 150 | if m["name"] != "status" { |
| 151 | continue |
| 152 | } |
| 153 | d := m["description"].(string) |
| 154 | if !strings.Contains(d, "runner.tools.renderer") || !strings.Contains(d, "anything but ok needs the runner updated, not another try") { |
| 155 | t.Fatalf("status's description: %s", d) |
| 156 | } |
| 157 | return |
| 158 | } |
| 159 | t.Fatal("no status tool") |
| 160 | } |
| 161 | |