| 1 | package main |
| 2 | |
| 3 | import ( |
| 4 | "bytes" |
| 5 | "context" |
| 6 | "log/slog" |
| 7 | "os" |
| 8 | "path/filepath" |
| 9 | "strings" |
| 10 | "testing" |
| 11 | ) |
| 12 | |
| 13 | func TestToolVerbClosed(t *testing.T) { |
| 14 | for _, args := range [][]string{nil, {"serve"}, {"tool", "secret"}, {"tool"}} { |
| 15 | var out, log bytes.Buffer |
| 16 | if run(context.Background(), args, strings.NewReader(""), &out, &log, func(string) string { return "/missing" }) != 1 || strings.Contains(log.String(), "secret") || out.Len() != 0 { |
| 17 | t.Fatal("unsafe tool failure", args) |
| 18 | } |
| 19 | } |
| 20 | } |
| 21 | |
| 22 | func TestRunnerServiceVerbs(t *testing.T) { |
| 23 | for _, verb := range []string{"run", "preflight", "enrol", "verify-installed"} { |
| 24 | var out, log bytes.Buffer |
| 25 | if run(context.Background(), []string{verb}, strings.NewReader(""), &out, &log, func(string) string { return "" }) != 1 { |
| 26 | t.Fatal("missing config accepted", verb) |
| 27 | } |
| 28 | } |
| 29 | var out, log bytes.Buffer |
| 30 | if run(context.Background(), []string{"version"}, strings.NewReader(""), &out, &log, func(string) string { return "" }) != 0 || out.Len() == 0 { |
| 31 | t.Fatal("version unavailable") |
| 32 | } |
| 33 | } |
| 34 | |
| 35 | func TestRunnerDiagnosticIsCredentialFree(t *testing.T) { |
| 36 | var out, log bytes.Buffer |
| 37 | code := run(context.Background(), []string{"run"}, strings.NewReader(""), &out, &log, func(string) string { return "https://user:secret@example.com" }) |
| 38 | if code != 1 || !strings.Contains(log.String(), "EQUIPO_ORIGIN") || !strings.Contains(log.String(), "runner.config.origin") || strings.Contains(log.String(), "secret") { |
| 39 | t.Fatal("missing or unsafe diagnostic", log.String()) |
| 40 | } |
| 41 | } |
| 42 | |
| 43 | // run's first line says what starts and where it connects: the version, |
| 44 | // the platform, the role, the origin's host alone, the machine's label |
| 45 | // and the env file it read. It comes before anything that could hang. |
| 46 | func TestRunLogsItsStart(t *testing.T) { |
| 47 | root, err := filepath.EvalSymlinks(t.TempDir()) |
| 48 | if err != nil { |
| 49 | t.Fatal(err) |
| 50 | } |
| 51 | vars := map[string]string{"EQUIPO_ORIGIN": "https://equipo.example.com", "EQUIPO_INSTANCE_ID": "i1", "EQUIPO_FAKE_PLATFORM": "darwin/arm64", "EQUIPO_MACHINE_LABEL": `"Test Mac"`, "EQUIPO_AMADAN_PATH": "/bin/sh"} |
| 52 | for _, v := range []string{"BWRAP_PATH", "CLAUDE_PATH", "CODEX_CODE_MODE_HOST_PATH", "RUNNER_STATE_DIR", "AMADAN_CREDENTIAL_PATH", "ACCOUNT_BINDINGS_PATH", "CODEX_PATH", "GO_ROOT", "GO_BINARY", "WORKSPACE_ROOT", "SCRATCH_ROOT", "GIT_PATH"} { |
| 53 | vars["EQUIPO_"+v] = filepath.Join(root, strings.ToLower(v)) |
| 54 | } |
| 55 | var lines []string |
| 56 | for k, v := range vars { |
| 57 | lines = append(lines, k+"="+v) |
| 58 | } |
| 59 | envFile := filepath.Join(root, "runner.env") |
| 60 | if err := os.WriteFile(envFile, []byte(strings.Join(lines, "\n")+"\n"), 0600); err != nil { |
| 61 | t.Fatal(err) |
| 62 | } |
| 63 | var out, log bytes.Buffer |
| 64 | // No credential in the state directory: the run ends at once, after |
| 65 | // its first line. |
| 66 | run(context.Background(), []string{"run", "--env-file", envFile}, strings.NewReader(""), &out, &log, func(string) string { return "" }) |
| 67 | first, _, _ := strings.Cut(log.String(), "\n") |
| 68 | for _, want := range []string{`level=INFO msg="runner starting" version=`, " os=" + goos() + " arch=" + goarch() + " role=worker origin=equipo.example.com", `label="Test Mac"`, "env_file=" + envFile} { |
| 69 | if !strings.Contains(first, want) { |
| 70 | t.Fatalf("start line lacks %q:\n%s", want, log.String()) |
| 71 | } |
| 72 | } |
| 73 | if strings.Contains(first, "https://") { |
| 74 | t.Fatalf("start line names more than the origin's host: %s", first) |
| 75 | } |
| 76 | } |
| 77 | |
| 78 | // The fetcher verb reads no configuration: it needs none, and the |
| 79 | // sandbox it runs in has no environment. |
| 80 | func TestWebreadVerbNeedsNoConfiguration(t *testing.T) { |
| 81 | var out, log bytes.Buffer |
| 82 | getenv := func(k string) string { t.Fatalf("webread read %s", k); return "" } |
| 83 | if code := run(context.Background(), []string{"webread"}, strings.NewReader(""), &out, &log, getenv); code != 2 || !strings.Contains(log.String(), "webread <outdir>") { |
| 84 | t.Fatalf("exit %d: %s", code, log.String()) |
| 85 | } |
| 86 | } |
| 87 | |
| 88 | // The render sandbox's first process reads no configuration either, and |
| 89 | // takes no arguments. |
| 90 | func TestRenderSandboxVerbNeedsNoConfiguration(t *testing.T) { |
| 91 | var out, log bytes.Buffer |
| 92 | getenv := func(k string) string { t.Fatalf("render-sandbox read %s", k); return "" } |
| 93 | if code := run(context.Background(), []string{"render-sandbox", "/site"}, strings.NewReader(""), &out, &log, getenv); code != 2 || out.Len() != 0 { |
| 94 | t.Fatalf("exit %d: %s", code, log.String()) |
| 95 | } |
| 96 | } |
| 97 | |
| 98 | // install-opencode takes one absolute, clean root and nothing else. |
| 99 | func TestInstallOpenCodeArguments(t *testing.T) { |
| 100 | for _, args := range [][]string{nil, {"relative"}, {"/a/../b"}, {"/a", "/b"}} { |
| 101 | var out, errout strings.Builder |
| 102 | if code := installOpenCode(context.Background(), args, &out, &errout); code != 2 || out.Len() != 0 || !strings.Contains(errout.String(), "install-opencode") { |
| 103 | t.Fatalf("%q: %d %q %q", args, code, out.String(), errout.String()) |
| 104 | } |
| 105 | } |
| 106 | } |
| 107 | |
| 108 | // The runner logs at info unless EQUIPO_RUNNER_LOG_LEVEL=debug asks for |
| 109 | // its debug lines too (an Assistant turn's phases among them). |
| 110 | func TestRunnerLogLevel(t *testing.T) { |
| 111 | for v, want := range map[string]slog.Level{"": slog.LevelInfo, "info": slog.LevelInfo, "verbose": slog.LevelInfo, "debug": slog.LevelDebug} { |
| 112 | o := logOptions(func(k string) string { |
| 113 | if k == "EQUIPO_RUNNER_LOG_LEVEL" { |
| 114 | return v |
| 115 | } |
| 116 | return "" |
| 117 | }) |
| 118 | if got := o.Level.Level(); got != want { |
| 119 | t.Errorf("EQUIPO_RUNNER_LOG_LEVEL=%q: level %v, want %v", v, got, want) |
| 120 | } |
| 121 | } |
| 122 | } |
| 123 | |