Skip to content

Commit

Permalink
Merge pull request #2643 from cpanato/GH-2433-7
Browse files Browse the repository at this point in the history
🏃cmd-clusterctl-client-repository/test: standardize gomega imports
  • Loading branch information
k8s-ci-robot committed Mar 13, 2020
2 parents 5252a7a + 51a0c61 commit ce5d517
Show file tree
Hide file tree
Showing 8 changed files with 185 additions and 267 deletions.
18 changes: 9 additions & 9 deletions cmd/clusterctl/client/repository/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,22 +20,24 @@ 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)

dst1 := createLocalTestProviderFile(t, tmpDir, "bootstrap-foo/v1.0.0/bootstrap-components.yaml", "")
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
Expand All @@ -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())
})
}
}
61 changes: 19 additions & 42 deletions cmd/clusterctl/client/repository/components_client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -233,64 +233,41 @@ 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 {
t.Errorf("got.Yaml() error = %v", err)
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())
}
}
})
Expand Down
Loading

0 comments on commit ce5d517

Please sign in to comment.