Skip to content

Commit

Permalink
Merge pull request #2681 from cpanato/GH-2433-13
Browse files Browse the repository at this point in the history
🏃 util/tests: standardize gomega imports
  • Loading branch information
k8s-ci-robot committed Mar 16, 2020
2 parents 411cfbd + cb9b2dc commit 1d5ae9d
Show file tree
Hide file tree
Showing 4 changed files with 77 additions and 121 deletions.
61 changes: 19 additions & 42 deletions util/kubeconfig/kubeconfig_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ import (
"crypto/x509"
"crypto/x509/pkix"
"math/big"
"reflect"
"testing"
"time"

Expand Down Expand Up @@ -85,19 +84,17 @@ func setupScheme() *runtime.Scheme {
}

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

clusterKey := client.ObjectKey{
Name: "test1",
Namespace: "test",
}
client := fake.NewFakeClientWithScheme(setupScheme(), validSecret)
found, err := FromSecret(context.Background(), client, clusterKey)
if err != nil {
t.Fatalf("Expected no error, got %v", err)
}

if !reflect.DeepEqual(validSecret.Data[secret.KubeconfigDataName], found) {
t.Fatalf("Expected found secret to be equal to input")
}
found, err := FromSecret(context.Background(), client, clusterKey)
g.Expect(err).NotTo(HaveOccurred())
g.Expect(found).To(Equal(validSecret.Data[secret.KubeconfigDataName]))
}

func getTestCACert(key *rsa.PrivateKey) (*x509.Certificate, error) {
Expand Down Expand Up @@ -132,6 +129,8 @@ func getTestCACert(key *rsa.PrivateKey) (*x509.Certificate, error) {
}

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

testCases := []struct {
cluster string
endpoint string
Expand Down Expand Up @@ -161,47 +160,25 @@ func TestNew(t *testing.T) {

for _, tc := range testCases {
caKey, err := certs.NewPrivateKey()
if err != nil {
t.Fatalf("Failed to generate private key for ca cert, %v", err)
}
g.Expect(err).NotTo(HaveOccurred())

caCert, err := getTestCACert(caKey)
if err != nil {
t.Fatalf("Failed to generate test ca cert, %v", err)
}
g.Expect(err).NotTo(HaveOccurred())

actualConfig, actualError := New(tc.cluster, tc.endpoint, caCert, caKey)
if tc.expectError {
if actualError == nil {
t.Fatalf("Expected error but go nil error")
} else {
continue
}
g.Expect(actualError).To(HaveOccurred())
continue
}

if len(actualConfig.Clusters) != len(tc.expectedConfig.Clusters) {
t.Fatalf("Unexpected number of clusters in generated kubeconfig, Want: %d, Got: %d",
len(tc.expectedConfig.Clusters), len(actualConfig.Clusters))
}
if len(actualConfig.Contexts) != len(tc.expectedConfig.Contexts) {
t.Fatalf("Unexpected number of contexts in generated kubeconfig, Want: %d, Got: %d",
len(tc.expectedConfig.Contexts), len(actualConfig.Contexts))
}
if _, found := actualConfig.Clusters[tc.cluster]; !found {
t.Fatalf("Cluster %q not found in generated kubeconfig", tc.cluster)
}
if _, found := actualConfig.Contexts[tc.expectedConfig.CurrentContext]; !found {
t.Fatalf("Context %q not found in generated kubeconfig", tc.expectedConfig.CurrentContext)
}
if actualConfig.CurrentContext != tc.expectedConfig.CurrentContext {
t.Fatalf("Unexpected value for current config in generated kubeconfig, Want: %q, Got :%q",
tc.expectedConfig.CurrentContext, actualConfig.CurrentContext)
}
if actualConfig.Contexts[tc.expectedConfig.CurrentContext].AuthInfo != tc.expectedConfig.Contexts[tc.expectedConfig.CurrentContext].AuthInfo {
t.Fatalf("Unexpected AuthInfo in context for cluter %q in generated kubeconfig, Want: %q, Got: %q",
tc.cluster, tc.expectedConfig.Contexts[tc.expectedConfig.CurrentContext].AuthInfo,
actualConfig.Contexts[tc.expectedConfig.CurrentContext].AuthInfo)
}
g.Expect(actualConfig.Clusters).To(HaveLen(len(tc.expectedConfig.Clusters)))
g.Expect(actualConfig.Contexts).To(HaveLen(len(tc.expectedConfig.Contexts)))

g.Expect(actualConfig.Clusters[tc.cluster]).NotTo(BeNil())
g.Expect(actualConfig.Contexts[tc.expectedConfig.CurrentContext]).NotTo(BeNil())
g.Expect(actualConfig.CurrentContext).To(Equal(tc.expectedConfig.CurrentContext))
g.Expect(actualConfig.Contexts).To(Equal(tc.expectedConfig.Contexts))

}
}

Expand Down
14 changes: 8 additions & 6 deletions util/secret/certificates_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,27 +19,29 @@ package secret_test
import (
"testing"

. "github.com/onsi/gomega"

"sigs.k8s.io/cluster-api/bootstrap/kubeadm/types/v1beta1"
"sigs.k8s.io/cluster-api/util/secret"
)

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

config := &v1beta1.ClusterConfiguration{}
certs := secret.NewCertificatesForInitialControlPlane(config)
if certs.GetByPurpose(secret.EtcdCA).KeyFile == "" {
t.Fatal("stacked control planes must define etcd CA key file")
}
g.Expect(certs.GetByPurpose(secret.EtcdCA).KeyFile).NotTo(BeEmpty())
}

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

config := &v1beta1.ClusterConfiguration{
Etcd: v1beta1.Etcd{
External: &v1beta1.ExternalEtcd{},
},
}

certs := secret.NewCertificatesForInitialControlPlane(config)
if certs.GetByPurpose(secret.EtcdCA).KeyFile != "" {
t.Fatal("control planes with external etcd must *not* define the etcd key file")
}
g.Expect(certs.GetByPurpose(secret.EtcdCA).KeyFile).To(BeEmpty())
}
72 changes: 28 additions & 44 deletions util/util_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ package util

