summaryrefslogtreecommitdiff
path: root/internal/sandbox/dockersandbox_test.go
blob: d95faa5f69e91978fe9a4bea52a15fb5485da974 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
package sandbox

import (
	"context"
	"os"
	"os/exec"
	"path/filepath"
	"strings"
	"testing"
)

// dockerAvailable reports whether a working Docker daemon is reachable,
// mirroring the check the task plan asked for ("confirm Docker actually IS
// available... via `docker info`"). Real container-lifecycle tests below
// skip gracefully when it is not, the same way
// TestNativeRunner_Run_ProjectDir_SandboxToolsAndPush skips when `git` isn't
// on PATH.
func dockerAvailable(t *testing.T) bool {
	t.Helper()
	if _, err := exec.LookPath("docker"); err != nil {
		return false
	}
	if err := exec.Command("docker", "info").Run(); err != nil {
		return false
	}
	return true
}

// --- Unit tests: exec.CommandContext mocked out, mirroring
// executor.ContainerRunner's Command-field mocking pattern, so these run
// without Docker and assert the argument construction / control flow in
// isolation. ---

// recordingCmds captures every (name, args) pair passed to DockerSandbox's
// cmd() so tests can assert on the docker invocations without a real
// daemon.
type recordingCmds struct {
	calls [][]string
	// containerID is returned as the (fake) stdout of the "docker run"
	// call, so ensureContainerLocked has something to parse.
	containerID string
}

func (r *recordingCmds) command(ctx context.Context, name string, arg ...string) *exec.Cmd {
	call := append([]string{name}, arg...)
	r.calls = append(r.calls, call)

	if name == "docker" && len(arg) > 0 && arg[0] == "run" {
		return exec.Command("echo", r.containerID)
	}
	if name == "git" && len(arg) > 0 && arg[0] == "clone" {
		dir := arg[len(arg)-1]
		if err := os.MkdirAll(dir, 0755); err != nil {
			return exec.Command("false")
		}
		return exec.Command("true")
	}
	return exec.Command("true")
}

func newMockedDockerSandbox() (*DockerSandbox, *recordingCmds) {
	rec := &recordingCmds{containerID: "fakecontainerid123"}
	s := NewDockerSandbox("test-image:latest", "")
	s.command = rec.command
	return s, rec
}

func TestDockerSandbox_GitClone_StartsContainer(t *testing.T) {
	s, rec := newMockedDockerSandbox()

	if err := s.GitClone(context.Background(), "https://example.com/repo.git", ""); err != nil {
		t.Fatalf("GitClone: %v", err)
	}
	defer os.RemoveAll(s.WorkDir())

	if s.WorkDir() == "" {
		t.Fatal("expected WorkDir to be set after GitClone")
	}

	var sawClone, sawRun bool
	for _, call := range rec.calls {
		if call[0] == "git" && len(call) > 1 && call[1] == "clone" {
			sawClone = true
		}
		if call[0] == "docker" && len(call) > 1 && call[1] == "run" {
			sawRun = true
			// Confirm the bind mount and uid/gid remap are present, mirroring
			// ContainerRunner.buildDockerArgs' --user/-v conventions.
			joined := strings.Join(call, " ")
			if !strings.Contains(joined, "--user=") {
				t.Errorf("docker run args missing --user=: %v", call)
			}
			if !strings.Contains(joined, s.WorkDir()+":/workspace") {
				t.Errorf("docker run args missing bind mount: %v", call)
			}
		}
	}
	if !sawClone {
		t.Error("expected a git clone invocation")
	}
	if !sawRun {
		t.Error("expected a docker run invocation")
	}
}

func TestDockerSandbox_RunBash_NoWorkingDirectory_Errors(t *testing.T) {
	s, _ := newMockedDockerSandbox()
	_, _, _, err := s.RunBash(context.Background(), "echo hi")
	if err == nil || !strings.Contains(err.Error(), "no sandbox working directory") {
		t.Errorf("expected 'no sandbox working directory' error, got %v", err)
	}
}

func TestDockerSandbox_RunBash_ReusesRunningContainer(t *testing.T) {
	s, rec := newMockedDockerSandbox()
	if err := s.GitClone(context.Background(), "https://example.com/repo.git", ""); err != nil {
		t.Fatalf("GitClone: %v", err)
	}
	defer os.RemoveAll(s.WorkDir())

	runCallsBefore := countDockerRun(rec.calls)

	if _, _, _, err := s.RunBash(context.Background(), "echo hi"); err != nil {
		t.Fatalf("RunBash: %v", err)
	}
	if _, _, _, err := s.RunBash(context.Background(), "echo bye"); err != nil {
		t.Fatalf("RunBash: %v", err)
	}

	if got := countDockerRun(rec.calls); got != runCallsBefore {
		t.Errorf("expected no additional 'docker run' calls once a container is running, went from %d to %d", runCallsBefore, got)
	}

	var execCount int
	for _, call := range rec.calls {
		if call[0] == "docker" && len(call) > 1 && call[1] == "exec" {
			execCount++
		}
	}
	if execCount != 2 {
		t.Errorf("expected 2 'docker exec' calls, got %d", execCount)
	}
}

