Skip to content

Commit

Permalink
Merge pull request #2680 from cpanato/GH-2433-12
Browse files Browse the repository at this point in the history
🏃test/tests: standardize gomega imports
  • Loading branch information
k8s-ci-robot committed Mar 16, 2020
2 parents 1d5ae9d + b1bfdfb commit 0b27ad5
Show file tree
Hide file tree
Showing 6 changed files with 65 additions and 104 deletions.
9 changes: 5 additions & 4 deletions test/framework/types_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"))
}
22 changes: 9 additions & 13 deletions test/infrastructure/docker/cloudinit/kindadapter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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))
}
}
52 changes: 18 additions & 34 deletions test/infrastructure/docker/cloudinit/runcmd_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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))
}
27 changes: 11 additions & 16 deletions test/infrastructure/docker/cloudinit/unknown_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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))
}
36 changes: 16 additions & 20 deletions test/infrastructure/docker/cloudinit/writefiles_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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")
Expand All @@ -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 {
Expand Down

0 comments on commit 0b27ad5

Please sign in to comment.