Repository navigation
Expand file tree
/
Copy pathcreate_test.go
More file actions
571 lines (555 loc) · 15.9 KB
/
Copy pathcreate_test.go
File metadata and controls
571 lines (555 loc) · 15.9 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
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
package main
import (
"encoding/base64"
"flag"
"io"
"os"
"path/filepath"
"slices"
"strings"
"testing"
)
func TestResolveResources(t *testing.T) {
for _, tc := range []struct {
name string
settings string
vm string
argv []string
want resources
}{
{
name: "no settings, no flags",
vm: "myvm",
want: resources{cpus: 2, memory: 2, disk: 50},
},
{
name: "default block",
settings: `{"default": {"cpus": 3, "memory": 4, "disk": 55}}`,
vm: "myvm",
want: resources{cpus: 3, memory: 4, disk: 55},
},
{
name: "partial default block",
settings: `{"default": {"memory": 8}}`,
vm: "myvm",
want: resources{cpus: 2, memory: 8, disk: 50},
},
{
name: "vm block overrides the default block",
settings: `{"default": {"cpus": 8, "memory": 16, "disk": 100},
"vms": {"myvm": {"cpus": 4, "memory": 4}}}`,
vm: "myvm",
want: resources{cpus: 4, memory: 4, disk: 100},
},
{
name: "vm block applies to its own VM only",
settings: `{"default": {"cpus": 8, "memory": 16, "disk": 100},
"vms": {"other": {"cpus": 4}}}`,
vm: "myvm",
want: resources{cpus: 8, memory: 16, disk: 100},
},
{
name: "flag beats both blocks",
settings: `{"default": {"cpus": 3}, "vms": {"myvm": {"cpus": 4}}}`,
vm: "myvm",
argv: []string{"-cpus", "6"},
want: resources{cpus: 6, memory: 2, disk: 50},
},
{
name: "all flags",
vm: "myvm",
argv: []string{"-cpus", "8", "-memory", "16", "-disk", "100"},
want: resources{cpus: 8, memory: 16, disk: 100},
},
} {
t.Run(tc.name, func(t *testing.T) {
withSettings(t, tc.settings)
// Same wiring as cmdCreate: zero flag defaults, settings applied
// afterwards to whatever the command line left unset.
fs := flag.NewFlagSet("create", flag.ContinueOnError)
var flags resources
fs.IntVar(&flags.cpus, "cpus", 0, "")
fs.IntVar(&flags.memory, "memory", 0, "")
fs.IntVar(&flags.disk, "disk", 0, "")
if err := fs.Parse(tc.argv); err != nil {
t.Fatal(err)
}
got := resolveResources(flags, flagsSet(fs), loadSettings(tc.vm))
if got != tc.want {
t.Errorf("resolveResources() = %+v, want %+v", got, tc.want)
}
})
}
}
func TestResolveDotfiles(t *testing.T) {
for _, tc := range []struct {
name string
settings string
vm string
flag string
noDotfiles bool
want string
}{
{
name: "no settings",
vm: "myvm",
},
{
name: "default block",
settings: `{"default": {"dotfiles": "git@github.com:user/dotfiles.git"}}`,
vm: "myvm",
want: "git@github.com:user/dotfiles.git",
},
{
name: "vm block overrides the default block",
settings: `{"default": {"dotfiles": "git@github.com:user/dotfiles.git"},
"vms": {"myvm": {"dotfiles": "git@github.com:user/other.git"}}}`,
vm: "myvm",
want: "git@github.com:user/other.git",
},
{
name: "vm block turns dotfiles off",
settings: `{"default": {"dotfiles": "git@github.com:user/dotfiles.git"}, "vms": {"myvm": {"dotfiles": ""}}}`,
vm: "myvm",
},
{
name: "flag beats settings",
settings: `{"default": {"dotfiles": "git@github.com:user/dotfiles.git"}}`,
vm: "myvm",
flag: "git@github.com:user/flag.git",
want: "git@github.com:user/flag.git",
},
{
name: "no-dotfiles beats settings",
settings: `{"default": {"dotfiles": "git@github.com:user/dotfiles.git"}}`,
vm: "myvm",
noDotfiles: true,
},
} {
t.Run(tc.name, func(t *testing.T) {
withSettings(t, tc.settings)
got := resolveDotfiles(tc.flag, tc.noDotfiles, loadSettings(tc.vm))
if got != tc.want {
t.Errorf("resolveDotfiles() = %q, want %q", got, tc.want)
}
})
}
}
func TestResourceFlagRejectsNonInteger(t *testing.T) {
fs := flag.NewFlagSet("create", flag.ContinueOnError)
fs.SetOutput(io.Discard)
memory := 2
fs.IntVar(&memory, "memory", memory, "")
if err := fs.Parse([]string{"-memory", "8GiB"}); err == nil {
t.Error("Parse(-memory 8GiB) = nil, want error")
}
}
// withSettings points settingsFile at a temporary file for the test; empty
// content means no settings file at all.
func withSettings(t *testing.T, content string) {
t.Helper()
path := filepath.Join(t.TempDir(), "settings.json")
if content != "" {
if err := os.WriteFile(path, []byte(content), 0o600); err != nil {
t.Fatal(err)
}
}
old := settingsFile
settingsFile = path
t.Cleanup(func() { settingsFile = old })
}
func TestSettingsClones(t *testing.T) {
for _, tc := range []struct {
name string
settings string
vm string
want []cloneRepo
}{
{
name: "no settings",
vm: "myvm",
},
{
name: "settings without a clone key",
settings: `{"default": {"cpus": 4}}`,
vm: "myvm",
},
{
name: "one group, two repositories",
settings: `{"default": {"clone": [{"org": "robsonpeixoto",
"basedir": "${HOME}/Code/robsonpeixoto",
"repositories": ["dev-vm", "echo-server"]}]}}`,
vm: "myvm",
want: []cloneRepo{
{basedir: "${HOME}/Code/robsonpeixoto", repo: "robsonpeixoto/dev-vm"},
{basedir: "${HOME}/Code/robsonpeixoto", repo: "robsonpeixoto/echo-server"},
},
},
{
name: "two groups",
settings: `{"default": {"clone": [
{"org": "one", "basedir": "/srv/one", "repositories": ["a"]},
{"org": "two", "basedir": "/srv/two", "repositories": ["b"]}]}}`,
vm: "myvm",
want: []cloneRepo{
{basedir: "/srv/one", repo: "one/a"},
{basedir: "/srv/two", repo: "two/b"},
},
},
{
name: "group without repositories",
settings: `{"default": {"clone": [{"org": "one", "basedir": "/srv/one", "repositories": []}]}}`,
vm: "myvm",
},
{
name: "vm block replaces the default list",
settings: `{"default": {"clone": [{"org": "one", "basedir": "/srv/one", "repositories": ["a"]}]},
"vms": {"myvm": {"clone": [{"org": "two", "basedir": "/srv/two", "repositories": ["b"]}]}}}`,
vm: "myvm",
want: []cloneRepo{{basedir: "/srv/two", repo: "two/b"}},
},
{
name: "empty vm list clones nothing",
settings: `{"default": {"clone": [{"org": "one", "basedir": "/srv/one", "repositories": ["a"]}]},
"vms": {"myvm": {"clone": []}}}`,
vm: "myvm",
},
{
name: "vm block keeps the default list for other VMs",
settings: `{"default": {"clone": [{"org": "one", "basedir": "/srv/one", "repositories": ["a"]}]},
"vms": {"other": {"clone": []}}}`,
vm: "myvm",
want: []cloneRepo{{basedir: "/srv/one", repo: "one/a"}},
},
} {
t.Run(tc.name, func(t *testing.T) {
withSettings(t, tc.settings)
got := settingsClones(loadSettings(tc.vm))
if !slices.Equal(got, tc.want) {
t.Errorf("settingsClones() = %+v, want %+v", got, tc.want)
}
})
}
}
func TestCloneList(t *testing.T) {
clones := []cloneRepo{
{basedir: "${HOME}/Code/robsonpeixoto", repo: "robsonpeixoto/dev-vm"},
{basedir: "/srv/one", repo: "one/a"},
}
got := cloneList(clones)
want := "${HOME}/Code/robsonpeixoto\trobsonpeixoto/dev-vm\n/srv/one\tone/a\n"
if !strings.HasSuffix(got, want) {
t.Errorf("cloneList() = %q, want it to end with %q", got, want)
}
if !strings.HasPrefix(got, "#") {
t.Errorf("cloneList() = %q, want a comment header keeping the file non-empty", got)
}
if empty := cloneList(nil); !strings.HasPrefix(empty, "#") || strings.Count(empty, "\n") != 1 {
t.Errorf("cloneList(nil) = %q, want only the comment header", empty)
}
}
func TestMkcertEnabled(t *testing.T) {
for _, tc := range []struct {
name string
settings string
vm string
want bool
}{
{
name: "no settings",
vm: "myvm",
},
{
name: "default block",
settings: `{"default": {"mkcert": true}}`,
vm: "myvm",
want: true,
},
{
name: "vm block turns mkcert off",
settings: `{"default": {"mkcert": true}, "vms": {"myvm": {"mkcert": false}}}`,
vm: "myvm",
},
{
name: "vm block turns mkcert on",
settings: `{"vms": {"myvm": {"mkcert": true}}}`,
vm: "myvm",
want: true,
},
{
name: "vm block applies to its own VM only",
settings: `{"vms": {"other": {"mkcert": true}}}`,
vm: "myvm",
},
} {
t.Run(tc.name, func(t *testing.T) {
withSettings(t, tc.settings)
if got := mkcertEnabled(loadSettings(tc.vm)); got != tc.want {
t.Errorf("mkcertEnabled() = %v, want %v", got, tc.want)
}
})
}
}
func TestGhosttyEnabled(t *testing.T) {
for _, tc := range []struct {
name string
settings string
vm string
want bool
}{
{
name: "no settings",
vm: "myvm",
},
{
name: "default block",
settings: `{"default": {"ghostty": true}}`,
vm: "myvm",
want: true,
},
{
name: "vm block turns ghostty off",
settings: `{"default": {"ghostty": true}, "vms": {"myvm": {"ghostty": false}}}`,
vm: "myvm",
},
{
name: "vm block turns ghostty on",
settings: `{"vms": {"myvm": {"ghostty": true}}}`,
vm: "myvm",
want: true,
},
} {
t.Run(tc.name, func(t *testing.T) {
withSettings(t, tc.settings)
if got := ghosttyEnabled(loadSettings(tc.vm)); got != tc.want {
t.Errorf("ghosttyEnabled() = %v, want %v", got, tc.want)
}
})
}
}
func TestTerminfoB64(t *testing.T) {
if got := terminfoB64(""); got != "" {
t.Errorf("terminfoB64(\"\") = %q, want the empty string", got)
}
// The acsc capability of the real entry carries the braces that make the
// encoding necessary: Lima templates data content on the host.
src := "xterm-ghostty|ghostty,\n\tacsc=++\\,\\,--..00``zz{{||}}~~,\n"
got := terminfoB64(src)
if strings.Contains(got, "{{") {
t.Errorf("terminfoB64() = %q, want no Go template braces", got)
}
for line := range strings.Lines(got) {
if n := len(strings.TrimSuffix(line, "\n")); n > 76 {
t.Errorf("terminfoB64() line %q is %d characters, want at most 76", line, n)
}
}
decoded, err := base64.StdEncoding.DecodeString(strings.ReplaceAll(got, "\n", ""))
if err != nil {
t.Fatal(err)
}
if string(decoded) != src {
t.Errorf("decoded terminfoB64() = %q, want %q", decoded, src)
}
}
func TestStartSet(t *testing.T) {
res := resources{cpus: 4, memory: 8, disk: 100}
got := startSet(res, "git@github.com:user/dotfiles.git", "", false, nil)
want := `.cpus = 4 | .memory = "8GiB" | .disk = "100GiB" | .param.DOTFILES_REPO = "git@github.com:user/dotfiles.git"` +
` | .nestedVirtualization = false | .mounts = []`
if got != want {
t.Errorf("startSet() = %q, want %q", got, want)
}
// The CA path is the host one, quoted: the default CAROOT on macOS has a
// space in it.
got = startSet(res, "", "/Users/x/Library/Application Support/mkcert", false, nil)
want = strings.Replace(want, ` | .mounts = []`,
` | .caCerts.files = ["/Users/x/Library/Application Support/mkcert/rootCA.pem"] | .mounts = []`, 1)
want = strings.Replace(want, `"git@github.com:user/dotfiles.git"`, `""`, 1)
if got != want {
t.Errorf("startSet() with a CAROOT = %q, want %q", got, want)
}
got = startSet(res, "", "", true, nil)
if !strings.Contains(got, ".nestedVirtualization = true") {
t.Errorf("startSet() with nesting = %q, want .nestedVirtualization = true", got)
}
got = startSet(res, "", "", false, []mount{
{location: "~/Code", mountPoint: "{{.Home}}/Code", writable: true},
{location: "/Users/x/Application Support"},
})
wantMounts := ` | .mounts = [{"location": "~/Code", "mountPoint": "{{.Home}}/Code", "writable": true}, ` +
`{"location": "/Users/x/Application Support", "writable": false}]`
if !strings.HasSuffix(got, wantMounts) {
t.Errorf("startSet() with mounts = %q, want it to end with %q", got, wantMounts)
}
}
func TestSettingsMounts(t *testing.T) {
for _, tc := range []struct {
name string
settings string
vm string
want []mount
}{
{
name: "no settings",
vm: "myvm",
},
{
name: "all fields",
settings: `{"default": {"mounts": [
{"source": "/Users/x/Code", "destination": "/srv/code", "mode": "rw"}]}}`,
vm: "myvm",
want: []mount{{location: "/Users/x/Code", mountPoint: "/srv/code", writable: true}},
},
{
name: "destination and mode default to Lima's",
settings: `{"default": {"mounts": [{"source": "~/Code"}]}}`,
vm: "myvm",
want: []mount{{location: "~/Code"}},
},
{
name: "tilde destination is the guest home",
settings: `{"default": {"mounts": [
{"source": "~/Code", "destination": "~/Code", "mode": "ro"},
{"source": "~", "destination": "~"}]}}`,
vm: "myvm",
want: []mount{
{location: "~/Code", mountPoint: "{{.Home}}/Code"},
{location: "~", mountPoint: "{{.Home}}"},
},
},
{
name: "vm block replaces the default list",
settings: `{"default": {"mounts": [{"source": "/a"}]},
"vms": {"myvm": {"mounts": [{"source": "/b"}]}}}`,
vm: "myvm",
want: []mount{{location: "/b"}},
},
{
name: "empty vm list mounts nothing",
settings: `{"default": {"mounts": [{"source": "/a"}]},
"vms": {"myvm": {"mounts": []}}}`,
vm: "myvm",
},
} {
t.Run(tc.name, func(t *testing.T) {
withSettings(t, tc.settings)
got := settingsMounts(loadSettings(tc.vm))
if !slices.Equal(got, tc.want) {
t.Errorf("settingsMounts() = %+v, want %+v", got, tc.want)
}
})
}
}
func TestIsMountPath(t *testing.T) {
for _, tc := range []struct {
path string
want bool
}{
{path: "/Users/x/Code", want: true},
{path: "~", want: true},
{path: "~/Code", want: true},
{path: "~other/Code", want: false},
{path: "Code", want: false},
{path: "", want: false},
} {
t.Run(tc.path, func(t *testing.T) {
if got := isMountPath(tc.path); got != tc.want {
t.Errorf("isMountPath(%q) = %v, want %v", tc.path, got, tc.want)
}
})
}
}
func TestMountPathRE(t *testing.T) {
for _, tc := range []struct {
path string
want bool
}{
{path: "/Users/x/Library/Application Support", want: true},
{path: `/a"b`, want: false},
{path: `/a\b`, want: false},
{path: "/a/{{.Name}}", want: false},
{path: "/a\nb", want: false},
} {
t.Run(tc.path, func(t *testing.T) {
if got := mountPathRE.MatchString(tc.path); got != tc.want {
t.Errorf("mountPathRE.MatchString(%q) = %v, want %v", tc.path, got, tc.want)
}
})
}
}
func TestResolveNested(t *testing.T) {
for _, tc := range []struct {
name string
settings string
vm string
argv []string
want bool
}{
{
name: "no settings, no flag",
vm: "myvm",
},
{
name: "default block",
settings: `{"default": {"nested": true}}`,
vm: "myvm",
want: true,
},
{
name: "vm block turns nesting off",
settings: `{"default": {"nested": true}, "vms": {"myvm": {"nested": false}}}`,
vm: "myvm",
},
{
name: "vm block applies to its own VM only",
settings: `{"vms": {"other": {"nested": true}}}`,
vm: "myvm",
},
{
name: "flag alone",
vm: "myvm",
argv: []string{"-nested"},
want: true,
},
{
name: "flag beats the settings",
settings: `{"default": {"nested": true}}`,
vm: "myvm",
argv: []string{"-nested=false"},
},
} {
t.Run(tc.name, func(t *testing.T) {
withSettings(t, tc.settings)
fs := flag.NewFlagSet("create", flag.ContinueOnError)
var nested bool
fs.BoolVar(&nested, "nested", false, "")
if err := fs.Parse(tc.argv); err != nil {
t.Fatal(err)
}
if got := resolveNested(nested, flagsSet(fs), loadSettings(tc.vm)); got != tc.want {
t.Errorf("resolveNested() = %v, want %v", got, tc.want)
}
})
}
}
func TestNestedSupported(t *testing.T) {
for _, tc := range []struct {
brand string
want bool
}{
{brand: "Apple M1", want: false},
{brand: "Apple M2 Max", want: false},
{brand: "Apple M3", want: true},
{brand: "Apple M4 Pro", want: true},
{brand: "Apple M5 Pro", want: true},
{brand: "Apple M10", want: true},
{brand: "Intel(R) Core(TM) i9-9880H CPU @ 2.30GHz", want: false},
{brand: "", want: false},
} {
t.Run(tc.brand, func(t *testing.T) {
if got := nestedSupported(tc.brand); got != tc.want {
t.Errorf("nestedSupported(%q) = %v, want %v", tc.brand, got, tc.want)
}
})
}
}