package main import ( "bytes" "os" "os/exec" "path/filepath" "regexp" "strings" "testing" "amadan.net/oficina/equipo/internal/testci" ) // marker is the fake worker's marker (internal/runner/thin // fakeworker_testbuild.go). const marker = "equipo-fake-worker-unsandboxed-process-group" // The test build's fake Mac worker runs checks without a sandbox, so it // must never be compiled into a release runner: built exactly as `make // release-runner` builds it, the binary does not contain it (and the // release recipe passes no build tags and checks the binary itself), // while a test build of the thin runner does, so this test can see it. func TestReleaseRunnerHasNoFakeWorker(t *testing.T) { makefile, err := os.ReadFile("../../Makefile") if err != nil { t.Fatal(err) } 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) if recipe == nil { t.Fatal("the release-runner recipe changed: build the runner without tags and check it with scripts/no-fake-worker.sh") } if strings.Contains(string(makefile[bytes.Index(makefile, []byte("release-runner:")):]), "-tags testbuild") { t.Fatal("a release recipe builds with the testbuild tag") } dir := t.TempDir() bin := filepath.Join(dir, "equipo-runner") // The Linux release, on whatever machine runs this. build := exec.Command("go", "build", "-trimpath", "-ldflags", "-s -w", "-o", bin, ".") build.Env = append(os.Environ(), "CGO_ENABLED=0", "GOOS=linux", "GOARCH=amd64") testci.GoCommand(t, build) testci.Files(t, "../../scripts/no-fake-worker.sh") if out, err := build.CombinedOutput(); err != nil { t.Fatalf("release build: %v %s", err, out) } raw, err := os.ReadFile(bin) if err != nil { t.Fatal(err) } if bytes.Contains(raw, []byte(marker)) { t.Fatal("the release runner contains the fake worker") } if bytes.Contains(raw, []byte(egressMarker)) { t.Fatal("the release runner contains the test build's egress route") } // A Mac's MCP endpoint is built for darwin and the test build: a // Linux release runs the certified equipo-tool-proxy instead. if bytes.Contains(raw, []byte(darwinProxyMarker)) { t.Fatal("the Linux release runner contains the Mac's tool endpoint") } if out, err := exec.Command("../../scripts/no-fake-worker.sh", bin).CombinedOutput(); err != nil { t.Fatalf("the release check refused a clean binary: %s", out) } // The Mac release (darwin/arm64, as make release-runner builds it) // has the Mac's endpoint and nothing of the test build. mac := filepath.Join(dir, "equipo-runner-darwin") build = exec.Command("go", "build", "-trimpath", "-ldflags", "-s -w", "-o", mac, ".") build.Env = append(os.Environ(), "CGO_ENABLED=0", "GOOS=darwin", "GOARCH=arm64") testci.GoCommand(t, build) if out, err := build.CombinedOutput(); err != nil { t.Fatalf("Mac release build: %v %s", err, out) } raw, err = os.ReadFile(mac) if err != nil { t.Fatal(err) } if !bytes.Contains(raw, []byte(darwinProxyMarker)) || bytes.Contains(raw, []byte(marker)) || bytes.Contains(raw, []byte(egressMarker)) { t.Fatal("the Mac release runner lacks its tool endpoint or holds the test build's code") } if out, err := exec.Command("../../scripts/no-fake-worker.sh", mac).CombinedOutput(); err != nil { t.Fatalf("the release check refused the Mac runner: %s", out) } archive := filepath.Join(dir, "thin.a") test := exec.Command("go", "build", "-tags", "testbuild", "-o", archive, "../../internal/runner/thin") test.Env = append(os.Environ(), "CGO_ENABLED=0") testci.GoCommand(t, test) if out, err := test.CombinedOutput(); err != nil { t.Fatalf("test build: %v %s", err, out) } if raw, _ := os.ReadFile(archive); !bytes.Contains(raw, []byte(marker)) { t.Fatal("the test build has no marker: this guard sees nothing") } if out, err := exec.Command("../../scripts/no-fake-worker.sh", archive).CombinedOutput(); err == nil { t.Fatalf("the release check accepted the fake worker: %s", out) } // The egress route to a fake provider (a real Pi against a fake // TensorX) is in a test build of the relay and refused the same way. egress := filepath.Join(dir, "egress.a") test = exec.Command("go", "build", "-tags", "testbuild", "-o", egress, "../../internal/runner/egress") test.Env = append(os.Environ(), "CGO_ENABLED=0") testci.GoCommand(t, test) if out, err := test.CombinedOutput(); err != nil { t.Fatalf("test build: %v %s", err, out) } if raw, _ := os.ReadFile(egress); !bytes.Contains(raw, []byte(egressMarker)) { t.Fatal("the test build has no egress route: this guard sees nothing") } if out, err := exec.Command("../../scripts/no-fake-worker.sh", egress).CombinedOutput(); err == nil { t.Fatalf("the release check accepted the egress route: %s", out) } // The test build does have the Mac's endpoint, so the check above // can see it. proxy := filepath.Join(dir, "runner.a") test = exec.Command("go", "build", "-tags", "testbuild", "-o", proxy, "../../internal/runner") test.Env = append(os.Environ(), "CGO_ENABLED=0") testci.GoCommand(t, test) if out, err := test.CombinedOutput(); err != nil { t.Fatalf("test build: %v %s", err, out) } if raw, _ := os.ReadFile(proxy); !bytes.Contains(raw, []byte(darwinProxyMarker)) { t.Fatal("the test build has no Mac tool endpoint: this guard sees nothing") } } // darwinProxyMarker names a Mac's MCP tool endpoint (internal/runner // tool_proxy_mac.go). const darwinProxyMarker = "ServeDarwinToolProxy" // egressMarker names the test build's egress route (internal/runner/egress // proxy_testbuild.go). const egressMarker = "EQUIPO_TEST_EGRESS_UPSTREAM" // The release check refuses each test-build marker on its own: a binary // that holds only one of them (and none of the others) is refused, and // one holding none is accepted. func TestReleaseCheckRefusesEachMarker(t *testing.T) { testci.Files(t, "../../scripts/no-fake-worker.sh") dir := t.TempDir() check := func(name, content string) error { path := filepath.Join(dir, name) if err := os.WriteFile(path, []byte("\x7fELF padding "+content+" padding"), 0600); err != nil { t.Fatal(err) } _, err := exec.Command("../../scripts/no-fake-worker.sh", path).CombinedOutput() return err } if err := check("clean", "nothing of the test build"); err != nil { t.Fatalf("a clean binary was refused: %v", err) } for name, m := range map[string]string{"fake worker": marker, "egress route": egressMarker} { if err := check(strings.ReplaceAll(name, " ", "-"), m); err == nil { t.Errorf("a binary with only the %s marker was accepted", name) } } }