Skip to content

Commit 2eb8b2f

Browse files
authored
Merge pull request #2587 from norio-nomura/use-slices-Clone
`limayaml/defaults_test.go`: use `slices.Clone()` to create `expect` variable
2 parents 42b9595 + 621ee4c commit 2eb8b2f

File tree

1 file changed

+44
-36
lines changed

1 file changed

+44
-36
lines changed

pkg/limayaml/defaults_test.go

+44-36
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import (
66
"os"
77
"path/filepath"
88
"runtime"
9+
"slices"
910
"testing"
1011

1112
"github.com/google/go-cmp/cmp"
@@ -201,7 +202,7 @@ func TestFillDefault(t *testing.T) {
201202
"MY.Host": "host.lima.internal",
202203
}
203204

204-
expect.Mounts = y.Mounts
205+
expect.Mounts = slices.Clone(y.Mounts)
205206
expect.Mounts[0].MountPoint = expect.Mounts[0].Location
206207
expect.Mounts[0].Writable = ptr.Of(false)
207208
expect.Mounts[0].SSHFS.Cache = ptr.Of(true)
@@ -211,27 +212,27 @@ func TestFillDefault(t *testing.T) {
211212
expect.Mounts[0].NineP.ProtocolVersion = ptr.Of(Default9pProtocolVersion)
212213
expect.Mounts[0].NineP.Msize = ptr.Of(Default9pMsize)
213214
expect.Mounts[0].NineP.Cache = ptr.Of(Default9pCacheForRO)
214-
expect.Mounts[0].Virtiofs.QueueSize = ptr.Of(DefaultVirtiofsQueueSize)
215+
expect.Mounts[0].Virtiofs.QueueSize = nil
215216
// Only missing Mounts field is Writable, and the default value is also the null value: false
216217

217218
expect.MountType = ptr.Of(NINEP)
218219

219220
expect.MountInotify = ptr.Of(false)
220221

221-
expect.Provision = y.Provision
222+
expect.Provision = slices.Clone(y.Provision)
222223
expect.Provision[0].Mode = ProvisionModeSystem
223224
expect.Provision[0].Script = "#!/bin/true # Eins"
224225

225-
expect.Probes = y.Probes
226+
expect.Probes = slices.Clone(y.Probes)
226227
expect.Probes[0].Mode = ProbeModeReadiness
227228
expect.Probes[0].Description = "user probe 1/1"
228-
expect.Probes[0].Script = "#!/bin/true # Eins"
229+
expect.Probes[0].Script = "#!/bin/false # Eins"
229230

230-
expect.Networks = y.Networks
231+
expect.Networks = slices.Clone(y.Networks)
231232
expect.Networks[0].MACAddress = MACAddress(fmt.Sprintf("%s#%d", filePath, 0))
232233
expect.Networks[0].Interface = "lima0"
233234

234-
expect.DNS = y.DNS
235+
expect.DNS = slices.Clone(y.DNS)
235236
expect.PortForwards = []PortForward{
236237
defaultPortForward,
237238
defaultPortForward,
@@ -273,6 +274,7 @@ func TestFillDefault(t *testing.T) {
273274

274275
expect.TimeZone = y.TimeZone
275276
expect.Firmware = y.Firmware
277+
expect.Firmware.Images = slices.Clone(y.Firmware.Images)
276278

277279
expect.Rosetta = Rosetta{
278280
Enabled: ptr.Of(false),
@@ -410,7 +412,9 @@ func TestFillDefault(t *testing.T) {
410412

411413
expect = d
412414
// Also verify that archive arch is filled in
415+
expect.Containerd.Archives = slices.Clone(d.Containerd.Archives)
413416
expect.Containerd.Archives[0].Arch = *d.Arch
417+
expect.Mounts = slices.Clone(d.Mounts)
414418
expect.Mounts[0].MountPoint = expect.Mounts[0].Location
415419
expect.Mounts[0].SSHFS.Cache = ptr.Of(true)
416420
expect.Mounts[0].SSHFS.FollowSymlinks = ptr.Of(false)
@@ -419,7 +423,7 @@ func TestFillDefault(t *testing.T) {
419423
expect.Mounts[0].NineP.ProtocolVersion = ptr.Of(Default9pProtocolVersion)
420424
expect.Mounts[0].NineP.Msize = ptr.Of(Default9pMsize)
421425
expect.Mounts[0].NineP.Cache = ptr.Of(Default9pCacheForRO)
422-
expect.Mounts[0].Virtiofs.QueueSize = ptr.Of(DefaultVirtiofsQueueSize)
426+
expect.Mounts[0].Virtiofs.QueueSize = nil
423427
expect.HostResolver.Hosts = map[string]string{
424428
"default": d.HostResolver.Hosts["default"],
425429
}
@@ -447,6 +451,8 @@ func TestFillDefault(t *testing.T) {
447451
FillDefault(&y, &d, &LimaYAML{}, filePath)
448452
assert.DeepEqual(t, &y, &expect, opts...)
449453

454+
dExpect := expect
455+
450456
// ------------------------------------------------------------------------------------
451457
// User-provided defaults should not override user-provided config values
452458

@@ -456,26 +462,27 @@ func TestFillDefault(t *testing.T) {
456462

457463
expect = y
458464

459-
expect.Provision = append(append([]Provision{}, y.Provision...), d.Provision...)
460-
expect.Probes = append(append([]Probe{}, y.Probes...), d.Probes...)
461-
expect.PortForwards = append(append([]PortForward{}, y.PortForwards...), d.PortForwards...)
462-
expect.CopyToHost = append(append([]CopyToHost{}, y.CopyToHost...), d.CopyToHost...)
463-
expect.Containerd.Archives = append(append([]File{}, y.Containerd.Archives...), d.Containerd.Archives...)
464-
expect.AdditionalDisks = append(append([]Disk{}, y.AdditionalDisks...), d.AdditionalDisks...)
465-
expect.Firmware.Images = append(append([]FileWithVMType{}, y.Firmware.Images...), d.Firmware.Images...)
465+
expect.Provision = append(append([]Provision{}, y.Provision...), dExpect.Provision...)
466+
expect.Probes = append(append([]Probe{}, y.Probes...), dExpect.Probes...)
467+
expect.PortForwards = append(append([]PortForward{}, y.PortForwards...), dExpect.PortForwards...)
468+
expect.CopyToHost = append(append([]CopyToHost{}, y.CopyToHost...), dExpect.CopyToHost...)
469+
expect.Containerd.Archives = append(append([]File{}, y.Containerd.Archives...), dExpect.Containerd.Archives...)
470+
expect.Containerd.Archives[2].Arch = *expect.Arch
471+
expect.AdditionalDisks = append(append([]Disk{}, y.AdditionalDisks...), dExpect.AdditionalDisks...)
472+
expect.Firmware.Images = append(append([]FileWithVMType{}, y.Firmware.Images...), dExpect.Firmware.Images...)
466473

467474
// Mounts and Networks start with lowest priority first, so higher priority entries can overwrite
468-
expect.Mounts = append(append([]Mount{}, d.Mounts...), y.Mounts...)
469-
expect.Networks = append(append([]Network{}, d.Networks...), y.Networks...)
475+
expect.Mounts = append(append([]Mount{}, dExpect.Mounts...), y.Mounts...)
476+
expect.Networks = append(append([]Network{}, dExpect.Networks...), y.Networks...)
470477

471-
expect.HostResolver.Hosts["default"] = d.HostResolver.Hosts["default"]
478+
expect.HostResolver.Hosts["default"] = dExpect.HostResolver.Hosts["default"]
472479

473-
// d.DNS will be ignored, and not appended to y.DNS
480+
// dExpect.DNS will be ignored, and not appended to y.DNS
474481

475-
// "TWO" does not exist in filledDefaults.Env, so is set from d.Env
476-
expect.Env["TWO"] = d.Env["TWO"]
482+
// "TWO" does not exist in filledDefaults.Env, so is set from dExpect.Env
483+
expect.Env["TWO"] = dExpect.Env["TWO"]
477484

478-
expect.Param["TWO"] = d.Param["TWO"]
485+
expect.Param["TWO"] = dExpect.Param["TWO"]
479486

480487
t.Logf("d.vmType=%q, y.vmType=%q, expect.vmType=%q", *d.VMType, *y.VMType, *expect.VMType)
481488

@@ -621,19 +628,20 @@ func TestFillDefault(t *testing.T) {
621628

622629
expect = o
623630

624-
expect.Provision = append(append(o.Provision, y.Provision...), d.Provision...)
625-
expect.Probes = append(append(o.Probes, y.Probes...), d.Probes...)
626-
expect.PortForwards = append(append(o.PortForwards, y.PortForwards...), d.PortForwards...)
627-
expect.CopyToHost = append(append(o.CopyToHost, y.CopyToHost...), d.CopyToHost...)
628-
expect.Containerd.Archives = append(append(o.Containerd.Archives, y.Containerd.Archives...), d.Containerd.Archives...)
629-
expect.AdditionalDisks = append(append(o.AdditionalDisks, y.AdditionalDisks...), d.AdditionalDisks...)
630-
expect.Firmware.Images = append(append(o.Firmware.Images, y.Firmware.Images...), d.Firmware.Images...)
631+
expect.Provision = append(append(o.Provision, y.Provision...), dExpect.Provision...)
632+
expect.Probes = append(append(o.Probes, y.Probes...), dExpect.Probes...)
633+
expect.PortForwards = append(append(o.PortForwards, y.PortForwards...), dExpect.PortForwards...)
634+
expect.CopyToHost = append(append(o.CopyToHost, y.CopyToHost...), dExpect.CopyToHost...)
635+
expect.Containerd.Archives = append(append(o.Containerd.Archives, y.Containerd.Archives...), dExpect.Containerd.Archives...)
636+
expect.Containerd.Archives[3].Arch = *expect.Arch
637+
expect.AdditionalDisks = append(append(o.AdditionalDisks, y.AdditionalDisks...), dExpect.AdditionalDisks...)
638+
expect.Firmware.Images = append(append(o.Firmware.Images, y.Firmware.Images...), dExpect.Firmware.Images...)
631639

632-
expect.HostResolver.Hosts["default"] = d.HostResolver.Hosts["default"]
633-
expect.HostResolver.Hosts["MY.Host"] = d.HostResolver.Hosts["host.lima.internal"]
640+
expect.HostResolver.Hosts["default"] = dExpect.HostResolver.Hosts["default"]
641+
expect.HostResolver.Hosts["MY.Host"] = dExpect.HostResolver.Hosts["host.lima.internal"]
634642

635-
// o.Mounts just makes d.Mounts[0] writable because the Location matches
636-
expect.Mounts = append(append([]Mount{}, d.Mounts...), y.Mounts...)
643+
// o.Mounts just makes dExpect.Mounts[0] writable because the Location matches
644+
expect.Mounts = append(append([]Mount{}, dExpect.Mounts...), y.Mounts...)
637645
expect.Mounts[0].Writable = ptr.Of(true)
638646
expect.Mounts[0].SSHFS.Cache = ptr.Of(false)
639647
expect.Mounts[0].SSHFS.FollowSymlinks = ptr.Of(true)
@@ -646,12 +654,12 @@ func TestFillDefault(t *testing.T) {
646654
expect.MountType = ptr.Of(NINEP)
647655
expect.MountInotify = ptr.Of(true)
648656

649-
// o.Networks[1] is overriding the d.Networks[0].Lima entry for the "def0" interface
650-
expect.Networks = append(append(d.Networks, y.Networks...), o.Networks[0])
657+
// o.Networks[1] is overriding the dExpect.Networks[0].Lima entry for the "def0" interface
658+
expect.Networks = append(append(dExpect.Networks, y.Networks...), o.Networks[0])
651659
expect.Networks[0].Lima = o.Networks[1].Lima
652660

653661
// Only highest prio DNS are retained
654-
expect.DNS = o.DNS
662+
expect.DNS = slices.Clone(o.DNS)
655663

656664
// ONE remains from filledDefaults.Env; the rest are set from o
657665
expect.Env["ONE"] = y.Env["ONE"]

0 commit comments

Comments
 (0)