Skip to content

Commit

Permalink
Merge pull request #2614 from cpanato/GH-2433-3
Browse files Browse the repository at this point in the history
🏃 bootstrap/tests: standardize gomega imports - follow up
  • Loading branch information
k8s-ci-robot authored Mar 11, 2020
2 parents bfcc6f3 + 8979daa commit 5426006
Show file tree
Hide file tree
Showing 5 changed files with 110 additions and 172 deletions.
25 changes: 12 additions & 13 deletions bootstrap/kubeadm/internal/cloudinit/cloudinit_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,18 @@ limitations under the License.
package cloudinit

import (
"bytes"
"testing"

. "github.com/onsi/gomega"

infrav1 "sigs.k8s.io/cluster-api/bootstrap/kubeadm/api/v1alpha3"
"sigs.k8s.io/cluster-api/util/certs"
"sigs.k8s.io/cluster-api/util/secret"
)

func TestNewInitControlPlaneAdditionalFileEncodings(t *testing.T) {
g := NewWithT(t)

cpinput := &ControlPlaneInput{
BaseUserData: BaseUserData{
Header: "test",
Expand Down Expand Up @@ -59,9 +62,8 @@ func TestNewInitControlPlaneAdditionalFileEncodings(t *testing.T) {
}

out, err := NewInitControlPlane(cpinput)
if err != nil {
t.Fatal(err)
}
g.Expect(err).NotTo(HaveOccurred())

expectedFiles := []string{
`- path: /tmp/my-path
encoding: "base64"
Expand All @@ -72,13 +74,13 @@ func TestNewInitControlPlaneAdditionalFileEncodings(t *testing.T) {
hi`,
}
for _, f := range expectedFiles {
if !bytes.Contains(out, []byte(f)) {
t.Errorf("%s\ndid not contain\n%s", out, f)
}
g.Expect(out).To(ContainSubstring(f))
}
}

func TestNewInitControlPlaneCommands(t *testing.T) {
g := NewWithT(t)

cpinput := &ControlPlaneInput{
BaseUserData: BaseUserData{
Header: "test",
Expand All @@ -102,16 +104,13 @@ func TestNewInitControlPlaneCommands(t *testing.T) {
}

out, err := NewInitControlPlane(cpinput)
if err != nil {
t.Fatal(err)
}
g.Expect(err).NotTo(HaveOccurred())

expectedCommands := []string{
`"\"echo $(date) ': hello world!'\""`,
`"echo $(date) ': hello world!'"`,
}
for _, f := range expectedCommands {
if !bytes.Contains(out, []byte(f)) {
t.Errorf("%s\ndid not contain\n%s", out, f)
}
g.Expect(out).To(ContainSubstring(f))
}
}
9 changes: 5 additions & 4 deletions bootstrap/kubeadm/internal/cloudinit/controlplane_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,13 @@ package cloudinit

import (
"testing"

. "github.com/onsi/gomega"
)

func TestTemplateYAMLIndent(t *testing.T) {
g := NewWithT(t)

testcases := []struct {
name string
input string
Expand All @@ -44,10 +48,7 @@ func TestTemplateYAMLIndent(t *testing.T) {
for _, tc := range testcases {
tc := tc
t.Run(tc.name, func(t *testing.T) {
out := templateYAMLIndent(tc.indent, tc.input)
if out != tc.expected {
t.Fatalf("\nout:\n%+q\nexpected:\n%+q\n", out, tc.expected)
}
g.Expect(templateYAMLIndent(tc.indent, tc.input)).To(Equal(tc.expected))
})
}
}
63 changes: 26 additions & 37 deletions bootstrap/kubeadm/internal/locking/control_plane_init_mutex_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ import (
"fmt"
"testing"

. "github.com/onsi/gomega"

"github.com/go-logr/logr"
corev1 "k8s.io/api/core/v1"
apierrors "k8s.io/apimachinery/pkg/api/errors"
Expand All @@ -47,13 +49,12 @@ func init() {
}

func TestControlPlaneInitMutex_Lock(t *testing.T) {
g := NewWithT(t)

scheme := runtime.NewScheme()
if err := clusterv1.AddToScheme(scheme); err != nil {
t.Fatal(err)
}
if err := corev1.AddToScheme(scheme); err != nil {
t.Fatal(err)
}
g.Expect(clusterv1.AddToScheme(scheme)).To(Succeed())
g.Expect(corev1.AddToScheme(scheme)).To(Succeed())

uid := types.UID("test-uid")

tests := []struct {
Expand Down Expand Up @@ -127,21 +128,17 @@ func TestControlPlaneInitMutex_Lock(t *testing.T) {
},
}

actual := l.Lock(context.Background(), cluster, machine)
if actual != tc.shouldAcquire {
t.Fatalf("acquired was %v, but it should be %v", actual, tc.shouldAcquire)
}
g.Expect(l.Lock(context.Background(), cluster, machine)).To(Equal(tc.shouldAcquire))
})
}
}
func TestControlPlaneInitMutex_UnLock(t *testing.T) {
g := NewWithT(t)

scheme := runtime.NewScheme()
if err := clusterv1.AddToScheme(scheme); err != nil {
t.Fatal(err)
}
if err := corev1.AddToScheme(scheme); err != nil {
t.Fatal(err)
}
g.Expect(clusterv1.AddToScheme(scheme)).To(Succeed())
g.Expect(corev1.AddToScheme(scheme)).To(Succeed())

uid := types.UID("test-uid")
configMap := &corev1.ConfigMap{
ObjectMeta: metav1.ObjectMeta{
Expand Down Expand Up @@ -208,29 +205,23 @@ func TestControlPlaneInitMutex_UnLock(t *testing.T) {
},
}

released := l.Unlock(context.Background(), cluster)
if released != tc.shouldRelease {
t.Fatalf("released was %v, but it should be %v\n", released, tc.shouldRelease)
}

g.Expect(l.Unlock(context.Background(), cluster)).To(Equal(tc.shouldRelease))
})
}
}

func TestInfoLines_Lock(t *testing.T) {
g := NewWithT(t)

scheme := runtime.NewScheme()
if err := clusterv1.AddToScheme(scheme); err != nil {
t.Fatal(err)
}
if err := corev1.AddToScheme(scheme); err != nil {
t.Fatal(err)
}
g.Expect(clusterv1.AddToScheme(scheme)).To(Succeed())
g.Expect(corev1.AddToScheme(scheme)).To(Succeed())

uid := types.UID("test-uid")
info := information{MachineName: "my-control-plane"}
b, err := json.Marshal(info)
if err != nil {
t.Fatal("failed to marshal info")
}
g.Expect(err).NotTo(HaveOccurred())

c := &fakeClient{
Client: fake.NewFakeClientWithScheme(scheme, &corev1.ConfigMap{
ObjectMeta: metav1.ObjectMeta{
Expand Down Expand Up @@ -261,21 +252,19 @@ func TestInfoLines_Lock(t *testing.T) {
Name: fmt.Sprintf("machine-%s", cluster.Name),
},
}
if l.Lock(context.Background(), cluster, machine) != false {
t.Fatal("acquired lock but did not expect to")
}

g.Expect(l.Lock(context.Background(), cluster, machine)).To(BeFalse())

foundLogLine := false
for _, line := range logtester.InfoLog {
fmt.Println(line)
for k, v := range line.data {
if k == "init-machine" && v.(string) == "my-control-plane" {
foundLogLine = true
}
}
}
if !foundLogLine {
t.Fatalf("Did not find the log line containing the name of the machine currently intializing")
}

g.Expect(foundLogLine).To(BeTrue())
}

type fakeClient struct {
Expand Down
93 changes: 34 additions & 59 deletions bootstrap/kubeadm/types/v1beta1/bootstraptokenstring_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,14 @@ import (
"reflect"
"testing"

. "github.com/onsi/gomega"

"github.com/pkg/errors"
)

func TestMarshalJSON(t *testing.T) {
g := NewWithT(t)

var tests = []struct {
bts BootstrapTokenString
expected string
Expand All @@ -36,21 +40,15 @@ func TestMarshalJSON(t *testing.T) {
for _, rt := range tests {
t.Run(rt.bts.ID, func(t *testing.T) {
b, err := json.Marshal(rt.bts)
if err != nil {
t.Fatalf("json.Marshal returned an unexpected error: %v", err)
}
if string(b) != rt.expected {
t.Errorf(
"failed BootstrapTokenString.MarshalJSON:\n\texpected: %s\n\t actual: %s",
rt.expected,
string(b),
)
}
g.Expect(err).NotTo(HaveOccurred())
g.Expect(b).To(BeEquivalentTo(rt.expected))
})
}
}

func TestUnmarshalJSON(t *testing.T) {
g := NewWithT(t)

var tests = []struct {
input string
bts *BootstrapTokenString
Expand All @@ -69,20 +67,19 @@ func TestUnmarshalJSON(t *testing.T) {
t.Run(rt.input, func(t *testing.T) {
newbts := &BootstrapTokenString{}
err := json.Unmarshal([]byte(rt.input), newbts)
if (err != nil) != rt.expectedError {
t.Errorf("failed BootstrapTokenString.UnmarshalJSON:\n\texpected error: %t\n\t actual error: %v", rt.expectedError, err)
} else if !reflect.DeepEqual(rt.bts, newbts) {
t.Errorf(
"failed BootstrapTokenString.UnmarshalJSON:\n\texpected: %v\n\t actual: %v",
rt.bts,
newbts,
)
if rt.expectedError {
g.Expect(err).To(HaveOccurred())
} else {
g.Expect(err).NotTo(HaveOccurred())
}
g.Expect(newbts).To(Equal(rt.bts))
})
}
}

func TestJSONRoundtrip(t *testing.T) {
g := NewWithT(t)

var tests = []struct {
input string
bts *BootstrapTokenString
Expand All @@ -92,9 +89,7 @@ func TestJSONRoundtrip(t *testing.T) {
}
for _, rt := range tests {
t.Run(rt.input, func(t *testing.T) {
if err := roundtrip(rt.input, rt.bts); err != nil {
t.Errorf("failed BootstrapTokenString JSON roundtrip with error: %v", err)
}
g.Expect(roundtrip(rt.input, rt.bts)).To(Succeed())
})
}
}
Expand Down Expand Up @@ -137,6 +132,8 @@ func roundtrip(input string, bts *BootstrapTokenString) error {
}

func TestTokenFromIDAndSecret(t *testing.T) {
g := NewWithT(t)

var tests = []struct {
bts BootstrapTokenString
expected string
Expand All @@ -147,19 +144,14 @@ func TestTokenFromIDAndSecret(t *testing.T) {
}
for _, rt := range tests {
t.Run(rt.bts.ID, func(t *testing.T) {
actual := rt.bts.String()
if actual != rt.expected {
t.Errorf(
"failed BootstrapTokenString.String():\n\texpected: %s\n\t actual: %s",
rt.expected,
actual,
)
}
g.Expect(rt.bts.String()).To(Equal(rt.expected))
})
}
}

func TestNewBootstrapTokenString(t *testing.T) {
g := NewWithT(t)

var tests = []struct {
token string
expectedError bool
Expand All @@ -185,26 +177,20 @@ func TestNewBootstrapTokenString(t *testing.T) {
for _, rt := range tests {
t.Run(rt.token, func(t *testing.T) {
actual, err := NewBootstrapTokenString(rt.token)
if (err != nil) != rt.expectedError {
t.Errorf(
"failed NewBootstrapTokenString for the token %q\n\texpected error: %t\n\t actual error: %v",
rt.token,
rt.expectedError,
err,
)
} else if !reflect.DeepEqual(actual, rt.bts) {
t.Errorf(
"failed NewBootstrapTokenString for the token %q\n\texpected: %v\n\t actual: %v",
rt.token,
rt.bts,
actual,
)
if rt.expectedError {
g.Expect(err).To(HaveOccurred())
} else {
g.Expect(err).NotTo(HaveOccurred())
}
g.Expect(actual).To(Equal(rt.bts))

})
}
}

func TestNewBootstrapTokenStringFromIDAndSecret(t *testing.T) {
g := NewWithT(t)

var tests = []struct {
id, secret string
expectedError bool
Expand All @@ -227,23 +213,12 @@ func TestNewBootstrapTokenStringFromIDAndSecret(t *testing.T) {
for _, rt := range tests {
t.Run(rt.id, func(t *testing.T) {
actual, err := NewBootstrapTokenStringFromIDAndSecret(rt.id, rt.secret)
if (err != nil) != rt.expectedError {
t.Errorf(
"failed NewBootstrapTokenStringFromIDAndSecret for the token with id %q and secret %q\n\texpected error: %t\n\t actual error: %v",
rt.id,
rt.secret,
rt.expectedError,
err,
)
} else if !reflect.DeepEqual(actual, rt.bts) {
t.Errorf(
"failed NewBootstrapTokenStringFromIDAndSecret for the token with id %q and secret %q\n\texpected: %v\n\t actual: %v",
rt.id,
rt.secret,
rt.bts,
actual,
)
if rt.expectedError {
g.Expect(err).To(HaveOccurred())
} else {
g.Expect(err).NotTo(HaveOccurred())
}
g.Expect(actual).To(Equal(rt.bts))
})
}
}
Loading

0 comments on commit 5426006

Please sign in to comment.