Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

🏃 cmd-clusterctl-client/tests: standardize gomega imports #2645

Merged
merged 1 commit into from
Mar 16, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 {
cpanato marked this conversation as resolved.
Show resolved Hide resolved
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())
cpanato marked this conversation as resolved.
Show resolved Hide resolved
g.Expect(got).To(HaveLen(len(tt.wantProviders)))

for i, g := range got {
for i, gotProvider := range got {
detiber marked this conversation as resolved.
Show resolved Hide resolved
cpanato marked this conversation as resolved.
Show resolved Hide resolved
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())
cpanato marked this conversation as resolved.
Show resolved Hide resolved

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())
cpanato marked this conversation as resolved.
Show resolved Hide resolved

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())
cpanato marked this conversation as resolved.
Show resolved Hide resolved

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))
cpanato marked this conversation as resolved.
Show resolved Hide resolved
})
}
}
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())
cpanato marked this conversation as resolved.
Show resolved Hide resolved

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())
cpanato marked this conversation as resolved.
Show resolved Hide resolved

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 {
cpanato marked this conversation as resolved.
Show resolved Hide resolved
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