| 1 | package main |
| 2 | |
| 3 | import ( |
| 4 | "bytes" |
| 5 | "os" |
| 6 | "os/exec" |
| 7 | "path/filepath" |
| 8 | "regexp" |
| 9 | "strings" |
| 10 | "testing" |
| 11 | |
| 12 | "amadan.net/oficina/equipo/internal/testci" |
| 13 | ) |
| 14 | |
| 15 | // marker is the fake worker's marker (internal/runner/thin |
| 16 | // fakeworker_testbuild.go). |
| 17 | const marker = "equipo-fake-worker-unsandboxed-process-group" |
| 18 | |
| 19 | // The test build's fake Mac worker runs checks without a sandbox, so it |
| 20 | // must never be compiled into a release runner: built exactly as `make |
| 21 | // release-runner` builds it, the binary does not contain it (and the |
| 22 | // release recipe passes no build tags and checks the binary itself), |
| 23 | // while a test build of the thin runner does, so this test can see it. |
| 24 | func TestReleaseRunnerHasNoFakeWorker(t *testing.T) { |
| 25 | makefile, err := os.ReadFile("../../Makefile") |
| 26 | if err != nil { |
| 27 | t.Fatal(err) |
| 28 | } |
| 29 | recipe := regexp.MustCompile(`(?s)go build -trimpath \\\s*\n\s*-ldflags "([^"]*)" \\\s*\n\s*-o \$\(RUNNER_RELEASE_DIR\)/equipo-runner \./cmd/equipo-runner\n\s*scripts/no-fake-worker\.sh \$\(RUNNER_RELEASE_DIR\)/equipo-runner`).FindSubmatch(makefile) |
| 30 | if recipe == nil { |
| 31 | t.Fatal("the release-runner recipe changed: build the runner without tags and check it with scripts/no-fake-worker.sh") |
| 32 | } |
| 33 | if strings.Contains(string(makefile[bytes.Index(makefile, []byte("release-runner:")):]), "-tags testbuild") { |
| 34 | t.Fatal("a release recipe builds with the testbuild tag") |
| 35 | } |
| 36 | dir := t.TempDir() |
| 37 | bin := filepath.Join(dir, "equipo-runner") |
| 38 | // The Linux release, on whatever machine runs this. |
| 39 | build := exec.Command("go", "build", "-trimpath", "-ldflags", "-s -w", "-o", bin, ".") |
| 40 | build.Env = append(os.Environ(), "CGO_ENABLED=0", "GOOS=linux", "GOARCH=amd64") |
| 41 | testci.GoCommand(t, build) |
| 42 | testci.Files(t, "../../scripts/no-fake-worker.sh") |
| 43 | if out, err := build.CombinedOutput(); err != nil { |
| 44 | t.Fatalf("release build: %v %s", err, out) |
| 45 | } |
| 46 | raw, err := os.ReadFile(bin) |
| 47 | if err != nil { |
| 48 | t.Fatal(err) |
| 49 | } |
| 50 | if bytes.Contains(raw, []byte(marker)) { |
| 51 | t.Fatal("the release runner contains the fake worker") |
| 52 | } |
| 53 | if bytes.Contains(raw, []byte(egressMarker)) { |
| 54 | t.Fatal("the release runner contains the test build's egress route") |
| 55 | } |
| 56 | // A Mac's MCP endpoint is built for darwin and the test build: a |
| 57 | // Linux release runs the certified equipo-tool-proxy instead. |
| 58 | if bytes.Contains(raw, []byte(darwinProxyMarker)) { |
| 59 | t.Fatal("the Linux release runner contains the Mac's tool endpoint") |
| 60 | } |
| 61 | if out, err := exec.Command("../../scripts/no-fake-worker.sh", bin).CombinedOutput(); err != nil { |
| 62 | t.Fatalf("the release check refused a clean binary: %s", out) |
| 63 | } |
| 64 | // The Mac release (darwin/arm64, as make release-runner builds it) |
| 65 | // has the Mac's endpoint and nothing of the test build. |
| 66 | mac := filepath.Join(dir, "equipo-runner-darwin") |
| 67 | build = exec.Command("go", "build", "-trimpath", "-ldflags", "-s -w", "-o", mac, ".") |
| 68 | build.Env = append(os.Environ(), "CGO_ENABLED=0", "GOOS=darwin", "GOARCH=arm64") |
| 69 | testci.GoCommand(t, build) |
| 70 | if out, err := build.CombinedOutput(); err != nil { |
| 71 | t.Fatalf("Mac release build: %v %s", err, out) |
| 72 | } |
| 73 | raw, err = os.ReadFile(mac) |
| 74 | if err != nil { |
| 75 | t.Fatal(err) |
| 76 | } |
| 77 | if !bytes.Contains(raw, []byte(darwinProxyMarker)) || bytes.Contains(raw, []byte(marker)) || bytes.Contains(raw, []byte(egressMarker)) { |
| 78 | t.Fatal("the Mac release runner lacks its tool endpoint or holds the test build's code") |
| 79 | } |
| 80 | if out, err := exec.Command("../../scripts/no-fake-worker.sh", mac).CombinedOutput(); err != nil { |
| 81 | t.Fatalf("the release check refused the Mac runner: %s", out) |
| 82 | } |
| 83 | archive := filepath.Join(dir, "thin.a") |
| 84 | test := exec.Command("go", "build", "-tags", "testbuild", "-o", archive, "../../internal/runner/thin") |
| 85 | test.Env = append(os.Environ(), "CGO_ENABLED=0") |
| 86 | testci.GoCommand(t, test) |
| 87 | if out, err := test.CombinedOutput(); err != nil { |
| 88 | t.Fatalf("test build: %v %s", err, out) |
| 89 | } |
| 90 | if raw, _ := os.ReadFile(archive); !bytes.Contains(raw, []byte(marker)) { |
| 91 | t.Fatal("the test build has no marker: this guard sees nothing") |
| 92 | } |
| 93 | if out, err := exec.Command("../../scripts/no-fake-worker.sh", archive).CombinedOutput(); err == nil { |
| 94 | t.Fatalf("the release check accepted the fake worker: %s", out) |
| 95 | } |
| 96 | // The egress route to a fake provider (a real Pi against a fake |
| 97 | // TensorX) is in a test build of the relay and refused the same way. |
| 98 | egress := filepath.Join(dir, "egress.a") |
| 99 | test = exec.Command("go", "build", "-tags", "testbuild", "-o", egress, "../../internal/runner/egress") |
| 100 | test.Env = append(os.Environ(), "CGO_ENABLED=0") |
| 101 | testci.GoCommand(t, test) |
| 102 | if out, err := test.CombinedOutput(); err != nil { |
| 103 | t.Fatalf("test build: %v %s", err, out) |
| 104 | } |
| 105 | if raw, _ := os.ReadFile(egress); !bytes.Contains(raw, []byte(egressMarker)) { |
| 106 | t.Fatal("the test build has no egress route: this guard sees nothing") |
| 107 | } |
| 108 | if out, err := exec.Command("../../scripts/no-fake-worker.sh", egress).CombinedOutput(); err == nil { |
| 109 | t.Fatalf("the release check accepted the egress route: %s", out) |
| 110 | } |
| 111 | // The test build does have the Mac's endpoint, so the check above |
| 112 | // can see it. |
| 113 | proxy := filepath.Join(dir, "runner.a") |
| 114 | test = exec.Command("go", "build", "-tags", "testbuild", "-o", proxy, "../../internal/runner") |
| 115 | test.Env = append(os.Environ(), "CGO_ENABLED=0") |
| 116 | testci.GoCommand(t, test) |
| 117 | if out, err := test.CombinedOutput(); err != nil { |
| 118 | t.Fatalf("test build: %v %s", err, out) |
| 119 | } |
| 120 | if raw, _ := os.ReadFile(proxy); !bytes.Contains(raw, []byte(darwinProxyMarker)) { |
| 121 | t.Fatal("the test build has no Mac tool endpoint: this guard sees nothing") |
| 122 | } |
| 123 | } |
| 124 | |
| 125 | // darwinProxyMarker names a Mac's MCP tool endpoint (internal/runner |
| 126 | // tool_proxy_mac.go). |
| 127 | const darwinProxyMarker = "ServeDarwinToolProxy" |
| 128 | |
| 129 | // egressMarker names the test build's egress route (internal/runner/egress |
| 130 | // proxy_testbuild.go). |
| 131 | const egressMarker = "EQUIPO_TEST_EGRESS_UPSTREAM" |
| 132 | |
| 133 | // The release check refuses each test-build marker on its own: a binary |
| 134 | // that holds only one of them (and none of the others) is refused, and |
| 135 | // one holding none is accepted. |
| 136 | func TestReleaseCheckRefusesEachMarker(t *testing.T) { |
| 137 | testci.Files(t, "../../scripts/no-fake-worker.sh") |
| 138 | dir := t.TempDir() |
| 139 | check := func(name, content string) error { |
| 140 | path := filepath.Join(dir, name) |
| 141 | if err := os.WriteFile(path, []byte("\x7fELF padding "+content+" padding"), 0600); err != nil { |
| 142 | t.Fatal(err) |
| 143 | } |
| 144 | _, err := exec.Command("../../scripts/no-fake-worker.sh", path).CombinedOutput() |
| 145 | return err |
| 146 | } |
| 147 | if err := check("clean", "nothing of the test build"); err != nil { |
| 148 | t.Fatalf("a clean binary was refused: %v", err) |
| 149 | } |
| 150 | for name, m := range map[string]string{"fake worker": marker, "egress route": egressMarker} { |
| 151 | if err := check(strings.ReplaceAll(name, " ", "-"), m); err == nil { |
| 152 | t.Errorf("a binary with only the %s marker was accepted", name) |
| 153 | } |
| 154 | } |
| 155 | } |
| 156 | |