|
| 1 | +package cli |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "errors" |
| 6 | + "fmt" |
| 7 | + "io" |
| 8 | + "os" |
| 9 | + "path/filepath" |
| 10 | + "reflect" |
| 11 | + "testing" |
| 12 | + |
| 13 | + "github.com/buildkite/cleanroom/internal/backend" |
| 14 | + "github.com/buildkite/cleanroom/internal/controlclient" |
| 15 | + "github.com/buildkite/cleanroom/internal/endpoint" |
| 16 | + cleanroomv1 "github.com/buildkite/cleanroom/internal/gen/cleanroom/v1" |
| 17 | +) |
| 18 | + |
| 19 | +func runAgentCodexWithCapture(cmd AgentCodexCommand, stdinData string, ctx runtimeContext) execOutcome { |
| 20 | + tmpDir, err := os.MkdirTemp("", "cleanroom-agent-codex-test-*") |
| 21 | + if err != nil { |
| 22 | + return execOutcome{cause: fmt.Errorf("create temp dir: %w", err)} |
| 23 | + } |
| 24 | + defer os.RemoveAll(tmpDir) |
| 25 | + |
| 26 | + stdoutPath := filepath.Join(tmpDir, "stdout.log") |
| 27 | + stderrPath := filepath.Join(tmpDir, "stderr.log") |
| 28 | + |
| 29 | + stdoutFile, err := os.Create(stdoutPath) |
| 30 | + if err != nil { |
| 31 | + return execOutcome{cause: fmt.Errorf("create stdout capture file: %w", err)} |
| 32 | + } |
| 33 | + defer stdoutFile.Close() |
| 34 | + |
| 35 | + stderrFile, err := os.Create(stderrPath) |
| 36 | + if err != nil { |
| 37 | + return execOutcome{cause: fmt.Errorf("create stderr capture file: %w", err)} |
| 38 | + } |
| 39 | + defer stderrFile.Close() |
| 40 | + |
| 41 | + stdinReader, stdinWriter, err := os.Pipe() |
| 42 | + if err != nil { |
| 43 | + return execOutcome{cause: fmt.Errorf("create stdin pipe: %w", err)} |
| 44 | + } |
| 45 | + if stdinData != "" { |
| 46 | + if _, err := io.WriteString(stdinWriter, stdinData); err != nil { |
| 47 | + return execOutcome{cause: fmt.Errorf("write stdin payload: %w", err)} |
| 48 | + } |
| 49 | + } |
| 50 | + _ = stdinWriter.Close() |
| 51 | + defer stdinReader.Close() |
| 52 | + |
| 53 | + oldStdin := os.Stdin |
| 54 | + oldStderr := os.Stderr |
| 55 | + os.Stdin = stdinReader |
| 56 | + os.Stderr = stderrFile |
| 57 | + defer func() { |
| 58 | + os.Stdin = oldStdin |
| 59 | + os.Stderr = oldStderr |
| 60 | + }() |
| 61 | + |
| 62 | + ctx.Stdout = stdoutFile |
| 63 | + runErr := cmd.Run(&ctx) |
| 64 | + |
| 65 | + if err := stdoutFile.Sync(); err != nil { |
| 66 | + return execOutcome{cause: fmt.Errorf("sync stdout capture: %w", err)} |
| 67 | + } |
| 68 | + if err := stderrFile.Sync(); err != nil { |
| 69 | + return execOutcome{cause: fmt.Errorf("sync stderr capture: %w", err)} |
| 70 | + } |
| 71 | + |
| 72 | + stdoutBytes, err := os.ReadFile(stdoutPath) |
| 73 | + if err != nil { |
| 74 | + return execOutcome{cause: fmt.Errorf("read stdout capture: %w", err)} |
| 75 | + } |
| 76 | + stderrBytes, err := os.ReadFile(stderrPath) |
| 77 | + if err != nil { |
| 78 | + return execOutcome{cause: fmt.Errorf("read stderr capture: %w", err)} |
| 79 | + } |
| 80 | + |
| 81 | + return execOutcome{ |
| 82 | + err: runErr, |
| 83 | + stdout: string(stdoutBytes), |
| 84 | + stderr: string(stderrBytes), |
| 85 | + } |
| 86 | +} |
| 87 | + |
| 88 | +func TestAgentCodexIntegrationStartsPersistentSandbox(t *testing.T) { |
| 89 | + var gotCommand []string |
| 90 | + adapter := &integrationAdapter{ |
| 91 | + runStreamFn: func(_ context.Context, req backend.RunRequest, stream backend.OutputStream) (*backend.RunResult, error) { |
| 92 | + if !req.TTY { |
| 93 | + return nil, errors.New("expected tty execution") |
| 94 | + } |
| 95 | + gotCommand = append([]string(nil), req.Command...) |
| 96 | + if stream.OnStdout != nil { |
| 97 | + stream.OnStdout([]byte("codex-ready\n")) |
| 98 | + } |
| 99 | + return &backend.RunResult{ |
| 100 | + RunID: req.RunID, |
| 101 | + ExitCode: 0, |
| 102 | + Stdout: "codex-ready\n", |
| 103 | + Message: "ok", |
| 104 | + }, nil |
| 105 | + }, |
| 106 | + } |
| 107 | + |
| 108 | + host, _ := startIntegrationServer(t, adapter) |
| 109 | + cwd := t.TempDir() |
| 110 | + outcome := runAgentCodexWithCapture(AgentCodexCommand{ |
| 111 | + clientFlags: clientFlags{Host: host}, |
| 112 | + Chdir: cwd, |
| 113 | + }, "", runtimeContext{ |
| 114 | + CWD: cwd, |
| 115 | + Loader: integrationLoader{}, |
| 116 | + }) |
| 117 | + |
| 118 | + if outcome.cause != nil { |
| 119 | + t.Fatalf("capture failure: %v", outcome.cause) |
| 120 | + } |
| 121 | + if outcome.err != nil { |
| 122 | + t.Fatalf("AgentCodexCommand.Run returned error: %v", outcome.err) |
| 123 | + } |
| 124 | + if got, want := gotCommand, []string{"codex"}; !reflect.DeepEqual(got, want) { |
| 125 | + t.Fatalf("unexpected command: got %v want %v", got, want) |
| 126 | + } |
| 127 | + |
| 128 | + ep, err := endpoint.Resolve(host) |
| 129 | + if err != nil { |
| 130 | + t.Fatalf("resolve endpoint: %v", err) |
| 131 | + } |
| 132 | + client, err := controlclient.New(ep) |
| 133 | + if err != nil { |
| 134 | + t.Fatalf("create control client: %v", err) |
| 135 | + } |
| 136 | + listResp, err := client.ListSandboxes(context.Background(), &cleanroomv1.ListSandboxesRequest{}) |
| 137 | + if err != nil { |
| 138 | + t.Fatalf("ListSandboxes returned error: %v", err) |
| 139 | + } |
| 140 | + if got, want := len(listResp.GetSandboxes()), 1; got != want { |
| 141 | + t.Fatalf("unexpected sandbox count: got %d want %d", got, want) |
| 142 | + } |
| 143 | + if got, want := listResp.GetSandboxes()[0].GetStatus(), cleanroomv1.SandboxStatus_SANDBOX_STATUS_READY; got != want { |
| 144 | + t.Fatalf("unexpected sandbox status: got %v want %v", got, want) |
| 145 | + } |
| 146 | +} |
| 147 | + |
| 148 | +func TestAgentCodexIntegrationPassesArgsToCodex(t *testing.T) { |
| 149 | + var gotCommand []string |
| 150 | + adapter := &integrationAdapter{ |
| 151 | + runStreamFn: func(_ context.Context, req backend.RunRequest, stream backend.OutputStream) (*backend.RunResult, error) { |
| 152 | + gotCommand = append([]string(nil), req.Command...) |
| 153 | + return &backend.RunResult{ |
| 154 | + RunID: req.RunID, |
| 155 | + ExitCode: 0, |
| 156 | + Message: "ok", |
| 157 | + }, nil |
| 158 | + }, |
| 159 | + } |
| 160 | + |
| 161 | + host, _ := startIntegrationServer(t, adapter) |
| 162 | + cwd := t.TempDir() |
| 163 | + outcome := runAgentCodexWithCapture(AgentCodexCommand{ |
| 164 | + clientFlags: clientFlags{Host: host}, |
| 165 | + Chdir: cwd, |
| 166 | + Args: []string{"exec", "--yolo", "fix lint failures"}, |
| 167 | + }, "", runtimeContext{ |
| 168 | + CWD: cwd, |
| 169 | + Loader: integrationLoader{}, |
| 170 | + }) |
| 171 | + |
| 172 | + if outcome.cause != nil { |
| 173 | + t.Fatalf("capture failure: %v", outcome.cause) |
| 174 | + } |
| 175 | + if outcome.err != nil { |
| 176 | + t.Fatalf("AgentCodexCommand.Run returned error: %v", outcome.err) |
| 177 | + } |
| 178 | + if got, want := gotCommand, []string{"codex", "exec", "--yolo", "fix lint failures"}; !reflect.DeepEqual(got, want) { |
| 179 | + t.Fatalf("unexpected command: got %v want %v", got, want) |
| 180 | + } |
| 181 | +} |
0 commit comments