func TestDockerSandbox_Cleanup_RemovesContainer(t *testing.T) {
	s, rec := newMockedDockerSandbox()
	if err := s.GitClone(context.Background(), "https://example.com/repo.git", ""); err != nil {
		t.Fatalf("GitClone: %v", err)
	}
	defer os.RemoveAll(s.WorkDir())

	if err := s.Cleanup(context.Background()); err != nil {
		t.Fatalf("Cleanup: %v", err)
	}

	var sawRM bool
	for _, call := range rec.calls {
		if call[0] == "docker" && len(call) >= 3 && call[1] == "rm" {
			sawRM = true
			joined := strings.Join(call, " ")
			if !strings.Contains(joined, "fakecontainerid123") {
				t.Errorf("expected docker rm to target the started container, got %v", call)
			}
		}
	}
	if !sawRM {
		t.Error("expected a 'docker rm' invocation")
	}

	// Cleanup is idempotent: a second call with no container running is a
	// no-op, not an error.
	if err := s.Cleanup(context.Background()); err != nil {
		t.Errorf("second Cleanup should be a no-op, got %v", err)
	}
}

func TestDockerSandbox_NewDockerSandbox_DefaultsImage(t *testing.T) {
	s := NewDockerSandbox("", "")
	if s.image != defaultSandboxImage {
		t.Errorf("expected default image %q, got %q", defaultSandboxImage, s.image)
	}
}

func countDockerRun(calls [][]string) int {
	n := 0
	for _, c := range calls {
		if c[0] == "docker" && len(c) > 1 && c[1] == "run" {
			n++
		}
	}
	return n
}

// --- Real integration test: exercises actual container lifecycle. Skips
// gracefully when Docker isn't available in the test environment, per the
// same convention executor.container_test.go's TestNativeRunner-style tests
// use for `git`. ---

// TestDockerSandbox_RealContainer_Lifecycle mirrors
// executor.TestNativeRunner_Run_ProjectDir_SandboxToolsAndPush's bare-origin-
// repo setup, but drives DockerSandbox directly (GitClone, WriteFile,
// RunBash, ReadFile, GitPush, Cleanup) against a real docker daemon, then
// confirms `docker ps -a` shows the container gone after Cleanup.
func TestDockerSandbox_RealContainer_Lifecycle(t *testing.T) {
	if !dockerAvailable(t) {
		t.Skip("docker not available in this environment")
	}
	if _, err := exec.LookPath("git"); err != nil {
		t.Skip("git not available")
	}

	// Bare origin repo so `git push` from the sandbox clone doesn't hit
	// receive.denyCurrentBranch.
	origin := t.TempDir()
	run := func(dir string, args ...string) {
		t.Helper()
		cmd := exec.Command("git", args...)
		cmd.Dir = dir
		cmd.Env = append(os.Environ(),
			"GIT_AUTHOR_NAME=test", "GIT_AUTHOR_EMAIL=test@example.com",
			"GIT_COMMITTER_NAME=test", "GIT_COMMITTER_EMAIL=test@example.com",
		)
		out, err := cmd.CombinedOutput()
		if err != nil {
			t.Fatalf("git %v: %v\n%s", args, err, out)
		}
	}
	run(origin, "init", "--bare", "-b", "main")

	seed := t.TempDir()
	run(seed, "init", "-b", "main")
	run(seed, "remote", "add", "origin", origin)
	if err := os.WriteFile(filepath.Join(seed, "README.md"), []byte("seed\n"), 0o644); err != nil {
		t.Fatal(err)
	}
	run(seed, "add", "-A")
	run(seed, "commit", "-m", "seed")
	run(seed, "push", "origin", "main")

	s := NewDockerSandbox("", "") // default image (agent-base)
	ctx := context.Background()

	if err := s.GitClone(ctx, origin, ""); err != nil {
		t.Fatalf("GitClone: %v", err)
	}
	workDir := s.WorkDir()
	containerID := s.containerID
	defer os.RemoveAll(workDir)
	defer s.Cleanup(ctx)

	if containerID == "" {
		t.Fatal("expected a running container after GitClone")
	}

	if err := s.WriteFile(ctx, "hello.txt", "hello from docker sandbox"); err != nil {
		t.Fatalf("WriteFile: %v", err)
	}

	content, err := s.ReadFile(ctx, "hello.txt")
	if err != nil {
		t.Fatalf("ReadFile: %v", err)
	}
	if content != "hello from docker sandbox" {
		t.Errorf("ReadFile: got %q", content)
	}

	stdout, stderr, exitCode, err := s.RunBash(ctx, "cat hello.txt && wc -l < hello.txt")
	if err != nil {
		t.Fatalf("RunBash: %v", err)
	}
	if exitCode != 0 {
		t.Fatalf("RunBash exit code: %d, stderr: %s", exitCode, stderr)
	}
	if !strings.Contains(stdout, "hello from docker sandbox") {
		t.Errorf("RunBash stdout: %q", stdout)
	}

	matches, err := s.Glob(ctx, "*.txt")
	if err != nil {
		t.Fatalf("Glob: %v", err)
	}
	if len(matches) != 1 || matches[0] != "hello.txt" {
		t.Errorf("Glob: want [hello.txt], got %v", matches)
	}

	if _, _, _, err := s.RunBash(ctx, "git add -A && git -c user.name=test -c user.email=test@example.com commit -m 'add hello'"); err != nil {
		t.Fatalf("RunBash (commit): %v", err)
	}

	if err := s.GitPush(ctx, ""); err != nil {
		t.Fatalf("GitPush: %v", err)
	}

	logOut, err := exec.Command("git", "-C", origin, "log", "--oneline", "main").CombinedOutput()
	if err != nil {
		t.Fatalf("git log on origin: %v\n%s", err, logOut)
	}
	if !strings.Contains(string(logOut), "add hello") {
		t.Errorf("expected pushed commit 'add hello' in origin log, got:\n%s", logOut)
	}

	if err := s.Cleanup(ctx); err != nil {
		t.Fatalf("Cleanup: %v", err)
	}

	psOut, err := exec.Command("docker", "ps", "-a", "-q", "-f", "id="+containerID).CombinedOutput()
	if err != nil {
		t.Fatalf("docker ps: %v\n%s", err, psOut)
	}
	if strings.TrimSpace(string(psOut)) != "" {
		t.Errorf("expected container %s to be gone after Cleanup, docker ps -a shows: %s", containerID, psOut)
	}
}

