diff --git a/test/framework/types_test.go b/test/framework/types_test.go index 9911f0a101b0..e80ac7f09bcd 100644 --- a/test/framework/types_test.go +++ b/test/framework/types_test.go @@ -19,14 +19,15 @@ package framework_test import ( "testing" + . "github.com/onsi/gomega" + "sigs.k8s.io/cluster-api/test/framework" ) func TestTypeToKind(t *testing.T) { + g := NewWithT(t) + type hello struct{} - out := framework.TypeToKind(&hello{}) - if out != "hello" { - t.Fatalf("Expected %q from pointer input but got %q", "hello", out) - } + g.Expect(framework.TypeToKind(&hello{})).To(Equal("hello")) } diff --git a/test/infrastructure/docker/cloudinit/kindadapter_test.go b/test/infrastructure/docker/cloudinit/kindadapter_test.go index cedf708a6aef..054d6cd0d91e 100644 --- a/test/infrastructure/docker/cloudinit/kindadapter_test.go +++ b/test/infrastructure/docker/cloudinit/kindadapter_test.go @@ -17,11 +17,14 @@ limitations under the License. package cloudinit import ( - "reflect" "testing" + + . "github.com/onsi/gomega" ) func TestRealUseCase(t *testing.T) { + g := NewWithT(t) + cloudData := []byte(` #cloud-config @@ -137,19 +140,12 @@ write_files: } commands, err := Commands(cloudData) - if err != nil { - t.Fatalf("Run returned unexpected errors %v", err) - } - if len(commands) != len(expectedCmds) { - t.Fatal("commands and expected commands should be the same length") - } + g.Expect(err).NotTo(HaveOccurred()) + g.Expect(commands).To(HaveLen(len(expectedCmds))) + for i, cmd := range commands { expected := expectedCmds[i] - if cmd.Cmd != expected.Cmd { - t.Fatalf("commands should be the same: %s vs %s", cmd.Cmd, expected.Cmd) - } - if !reflect.DeepEqual(cmd.Args, expected.Args) { - t.Fatalf("command args should be the same: %v vs %v", cmd.Args, expected.Cmd) - } + g.Expect(cmd.Cmd).To(Equal(expected.Cmd)) + g.Expect(cmd.Args).To(ConsistOf(expected.Args)) } } diff --git a/test/infrastructure/docker/cloudinit/runcmd_test.go b/test/infrastructure/docker/cloudinit/runcmd_test.go index c8bb2b343f05..fc52dc64c10f 100644 --- a/test/infrastructure/docker/cloudinit/runcmd_test.go +++ b/test/infrastructure/docker/cloudinit/runcmd_test.go @@ -17,37 +17,33 @@ limitations under the License. package cloudinit import ( - "reflect" "testing" + + . "github.com/onsi/gomega" ) func TestRunCmdUnmarshal(t *testing.T) { + g := NewWithT(t) + cloudData := ` runcmd: - [ ls, -l, / ] - "ls -l /"` r := runCmd{} err := r.Unmarshal([]byte(cloudData)) - if err != nil { - t.Fatal(err) - } - - if len(r.Cmds) != 2 { - t.Errorf("Expected 2 commands, found %d", len(r.Cmds)) - } + g.Expect(err).NotTo(HaveOccurred()) + g.Expect(r.Cmds).To(HaveLen(2)) expected0 := Cmd{Cmd: "ls", Args: []string{"-l", "/"}} - if !reflect.DeepEqual(r.Cmds[0], expected0) { - t.Errorf("Expected %+v commands, found %+v", expected0, r.Cmds[0]) - } + g.Expect(r.Cmds[0]).To(Equal(expected0)) expected1 := Cmd{Cmd: "/bin/sh", Args: []string{"-c", "ls -l /"}} - if !reflect.DeepEqual(r.Cmds[1], expected1) { - t.Errorf("Expected %+v commands, found %+v", expected1, r.Cmds[1]) - } + g.Expect(r.Cmds[1]).To(Equal(expected1)) } func TestRunCmdRun(t *testing.T) { + g := NewWithT(t) + var useCases = []struct { name string r runCmd @@ -82,43 +78,31 @@ func TestRunCmdRun(t *testing.T) { for _, rt := range useCases { t.Run(rt.name, func(t *testing.T) { commands, err := rt.r.Commands() - if err != nil { - t.Fatal(err) - } - - if !reflect.DeepEqual(rt.expectedCmds, commands) { - t.Errorf("expected %s, got %s", rt.expectedCmds, commands) - } + g.Expect(err).NotTo(HaveOccurred()) + g.Expect(rt.expectedCmds).To(Equal(commands)) }) } } func TestHackKubeadmIgnoreErrors(t *testing.T) { + g := NewWithT(t) + cloudData := ` runcmd: - kubeadm init --config=/tmp/kubeadm.yaml - [ kubeadm, join, --config=/tmp/kubeadm-controlplane-join-config.yaml ]` r := runCmd{} err := r.Unmarshal([]byte(cloudData)) - if err != nil { - t.Fatal(err) - } - - if len(r.Cmds) != 2 { - t.Errorf("Expected 2 commands, found %d", len(r.Cmds)) - } + g.Expect(err).NotTo(HaveOccurred()) + g.Expect(r.Cmds).To(HaveLen(2)) r.Cmds[0] = hackKubeadmIgnoreErrors(r.Cmds[0]) expected0 := Cmd{Cmd: "/bin/sh", Args: []string{"-c", "kubeadm init --config=/tmp/kubeadm.yaml --ignore-preflight-errors=all"}} - if !reflect.DeepEqual(r.Cmds[0], expected0) { - t.Errorf("Expected %+v commands, found %+v", expected0, r.Cmds[0]) - } + g.Expect(r.Cmds[0]).To(Equal(expected0)) r.Cmds[1] = hackKubeadmIgnoreErrors(r.Cmds[1]) expected1 := Cmd{Cmd: "kubeadm", Args: []string{"join", "--config=/tmp/kubeadm-controlplane-join-config.yaml", "--ignore-preflight-errors=all"}} - if !reflect.DeepEqual(r.Cmds[1], expected1) { - t.Errorf("Expected %+v commands, found %+v", expected1, r.Cmds[1]) - } + g.Expect(r.Cmds[1]).To(Equal(expected1)) } diff --git a/test/infrastructure/docker/cloudinit/unknown_test.go b/test/infrastructure/docker/cloudinit/unknown_test.go index 949e89980e86..503f7b1ff7ae 100644 --- a/test/infrastructure/docker/cloudinit/unknown_test.go +++ b/test/infrastructure/docker/cloudinit/unknown_test.go @@ -17,34 +17,29 @@ limitations under the License. package cloudinit import ( - "reflect" "testing" + + . "github.com/onsi/gomega" ) func TestUnknown_Run(t *testing.T) { + g := NewWithT(t) + u := &unknown{ lines: []string{}, } lines, err := u.Commands() - if err != nil { - t.Fatal("err should always be nil") - } - if len(lines) != 0 { - t.Fatalf("return exactly what was parsed. did not expect anything here but got: %v", lines) - } + g.Expect(err).NotTo(HaveOccurred()) + g.Expect(lines).To(HaveLen(0)) } func TestUnknown_Unmarshal(t *testing.T) { + g := NewWithT(t) + u := &unknown{} expected := []string{"test 1", "test 2", "test 3"} input := `["test 1", "test 2", "test 3"]` - if err := u.Unmarshal([]byte(input)); err != nil { - t.Fatalf("should not return an error but got: %v", err) - } - if len(u.lines) != len(expected) { - t.Fatalf("expected exactly %v lines but got %v", 3, u.lines) - } - if !reflect.DeepEqual(u.lines, expected) { - t.Fatalf("lines should be %v but it is %v", expected, u.lines) - } + + g.Expect(u.Unmarshal([]byte(input))).To(Succeed()) + g.Expect(u.lines).To(Equal(expected)) } diff --git a/test/infrastructure/docker/cloudinit/writefiles_test.go b/test/infrastructure/docker/cloudinit/writefiles_test.go index 9fffe1ce96d6..b76b2f3fd75c 100644 --- a/test/infrastructure/docker/cloudinit/writefiles_test.go +++ b/test/infrastructure/docker/cloudinit/writefiles_test.go @@ -19,11 +19,14 @@ package cloudinit import ( "bytes" "compress/gzip" - "reflect" "testing" + + . "github.com/onsi/gomega" ) func TestWriteFiles(t *testing.T) { + g := NewWithT(t) + var useCases = []struct { name string w writeFilesAction @@ -87,18 +90,15 @@ func TestWriteFiles(t *testing.T) { for _, rt := range useCases { t.Run(rt.name, func(t *testing.T) { cmds, err := rt.w.Commands() - if err != nil { - t.Fatal(err) - } - - if !reflect.DeepEqual(rt.expectedCmds, cmds) { - t.Errorf("Expected %s, got %s", rt.expectedCmds, cmds) - } + g.Expect(err).NotTo(HaveOccurred()) + g.Expect(rt.expectedCmds).To(Equal(cmds)) }) } } func TestFixContent(t *testing.T) { + g := NewWithT(t) + v := "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" gv, _ := gZipData([]byte(v)) var useCases = []struct { @@ -131,28 +131,24 @@ func TestFixContent(t *testing.T) { t.Run(rt.name, func(t *testing.T) { encoding := fixEncoding(rt.encoding) c, err := fixContent(rt.content, encoding) - - if err == nil && rt.expectedError { - t.Error("Expected error, got nil") - } - if err != nil && !rt.expectedError { - t.Errorf("Expected nil, got error %v", err) + if rt.expectedError { + g.Expect(err).To(HaveOccurred()) + } else { + g.Expect(err).NotTo(HaveOccurred()) } - if rt.expectedContent != c { - t.Errorf("Expected %s, got %s", rt.expectedContent, c) - } + g.Expect(rt.expectedContent).To(Equal(c)) }) } } func TestUnzipData(t *testing.T) { + g := NewWithT(t) + value := []byte("foobarbazquxfoobarbazquxfoobarbazquxfoobarbazquxfoobarbazquxfoobarbazquxfoobarbazquxfoobarbazquxfoobarbazqux") gvalue, _ := gZipData(value) dvalue, _ := gUnzipData(gvalue) - if !bytes.Equal(value, dvalue) { - t.Errorf("ss") - } + g.Expect(value).To(Equal(dvalue)) } func gZipData(data []byte) ([]byte, error) { diff --git a/test/infrastructure/docker/controllers/dockermachine_controller_test.go b/test/infrastructure/docker/controllers/dockermachine_controller_test.go index fab0ab44b639..cce2058cb9be 100644 --- a/test/infrastructure/docker/controllers/dockermachine_controller_test.go +++ b/test/infrastructure/docker/controllers/dockermachine_controller_test.go @@ -19,6 +19,8 @@ package controllers import ( "testing" + . "github.com/onsi/gomega" + v1 "k8s.io/api/core/v1" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" "k8s.io/apimachinery/pkg/runtime" @@ -46,6 +48,8 @@ func setupScheme() *runtime.Scheme { } func TestDockerMachineReconciler_DockerClusterToDockerMachines(t *testing.T) { + g := NewWithT(t) + clusterName := "my-cluster" dockerCluster := newDockerCluster(clusterName, "my-docker-cluster") dockerMachine1 := newDockerMachine("my-docker-machine-0") @@ -71,23 +75,8 @@ func TestDockerMachineReconciler_DockerClusterToDockerMachines(t *testing.T) { for i := range out { machineNames[i] = out[i].Name } - if len(out) != 2 { - t.Fatal("expected 2 docker machines to reconcile but got", len(out)) - } - for _, expectedName := range []string{"my-machine-0", "my-machine-1"} { - if !contains(machineNames, expectedName) { - t.Fatalf("expected %q in slice %v", expectedName, machineNames) - } - } -} - -func contains(haystack []string, needle string) bool { - for _, straw := range haystack { - if straw == needle { - return true - } - } - return false + g.Expect(out).To(HaveLen(2)) + g.Expect(machineNames).To(ConsistOf("my-machine-0", "my-machine-1")) } func newCluster(clusterName string) *clusterv1.Cluster {