-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcompose_test.go
More file actions
462 lines (418 loc) · 15.3 KB
/
Copy pathcompose_test.go
File metadata and controls
462 lines (418 loc) · 15.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
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
package main
import (
"os"
"path/filepath"
"strings"
"testing"
)
func TestBaseComposeArgs(t *testing.T) {
cfg := cbConfig{
Home: "/opt/back2base",
ConfigDir: "/etc/back2base",
StateDir: "/etc/back2base/state",
EnvFile: "/etc/back2base/env",
}
args := baseComposeArgs(cfg)
joined := strings.Join(args, " ")
if !strings.Contains(joined, "-f /opt/back2base/docker-compose.yml") {
t.Errorf("missing compose file flag: %v", args)
}
if !strings.Contains(joined, "--env-file /etc/back2base/env") {
t.Errorf("missing env-file flag: %v", args)
}
if !strings.Contains(joined, "--project-directory /opt/back2base") {
t.Errorf("missing project-directory flag: %v", args)
}
}
func TestBuildRunArgs(t *testing.T) {
cfg := cbConfig{
Home: "/opt/back2base",
ConfigDir: "/etc/back2base",
StateDir: "/etc/back2base/state",
EnvFile: "/etc/back2base/env",
}
args := buildRunArgs(cfg, runOpts{
extraDirs: []string{"/home/user/proj1", "/home/user/proj2"},
prompt: "write tests",
model: "claude-opus-4-6",
})
joined := strings.Join(args, " ")
if !strings.Contains(joined, "-v /home/user/proj1:/repos/proj1") {
t.Errorf("missing proj1 mount: %v", args)
}
if !strings.Contains(joined, "-v /home/user/proj2:/repos/proj2") {
t.Errorf("missing proj2 mount: %v", args)
}
if !strings.Contains(joined, "--mcp-config /home/node/.claude/.mcp.json") {
t.Errorf("missing mcp-config flag: %v", args)
}
if !strings.Contains(joined, "-p write tests") {
t.Errorf("missing prompt: %v", args)
}
if !strings.Contains(joined, "--model claude-opus-4-6") {
t.Errorf("missing model: %v", args)
}
if !strings.Contains(joined, "--add-dir /repos/proj1") {
t.Errorf("missing add-dir flag: %v", args)
}
}
func TestBuildRunArgsMinimal(t *testing.T) {
cfg := cbConfig{
Home: "/opt/back2base",
ConfigDir: "/etc/back2base",
StateDir: "/etc/back2base/state",
EnvFile: "/etc/back2base/env",
}
args := buildRunArgs(cfg, runOpts{})
joined := strings.Join(args, " ")
if strings.Contains(joined, " -p ") {
t.Errorf("unexpected -p flag: %v", args)
}
if strings.Contains(joined, "--model") {
t.Errorf("unexpected --model flag: %v", args)
}
if strings.Contains(joined, "--add-dir") {
t.Errorf("unexpected --add-dir flag: %v", args)
}
if !strings.Contains(joined, "--permission-mode bypassPermissions") {
t.Errorf("missing permission-mode: %v", args)
}
}
func TestBuildRunArgs_ResumeIDAppendsFlag(t *testing.T) {
cfg := cbConfig{
Home: "/opt/back2base", ConfigDir: "/c", StateDir: "/c/state", EnvFile: "/c/env",
}
args := buildRunArgs(cfg, runOpts{resumeID: "abc-123"})
joined := strings.Join(args, " ")
if !strings.Contains(joined, "--resume abc-123") {
t.Fatalf("expected --resume abc-123 in args: %v", args)
}
idxResume := strings.Index(joined, "--resume")
idxSkip := strings.Index(joined, "--dangerously-skip-permissions")
if idxResume < idxSkip {
t.Fatalf("--resume should follow --dangerously-skip-permissions: %s", joined)
}
}
func TestBuildRunArgs_NoResumeIDNoFlag(t *testing.T) {
cfg := cbConfig{Home: "/opt/back2base", ConfigDir: "/c", StateDir: "/c/state", EnvFile: "/c/env"}
args := buildRunArgs(cfg, runOpts{})
for _, a := range args {
if a == "--resume" {
t.Fatalf("unexpected --resume flag: %v", args)
}
}
}
func TestBuildRunArgs_ResumeIDAndModelOrder(t *testing.T) {
cfg := cbConfig{Home: "/opt/back2base", ConfigDir: "/c", StateDir: "/c/state", EnvFile: "/c/env"}
args := buildRunArgs(cfg, runOpts{resumeID: "abc-123", model: "claude-opus-4-7"})
// Both flags present.
var idxResume, idxModel = -1, -1
for i, a := range args {
switch a {
case "--resume":
idxResume = i
case "--model":
idxModel = i
}
}
if idxResume == -1 {
t.Fatalf("missing --resume in args: %v", args)
}
if idxModel == -1 {
t.Fatalf("missing --model in args: %v", args)
}
// --resume before --model: matches the build order (resume is appended
// right after --dangerously-skip-permissions, model is appended after).
if idxResume > idxModel {
t.Fatalf("--resume should come before --model: %v", args)
}
// Each flag must be followed by its value.
if args[idxResume+1] != "abc-123" {
t.Fatalf("--resume not followed by id: %v", args)
}
if args[idxModel+1] != "claude-opus-4-7" {
t.Fatalf("--model not followed by value: %v", args)
}
}
// TestWriteHostCredsOverride_NoDirsReturnsEmpty: when none of the host
// config dirs exist, the override is not written and the function
// returns an empty string so callers fall back to no override.
func TestWriteHostCredsOverride_NoDirsReturnsEmpty(t *testing.T) {
tmpHome := t.TempDir()
tmpState := t.TempDir()
t.Setenv("HOME", tmpHome)
cfg := cbConfig{StateDir: tmpState}
got := writeHostCredsOverride(cfg)
if got != "" {
t.Fatalf("expected empty string when no host dirs exist, got %q", got)
}
if _, err := os.Stat(filepath.Join(tmpState, "run", "host-creds-override.yml")); !os.IsNotExist(err) {
t.Fatalf("override file should not exist: err=%v", err)
}
}
// TestWriteHostCredsOverride_PartialIncludesOnlyExisting: only the
// host dirs that actually exist get bind mounts in the generated
// override file.
func TestWriteHostCredsOverride_PartialIncludesOnlyExisting(t *testing.T) {
tmpHome := t.TempDir()
tmpState := t.TempDir()
if err := os.MkdirAll(filepath.Join(tmpHome, ".aws"), 0o700); err != nil {
t.Fatal(err)
}
if err := os.MkdirAll(filepath.Join(tmpHome, ".config", "gh"), 0o700); err != nil {
t.Fatal(err)
}
// .kube intentionally absent.
t.Setenv("HOME", tmpHome)
cfg := cbConfig{StateDir: tmpState}
path := writeHostCredsOverride(cfg)
if path == "" {
t.Fatal("expected non-empty override path when host dirs exist")
}
body, err := os.ReadFile(path)
if err != nil {
t.Fatalf("read override: %v", err)
}
s := string(body)
if !strings.Contains(s, ".aws:/home/node/.aws-host:ro") {
t.Errorf("missing aws mount: %s", s)
}
if !strings.Contains(s, ".config/gh:/home/node/.config/gh-host:ro") {
t.Errorf("missing gh mount: %s", s)
}
if strings.Contains(s, "kube-host") {
t.Errorf("kube mount should be absent: %s", s)
}
}
// TestWriteHostCredsOverride_FileOnlyIsIgnored: a regular file at
// ~/.aws (not a dir) does not produce a bind mount.
func TestWriteHostCredsOverride_FileOnlyIsIgnored(t *testing.T) {
tmpHome := t.TempDir()
tmpState := t.TempDir()
if err := os.WriteFile(filepath.Join(tmpHome, ".aws"), []byte("x"), 0o600); err != nil {
t.Fatal(err)
}
t.Setenv("HOME", tmpHome)
cfg := cbConfig{StateDir: tmpState}
got := writeHostCredsOverride(cfg)
if got != "" {
t.Fatalf("expected empty when ~/.aws is a file, got %q", got)
}
}
// TestBuildRunArgs_AppendsHostCredsOverrideWhenPresent: when host
// creds dirs exist, buildRunArgs threads the generated override into
// the compose command via an extra -f flag, AFTER the base compose
// file.
func TestBuildRunArgs_AppendsHostCredsOverrideWhenPresent(t *testing.T) {
tmpHome := t.TempDir()
if err := os.MkdirAll(filepath.Join(tmpHome, ".kube"), 0o700); err != nil {
t.Fatal(err)
}
t.Setenv("HOME", tmpHome)
tmpState := t.TempDir()
cfg := cbConfig{
Home: "/opt/back2base",
ConfigDir: "/etc/back2base",
StateDir: tmpState,
EnvFile: "/etc/back2base/env",
}
args := buildRunArgs(cfg, runOpts{})
joined := strings.Join(args, " ")
overridePath := filepath.Join(tmpState, "run", "host-creds-override.yml")
if !strings.Contains(joined, "-f "+overridePath) {
t.Fatalf("expected -f %s in args: %v", overridePath, args)
}
idxBase := strings.Index(joined, "/opt/back2base/docker-compose.yml")
idxOverride := strings.Index(joined, overridePath)
if idxBase < 0 || idxOverride < 0 || idxOverride < idxBase {
t.Fatalf("override -f must follow base compose -f: %s", joined)
}
}
// TestWriteManagedSettingsOverride_NoDirReturnsEmpty: when the host
// managed-settings dir doesn't exist (or is empty of the three known
// artifacts), no override is written.
func TestWriteManagedSettingsOverride_NoDirReturnsEmpty(t *testing.T) {
t.Setenv("BACK2BASE_MANAGED_SETTINGS_DIR", filepath.Join(t.TempDir(), "does-not-exist"))
cfg := cbConfig{StateDir: t.TempDir()}
if got := writeManagedSettingsOverride(cfg); got != "" {
t.Fatalf("expected empty when dir is absent, got %q", got)
}
}
// TestWriteManagedSettingsOverride_PartialIncludesOnlyExisting: only the
// managed-settings artifacts that actually exist on the host get bind
// mounts; missing ones are silently skipped.
func TestWriteManagedSettingsOverride_PartialIncludesOnlyExisting(t *testing.T) {
hostDir := t.TempDir()
// managed-settings.json present, CLAUDE.md present, managed-settings.d absent.
if err := os.WriteFile(filepath.Join(hostDir, "managed-settings.json"), []byte(`{}`), 0o644); err != nil {
t.Fatal(err)
}
if err := os.WriteFile(filepath.Join(hostDir, "CLAUDE.md"), []byte("# policy"), 0o644); err != nil {
t.Fatal(err)
}
t.Setenv("BACK2BASE_MANAGED_SETTINGS_DIR", hostDir)
cfg := cbConfig{StateDir: t.TempDir()}
path := writeManagedSettingsOverride(cfg)
if path == "" {
t.Fatal("expected non-empty override path when artifacts exist")
}
data, err := os.ReadFile(path)
if err != nil {
t.Fatalf("read override: %v", err)
}
body := string(data)
if !strings.Contains(body, "/etc/claude-code/managed-settings.json") {
t.Errorf("override missing managed-settings.json mount target:\n%s", body)
}
if !strings.Contains(body, "/etc/claude-code/CLAUDE.md") {
t.Errorf("override missing CLAUDE.md mount target:\n%s", body)
}
if strings.Contains(body, "managed-settings.d") {
t.Errorf("override unexpectedly references managed-settings.d (host dir absent):\n%s", body)
}
if !strings.Contains(body, ":ro") {
t.Errorf("override mount must be read-only:\n%s", body)
}
}
// TestBuildRunArgs_AppendsManagedSettingsOverrideWhenPresent: when host
// managed-settings artifacts exist, buildRunArgs threads the generated
// override into the compose command alongside (and after) the base
// compose file.
func TestBuildRunArgs_AppendsManagedSettingsOverrideWhenPresent(t *testing.T) {
hostDir := t.TempDir()
if err := os.WriteFile(filepath.Join(hostDir, "managed-settings.json"), []byte(`{"permissions":{"deny":["Bash"]}}`), 0o644); err != nil {
t.Fatal(err)
}
t.Setenv("BACK2BASE_MANAGED_SETTINGS_DIR", hostDir)
// HOME isolation so the host-creds probe doesn't add unrelated overrides.
t.Setenv("HOME", t.TempDir())
tmpState := t.TempDir()
cfg := cbConfig{
Home: "/opt/back2base",
ConfigDir: "/etc/back2base",
StateDir: tmpState,
EnvFile: "/etc/back2base/env",
}
args := buildRunArgs(cfg, runOpts{})
joined := strings.Join(args, " ")
overridePath := filepath.Join(tmpState, "run", "managed-settings-override.yml")
if !strings.Contains(joined, "-f "+overridePath) {
t.Fatalf("expected -f %s in args: %v", overridePath, args)
}
idxBase := strings.Index(joined, "/opt/back2base/docker-compose.yml")
idxOverride := strings.Index(joined, overridePath)
if idxBase < 0 || idxOverride < 0 || idxOverride < idxBase {
t.Fatalf("managed-settings override -f must follow base compose -f: %s", joined)
}
}
func TestWriteDataDirOverride(t *testing.T) {
state := t.TempDir()
data := t.TempDir()
cfg := cbConfig{StateDir: state, EnvFile: filepath.Join(state, "noenv")}
// writeDataDirOverride logs to stderr on both the success and skip paths;
// silence it so `go test` output stays clean.
origStderr := os.Stderr
devnull, _ := os.OpenFile(os.DevNull, os.O_WRONLY, 0)
os.Stderr = devnull
defer func() { os.Stderr = origStderr; devnull.Close() }()
// Unset → no override, current behavior preserved.
t.Setenv("BACK2BASE_DATA_DIR", "")
if p := writeDataDirOverride(cfg); p != "" {
t.Fatalf("expected no override when unset, got %q", p)
}
// Set to an existing dir → override with both mounts; subdirs created.
t.Setenv("BACK2BASE_DATA_DIR", data)
p := writeDataDirOverride(cfg)
if p == "" {
t.Fatal("expected an override path, got empty")
}
body, err := os.ReadFile(p)
if err != nil {
t.Fatalf("read override: %v", err)
}
s := string(body)
if !strings.Contains(s, filepath.Join(data, "memories")) ||
!strings.Contains(s, "/home/node/.back2base/memories") {
t.Errorf("missing memories mount:\n%s", s)
}
if !strings.Contains(s, filepath.Join(data, "plans")) ||
!strings.Contains(s, "/home/node/.back2base/plans") {
t.Errorf("missing plans mount:\n%s", s)
}
if _, err := os.Stat(filepath.Join(data, "plans")); err != nil {
t.Errorf("plans subdir not created: %v", err)
}
if _, err := os.Stat(filepath.Join(data, "memories")); err != nil {
t.Errorf("memories subdir not created: %v", err)
}
if strings.Contains(s, ":ro") {
t.Errorf("data-dir override must be read-write (got :ro):\n%s", s)
}
if strings.Contains(s, "\"") {
t.Errorf("override YAML must not contain double-quoted paths (breaks compose parsing):\n%s", s)
}
// OSS has no cloud sync — the override must NOT carry an environment block.
if strings.Contains(s, "environment:") {
t.Errorf("OSS override should not set environment:\n%s", s)
}
// Set to a non-existent path → no override (warning path).
t.Setenv("BACK2BASE_DATA_DIR", filepath.Join(data, "nope"))
if p := writeDataDirOverride(cfg); p != "" {
t.Errorf("expected no override for missing dir, got %q", p)
}
// Existing dir whose name contains a newline → rejected (a raw newline
// would corrupt the generated override YAML).
if nlDir := filepath.Join(data, "bad\nname"); os.MkdirAll(nlDir, 0o700) == nil {
t.Setenv("BACK2BASE_DATA_DIR", nlDir)
if p := writeDataDirOverride(cfg); p != "" {
t.Errorf("expected no override for newline path, got %q", p)
}
}
}
// TestManagedSettingsOverrideYAMLValid: the generated YAML must NOT contain the
// per-half-quoted form ("src":"dst":ro) which breaks docker compose config, and
// the host path — including any spaces — must survive intact in the override.
func TestManagedSettingsOverrideYAMLValid(t *testing.T) {
// Create a host dir with a space in its path (mirrors macOS production path).
base := t.TempDir()
hostDir := filepath.Join(base, "App Support")
if err := os.MkdirAll(hostDir, 0o755); err != nil {
t.Fatal(err)
}
if err := os.WriteFile(filepath.Join(hostDir, "managed-settings.json"), []byte(`{}`), 0o644); err != nil {
t.Fatal(err)
}
t.Setenv("BACK2BASE_MANAGED_SETTINGS_DIR", hostDir)
cfg := cbConfig{StateDir: t.TempDir()}
path := writeManagedSettingsOverride(cfg)
if path == "" {
t.Fatal("expected override path, got empty")
}
data, err := os.ReadFile(path)
if err != nil {
t.Fatalf("read override: %v", err)
}
body := string(data)
// The broken per-half-quoted pattern is a quote immediately followed by a
// colon immediately followed by another quote: ":"
if strings.Contains(body, `":"`) {
t.Errorf("override contains per-half-quoted volume pattern (bug: %%q:%%q:ro form):\n%s", body)
}
// The space-containing source path must appear verbatim in the YAML body.
src := filepath.Join(hostDir, "managed-settings.json")
if !strings.Contains(body, src) {
t.Errorf("override does not contain source path %q:\n%s", src, body)
}
}
func TestResolveDataDirEnvFileFallback(t *testing.T) {
dir := t.TempDir()
envFile := filepath.Join(dir, "env")
if err := os.WriteFile(envFile, []byte("BACK2BASE_DATA_DIR="+dir+"\n"), 0o600); err != nil {
t.Fatalf("write env file: %v", err)
}
cfg := cbConfig{StateDir: dir, EnvFile: envFile}
t.Setenv("BACK2BASE_DATA_DIR", "") // process env empty → must fall back to the file
if got := resolveDataDir(cfg); got != dir {
t.Errorf("resolveDataDir: want %q from env file, got %q", dir, got)
}
}