Skip to content

Commit

Permalink
Merge pull request #2645 from cpanato/GH-2433-8
Browse files Browse the repository at this point in the history
🏃 cmd-clusterctl-client/tests: standardize gomega imports
  • Loading branch information
k8s-ci-robot committed Mar 16, 2020
2 parents 3dd2de5 + 3af93dc commit 411cfbd
Show file tree
Hide file tree
Showing 5 changed files with 84 additions and 130 deletions.
23 changes: 14 additions & 9 deletions cmd/clusterctl/client/common_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down Expand Up @@ -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))
})
}
}
84 changes: 31 additions & 53 deletions cmd/clusterctl/client/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,16 +21,19 @@ 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"
"sigs.k8s.io/cluster-api/cmd/clusterctl/client/config"
)

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

customProviderConfig := config.NewProvider("custom", "url", clusterctlv1.BootstrapProviderType)

type field struct {
Expand Down Expand Up @@ -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)

Expand Down Expand Up @@ -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
}
Expand Down Expand Up @@ -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{
Expand Down Expand Up @@ -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))
})
}
}
24 changes: 10 additions & 14 deletions cmd/clusterctl/client/delete_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down Expand Up @@ -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))
})
}
}
Expand Down
47 changes: 14 additions & 33 deletions cmd/clusterctl/client/init_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,17 @@ 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"
"sigs.k8s.io/cluster-api/cmd/clusterctl/internal/util"
)

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

type field struct {
client *fakeClient
hasCRD bool
Expand Down Expand Up @@ -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{
Expand All @@ -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))
}
})
}
Expand Down
Loading

0 comments on commit 411cfbd

Please sign in to comment.