-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathintegration_test.go
More file actions
391 lines (318 loc) · 10.3 KB
/
integration_test.go
File metadata and controls
391 lines (318 loc) · 10.3 KB
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
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
//go:build integration
// +build integration
package sbox
import (
"bytes"
"fmt"
"os"
"os/exec"
"strings"
"testing"
"time"
)
// Integration tests for Docker profiles
// These tests actually build Docker images and are expensive to run.
// Run with: go test -tags=integration -v ./...
func TestIntegration_DockerAvailable(t *testing.T) {
cmd := exec.Command("docker", "version")
if err := cmd.Run(); err != nil {
t.Skip("Docker not available, skipping integration tests")
}
}
func TestIntegration_GoProfile(t *testing.T) {
if testing.Short() {
t.Skip("Skipping integration test in short mode")
}
testProfileBuild(t, "go", []string{
"go version",
}, []string{
"go version go",
})
}
func TestIntegration_RustProfile(t *testing.T) {
if testing.Short() {
t.Skip("Skipping integration test in short mode")
}
// Test basic commands and also rustup operations which require write access
// to /usr/local/rustup/tmp/ - this validates the chmod a+rwx fix
testProfileBuild(t, "rust", []string{
"rustc --version",
"cargo --version",
"rustup show", // This exercises rustup's write to tmp directory
}, []string{
"rustc",
"cargo",
"stable", // rustup show should display the stable toolchain
})
}
func TestIntegration_DockerProfile(t *testing.T) {
if testing.Short() {
t.Skip("Skipping integration test in short mode")
}
testProfileBuild(t, "docker", []string{
"docker --version",
}, []string{
"Docker version",
})
}
func TestIntegration_BashUtilsProfile(t *testing.T) {
if testing.Short() {
t.Skip("Skipping integration test in short mode")
}
testProfileBuild(t, "bash-utils", []string{
"jq --version",
"yq --version",
"git --version",
"curl --version",
}, []string{
"jq-",
"yq",
"git version",
"curl",
})
}
func TestIntegration_CppProfile(t *testing.T) {
if testing.Short() {
t.Skip("Skipping integration test in short mode")
}
testProfileBuild(t, "cpp", []string{
"g++ --version",
"make --version",
"autoconf --version",
"automake --version",
"libtool --version",
"ninja --version",
"pkg-config --exists zlib && echo zlib-ok",
"pkg-config --exists libzstd && echo zstd-ok",
}, []string{
"g++",
"GNU Make",
"autoconf",
"automake",
"libtool",
"", // ninja just outputs version number
"zlib-ok",
"zstd-ok",
})
}
// testProfileBuild builds a Docker image with the given profile and runs verification commands
// Commands are run as a non-root user to simulate the sandbox environment where user 'agent' runs
func testProfileBuild(t *testing.T, profileName string, commands []string, expectedOutputs []string) {
t.Helper()
profile, ok := GetProfile(profileName)
if !ok {
t.Fatalf("Profile %q not found", profileName)
}
// Create a temporary Dockerfile
tempDir := t.TempDir()
dockerfilePath := fmt.Sprintf("%s/Dockerfile", tempDir)
// The Dockerfile creates user 'agent' to match the sandbox environment.
// In the real sandbox, docker/sandbox-templates:claude-code runs as user 'agent'.
dockerfile := fmt.Sprintf(`FROM debian:bookworm-slim
# Install basic dependencies
RUN apt-get update && apt-get install -y ca-certificates && rm -rf /var/lib/apt/lists/*
%s
# Create 'agent' user to match the sandbox environment
RUN useradd -m -s /bin/bash agent
# Switch to agent user (same as sandbox)
USER agent
# Set default command
CMD ["bash"]
`, profile.DockerfileSnippet)
if err := os.WriteFile(dockerfilePath, []byte(dockerfile), 0644); err != nil {
t.Fatalf("Failed to write Dockerfile: %v", err)
}
// Build the image
imageName := fmt.Sprintf("sbox-test-%s:%d", profileName, time.Now().Unix())
t.Logf("Building image %s...", imageName)
buildCmd := exec.Command("docker", "build", "-t", imageName, "-f", dockerfilePath, tempDir)
buildCmd.Stdout = os.Stdout
buildCmd.Stderr = os.Stderr
if err := buildCmd.Run(); err != nil {
t.Fatalf("Failed to build Docker image: %v", err)
}
// Clean up image after test
defer func() {
cleanupCmd := exec.Command("docker", "rmi", "-f", imageName)
cleanupCmd.Run() // Ignore errors on cleanup
}()
// Run verification commands
for i, command := range commands {
t.Logf("Running verification: %s", command)
runCmd := exec.Command("docker", "run", "--rm", imageName, "bash", "-c", command)
var stdout, stderr bytes.Buffer
runCmd.Stdout = &stdout
runCmd.Stderr = &stderr
if err := runCmd.Run(); err != nil {
t.Errorf("Command %q failed: %v\nStderr: %s", command, err, stderr.String())
continue
}
output := stdout.String()
if i < len(expectedOutputs) {
if !strings.Contains(output, expectedOutputs[i]) {
t.Errorf("Command %q output %q does not contain expected %q", command, output, expectedOutputs[i])
}
}
}
}
func TestIntegration_MultipleProfiles(t *testing.T) {
if testing.Short() {
t.Skip("Skipping integration test in short mode")
}
// Test building an image with multiple profiles
profiles := []string{"go", "bash-utils"}
tempDir := t.TempDir()
dockerfilePath := fmt.Sprintf("%s/Dockerfile", tempDir)
var snippets strings.Builder
for _, name := range profiles {
profile, ok := GetProfile(name)
if !ok {
t.Fatalf("Profile %q not found", name)
}
snippets.WriteString(profile.DockerfileSnippet)
snippets.WriteString("\n")
}
dockerfile := fmt.Sprintf(`FROM debian:bookworm-slim
# Install basic dependencies
RUN apt-get update && apt-get install -y ca-certificates && rm -rf /var/lib/apt/lists/*
%s
CMD ["bash"]
`, snippets.String())
if err := os.WriteFile(dockerfilePath, []byte(dockerfile), 0644); err != nil {
t.Fatalf("Failed to write Dockerfile: %v", err)
}
imageName := fmt.Sprintf("sbox-test-multi:%d", time.Now().Unix())
t.Logf("Building multi-profile image %s...", imageName)
buildCmd := exec.Command("docker", "build", "-t", imageName, "-f", dockerfilePath, tempDir)
buildCmd.Stdout = os.Stdout
buildCmd.Stderr = os.Stderr
if err := buildCmd.Run(); err != nil {
t.Fatalf("Failed to build multi-profile Docker image: %v", err)
}
defer func() {
cleanupCmd := exec.Command("docker", "rmi", "-f", imageName)
cleanupCmd.Run()
}()
// Verify both go and jq are available
verifyCommands := []struct {
cmd string
contains string
}{
{"go version", "go version go"},
{"jq --version", "jq-"},
}
for _, v := range verifyCommands {
runCmd := exec.Command("docker", "run", "--rm", imageName, "bash", "-c", v.cmd)
var stdout bytes.Buffer
runCmd.Stdout = &stdout
if err := runCmd.Run(); err != nil {
t.Errorf("Command %q failed: %v", v.cmd, err)
continue
}
if !strings.Contains(stdout.String(), v.contains) {
t.Errorf("Command %q output %q does not contain %q", v.cmd, stdout.String(), v.contains)
}
}
}
func TestIntegration_SubstreamsProfile(t *testing.T) {
if testing.Short() {
t.Skip("Skipping integration test in short mode")
}
// Substreams profile has rust as a dependency, so we need to test the full
// resolved profile chain using the TemplateBuilder
config := &Config{}
builder := NewTemplateBuilder(config, []string{"substreams"}, DefaultAgent)
// Verify dependencies are resolved correctly
resolved := builder.ResolveProfiles()
if len(resolved) != 2 || resolved[0] != "rust" || resolved[1] != "substreams" {
t.Fatalf("Expected resolved profiles [rust, substreams], got %v", resolved)
}
// Build a test image with the full profile chain
tempDir := t.TempDir()
dockerfilePath := fmt.Sprintf("%s/Dockerfile", tempDir)
var snippets strings.Builder
for _, name := range resolved {
profile, ok := GetProfile(name)
if !ok {
t.Fatalf("Profile %q not found", name)
}
snippets.WriteString(fmt.Sprintf("# Profile: %s\n", name))
snippets.WriteString(profile.DockerfileSnippet)
snippets.WriteString("\n")
}
dockerfile := fmt.Sprintf(`FROM debian:bookworm-slim
# Install basic dependencies
RUN apt-get update && apt-get install -y ca-certificates && rm -rf /var/lib/apt/lists/*
%s
# Create 'agent' user to match the sandbox environment
RUN useradd -m -s /bin/bash agent
# Switch to agent user (same as sandbox)
USER agent
CMD ["bash"]
`, snippets.String())
if err := os.WriteFile(dockerfilePath, []byte(dockerfile), 0644); err != nil {
t.Fatalf("Failed to write Dockerfile: %v", err)
}
imageName := fmt.Sprintf("sbox-test-substreams:%d", time.Now().Unix())
t.Logf("Building substreams profile image %s...", imageName)
buildCmd := exec.Command("docker", "build", "-t", imageName, "-f", dockerfilePath, tempDir)
buildCmd.Stdout = os.Stdout
buildCmd.Stderr = os.Stderr
if err := buildCmd.Run(); err != nil {
t.Fatalf("Failed to build Docker image: %v", err)
}
// Clean up image after test
defer func() {
cleanupCmd := exec.Command("docker", "rmi", "-f", imageName)
cleanupCmd.Run()
}()
// Verify all tools are available
verifyCommands := []struct {
cmd string
contains string
}{
// Rust dependency tools
{"rustc --version", "rustc"},
{"cargo --version", "cargo"},
// Substreams tools
{"substreams --version", "substreams"},
{"firecore --version", "firecore"},
// Protobuf tools
{"buf --version", ""},
{"protoc --version", "libprotoc"},
}
for _, v := range verifyCommands {
t.Logf("Running verification: %s", v.cmd)
runCmd := exec.Command("docker", "run", "--rm", imageName, "bash", "-c", v.cmd)
var stdout, stderr bytes.Buffer
runCmd.Stdout = &stdout
runCmd.Stderr = &stderr
if err := runCmd.Run(); err != nil {
t.Errorf("Command %q failed: %v\nStdout: %s\nStderr: %s", v.cmd, err, stdout.String(), stderr.String())
continue
}
output := stdout.String() + stderr.String() // Some tools output version to stderr
if !strings.Contains(output, v.contains) {
t.Errorf("Command %q output %q does not contain expected %q", v.cmd, output, v.contains)
}
}
}
// TestIntegration_EndToEnd tests the full sbox workflow
func TestIntegration_EndToEnd(t *testing.T) {
if testing.Short() {
t.Skip("Skipping integration test in short mode")
}
// Check if docker sandbox is available
cmd := exec.Command("docker", "sandbox", "--help")
if err := cmd.Run(); err != nil {
t.Skip("Docker sandbox not available, skipping end-to-end test")
}
// This test would run the full sbox workflow but requires Docker Desktop with sandbox support
t.Log("Docker sandbox is available - end-to-end test would run here")
// In a real environment, we would:
// 1. Build sbox binary
// 2. Run sbox in a test workspace
// 3. Verify mounts are correct
// 4. Test sbox shell command
// 5. Test sbox clean command
}