func TestDockerSandbox_RealContainer_GuardedRejectionDoesNotStartExtraContainer(t *testing.T) {
	if !dockerAvailable(t) {
		t.Skip("docker not available in this environment")
	}
	if _, err := exec.LookPath("git"); err != nil {
		t.Skip("git not available")
	}

	origin := t.TempDir()
	run := func(dir string, args ...string) {
		t.Helper()
		cmd := exec.Command("git", args...)
		cmd.Dir = dir
		cmd.Env = append(os.Environ(),
			"GIT_AUTHOR_NAME=test", "GIT_AUTHOR_EMAIL=test@example.com",
			"GIT_COMMITTER_NAME=test", "GIT_COMMITTER_EMAIL=test@example.com",
		)
		out, err := cmd.CombinedOutput()
		if err != nil {
			t.Fatalf("git %v: %v\n%s", args, err, out)
		}
	}
	run(origin, "init", "--bare", "-b", "main")
	seed := t.TempDir()
	run(seed, "init", "-b", "main")
	run(seed, "remote", "add", "origin", origin)
	if err := os.WriteFile(filepath.Join(seed, "README.md"), []byte("seed\n"), 0o644); err != nil {
		t.Fatal(err)
	}
	run(seed, "add", "-A")
	run(seed, "commit", "-m", "seed")
	run(seed, "push", "origin", "main")

	base := NewDockerSandbox("", "")
	g := &Guarded{Sandbox: base, Hooks: DefaultHooks()}
	ctx := context.Background()

	if err := g.GitClone(ctx, origin, ""); err != nil {
		t.Fatalf("GitClone: %v", err)
	}
	defer os.RemoveAll(g.WorkDir())
	defer g.Cleanup(ctx)

	// A guardrail rejection must not touch the real sandbox at all — the
	// dangerous command never reaches `docker exec`, and the write to a
	// protected path never reaches the container's filesystem.
	if _, _, _, err := g.RunBash(ctx, "rm -rf /"); err == nil {
		t.Fatal("expected rejection for rm -rf /")
	}
	if err := g.WriteFile(ctx, ".env", "SECRET=1"); err == nil {
		t.Fatal("expected rejection for write to .env")
	}
	if _, err := base.ReadFile(ctx, ".env"); err == nil {
		t.Error("expected .env to not exist in the container (write was rejected)")
	}

	// An allowed command still works normally through the guard.
	if _, _, exitCode, err := g.RunBash(ctx, "echo still-works"); err != nil || exitCode != 0 {
		t.Fatalf("expected allowed command to succeed, exitCode=%d err=%v", exitCode, err)
	}
}