diff --git a/cmd/clusterctl/client/common_test.go b/cmd/clusterctl/client/common_test.go index 8afdb73526b7..8c44e866f422 100644 --- a/cmd/clusterctl/client/common_test.go +++ b/cmd/clusterctl/client/common_test.go @@ -16,9 +16,15 @@ limitations under the License. package client -import "testing" +import ( + "testing" + + . "github.com/onsi/gomega" +) func Test_parseProviderName(t *testing.T) { + g := NewWithT(t) + type args struct { provider string } @@ -51,15 +57,14 @@ func Test_parseProviderName(t *testing.T) { for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { gotName, gotVersion, err := parseProviderName(tt.args.provider) - if (err != nil) != tt.wantErr { - t.Fatalf("error = %v, wantErr %v", err, tt.wantErr) - } - if gotName != tt.wantName { - t.Errorf("gotName = %v, want %v", gotName, tt.wantName) - } - if gotVersion != tt.wantVersion { - t.Errorf("gotVersion = %v, want %v", gotVersion, tt.wantVersion) + if tt.wantErr { + g.Expect(err).To(HaveOccurred()) + } else { + g.Expect(err).NotTo(HaveOccurred()) } + g.Expect(gotName).To(Equal(tt.wantName)) + + g.Expect(gotVersion).To(Equal(tt.wantVersion)) }) } } diff --git a/cmd/clusterctl/client/config_test.go b/cmd/clusterctl/client/config_test.go index 425c0d344104..759645bd4257 100644 --- a/cmd/clusterctl/client/config_test.go +++ b/cmd/clusterctl/client/config_test.go @@ -21,9 +21,10 @@ import ( "io/ioutil" "os" "path/filepath" - "reflect" "testing" + . "github.com/onsi/gomega" + corev1 "k8s.io/api/core/v1" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" clusterctlv1 "sigs.k8s.io/cluster-api/cmd/clusterctl/api/v1alpha3" @@ -31,6 +32,8 @@ import ( ) func Test_clusterctlClient_GetProvidersConfig(t *testing.T) { + g := NewWithT(t) + customProviderConfig := config.NewProvider("custom", "url", clusterctlv1.BootstrapProviderType) type field struct { @@ -81,30 +84,25 @@ func Test_clusterctlClient_GetProvidersConfig(t *testing.T) { for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { got, err := tt.field.client.GetProvidersConfig() - if tt.wantErr != (err != nil) { - t.Fatalf("error = %v, wantErr = %v", err, tt.wantErr) - } if tt.wantErr { + g.Expect(err).To(HaveOccurred()) return } - if len(got) != len(tt.wantProviders) { - t.Errorf("got = %v items, want %v items", len(got), len(tt.wantProviders)) - return - } + g.Expect(err).NotTo(HaveOccurred()) + g.Expect(got).To(HaveLen(len(tt.wantProviders))) - for i, g := range got { + for i, gotProvider := range got { w := tt.wantProviders[i] - - if g.Name() != w { - t.Errorf("Item[%d].Name() got = %v, want = %v ", i, g.Name(), w) - } + g.Expect(gotProvider.Name()).To(Equal(w)) } }) } } func Test_clusterctlClient_GetProviderComponents(t *testing.T) { + g := NewWithT(t) + config1 := newFakeConfig(). WithProvider(capiProviderConfig) @@ -157,25 +155,21 @@ func Test_clusterctlClient_GetProviderComponents(t *testing.T) { for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { got, err := client.GetProviderComponents(tt.args.provider, capiProviderConfig.Type(), tt.args.targetNameSpace, tt.args.watchingNamespace) - if tt.wantErr != (err != nil) { - t.Fatalf("error = %v, wantErr = %v", err, tt.wantErr) - } if tt.wantErr { + g.Expect(err).To(HaveOccurred()) return } + g.Expect(err).NotTo(HaveOccurred()) - if got.Name() != tt.want.provider.Name() { - t.Errorf("got.Name() = %v, want = %v ", got.Name(), tt.want.provider.Name()) - } - - if got.Version() != tt.want.version { - t.Errorf("got.Version() = %v, want = %v ", got.Version(), tt.want.version) - } + g.Expect(got.Name()).To(Equal(tt.want.provider.Name())) + g.Expect(got.Version()).To(Equal(tt.want.version)) }) } } func Test_clusterctlClient_templateOptionsToVariables(t *testing.T) { + g := NewWithT(t) + type args struct { options GetClusterTemplateOptions } @@ -290,40 +284,33 @@ func Test_clusterctlClient_templateOptionsToVariables(t *testing.T) { configClient: config, } err := c.templateOptionsToVariables(tt.args.options) - if tt.wantErr != (err != nil) { - t.Fatalf("error = %v, wantErr = %v", err, tt.wantErr) - } if tt.wantErr { + g.Expect(err).To(HaveOccurred()) return } + g.Expect(err).NotTo(HaveOccurred()) for name, wantValue := range tt.wantVars { gotValue, err := config.Variables().Get(name) - if err != nil { - t.Fatalf("variable %s is not definied in config variables", name) - } - if gotValue != wantValue { - t.Errorf("variable %s, got = %v, want %v", name, gotValue, wantValue) - } + g.Expect(err).NotTo(HaveOccurred()) + g.Expect(gotValue).To(Equal(wantValue)) } }) } } func Test_clusterctlClient_GetClusterTemplate(t *testing.T) { + g := NewWithT(t) + rawTemplate := templateYAML("ns3", "${ CLUSTER_NAME }") // Template on a file tmpDir, err := ioutil.TempDir("", "cc") - if err != nil { - t.Fatal(err) - } + g.Expect(err).NotTo(HaveOccurred()) defer os.RemoveAll(tmpDir) path := filepath.Join(tmpDir, "cluster-template.yaml") - if err := ioutil.WriteFile(path, rawTemplate, 0644); err != nil { - t.Fatalf("err: %s", err) - } + g.Expect(ioutil.WriteFile(path, rawTemplate, 0644)).To(Succeed()) // Template on a repository & in a ConfigMap configMap := &corev1.ConfigMap{ @@ -476,27 +463,18 @@ func Test_clusterctlClient_GetClusterTemplate(t *testing.T) { for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { got, err := client.GetClusterTemplate(tt.args.options) - if tt.wantErr != (err != nil) { - t.Fatalf("error = %v, wantErr = %v", err, tt.wantErr) - } if tt.wantErr { + g.Expect(err).To(HaveOccurred()) return } + g.Expect(err).NotTo(HaveOccurred()) - if !reflect.DeepEqual(got.Variables(), tt.want.variables) { - t.Errorf("Variables() got = %v, want %v", got.Variables(), tt.want.variables) - } - if got.TargetNamespace() != tt.want.targetNamespace { - t.Errorf("TargetNamespace() got = %v, want %v", got.TargetNamespace(), tt.want.targetNamespace) - } + g.Expect(got.Variables()).To(Equal(tt.want.variables)) + g.Expect(got.TargetNamespace()).To(Equal(tt.want.targetNamespace)) gotYaml, err := got.Yaml() - if err != nil { - t.Fatalf("Yaml() error = %v, wantErr nil", err) - } - if !reflect.DeepEqual(gotYaml, tt.want.yaml) { - t.Errorf("Yaml() got = %v, want %v", gotYaml, tt.want.yaml) - } + g.Expect(err).NotTo(HaveOccurred()) + g.Expect(gotYaml).To(Equal(tt.want.yaml)) }) } } diff --git a/cmd/clusterctl/client/delete_test.go b/cmd/clusterctl/client/delete_test.go index 9db2c771e3ef..121947a434bb 100644 --- a/cmd/clusterctl/client/delete_test.go +++ b/cmd/clusterctl/client/delete_test.go @@ -18,14 +18,17 @@ package client import ( "context" - "reflect" "testing" + . "github.com/onsi/gomega" + "k8s.io/apimachinery/pkg/util/sets" clusterctlv1 "sigs.k8s.io/cluster-api/cmd/clusterctl/api/v1alpha3" ) func Test_clusterctlClient_Delete(t *testing.T) { + g := NewWithT(t) + type fields struct { client *fakeClient } @@ -105,33 +108,26 @@ func Test_clusterctlClient_Delete(t *testing.T) { } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { - if err := tt.fields.client.Delete(tt.args.options); (err != nil) != tt.wantErr { - t.Fatalf("error = %v, wantErr %v", err, tt.wantErr) - } + err := tt.fields.client.Delete(tt.args.options) if tt.wantErr { + g.Expect(err).To(HaveOccurred()) return } + g.Expect(err).NotTo(HaveOccurred()) proxy := tt.fields.client.clusters["kubeconfig"].Proxy() gotProviders := &clusterctlv1.ProviderList{} c, err := proxy.NewClient() - if err != nil { - t.Fatalf("failed to create client %v", err) - } - - if err := c.List(context.TODO(), gotProviders); err != nil { - t.Fatalf("failed to read providers %v", err) - } + g.Expect(err).NotTo(HaveOccurred()) + g.Expect(c.List(context.Background(), gotProviders)).To(Succeed()) gotProvidersSet := sets.NewString() for _, gotProvider := range gotProviders.Items { gotProvidersSet.Insert(gotProvider.Name) } - if !reflect.DeepEqual(gotProvidersSet, tt.wantProviders) { - t.Errorf("got = %v providers, want %v", len(gotProviders.Items), len(tt.wantProviders)) - } + g.Expect(gotProvidersSet).To(Equal(tt.wantProviders)) }) } } diff --git a/cmd/clusterctl/client/init_test.go b/cmd/clusterctl/client/init_test.go index 6accf80f7642..e7eb5c90fc5b 100644 --- a/cmd/clusterctl/client/init_test.go +++ b/cmd/clusterctl/client/init_test.go @@ -20,6 +20,8 @@ import ( "fmt" "testing" + . "github.com/onsi/gomega" + clusterctlv1 "sigs.k8s.io/cluster-api/cmd/clusterctl/api/v1alpha3" "sigs.k8s.io/cluster-api/cmd/clusterctl/client/config" "sigs.k8s.io/cluster-api/cmd/clusterctl/internal/test" @@ -27,6 +29,8 @@ import ( ) func Test_clusterctlClient_Init(t *testing.T) { + g := NewWithT(t) + type field struct { client *fakeClient hasCRD bool @@ -323,9 +327,7 @@ func Test_clusterctlClient_Init(t *testing.T) { t.Run(tt.name, func(t *testing.T) { if tt.field.hasCRD { - if err := tt.field.client.clusters["kubeconfig"].ProviderInventory().EnsureCustomResourceDefinitions(); err != nil { - t.Fatalf("EnsureMetadata() error = %v", err) - } + g.Expect(tt.field.client.clusters["kubeconfig"].ProviderInventory().EnsureCustomResourceDefinitions()).To(Succeed()) } got, err := tt.field.client.Init(InitOptions{ @@ -337,41 +339,20 @@ func Test_clusterctlClient_Init(t *testing.T) { TargetNamespace: tt.args.targetNameSpace, WatchingNamespace: tt.args.watchingNamespace, }) - - if (err != nil) != tt.wantErr { - t.Fatalf("error = %v, wantErr %v", err, tt.wantErr) - } if tt.wantErr { + g.Expect(err).To(HaveOccurred()) return } + g.Expect(err).NotTo(HaveOccurred()) - if len(got) != len(tt.want) { - t.Errorf("got = %v items, want %v items", len(got), len(tt.want)) - return - } - - for i, g := range got { + g.Expect(got).To(HaveLen(len(tt.want))) + for i, gItem := range got { w := tt.want[i] - - if g.Name() != w.provider.Name() { - t.Errorf("Item[%d].Name() got = %v, want = %v ", i, g.Name(), w.provider.Name()) - } - - if g.Type() != w.provider.Type() { - t.Errorf("Item[%d].Type() got = %v, want = %v ", i, g.Type(), w.provider.Type()) - } - - if g.Version() != w.version { - t.Errorf("Item[%d].Version() got = %v, want = %v ", i, g.Version(), w.version) - } - - if g.TargetNamespace() != w.targetNamespace { - t.Errorf("Item[%d].TargetNamespace() got = %v, want = %v ", i, g.TargetNamespace(), w.targetNamespace) - } - - if g.WatchingNamespace() != w.watchingNamespace { - t.Errorf("Item[%d].WatchingNamespace() got = %v, want = %v ", i, g.WatchingNamespace(), w.watchingNamespace) - } + g.Expect(gItem.Name()).To(Equal(w.provider.Name())) + g.Expect(gItem.Type()).To(Equal(w.provider.Type())) + g.Expect(gItem.Version()).To(Equal(w.version)) + g.Expect(gItem.TargetNamespace()).To(Equal(w.targetNamespace)) + g.Expect(gItem.WatchingNamespace()).To(Equal(w.watchingNamespace)) } }) } diff --git a/cmd/clusterctl/client/upgrade_test.go b/cmd/clusterctl/client/upgrade_test.go index 90f54892659c..68d236e15d42 100644 --- a/cmd/clusterctl/client/upgrade_test.go +++ b/cmd/clusterctl/client/upgrade_test.go @@ -18,10 +18,11 @@ package client import ( "context" - "reflect" "sort" "testing" + . "github.com/onsi/gomega" + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" clusterv1 "sigs.k8s.io/cluster-api/api/v1alpha3" clusterctlv1 "sigs.k8s.io/cluster-api/cmd/clusterctl/api/v1alpha3" @@ -30,6 +31,8 @@ import ( ) func Test_clusterctlClient_ApplyUpgrade(t *testing.T) { + g := NewWithT(t) + type fields struct { client *fakeClient } @@ -162,24 +165,20 @@ func Test_clusterctlClient_ApplyUpgrade(t *testing.T) { } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { - if err := tt.fields.client.ApplyUpgrade(tt.args.options); (err != nil) != tt.wantErr { - t.Fatalf("error = %v, wantErr %v", err, tt.wantErr) - } + err := tt.fields.client.ApplyUpgrade(tt.args.options) if tt.wantErr { + g.Expect(err).To(HaveOccurred()) return } + g.Expect(err).NotTo(HaveOccurred()) proxy := tt.fields.client.clusters["kubeconfig"].Proxy() gotProviders := &clusterctlv1.ProviderList{} c, err := proxy.NewClient() - if err != nil { - t.Fatalf("failed to create client %v", err) - } + g.Expect(err).NotTo(HaveOccurred()) - if err := c.List(context.TODO(), gotProviders); err != nil { - t.Fatalf("failed to read providers %v", err) - } + g.Expect(c.List(context.Background(), gotProviders)).To(Succeed()) sort.Slice(gotProviders.Items, func(i, j int) bool { return gotProviders.Items[i].Name < gotProviders.Items[j].Name @@ -190,10 +189,7 @@ func Test_clusterctlClient_ApplyUpgrade(t *testing.T) { for i := range gotProviders.Items { tt.wantProviders.Items[i].ResourceVersion = gotProviders.Items[i].ResourceVersion } - - if !reflect.DeepEqual(gotProviders, tt.wantProviders) { - t.Errorf("got = %v, want %v", gotProviders, tt.wantProviders) - } + g.Expect(gotProviders).To(Equal(tt.wantProviders)) }) } } @@ -264,6 +260,8 @@ func fakeProvider(name string, providerType clusterctlv1.ProviderType, version, } func Test_parseUpgradeItem(t *testing.T) { + g := NewWithT(t) + type args struct { provider string } @@ -329,17 +327,13 @@ func Test_parseUpgradeItem(t *testing.T) { for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { got, err := parseUpgradeItem(tt.args.provider, clusterctlv1.CoreProviderType) - if (err != nil) != tt.wantErr { - t.Fatalf("error = %v, wantErr %v", err, tt.wantErr) - } if tt.wantErr { + g.Expect(err).To(HaveOccurred()) return } + g.Expect(err).NotTo(HaveOccurred()) - if !reflect.DeepEqual(got, tt.want) { - t.Errorf("got = %v, want %v", got, tt.want) - } - + g.Expect(got).To(Equal(tt.want)) }) } }