import (
"context"
"reflect"
"testing"

. "github.com/onsi/gomega"
Expand All @@ -36,6 +35,8 @@ import (
)

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

var testcases = []struct {
name string
input schema.GroupVersionKind
Expand Down Expand Up @@ -103,14 +104,14 @@ func TestMachineToInfrastructureMapFunc(t *testing.T) {
t.Run(tc.name, func(t *testing.T) {
fn := MachineToInfrastructureMapFunc(tc.input)
out := fn(tc.request)
if !reflect.DeepEqual(out, tc.output) {
t.Fatalf("Unexpected output. Got: %v, Want: %v", out, tc.output)
}
g.Expect(out).To(Equal(tc.output))
})
}
}

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

var testcases = []struct {
name string
input schema.GroupVersionKind
Expand Down Expand Up @@ -178,14 +179,14 @@ func TestClusterToInfrastructureMapFunc(t *testing.T) {
t.Run(tc.name, func(t *testing.T) {
fn := ClusterToInfrastructureMapFunc(tc.input)
out := fn(tc.request)
if !reflect.DeepEqual(out, tc.output) {
t.Fatalf("Unexpected output. Got: %v, Want: %v", out, tc.output)
}
g.Expect(out).To(Equal(tc.output))
})
}
}

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

tests := []struct {
name string
refList []metav1.OwnerReference
Expand Down Expand Up @@ -254,14 +255,14 @@ func TestHasOwner(t *testing.T) {
clusterv1.GroupVersion.String(),
[]string{"MachineDeployment", "Cluster"},
)
if test.expected != result {
t.Errorf("expected hasOwner to be %v, got %v", test.expected, result)
}
g.Expect(result).To(Equal(test.expected))
})
}
}

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

targetID := "fri3ndsh1p"

meta := metav1.ObjectMeta{
Expand Down Expand Up @@ -306,19 +307,16 @@ func TestPointsTo(t *testing.T) {
})
}

result := PointsTo(pointer.OwnerReferences, &meta)
if result != test.expected {
t.Errorf("expected %v, got %v", test.expected, result)
}
g.Expect(PointsTo(pointer.OwnerReferences, &meta)).To(Equal(test.expected))
})
}
}

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

scheme := runtime.NewScheme()
if err := clusterv1.AddToScheme(scheme); err != nil {
t.Fatal("failed to register cluster api objects to scheme")
}
g.Expect(clusterv1.AddToScheme(scheme)).To(Succeed())

myCluster := &clusterv1.Cluster{
ObjectMeta: metav1.ObjectMeta{
Expand All @@ -340,19 +338,15 @@ func TestGetOwnerClusterSuccessByName(t *testing.T) {
Name: "my-resource-owned-by-cluster",
}
cluster, err := GetOwnerCluster(context.TODO(), c, objm)
if err != nil {
t.Fatalf("did not expect an error but found one: %v", err)
}
if cluster == nil {
t.Fatal("expected a cluster but got nil")
}
g.Expect(err).NotTo(HaveOccurred())
g.Expect(cluster).NotTo(BeNil())
}

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

scheme := runtime.NewScheme()
if err := clusterv1.AddToScheme(scheme); err != nil {
t.Fatal("failed to register cluster api objects to scheme")
}
g.Expect(clusterv1.AddToScheme(scheme)).To(Succeed())

myMachine := &clusterv1.Machine{
ObjectMeta: metav1.ObjectMeta{
Expand All @@ -374,19 +368,15 @@ func TestGetOwnerMachineSuccessByName(t *testing.T) {
Name: "my-resource-owned-by-machine",
}
machine, err := GetOwnerMachine(context.TODO(), c, objm)
if err != nil {
t.Fatalf("did not expect an error but found one: %v", err)
}
if machine == nil {
t.Fatal("expected a machine but got nil")
}
g.Expect(err).NotTo(HaveOccurred())
g.Expect(machine).NotTo(BeNil())
}

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

scheme := runtime.NewScheme()
if err := clusterv1.AddToScheme(scheme); err != nil {
t.Fatal("failed to register cluster api objects to scheme")
}
g.Expect(clusterv1.AddToScheme(scheme)).To(Succeed())

cluster := &clusterv1.Cluster{
ObjectMeta: metav1.ObjectMeta{
Expand Down Expand Up @@ -433,15 +423,9 @@ func TestGetMachinesForCluster(t *testing.T) {
)

machines, err := GetMachinesForCluster(context.Background(), c, cluster)
if err != nil {
t.Fatalf("failed to get machines for cluster: %s", err)
}
if len(machines.Items) != 1 {
t.Fatalf("expected list to have one machine, found %d", len(machines.Items))
}
if machines.Items[0].Labels[clusterv1.ClusterLabelName] != cluster.Name {
t.Fatalf("expected list to have machine %v, found %v", machine, machines.Items[0])
}
g.Expect(err).NotTo(HaveOccurred())
g.Expect(machines.Items).To(HaveLen(1))
g.Expect(machines.Items[0].Labels[clusterv1.ClusterLabelName]).To(Equal(cluster.Name))
}

func TestEnsureOwnerRef(t *testing.T) {
Expand Down
Loading

0 comments on commit 1d5ae9d

Please sign in to comment.