diff --git a/cmd/clusterctl/client/repository/client_test.go b/cmd/clusterctl/client/repository/client_test.go index 6c68ffa34dab..3a0a89383e47 100644 --- a/cmd/clusterctl/client/repository/client_test.go +++ b/cmd/clusterctl/client/repository/client_test.go @@ -20,12 +20,16 @@ import ( "os" "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" ) func Test_newRepositoryClient_LocalFileSystemRepository(t *testing.T) { + g := NewWithT(t) + tmpDir := createTempDir(t) defer os.RemoveAll(tmpDir) @@ -33,9 +37,7 @@ func Test_newRepositoryClient_LocalFileSystemRepository(t *testing.T) { dst2 := createLocalTestProviderFile(t, tmpDir, "bootstrap-bar/v2.0.0/bootstrap-components.yaml", "") configClient, err := config.New("", config.InjectReader(test.NewFakeReader())) - if err != nil { - t.Fatal(err) - } + g.Expect(err).NotTo(HaveOccurred()) type fields struct { provider config.Provider @@ -60,12 +62,10 @@ func Test_newRepositoryClient_LocalFileSystemRepository(t *testing.T) { for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { repoClient, err := newRepositoryClient(tt.fields.provider, configClient) - if err != nil { - t.Fatalf("got error %v when none was expected", err) - } - if _, ok := repoClient.repository.(*localRepository); !ok { - t.Fatalf("got repository of type %T when *repository.localRepository was expected", repoClient.repository) - } + g.Expect(err).NotTo(HaveOccurred()) + + _, ok := repoClient.repository.(*localRepository) + g.Expect(ok).To(BeTrue()) }) } } diff --git a/cmd/clusterctl/client/repository/components_client_test.go b/cmd/clusterctl/client/repository/components_client_test.go index 4a496b3854f8..51ed4a9ef95d 100644 --- a/cmd/clusterctl/client/repository/components_client_test.go +++ b/cmd/clusterctl/client/repository/components_client_test.go @@ -17,11 +17,11 @@ limitations under the License. package repository import ( - "bytes" "fmt" - "reflect" "testing" + . "github.com/onsi/gomega" + clusterv1 "sigs.k8s.io/cluster-api/api/v1alpha3" clusterctlv1 "sigs.k8s.io/cluster-api/cmd/clusterctl/api/v1alpha3" "sigs.k8s.io/cluster-api/cmd/clusterctl/client/config" @@ -59,12 +59,12 @@ var configMapYaml = []byte("apiVersion: v1\n" + " name: manager") func Test_componentsClient_Get(t *testing.T) { + g := NewWithT(t) + p1 := config.NewProvider("p1", "", clusterctlv1.BootstrapProviderType) configClient, err := config.New("", config.InjectReader(test.NewFakeReader().WithVar(variableName, variableValue))) - if err != nil { - t.Fatal(err) - } + g.Expect(err).NotTo(HaveOccurred()) type fields struct { provider config.Provider @@ -233,36 +233,19 @@ func Test_componentsClient_Get(t *testing.T) { t.Run(tt.name, func(t *testing.T) { f := newComponentsClient(tt.fields.provider, tt.fields.repository, configClient) got, err := f.Get(tt.args.version, tt.args.targetNamespace, 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 } - if got.Name() != tt.want.provider.Name() { - t.Errorf("got.Name() = %v, want = %v ", got.Name(), tt.want.provider.Name()) - } - - if got.Type() != tt.want.provider.Type() { - t.Errorf("got.Type() = %v, want = %v ", got.Type(), tt.want.provider.Type()) - } - - if got.Version() != tt.want.version { - t.Errorf("got.Version() = %v, want = %v ", got.Version(), tt.want.version) - } - - if got.TargetNamespace() != tt.want.targetNamespace { - t.Errorf("got.TargetNamespace() = %v, want = %v ", got.TargetNamespace(), tt.want.targetNamespace) - } - - if got.WatchingNamespace() != tt.want.watchingNamespace { - t.Errorf("got.WatchingNamespace() = %v, want = %v ", got.WatchingNamespace(), tt.want.watchingNamespace) - } + g.Expect(err).NotTo(HaveOccurred()) - if !reflect.DeepEqual(got.Variables(), tt.want.variables) { - t.Errorf("got.Variables() = %v, want = %v ", got.WatchingNamespace(), tt.want.watchingNamespace) - } + g.Expect(got.Name()).To(Equal(tt.want.provider.Name())) + g.Expect(got.Type()).To(Equal(tt.want.provider.Type())) + g.Expect(got.Version()).To(Equal(tt.want.version)) + g.Expect(got.TargetNamespace()).To(Equal(tt.want.targetNamespace)) + g.Expect(got.WatchingNamespace()).To(Equal(tt.want.watchingNamespace)) + g.Expect(got.Variables()).To(Equal(tt.want.variables)) yaml, err := got.Yaml() if err != nil { @@ -270,27 +253,21 @@ func Test_componentsClient_Get(t *testing.T) { return } - if len(tt.want.variables) > 0 && !bytes.Contains(yaml, []byte(variableValue)) { - t.Errorf("got.Yaml() does not containt value %s that is a replacement of %s variable", variableValue, variableName) - } - - if len(tt.want.variables) > 0 && !bytes.Contains(yaml, []byte(variableValue)) { - t.Errorf("got.Yaml() does not containt value %s that is a replacement of %s variable", variableValue, variableName) + if len(tt.want.variables) > 0 { + g.Expect(yaml).To(ContainSubstring(variableValue)) } for _, o := range got.InstanceObjs() { for _, v := range []string{clusterctlv1.ClusterctlLabelName, clusterv1.ProviderLabelName} { - if _, ok := o.GetLabels()[v]; !ok { - t.Errorf("got.InstanceObjs() object %s does not contains %s label", o.GetName(), v) - } + _, ok := o.GetLabels()[v] + g.Expect(ok).To(BeTrue()) } } for _, o := range got.SharedObjs() { for _, v := range []string{clusterctlv1.ClusterctlLabelName, clusterv1.ProviderLabelName, clusterctlv1.ClusterctlResourceLifecyleLabelName} { - if _, ok := o.GetLabels()[v]; !ok { - t.Errorf("got.SharedObjs() object %s does not contains %s label", o.GetName(), v) - } + _, ok := o.GetLabels()[v] + g.Expect(ok).To(BeTrue()) } } }) diff --git a/cmd/clusterctl/client/repository/components_test.go b/cmd/clusterctl/client/repository/components_test.go index e610942995f8..c00eeaf4cf22 100644 --- a/cmd/clusterctl/client/repository/components_test.go +++ b/cmd/clusterctl/client/repository/components_test.go @@ -18,9 +18,10 @@ package repository import ( "fmt" - "reflect" "testing" + . "github.com/onsi/gomega" + "k8s.io/apimachinery/pkg/apis/meta/v1/unstructured" clusterv1 "sigs.k8s.io/cluster-api/api/v1alpha3" clusterctlv1 "sigs.k8s.io/cluster-api/cmd/clusterctl/api/v1alpha3" @@ -29,6 +30,8 @@ import ( ) func Test_inspectVariables(t *testing.T) { + g := NewWithT(t) + type args struct { data string } @@ -61,14 +64,14 @@ func Test_inspectVariables(t *testing.T) { } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { - if got := inspectVariables([]byte(tt.args.data)); !reflect.DeepEqual(got, tt.want) { - t.Errorf("inspectVariables() = %v, want %v", got, tt.want) - } + g.Expect(inspectVariables([]byte(tt.args.data))).To(ConsistOf(tt.want)) }) } } func Test_replaceVariables(t *testing.T) { + g := NewWithT(t) + type args struct { yaml []byte variables []string @@ -105,21 +108,20 @@ func Test_replaceVariables(t *testing.T) { for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { got, err := replaceVariables(tt.args.yaml, tt.args.variables, tt.args.configVariablesClient) - if (err != nil) != tt.wantErr { - t.Fatalf("error = %v, wantErr %v", err, tt.wantErr) - } if tt.wantErr { + g.Expect(err).To(HaveOccurred()) return } - if !reflect.DeepEqual(got, tt.want) { - t.Errorf("got = %v, want %v", got, tt.want) - } + g.Expect(err).NotTo(HaveOccurred()) + g.Expect(got).To(ConsistOf(tt.want)) }) } } func Test_inspectTargetNamespace(t *testing.T) { + g := NewWithT(t) + type args struct { objs []unstructured.Unstructured } @@ -181,21 +183,20 @@ func Test_inspectTargetNamespace(t *testing.T) { for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { got, err := inspectTargetNamespace(tt.args.objs) - if (err != nil) != tt.wantErr { - t.Fatalf("error = %v, wantErr %v", err, tt.wantErr) - } if tt.wantErr { + g.Expect(err).To(HaveOccurred()) return } - if got != tt.want { - t.Errorf("got = %v, want %v", got, tt.want) - } + g.Expect(err).NotTo(HaveOccurred()) + g.Expect(got).To(Equal(tt.want)) }) } } func Test_fixTargetNamespace(t *testing.T) { + g := NewWithT(t) + type args struct { objs []unstructured.Unstructured targetNamespace string @@ -278,14 +279,15 @@ func Test_fixTargetNamespace(t *testing.T) { } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { - if got := fixTargetNamespace(tt.args.objs, tt.args.targetNamespace); !reflect.DeepEqual(got[0], tt.want[0]) { //skipping from test the automatically added namespace Object - t.Errorf("got = %v, want %v", got, tt.want) - } + got := fixTargetNamespace(tt.args.objs, tt.args.targetNamespace) + g.Expect(got).To(ContainElements(tt.want)) //skipping from test the automatically added namespace Object }) } } func Test_addNamespaceIfMissing(t *testing.T) { + g := NewWithT(t) + type args struct { objs []unstructured.Unstructured targetNamespace string @@ -323,18 +325,15 @@ func Test_addNamespaceIfMissing(t *testing.T) { got := addNamespaceIfMissing(tt.args.objs, tt.args.targetNamespace) wgot, err := inspectTargetNamespace(got) - if err != nil { - t.Fatal(err) - } - - if wgot != tt.args.targetNamespace { - t.Errorf("got.targetNamespace = %v, want %v", wgot, tt.args.targetNamespace) - } + g.Expect(err).NotTo(HaveOccurred()) + g.Expect(wgot).To(Equal(tt.args.targetNamespace)) }) } } func Test_fixRBAC(t *testing.T) { + g := NewWithT(t) + type args struct { objs []unstructured.Unstructured targetNamespace string @@ -584,16 +583,13 @@ func Test_fixRBAC(t *testing.T) { for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { got, err := fixRBAC(tt.args.objs, tt.args.targetNamespace) - if (err != nil) != tt.wantErr { - t.Fatalf("error = %v, wantErr %v", err, tt.wantErr) - } if tt.wantErr { + g.Expect(err).To(HaveOccurred()) return } - if !reflect.DeepEqual(got, tt.want) { - t.Errorf("got = %v, want %v", got, tt.want) - } + g.Expect(err).NotTo(HaveOccurred()) + g.Expect(got).To(Equal(tt.want)) }) } } @@ -624,6 +620,8 @@ func fakeDeployment(watchNamespace string) unstructured.Unstructured { } func Test_inspectWatchNamespace(t *testing.T) { + g := NewWithT(t) + type args struct { objs []unstructured.Unstructured } @@ -674,21 +672,20 @@ func Test_inspectWatchNamespace(t *testing.T) { for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { got, err := inspectWatchNamespace(tt.args.objs) - if (err != nil) != tt.wantErr { - t.Fatalf("error = %v, wantErr %v", err, tt.wantErr) - } if tt.wantErr { + g.Expect(err).To(HaveOccurred()) return } - if got != tt.want { - t.Errorf("got = %v, want %v", got, tt.want) - } + g.Expect(err).NotTo(HaveOccurred()) + g.Expect(got).To(Equal(tt.want)) }) } } func Test_fixWatchNamespace(t *testing.T) { + g := NewWithT(t) + type args struct { objs []unstructured.Unstructured watchingNamespace string @@ -732,26 +729,23 @@ func Test_fixWatchNamespace(t *testing.T) { for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { got, err := fixWatchNamespace(tt.args.objs, 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 } - wgot, err := inspectWatchNamespace(got) - if err != nil { - t.Fatal(err) - } + g.Expect(err).NotTo(HaveOccurred()) - if wgot != tt.args.watchingNamespace { - t.Errorf("got.watchingNamespace = %v, want %v", wgot, tt.args.watchingNamespace) - } + wgot, err := inspectWatchNamespace(got) + g.Expect(err).NotTo(HaveOccurred()) + g.Expect(wgot).To(Equal(tt.args.watchingNamespace)) }) } } func Test_addCommonLabels(t *testing.T) { + g := NewWithT(t) + type args struct { objs []unstructured.Unstructured name string @@ -792,14 +786,14 @@ func Test_addCommonLabels(t *testing.T) { } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { - if got := addCommonLabels(tt.args.objs, config.NewProvider(tt.args.name, "", tt.args.providerType)); !reflect.DeepEqual(got, tt.want) { - t.Errorf("got = %v, want %v", got, tt.want) - } + g.Expect(addCommonLabels(tt.args.objs, config.NewProvider(tt.args.name, "", tt.args.providerType))).To(Equal(tt.want)) }) } } func Test_splitInstanceAndSharedResources(t *testing.T) { + g := NewWithT(t) + type args struct { objs []unstructured.Unstructured } @@ -942,12 +936,8 @@ func Test_splitInstanceAndSharedResources(t *testing.T) { for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { gotInstanceObjs, gotWebHookObjs := splitInstanceAndSharedResources(tt.args.objs) - if !reflect.DeepEqual(gotInstanceObjs, tt.wantInstanceObjs) { - t.Errorf("splitInstanceAndSharedResources() gotInstanceObjs = %v, want %v", gotInstanceObjs, tt.wantInstanceObjs) - } - if !reflect.DeepEqual(gotWebHookObjs, tt.wantSharedObjs) { - t.Errorf("splitInstanceAndSharedResources() gotWebHookObjs = %v, want %v", gotWebHookObjs, tt.wantSharedObjs) - } + g.Expect(gotInstanceObjs).To(ConsistOf(tt.wantInstanceObjs)) + g.Expect(gotWebHookObjs).To(ConsistOf(tt.wantSharedObjs)) }) } } diff --git a/cmd/clusterctl/client/repository/metadata_client_test.go b/cmd/clusterctl/client/repository/metadata_client_test.go index 120770579cb8..bd5f63f20362 100644 --- a/cmd/clusterctl/client/repository/metadata_client_test.go +++ b/cmd/clusterctl/client/repository/metadata_client_test.go @@ -17,9 +17,10 @@ limitations under the License. package repository import ( - "reflect" "testing" + . "github.com/onsi/gomega" + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" clusterctlv1 "sigs.k8s.io/cluster-api/cmd/clusterctl/api/v1alpha3" "sigs.k8s.io/cluster-api/cmd/clusterctl/client/config" @@ -35,6 +36,8 @@ var metadataYaml = []byte("apiVersion: clusterctl.cluster.x-k8s.io/v1alpha3\n" + "") func Test_metadataClient_Get(t *testing.T) { + g := NewWithT(t) + type fields struct { provider config.Provider version string @@ -139,16 +142,13 @@ func Test_metadataClient_Get(t *testing.T) { repository: tt.fields.repository, } got, err := f.Get() - if (err != nil) != tt.wantErr { - t.Fatalf("error = %v, wantErr %v", err, tt.wantErr) - } if tt.wantErr { + g.Expect(err).To(HaveOccurred()) return } - if !reflect.DeepEqual(got, tt.want) { - t.Errorf("got = %v, want %v", got, tt.want) - } + g.Expect(err).NotTo(HaveOccurred()) + g.Expect(got).To(Equal(tt.want)) }) } } diff --git a/cmd/clusterctl/client/repository/repository_github_test.go b/cmd/clusterctl/client/repository/repository_github_test.go index a4a47c2e5d5c..2eac3402d4fd 100644 --- a/cmd/clusterctl/client/repository/repository_github_test.go +++ b/cmd/clusterctl/client/repository/repository_github_test.go @@ -22,7 +22,10 @@ import ( "reflect" "testing" + . "github.com/onsi/gomega" + "github.com/google/go-github/github" + "k8s.io/utils/pointer" 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" @@ -35,6 +38,8 @@ import ( //TODO: test getComponentsPath func Test_gitHubRepository_getVersions(t *testing.T) { + g := NewWithT(t) + client, mux, teardown := test.NewFakeGitHub() defer teardown() @@ -72,28 +77,27 @@ func Test_gitHubRepository_getVersions(t *testing.T) { } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { - g, err := newGitHubRepository(tt.field.providerConfig, configVariablesClient) - if err != nil { - t.Fatal(err) - } - g.injectClient = client + gitHub, err := newGitHubRepository(tt.field.providerConfig, configVariablesClient) + g.Expect(err).NotTo(HaveOccurred()) - got, err := g.getVersions() - if (err != nil) != tt.wantErr { - t.Fatalf("error = %v, wantErr %v", err, tt.wantErr) - } + gitHub.injectClient = client + + got, err := gitHub.getVersions() if tt.wantErr { + g.Expect(err).To(HaveOccurred()) return } - if len(got) != len(tt.want) { - t.Errorf("got = %v, want %v", got, tt.want) - } + g.Expect(err).NotTo(HaveOccurred()) + g.Expect(got).To(HaveLen(len(tt.want))) + g.Expect(got).To(ConsistOf(tt.want)) }) } } func Test_gitHubRepository_getLatestRelease(t *testing.T) { + g := NewWithT(t) + client, mux, teardown := test.NewFakeGitHub() defer teardown() @@ -144,28 +148,26 @@ func Test_gitHubRepository_getLatestRelease(t *testing.T) { } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { - g, err := newGitHubRepository(tt.field.providerConfig, configVariablesClient) - if err != nil { - t.Fatal(err) - } - g.injectClient = client + gRepo, err := newGitHubRepository(tt.field.providerConfig, configVariablesClient) + g.Expect(err).NotTo(HaveOccurred()) - got, err := g.getLatestRelease() - if (err != nil) != tt.wantErr { - t.Fatalf("error = %v, wantErr %v", err, tt.wantErr) - } + gRepo.injectClient = client + + got, err := gRepo.getLatestRelease() if tt.wantErr { + g.Expect(err).To(HaveOccurred()) return } - if got != tt.want { - t.Errorf("got = %v, want %v", got, tt.want) - } + g.Expect(err).NotTo(HaveOccurred()) + g.Expect(got).To(Equal(tt.want)) }) } } func Test_gitHubRepository_getReleaseByTag(t *testing.T) { + g := NewWithT(t) + client, mux, teardown := test.NewFakeGitHub() defer teardown() @@ -185,7 +187,7 @@ func Test_gitHubRepository_getReleaseByTag(t *testing.T) { tests := []struct { name string args args - wantTagName string + wantTagName *string wantErr bool }{ { @@ -193,7 +195,7 @@ func Test_gitHubRepository_getReleaseByTag(t *testing.T) { args: args{ tag: "foo", }, - wantTagName: "v0.4.1", + wantTagName: pointer.StringPtr("v0.4.1"), wantErr: false, }, { @@ -201,38 +203,31 @@ func Test_gitHubRepository_getReleaseByTag(t *testing.T) { args: args{ tag: "bar", }, - wantTagName: "", + wantTagName: nil, wantErr: true, }, } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { - g, err := newGitHubRepository(providerConfig, configVariablesClient) - if err != nil { - t.Fatal(err) - } - g.injectClient = client + gRepo, err := newGitHubRepository(providerConfig, configVariablesClient) + g.Expect(err).NotTo(HaveOccurred()) - got, err := g.getReleaseByTag(tt.args.tag) - if (err != nil) != tt.wantErr { - t.Fatalf("error = %v, wantErr %v", err, tt.wantErr) - } + gRepo.injectClient = client + + got, err := gRepo.getReleaseByTag(tt.args.tag) if tt.wantErr { + g.Expect(err).To(HaveOccurred()) return } - if tt.wantTagName == "" && got == nil { - return - } + g.Expect(err).NotTo(HaveOccurred()) - if tt.wantTagName != "" && got != nil { - if *got.TagName != tt.wantTagName { - t.Errorf("got = %v, want %v", *got.TagName, tt.wantTagName) - } + if tt.wantTagName == nil { + g.Expect(got).To(BeNil()) return } - t.Errorf("got = %v, want.TagName %v", got, tt.wantTagName) + g.Expect(got.TagName).To(Equal(tt.wantTagName)) }) } } diff --git a/cmd/clusterctl/client/repository/repository_local_test.go b/cmd/clusterctl/client/repository/repository_local_test.go index da24e687d2e0..c29624d46b77 100644 --- a/cmd/clusterctl/client/repository/repository_local_test.go +++ b/cmd/clusterctl/client/repository/repository_local_test.go @@ -20,15 +20,18 @@ import ( "io/ioutil" "os" "path/filepath" - "sort" "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" ) func Test_localRepository_newLocalRepository(t *testing.T) { + g := NewWithT(t) + type fields struct { provider config.Provider configVariablesClient config.VariablesClient @@ -107,28 +110,18 @@ func Test_localRepository_newLocalRepository(t *testing.T) { for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { got, err := newLocalRepository(tt.fields.provider, tt.fields.configVariablesClient) - if (err != nil) != tt.wantErr { - t.Fatalf("error = %v, wantErr %v", err, tt.wantErr) - } - if tt.wantErr { + g.Expect(err).To(HaveOccurred()) return } - if got.basepath != tt.want.basepath { - t.Errorf("got.basepath = %v, want = %v ", got.basepath, tt.want.basepath) - } - if got.providerLabel != tt.want.providerLabel { - t.Errorf("got.providerLabel = %v, want = %v ", got.providerLabel, tt.want.providerLabel) - } - if got.DefaultVersion() != tt.want.defaultVersion { - t.Errorf("got.DefaultVersion() = %v, want = %v ", got.DefaultVersion(), tt.want.defaultVersion) - } - if got.RootPath() != tt.want.rootPath { - t.Errorf("got.RootPath() = %v, want = %v ", got.RootPath(), tt.want.rootPath) - } - if got.ComponentsPath() != tt.want.componentsPath { - t.Errorf("got.ComponentsPath() = %v, want = %v ", got.ComponentsPath(), tt.want.componentsPath) - } + + g.Expect(err).NotTo(HaveOccurred()) + + g.Expect(got.basepath).To(Equal(tt.want.basepath)) + g.Expect(got.providerLabel).To(Equal(tt.want.providerLabel)) + g.Expect(got.DefaultVersion()).To(Equal(tt.want.defaultVersion)) + g.Expect(got.RootPath()).To(Equal(tt.want.rootPath)) + g.Expect(got.ComponentsPath()).To(Equal(tt.want.componentsPath)) }) } } @@ -142,19 +135,19 @@ func createTempDir(t *testing.T) string { } func createLocalTestProviderFile(t *testing.T, tmpDir, path, msg string) string { + g := NewWithT(t) + dst := filepath.Join(tmpDir, path) // Create all directories in the standard layout - if err := os.MkdirAll(filepath.Dir(dst), 0755); err != nil { - t.Fatalf("err: %s", err) - } + g.Expect(os.MkdirAll(filepath.Dir(dst), 0755)).To(Succeed()) + g.Expect(ioutil.WriteFile(dst, []byte(msg), 0644)).To(Succeed()) - if err := ioutil.WriteFile(dst, []byte(msg), 0644); err != nil { - t.Fatalf("err: %s", err) - } return dst } func Test_localRepository_newLocalRepository_Latest(t *testing.T) { + g := NewWithT(t) + tmpDir := createTempDir(t) defer os.RemoveAll(tmpDir) @@ -170,29 +163,18 @@ func Test_localRepository_newLocalRepository_Latest(t *testing.T) { p2 := config.NewProvider("foo", p2URLLatestAbs, clusterctlv1.BootstrapProviderType) got, err := newLocalRepository(p2, test.NewFakeVariableClient()) - if err != nil { - t.Fatalf("got error %v when none was expected", err) - } - - if got.basepath != tmpDir { - t.Errorf("got.basepath = %v, want = %v ", got.basepath, tmpDir) - } - if got.providerLabel != "bootstrap-foo" { - t.Errorf("got.providerLabel = %v, want = provider-2 ", got.providerLabel) - } - if got.DefaultVersion() != "v1.0.1" { - t.Errorf("got.DefaultVersion() = %v, want = v1.0.1 ", got.DefaultVersion()) - } - if got.RootPath() != "" { - t.Errorf("got.RootPath() = %v, want = \"\" ", got.RootPath()) - } - if got.ComponentsPath() != "bootstrap-components.yaml" { - t.Errorf("got.ComponentsPath() = %v, want = bootstrap-components.yaml ", got.ComponentsPath()) - } + g.Expect(err).NotTo(HaveOccurred()) + g.Expect(got.basepath).To(Equal(tmpDir)) + g.Expect(got.providerLabel).To(Equal("bootstrap-foo")) + g.Expect(got.DefaultVersion()).To(Equal("v1.0.1")) + g.Expect(got.RootPath()).To(BeEmpty()) + g.Expect(got.ComponentsPath()).To(Equal("bootstrap-components.yaml")) } func Test_localRepository_GetFile(t *testing.T) { + g := NewWithT(t) + tmpDir := createTempDir(t) defer os.RemoveAll(tmpDir) @@ -276,24 +258,23 @@ func Test_localRepository_GetFile(t *testing.T) { for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { r, err := newLocalRepository(tt.fields.provider, tt.fields.configVariablesClient) - if err != nil { - t.Fatalf("unexpected error: %v", err) - } + g.Expect(err).NotTo(HaveOccurred()) + got, err := r.GetFile(tt.args.version, tt.args.fileName) - if (err != nil) != tt.wantErr { - t.Fatalf("error = %v, wantErr %v", err, tt.wantErr) - } if tt.wantErr { + g.Expect(err).To(HaveOccurred()) return } - if string(got) != tt.want.contents { - t.Errorf("got %s expected %s", got, tt.want.contents) - } + + g.Expect(err).NotTo(HaveOccurred()) + g.Expect(string(got)).To(Equal(tt.want.contents)) }) } } func Test_localRepository_GetVersions(t *testing.T) { + g := NewWithT(t) + tmpDir := createTempDir(t) defer os.RemoveAll(tmpDir) @@ -352,28 +333,17 @@ func Test_localRepository_GetVersions(t *testing.T) { for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { r, err := newLocalRepository(tt.fields.provider, tt.fields.configVariablesClient) - if err != nil { - t.Fatalf("unexpected error: %v", err) - return - } + g.Expect(err).NotTo(HaveOccurred()) + got, err := r.GetVersions() - if (err != nil) != tt.wantErr { - t.Fatalf("error = %v, wantErr %v", err, tt.wantErr) - } if tt.wantErr { + g.Expect(err).To(HaveOccurred()) return } - if len(got) != len(tt.want.versions) { - t.Fatalf("got %v, expected %v versions", len(got), len(tt.want.versions)) - } - sort.Strings(tt.want.versions) - sort.Strings(got) - for i := range got { - if got[i] != tt.want.versions[i] { - t.Errorf("got %s expected %s", got, tt.want.versions) - } - } + g.Expect(err).NotTo(HaveOccurred()) + g.Expect(got).To(HaveLen(len(tt.want.versions))) + g.Expect(got).To(ConsistOf(tt.want.versions)) }) } } diff --git a/cmd/clusterctl/client/repository/template_client_test.go b/cmd/clusterctl/client/repository/template_client_test.go index 7c9327ed158d..40eaf7b9a88b 100644 --- a/cmd/clusterctl/client/repository/template_client_test.go +++ b/cmd/clusterctl/client/repository/template_client_test.go @@ -17,17 +17,20 @@ limitations under the License. package repository import ( - "bytes" + // "bytes" "fmt" - "reflect" "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" ) func Test_templates_Get(t *testing.T) { + g := NewWithT(t) + p1 := config.NewProvider("p1", "", clusterctlv1.BootstrapProviderType) type fields struct { @@ -158,36 +161,27 @@ func Test_templates_Get(t *testing.T) { t.Run(tt.name, func(t *testing.T) { f := newTemplateClient(tt.fields.provider, tt.fields.version, tt.fields.repository, tt.fields.configVariablesClient) got, err := f.Get(tt.args.flavor, tt.args.targetNamespace, tt.args.listVariablesOnly) - if (err != nil) != tt.wantErr { - t.Fatalf("error = %v, wantErr %v", err, tt.wantErr) - } if tt.wantErr { + g.Expect(err).To(HaveOccurred()) return } - if !reflect.DeepEqual(got.Variables(), tt.want.variables) { - t.Errorf("got.Variables() = %v, want = %v ", got.Variables(), tt.want.variables) - } + g.Expect(err).NotTo(HaveOccurred()) - if !reflect.DeepEqual(got.TargetNamespace(), tt.want.targetNamespace) { - t.Errorf("got.TargetNamespace() = %v, want = %v ", got.TargetNamespace(), tt.want.targetNamespace) - } + g.Expect(got.Variables()).To(ConsistOf(tt.want.variables)) + g.Expect(got.TargetNamespace()).To(Equal(tt.want.targetNamespace)) // check variable replaced in yaml yaml, err := got.Yaml() - if tt.wantErr { - t.Fatalf("got.Yaml error = %v", err) - } + g.Expect(err).NotTo(HaveOccurred()) - if !tt.args.listVariablesOnly && !bytes.Contains(yaml, []byte(fmt.Sprintf("variable: %s", variableValue))) { - t.Error("got.Yaml without variable substitution") + if tt.args.listVariablesOnly { + g.Expect(yaml).NotTo(ContainSubstring((fmt.Sprintf("variable: %s", variableValue)))) } // check if target namespace is set for _, o := range got.Objs() { - if o.GetNamespace() != tt.want.targetNamespace { - t.Errorf("got.Object[%s].Namespace = %v, want = %v ", o.GetName(), o.GetNamespace(), tt.want.targetNamespace) - } + g.Expect(o.GetNamespace()).To(Equal(tt.want.targetNamespace)) } }) } diff --git a/cmd/clusterctl/client/repository/template_test.go b/cmd/clusterctl/client/repository/template_test.go index cb9488f12ad9..0a20679faa9e 100644 --- a/cmd/clusterctl/client/repository/template_test.go +++ b/cmd/clusterctl/client/repository/template_test.go @@ -17,11 +17,11 @@ limitations under the License. package repository import ( - "bytes" "fmt" - "reflect" "testing" + . "github.com/onsi/gomega" + "sigs.k8s.io/cluster-api/cmd/clusterctl/client/config" "sigs.k8s.io/cluster-api/cmd/clusterctl/internal/test" ) @@ -36,6 +36,8 @@ var templateMapYaml = []byte("apiVersion: v1\n" + " name: manager") func Test_newTemplate(t *testing.T) { + g := NewWithT(t) + type args struct { rawYaml []byte configVariablesClient config.VariablesClient @@ -84,20 +86,15 @@ func Test_newTemplate(t *testing.T) { for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { got, err := NewTemplate(tt.args.rawYaml, tt.args.configVariablesClient, tt.args.targetNamespace, tt.args.listVariablesOnly) - if (err != nil) != tt.wantErr { - t.Fatalf("error = %v, wantErr %v", err, tt.wantErr) - } if tt.wantErr { + g.Expect(err).To(HaveOccurred()) return } - if !reflect.DeepEqual(got.Variables(), tt.want.variables) { - t.Errorf("got.Variables() = %v, want = %v ", got.Variables(), tt.want.variables) - } + g.Expect(err).NotTo(HaveOccurred()) - if !reflect.DeepEqual(got.TargetNamespace(), tt.want.targetNamespace) { - t.Errorf("got.TargetNamespace() = %v, want = %v ", got.TargetNamespace(), tt.want.targetNamespace) - } + g.Expect(got.Variables()).To(ConsistOf(tt.want.variables)) + g.Expect(got.TargetNamespace()).To(Equal(tt.want.targetNamespace)) if tt.args.listVariablesOnly { return @@ -105,13 +102,8 @@ func Test_newTemplate(t *testing.T) { // check variable replaced in components yaml, err := got.Yaml() - if err != nil { - t.Fatalf("got.Yaml error = %v", err) - } - - if !bytes.Contains(yaml, []byte(fmt.Sprintf("variable: %s", variableValue))) { - t.Error("got.Yaml without variable substitution") - } + g.Expect(err).NotTo(HaveOccurred()) + g.Expect(yaml).To(ContainSubstring((fmt.Sprintf("variable: %s", variableValue)))) }) } }