From cf1f3191d2be691b482ac86f9476c180f608ddbe Mon Sep 17 00:00:00 2001 From: Valentin Rothberg Date: Tue, 14 Jan 2020 09:53:02 +0100 Subject: make lint: include unit tests Include the unit tests (i.e., _test.go files) for linting to make the tests more robust and enforce the linters' coding styles etc. Signed-off-by: Valentin Rothberg --- Makefile | 2 +- cmd/podman/common_test.go | 15 --- cmd/podman/remoteclientconfig/configfile_test.go | 39 ++++--- libpod/container_internal_test.go | 3 +- libpod/image/image_test.go | 12 +- libpod/lock/file/file_lock_test.go | 4 +- libpod/state_test.go | 137 +++++++++++++++-------- pkg/apparmor/apparmor_linux_test.go | 2 +- pkg/hooks/1.0.0/when_test.go | 30 +++-- pkg/hooks/exec/exec_test.go | 6 +- pkg/hooks/exec/runtimeconfigfilter_test.go | 5 +- pkg/network/network_test.go | 5 +- pkg/network/subnet_test.go | 13 ++- pkg/systemdgen/systemdgen_test.go | 18 +-- 14 files changed, 170 insertions(+), 121 deletions(-) delete mode 100644 cmd/podman/common_test.go diff --git a/Makefile b/Makefile index bfcbce1e1..06b6fd03d 100644 --- a/Makefile +++ b/Makefile @@ -151,7 +151,7 @@ endif lint: golangci-lint golangci-lint: .gopathok varlink_generate .install.golangci-lint - $(GOBIN)/golangci-lint run --tests=false + $(GOBIN)/golangci-lint run gofmt: ## Verify the source code gofmt find . -name '*.go' ! -path './vendor/*' -exec gofmt -s -w {} \+ diff --git a/cmd/podman/common_test.go b/cmd/podman/common_test.go deleted file mode 100644 index a24173003..000000000 --- a/cmd/podman/common_test.go +++ /dev/null @@ -1,15 +0,0 @@ -package main - -import ( - "os/user" - "testing" -) - -func skipTestIfNotRoot(t *testing.T) { - u, err := user.Current() - if err != nil { - t.Skip("Could not determine user. Running without root may cause tests to fail") - } else if u.Uid != "0" { - t.Skip("tests will fail unless run as root") - } -} diff --git a/cmd/podman/remoteclientconfig/configfile_test.go b/cmd/podman/remoteclientconfig/configfile_test.go index 1710ee83f..4ad2c2100 100644 --- a/cmd/podman/remoteclientconfig/configfile_test.go +++ b/cmd/podman/remoteclientconfig/configfile_test.go @@ -92,14 +92,15 @@ func TestReadRemoteConfig(t *testing.T) { {"nouser", args{reader: strings.NewReader(noUser)}, makeNoUserResult(), false}, } for _, tt := range tests { + test := tt t.Run(tt.name, func(t *testing.T) { - got, err := ReadRemoteConfig(tt.args.reader) - if (err != nil) != tt.wantErr { - t.Errorf("ReadRemoteConfig() error = %v, wantErr %v", err, tt.wantErr) + got, err := ReadRemoteConfig(test.args.reader) + if (err != nil) != test.wantErr { + t.Errorf("ReadRemoteConfig() error = %v, wantErr %v", err, test.wantErr) return } - if !reflect.DeepEqual(got, tt.want) { - t.Errorf("ReadRemoteConfig() = %v, want %v", got, tt.want) + if !reflect.DeepEqual(got, test.want) { + t.Errorf("ReadRemoteConfig() = %v, want %v", got, test.want) } }) } @@ -150,17 +151,18 @@ func TestRemoteConfig_GetDefault(t *testing.T) { {"single", fields{Connections: none}, nil, true}, } for _, tt := range tests { - t.Run(tt.name, func(t *testing.T) { + test := tt + t.Run(test.name, func(t *testing.T) { r := &RemoteConfig{ - Connections: tt.fields.Connections, + Connections: test.fields.Connections, } got, err := r.GetDefault() - if (err != nil) != tt.wantErr { - t.Errorf("RemoteConfig.GetDefault() error = %v, wantErr %v", err, tt.wantErr) + if (err != nil) != test.wantErr { + t.Errorf("RemoteConfig.GetDefault() error = %v, wantErr %v", err, test.wantErr) return } - if !reflect.DeepEqual(got, tt.want) { - t.Errorf("RemoteConfig.GetDefault() = %v, want %v", got, tt.want) + if !reflect.DeepEqual(got, test.want) { + t.Errorf("RemoteConfig.GetDefault() = %v, want %v", got, test.want) } }) } @@ -192,17 +194,18 @@ func TestRemoteConfig_GetRemoteConnection(t *testing.T) { {"none", fields{Connections: blank}, args{name: "foobar"}, nil, true}, } for _, tt := range tests { - t.Run(tt.name, func(t *testing.T) { + test := tt + t.Run(test.name, func(t *testing.T) { r := &RemoteConfig{ - Connections: tt.fields.Connections, + Connections: test.fields.Connections, } - got, err := r.GetRemoteConnection(tt.args.name) - if (err != nil) != tt.wantErr { - t.Errorf("RemoteConfig.GetRemoteConnection() error = %v, wantErr %v", err, tt.wantErr) + got, err := r.GetRemoteConnection(test.args.name) + if (err != nil) != test.wantErr { + t.Errorf("RemoteConfig.GetRemoteConnection() error = %v, wantErr %v", err, test.wantErr) return } - if !reflect.DeepEqual(got, tt.want) { - t.Errorf("RemoteConfig.GetRemoteConnection() = %v, want %v", got, tt.want) + if !reflect.DeepEqual(got, test.want) { + t.Errorf("RemoteConfig.GetRemoteConnection() = %v, want %v", got, test.want) } }) } diff --git a/libpod/container_internal_test.go b/libpod/container_internal_test.go index f1e2b70a7..5428504ef 100644 --- a/libpod/container_internal_test.go +++ b/libpod/container_internal_test.go @@ -61,7 +61,8 @@ func TestPostDeleteHooks(t *testing.T) { } stateRegexp := `{"ociVersion":"1\.0\.1-dev","id":"123abc","status":"stopped","bundle":"` + strings.TrimSuffix(os.TempDir(), "/") + `/libpod_test_[0-9]*","annotations":{"a":"b"}}` - for _, path := range []string{statePath, copyPath} { + for _, p := range []string{statePath, copyPath} { + path := p t.Run(path, func(t *testing.T) { content, err := ioutil.ReadFile(path) if err != nil { diff --git a/libpod/image/image_test.go b/libpod/image/image_test.go index 5aff7d860..3ff6210d9 100644 --- a/libpod/image/image_test.go +++ b/libpod/image/image_test.go @@ -3,7 +3,6 @@ package image import ( "context" "fmt" - "io" "io/ioutil" "os" "testing" @@ -91,8 +90,7 @@ func TestImage_NewFromLocal(t *testing.T) { RunRoot: workdir, GraphRoot: workdir, } - var writer io.Writer - writer = os.Stdout + writer := os.Stdout // Need images to be present for this test ir, err := NewImageRuntimeFromOptions(so) @@ -108,7 +106,7 @@ func TestImage_NewFromLocal(t *testing.T) { for _, image := range tm { // tag our images - image.img.TagImage(image.taggedName) + err = image.img.TagImage(image.taggedName) assert.NoError(t, err) for _, name := range image.names { newImage, err := ir.NewFromLocal(name) @@ -142,8 +140,7 @@ func TestImage_New(t *testing.T) { // Build the list of pull names names = append(names, bbNames...) names = append(names, fedoraNames...) - var writer io.Writer - writer = os.Stdout + writer := os.Stdout // Iterate over the names and delete the image // after the pull @@ -213,7 +210,7 @@ func TestImage_RepoDigests(t *testing.T) { t.Fatal(err) } - for _, test := range []struct { + for _, tt := range []struct { name string names []string expected []string @@ -234,6 +231,7 @@ func TestImage_RepoDigests(t *testing.T) { expected: []string{"docker.io/library/busybox@sha256:7173b809ca12ec5dee4506cd86be934c4596dd234ee82c0662eac04a8c2c71dc"}, }, } { + test := tt t.Run(test.name, func(t *testing.T) { image := &Image{ image: &storage.Image{ diff --git a/libpod/lock/file/file_lock_test.go b/libpod/lock/file/file_lock_test.go index 6320d6b70..7ac8bf31a 100644 --- a/libpod/lock/file/file_lock_test.go +++ b/libpod/lock/file/file_lock_test.go @@ -17,10 +17,10 @@ func TestCreateAndDeallocate(t *testing.T) { assert.NoError(t, err) defer os.RemoveAll(d) - l, err := OpenFileLock(filepath.Join(d, "locks")) + _, err = OpenFileLock(filepath.Join(d, "locks")) assert.Error(t, err) - l, err = CreateFileLock(filepath.Join(d, "locks")) + l, err := CreateFileLock(filepath.Join(d, "locks")) assert.NoError(t, err) lock, err := l.AllocateLock() diff --git a/libpod/state_test.go b/libpod/state_test.go index d4a4149f9..39937d8e4 100644 --- a/libpod/state_test.go +++ b/libpod/state_test.go @@ -350,7 +350,8 @@ func TestAddCtrSameNamespaceSucceeds(t *testing.T) { testCtr.config.Namespace = "test1" - state.SetNamespace("test1") + err = state.SetNamespace("test1") + assert.NoError(t, err) err = state.AddContainer(testCtr) assert.NoError(t, err) @@ -369,12 +370,14 @@ func TestAddCtrDifferentNamespaceFails(t *testing.T) { testCtr.config.Namespace = "test1" - state.SetNamespace("test2") + err = state.SetNamespace("test2") + assert.NoError(t, err) err = state.AddContainer(testCtr) assert.Error(t, err) - state.SetNamespace("") + err = state.SetNamespace("") + assert.NoError(t, err) ctrs, err := state.AllContainers() assert.NoError(t, err) @@ -406,7 +409,8 @@ func TestGetContainerInDifferentNamespaceFails(t *testing.T) { err = state.AddContainer(testCtr) assert.NoError(t, err) - state.SetNamespace("test1") + err = state.SetNamespace("test1") + assert.NoError(t, err) _, err = state.Container(testCtr.ID()) assert.Error(t, err) @@ -423,7 +427,8 @@ func TestGetContainerInSameNamespaceSucceeds(t *testing.T) { err = state.AddContainer(testCtr) assert.NoError(t, err) - state.SetNamespace("test1") + err = state.SetNamespace("test1") + assert.NoError(t, err) ctr, err := state.Container(testCtr.ID()) assert.NoError(t, err) @@ -586,7 +591,8 @@ func TestLookupCtrInSameNamespaceSucceeds(t *testing.T) { err = state.AddContainer(testCtr) assert.NoError(t, err) - state.SetNamespace("test1") + err = state.SetNamespace("test1") + assert.NoError(t, err) ctr, err := state.LookupContainer(testCtr.ID()) assert.NoError(t, err) @@ -608,7 +614,8 @@ func TestLookupCtrInDifferentNamespaceFails(t *testing.T) { err = state.AddContainer(testCtr) assert.NoError(t, err) - state.SetNamespace("test2") + err = state.SetNamespace("test2") + assert.NoError(t, err) _, err = state.LookupContainer(testCtr.ID()) assert.Error(t, err) @@ -633,7 +640,8 @@ func TestLookupContainerMatchInDifferentNamespaceSucceeds(t *testing.T) { err = state.AddContainer(testCtr2) assert.NoError(t, err) - state.SetNamespace("test1") + err = state.SetNamespace("test1") + assert.NoError(t, err) ctr, err := state.LookupContainer("000") assert.NoError(t, err) @@ -698,7 +706,8 @@ func TestHasContainerSameNamespaceIsTrue(t *testing.T) { err = state.AddContainer(testCtr) assert.NoError(t, err) - state.SetNamespace("test1") + err = state.SetNamespace("test1") + assert.NoError(t, err) exists, err := state.HasContainer(testCtr.ID()) assert.NoError(t, err) @@ -716,7 +725,8 @@ func TestHasContainerDifferentNamespaceIsFalse(t *testing.T) { err = state.AddContainer(testCtr) assert.NoError(t, err) - state.SetNamespace("test2") + err = state.SetNamespace("test2") + assert.NoError(t, err) exists, err := state.HasContainer(testCtr.ID()) assert.NoError(t, err) @@ -759,7 +769,8 @@ func TestSaveAndUpdateContainerSameNamespaceSucceeds(t *testing.T) { err = state.AddContainer(testCtr) assert.NoError(t, err) - state.SetNamespace("test1") + err = state.SetNamespace("test1") + assert.NoError(t, err) retrievedCtr, err := state.Container(testCtr.ID()) assert.NoError(t, err) @@ -806,7 +817,8 @@ func TestUpdateContainerNotInNamespaceReturnsError(t *testing.T) { err = state.AddContainer(testCtr) assert.NoError(t, err) - state.SetNamespace("test2") + err = state.SetNamespace("test2") + assert.NoError(t, err) err = state.UpdateContainer(testCtr) assert.Error(t, err) @@ -841,7 +853,8 @@ func TestSaveContainerNotInNamespaceReturnsError(t *testing.T) { err = state.AddContainer(testCtr) assert.NoError(t, err) - state.SetNamespace("test2") + err = state.SetNamespace("test2") + assert.NoError(t, err) err = state.SaveContainer(testCtr) assert.Error(t, err) @@ -894,12 +907,14 @@ func TestRemoveContainerNotInNamespaceFails(t *testing.T) { assert.NoError(t, err) assert.Equal(t, 1, len(ctrs)) - state.SetNamespace("test2") + err = state.SetNamespace("test2") + assert.NoError(t, err) err = state.RemoveContainer(testCtr) assert.Error(t, err) - state.SetNamespace("") + err = state.SetNamespace("") + assert.NoError(t, err) ctrs2, err := state.AllContainers() assert.NoError(t, err) @@ -960,7 +975,8 @@ func TestGetAllContainersNoContainerInNamespace(t *testing.T) { err = state.AddContainer(testCtr) assert.NoError(t, err) - state.SetNamespace("test2") + err = state.SetNamespace("test2") + assert.NoError(t, err) ctrs, err := state.AllContainers() assert.NoError(t, err) @@ -984,7 +1000,8 @@ func TestGetContainerOneContainerInNamespace(t *testing.T) { err = state.AddContainer(testCtr2) assert.NoError(t, err) - state.SetNamespace("test1") + err = state.SetNamespace("test1") + assert.NoError(t, err) ctrs, err := state.AllContainers() assert.NoError(t, err) @@ -1020,7 +1037,8 @@ func TestContainerInUseCtrNotInNamespace(t *testing.T) { err = state.AddContainer(testCtr) assert.NoError(t, err) - state.SetNamespace("test2") + err = state.SetNamespace("test2") + assert.NoError(t, err) _, err = state.ContainerInUse(testCtr) assert.Error(t, err) @@ -1497,7 +1515,8 @@ func TestGetPodInNamespaceSucceeds(t *testing.T) { err = state.AddPod(testPod) assert.NoError(t, err) - state.SetNamespace("test1") + err = state.SetNamespace("test1") + assert.NoError(t, err) statePod, err := state.Pod(testPod.ID()) assert.NoError(t, err) @@ -1516,7 +1535,8 @@ func TestGetPodPodNotInNamespaceFails(t *testing.T) { err = state.AddPod(testPod) assert.NoError(t, err) - state.SetNamespace("test2") + err = state.SetNamespace("test2") + assert.NoError(t, err) _, err = state.Pod(testPod.ID()) assert.Error(t, err) @@ -1637,7 +1657,8 @@ func TestLookupPodInSameNamespaceSucceeds(t *testing.T) { err = state.AddPod(testPod) assert.NoError(t, err) - state.SetNamespace("test1") + err = state.SetNamespace("test1") + assert.NoError(t, err) statePod, err := state.LookupPod(testPod.ID()) assert.NoError(t, err) @@ -1656,7 +1677,8 @@ func TestLookupPodInDifferentNamespaceFails(t *testing.T) { err = state.AddPod(testPod) assert.NoError(t, err) - state.SetNamespace("test2") + err = state.SetNamespace("test2") + assert.NoError(t, err) _, err = state.LookupPod(testPod.ID()) assert.Error(t, err) @@ -1681,7 +1703,8 @@ func TestLookupPodOneInDifferentNamespaceFindsRightPod(t *testing.T) { err = state.AddPod(testPod2) assert.NoError(t, err) - state.SetNamespace("test1") + err = state.SetNamespace("test1") + assert.NoError(t, err) pod, err := state.LookupPod(strings.Repeat("1", 5)) assert.NoError(t, err) @@ -1757,7 +1780,8 @@ func TestHasPodSameNamespaceSucceeds(t *testing.T) { err = state.AddPod(testPod) assert.NoError(t, err) - state.SetNamespace("test1") + err = state.SetNamespace("test1") + assert.NoError(t, err) exist, err := state.HasPod(testPod.ID()) assert.NoError(t, err) @@ -1775,7 +1799,8 @@ func TestHasPodDifferentNamespaceFails(t *testing.T) { err = state.AddPod(testPod) assert.NoError(t, err) - state.SetNamespace("test2") + err = state.SetNamespace("test2") + assert.NoError(t, err) exist, err := state.HasPod(testPod.ID()) assert.NoError(t, err) @@ -1913,7 +1938,8 @@ func TestAddPodSameNamespaceSucceeds(t *testing.T) { testPod.config.Namespace = "test1" - state.SetNamespace("test1") + err = state.SetNamespace("test1") + assert.NoError(t, err) err = state.AddPod(testPod) assert.NoError(t, err) @@ -1933,12 +1959,14 @@ func TestAddPodDifferentNamespaceFails(t *testing.T) { testPod.config.Namespace = "test1" - state.SetNamespace("test2") + err = state.SetNamespace("test2") + assert.NoError(t, err) err = state.AddPod(testPod) assert.Error(t, err) - state.SetNamespace("") + err = state.SetNamespace("") + assert.NoError(t, err) allPods, err := state.AllPods() assert.NoError(t, err) @@ -2067,12 +2095,14 @@ func TestRemovePodNotInNamespaceFails(t *testing.T) { err = state.AddPod(testPod) assert.NoError(t, err) - state.SetNamespace("test2") + err = state.SetNamespace("test2") + assert.NoError(t, err) err = state.RemovePod(testPod) assert.Error(t, err) - state.SetNamespace("") + err = state.SetNamespace("") + assert.NoError(t, err) allPods, err := state.AllPods() assert.NoError(t, err) @@ -2152,7 +2182,8 @@ func TestAllPodsPodInDifferentNamespaces(t *testing.T) { err = state.AddPod(testPod) assert.NoError(t, err) - state.SetNamespace("test2") + err = state.SetNamespace("test2") + assert.NoError(t, err) allPods, err := state.AllPods() assert.NoError(t, err) @@ -2178,7 +2209,8 @@ func TestAllPodsOnePodInDifferentNamespace(t *testing.T) { err = state.AddPod(testPod2) assert.NoError(t, err) - state.SetNamespace("test1") + err = state.SetNamespace("test1") + assert.NoError(t, err) allPods, err := state.AllPods() assert.NoError(t, err) @@ -2274,7 +2306,8 @@ func TestPodHasContainerPodNotInNamespaceFails(t *testing.T) { err = state.AddPod(testPod) assert.NoError(t, err) - state.SetNamespace("test2") + err = state.SetNamespace("test2") + assert.NoError(t, err) _, err = state.PodHasContainer(testPod, strings.Repeat("2", 32)) assert.Error(t, err) @@ -2393,7 +2426,8 @@ func TestPodContainerByIDPodNotInNamespace(t *testing.T) { err = state.AddPod(testPod) assert.NoError(t, err) - state.SetNamespace("test2") + err = state.SetNamespace("test2") + assert.NoError(t, err) _, err = state.PodContainersByID(testPod) assert.Error(t, err) @@ -2513,7 +2547,8 @@ func TestPodContainersPodNotInNamespace(t *testing.T) { err = state.AddPod(testPod) assert.NoError(t, err) - state.SetNamespace("test2") + err = state.SetNamespace("test2") + assert.NoError(t, err) _, err = state.PodContainers(testPod) assert.Error(t, err) @@ -2686,7 +2721,8 @@ func TestRemoveContainersNotInNamespace(t *testing.T) { err = state.AddPod(testPod) assert.NoError(t, err) - state.SetNamespace("test2") + err = state.SetNamespace("test2") + assert.NoError(t, err) err = state.RemovePodContainers(testPod) assert.Error(t, err) @@ -3229,7 +3265,8 @@ func TestAddCtrToPodSameNamespaceSucceeds(t *testing.T) { err = state.AddPod(testPod) assert.NoError(t, err) - state.SetNamespace("test1") + err = state.SetNamespace("test1") + assert.NoError(t, err) err = state.AddContainerToPod(testPod, testCtr) assert.NoError(t, err) @@ -3253,15 +3290,17 @@ func TestAddCtrToPodDifferentNamespaceFails(t *testing.T) { testPod.config.Namespace = "test1" testCtr.config.Pod = testPod.ID() - state.AddPod(testPod) + err = state.AddPod(testPod) assert.NoError(t, err) - state.SetNamespace("test2") + err = state.SetNamespace("test2") + assert.NoError(t, err) err = state.AddContainerToPod(testPod, testCtr) assert.Error(t, err) - state.SetNamespace("") + err = state.SetNamespace("") + assert.NoError(t, err) ctrs, err := state.AllContainers() assert.NoError(t, err) @@ -3461,7 +3500,8 @@ func TestRemoveContainerFromPodSameNamespaceSucceeds(t *testing.T) { err = state.AddContainerToPod(testPod, testCtr) assert.NoError(t, err) - state.SetNamespace("test1") + err = state.SetNamespace("test1") + assert.NoError(t, err) err = state.RemoveContainerFromPod(testPod, testCtr) assert.NoError(t, err) @@ -3495,12 +3535,14 @@ func TestRemoveContainerFromPodDifferentNamespaceFails(t *testing.T) { err = state.AddContainerToPod(testPod, testCtr) assert.NoError(t, err) - state.SetNamespace("test2") + err = state.SetNamespace("test2") + assert.NoError(t, err) err = state.RemoveContainerFromPod(testPod, testCtr) assert.Error(t, err) - state.SetNamespace("") + err = state.SetNamespace("") + assert.NoError(t, err) ctrs, err := state.PodContainers(testPod) assert.NoError(t, err) @@ -3539,7 +3581,8 @@ func TestUpdatePodNotInNamespaceFails(t *testing.T) { err = state.AddPod(testPod) assert.NoError(t, err) - state.SetNamespace("test2") + err = state.SetNamespace("test2") + assert.NoError(t, err) err = state.UpdatePod(testPod) assert.Error(t, err) @@ -3573,7 +3616,8 @@ func TestSavePodNotInNamespaceFails(t *testing.T) { err = state.AddPod(testPod) assert.NoError(t, err) - state.SetNamespace("test2") + err = state.SetNamespace("test2") + assert.NoError(t, err) err = state.SavePod(testPod) assert.Error(t, err) @@ -3615,7 +3659,8 @@ func TestSaveAndUpdatePodSameNamespace(t *testing.T) { err = state.AddPod(testPod) assert.NoError(t, err) - state.SetNamespace("test1") + err = state.SetNamespace("test1") + assert.NoError(t, err) statePod, err := state.Pod(testPod.ID()) assert.NoError(t, err) diff --git a/pkg/apparmor/apparmor_linux_test.go b/pkg/apparmor/apparmor_linux_test.go index e94293d87..3ff6e18bc 100644 --- a/pkg/apparmor/apparmor_linux_test.go +++ b/pkg/apparmor/apparmor_linux_test.go @@ -134,7 +134,7 @@ func TestDefaultContent(t *testing.T) { if _, err := os.Stat(aapath); err != nil { t.Skip("AppArmor isn't available in this environment") } - if err := DefaultContent(profile); err != nil { + if _, err := DefaultContent(profile); err != nil { t.Fatalf("Couldn't retrieve default AppArmor profile content '%s': %v", profile, err) } } diff --git a/pkg/hooks/1.0.0/when_test.go b/pkg/hooks/1.0.0/when_test.go index a749063ff..94b0c3830 100644 --- a/pkg/hooks/1.0.0/when_test.go +++ b/pkg/hooks/1.0.0/when_test.go @@ -10,7 +10,8 @@ import ( func TestNoMatch(t *testing.T) { config := &rspec.Spec{} - for _, or := range []bool{true, false} { + for _, o := range []bool{true, false} { + or := o t.Run(fmt.Sprintf("or %t", or), func(t *testing.T) { when := When{Or: or} match, err := when.Match(config, map[string]string{}, false) @@ -27,9 +28,12 @@ func TestAlways(t *testing.T) { processStruct := &rspec.Process{ Args: []string{"/bin/sh", "a", "b"}, } - for _, always := range []bool{true, false} { - for _, or := range []bool{true, false} { - for _, process := range []*rspec.Process{processStruct, nil} { + for _, a := range []bool{true, false} { + always := a + for _, o := range []bool{true, false} { + or := o + for _, p := range []*rspec.Process{processStruct, nil} { + process := p t.Run(fmt.Sprintf("always %t, or %t, has process %t", always, or, process != nil), func(t *testing.T) { config.Process = process when := When{Always: &always, Or: or} @@ -48,7 +52,8 @@ func TestHasBindMountsAnd(t *testing.T) { hasBindMounts := true when := When{HasBindMounts: &hasBindMounts} config := &rspec.Spec{} - for _, containerHasBindMounts := range []bool{false, true} { + for _, b := range []bool{false, true} { + containerHasBindMounts := b t.Run(fmt.Sprintf("%t", containerHasBindMounts), func(t *testing.T) { match, err := when.Match(config, map[string]string{}, containerHasBindMounts) if err != nil { @@ -63,7 +68,8 @@ func TestHasBindMountsOr(t *testing.T) { hasBindMounts := true when := When{HasBindMounts: &hasBindMounts, Or: true} config := &rspec.Spec{} - for _, containerHasBindMounts := range []bool{false, true} { + for _, b := range []bool{false, true} { + containerHasBindMounts := b t.Run(fmt.Sprintf("%t", containerHasBindMounts), func(t *testing.T) { match, err := when.Match(config, map[string]string{}, containerHasBindMounts) if err != nil { @@ -82,7 +88,7 @@ func TestAnnotations(t *testing.T) { }, } config := &rspec.Spec{} - for _, test := range []struct { + for _, tt := range []struct { name string annotations map[string]string or bool @@ -131,6 +137,7 @@ func TestAnnotations(t *testing.T) { match: false, }, } { + test := tt t.Run(test.name, func(t *testing.T) { when.Or = test.or match, err := when.Match(config, test.annotations, false) @@ -149,7 +156,7 @@ func TestCommands(t *testing.T) { }, } config := &rspec.Spec{} - for _, test := range []struct { + for _, tt := range []struct { name string process *rspec.Process match bool @@ -173,6 +180,7 @@ func TestCommands(t *testing.T) { match: false, }, } { + test := tt t.Run(test.name, func(t *testing.T) { config.Process = test.process match, err := when.Match(config, map[string]string{}, false) @@ -209,7 +217,7 @@ func TestHasBindMountsAndCommands(t *testing.T) { }, } config := &rspec.Spec{Process: &rspec.Process{}} - for _, test := range []struct { + for _, tt := range []struct { name string command string hasBindMounts bool @@ -273,6 +281,7 @@ func TestHasBindMountsAndCommands(t *testing.T) { match: false, }, } { + test := tt t.Run(test.name, func(t *testing.T) { config.Process.Args = []string{test.command} when.Or = test.or @@ -287,7 +296,7 @@ func TestHasBindMountsAndCommands(t *testing.T) { func TestInvalidRegexp(t *testing.T) { config := &rspec.Spec{Process: &rspec.Process{Args: []string{"/bin/sh"}}} - for _, test := range []struct { + for _, tt := range []struct { name string when When expected string @@ -308,6 +317,7 @@ func TestInvalidRegexp(t *testing.T) { expected: "^command: error parsing regexp: .*", }, } { + test := tt t.Run(test.name, func(t *testing.T) { _, err := test.when.Match(config, map[string]string{"a": "b"}, false) if err == nil { diff --git a/pkg/hooks/exec/exec_test.go b/pkg/hooks/exec/exec_test.go index 7aac315cb..1e105373d 100644 --- a/pkg/hooks/exec/exec_test.go +++ b/pkg/hooks/exec/exec_test.go @@ -94,7 +94,7 @@ func TestRunEnvironment(t *testing.T) { Path: path, Args: []string{"sh", "-c", "env"}, } - for _, test := range []struct { + for _, tt := range []struct { name string env []string expected map[string]string @@ -120,6 +120,7 @@ func TestRunEnvironment(t *testing.T) { }, }, } { + test := tt t.Run(test.name, func(t *testing.T) { var stderr, stdout bytes.Buffer hook.Env = test.env @@ -147,7 +148,7 @@ func TestRunCancel(t *testing.T) { Args: []string{"sh", "-c", "echo waiting; sleep 2; echo done"}, } one := 1 - for _, test := range []struct { + for _, tt := range []struct { name string contextTimeout time.Duration hookTimeout *int @@ -174,6 +175,7 @@ func TestRunCancel(t *testing.T) { expectedRunError: context.DeadlineExceeded, }, } { + test := tt t.Run(test.name, func(t *testing.T) { ctx := context.Background() var stderr, stdout bytes.Buffer diff --git a/pkg/hooks/exec/runtimeconfigfilter_test.go b/pkg/hooks/exec/runtimeconfigfilter_test.go index 52d590d14..48dd2f998 100644 --- a/pkg/hooks/exec/runtimeconfigfilter_test.go +++ b/pkg/hooks/exec/runtimeconfigfilter_test.go @@ -25,9 +25,9 @@ func pointerFileMode(value os.FileMode) *os.FileMode { } func TestRuntimeConfigFilter(t *testing.T) { - unexpectedEndOfJSONInput := json.Unmarshal([]byte("{\n"), nil) + unexpectedEndOfJSONInput := json.Unmarshal([]byte("{\n"), nil) //nolint - for _, test := range []struct { + for _, tt := range []struct { name string contextTimeout time.Duration hooks []spec.Hook @@ -244,6 +244,7 @@ func TestRuntimeConfigFilter(t *testing.T) { expectedRunError: unexpectedEndOfJSONInput, }, } { + test := tt t.Run(test.name, func(t *testing.T) { ctx := context.Background() if test.contextTimeout > 0 { diff --git a/pkg/network/network_test.go b/pkg/network/network_test.go index dbffc33ad..1969e792c 100644 --- a/pkg/network/network_test.go +++ b/pkg/network/network_test.go @@ -25,9 +25,10 @@ func Test_networkIntersect(t *testing.T) { {"Two 24s", args{n1: parseCIDR("192.168.1.0/24"), n2: parseCIDR("192.168.2.0/24")}, false}, } for _, tt := range tests { + test := tt t.Run(tt.name, func(t *testing.T) { - if got := networkIntersect(tt.args.n1, tt.args.n2); got != tt.want { - t.Errorf("networkIntersect() = %v, want %v", got, tt.want) + if got := networkIntersect(test.args.n1, test.args.n2); got != test.want { + t.Errorf("networkIntersect() = %v, want %v", got, test.want) } }) } diff --git a/pkg/network/subnet_test.go b/pkg/network/subnet_test.go index 6ecfd2d17..917c3be88 100644 --- a/pkg/network/subnet_test.go +++ b/pkg/network/subnet_test.go @@ -20,14 +20,15 @@ func TestNextSubnet(t *testing.T) { {"class c", args{subnet: parseCIDR("192.168.1.0/24")}, parseCIDR("192.168.2.0/24"), false}, } for _, tt := range tests { - t.Run(tt.name, func(t *testing.T) { - got, err := NextSubnet(tt.args.subnet) - if (err != nil) != tt.wantErr { - t.Errorf("NextSubnet() error = %v, wantErr %v", err, tt.wantErr) + test := tt + t.Run(test.name, func(t *testing.T) { + got, err := NextSubnet(test.args.subnet) + if (err != nil) != test.wantErr { + t.Errorf("NextSubnet() error = %v, wantErr %v", err, test.wantErr) return } - if !reflect.DeepEqual(got, tt.want) { - t.Errorf("NextSubnet() got = %v, want %v", got, tt.want) + if !reflect.DeepEqual(got, test.want) { + t.Errorf("NextSubnet() got = %v, want %v", got, test.want) } }) } diff --git a/pkg/systemdgen/systemdgen_test.go b/pkg/systemdgen/systemdgen_test.go index e1da7e8e0..3894a0205 100644 --- a/pkg/systemdgen/systemdgen_test.go +++ b/pkg/systemdgen/systemdgen_test.go @@ -24,9 +24,10 @@ func TestValidateRestartPolicy(t *testing.T) { {"failblank", ContainerInfo{restart: ""}, true}, } for _, tt := range tests { + test := tt t.Run(tt.name, func(t *testing.T) { - if err := validateRestartPolicy(tt.ContainerInfo.restart); (err != nil) != tt.wantErr { - t.Errorf("ValidateRestartPolicy() error = %v, wantErr %v", err, tt.wantErr) + if err := validateRestartPolicy(test.ContainerInfo.restart); (err != nil) != test.wantErr { + t.Errorf("ValidateRestartPolicy() error = %v, wantErr %v", err, test.wantErr) } }) } @@ -221,18 +222,19 @@ WantedBy=multi-user.target` }, } for _, tt := range tests { + test := tt t.Run(tt.name, func(t *testing.T) { opts := Options{ Files: false, - New: tt.info.New, + New: test.info.New, } - got, err := CreateContainerSystemdUnit(&tt.info, opts) - if (err != nil) != tt.wantErr { - t.Errorf("CreateContainerSystemdUnit() error = \n%v, wantErr \n%v", err, tt.wantErr) + got, err := CreateContainerSystemdUnit(&test.info, opts) + if (err != nil) != test.wantErr { + t.Errorf("CreateContainerSystemdUnit() error = \n%v, wantErr \n%v", err, test.wantErr) return } - if got != tt.want { - t.Errorf("CreateContainerSystemdUnit() = \n%v\n---------> want\n%v", got, tt.want) + if got != test.want { + t.Errorf("CreateContainerSystemdUnit() = \n%v\n---------> want\n%v", got, test.want) } }) } -- cgit v1.2.3-54-g00ecf