Skip to content

Commit

Permalink
using fake client instead of go mock to test
Browse files Browse the repository at this point in the history
  • Loading branch information
cxbrowne1207 committed Feb 22, 2023
1 parent cb7ea5d commit 4ba2fd8
Showing 1 changed file with 39 additions and 36 deletions.
75 changes: 39 additions & 36 deletions pkg/cluster/cloudstack_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,15 @@ package cluster_test

import (
"context"
"errors"
"testing"

"github.com/golang/mock/gomock"
. "github.com/onsi/gomega"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
"sigs.k8s.io/controller-runtime/pkg/client/fake"

anywherev1 "github.com/aws/eks-anywhere/pkg/api/v1alpha1"
"github.com/aws/eks-anywhere/pkg/cluster"
"github.com/aws/eks-anywhere/pkg/cluster/mocks"
"github.com/aws/eks-anywhere/pkg/controller/clientutil"
)

func TestParseConfigMissingCloudstackDatacenter(t *testing.T) {
Expand All @@ -23,13 +21,46 @@ func TestParseConfigMissingCloudstackDatacenter(t *testing.T) {
g.Expect(got.CloudStackDatacenter).To(BeNil())
}

func TestDefaultConfigClientBuilderCloudStackCluster(t *testing.T) {
func TestDefaultConfigClientBuilderBuildCloudStackClusterSuccess(t *testing.T) {
g := NewWithT(t)
ctx := context.Background()
b := cluster.NewDefaultConfigClientBuilder()
ctrl := gomock.NewController(t)
client := mocks.NewMockClient(ctrl)
cluster := &anywherev1.Cluster{
cluster := cloudStackCluster()
datacenter := &anywherev1.CloudStackDatacenterConfig{
TypeMeta: metav1.TypeMeta{
Kind: anywherev1.CloudStackDatacenterKind,
APIVersion: anywherev1.GroupVersion.String(),
},
ObjectMeta: metav1.ObjectMeta{
Name: "datacenter",
Namespace: "default",
},
}

client := fake.NewClientBuilder().WithObjects().Build()
_, err := b.Build(ctx, clientutil.NewKubeClient(client), cluster)
g.Expect(err).To(MatchError(ContainSubstring("building Config from a cluster client")))

client = fake.NewClientBuilder().WithObjects(datacenter).Build()
config, err := b.Build(ctx, clientutil.NewKubeClient(client), cluster)
g.Expect(err).NotTo(HaveOccurred())
g.Expect(config).NotTo(BeNil())
g.Expect(config.Cluster).To(Equal(cluster))
g.Expect(config.CloudStackDatacenter).To(Equal(datacenter))
}

func TestDefaultConfigClientBuilderBuildCloudStackClusterFailure(t *testing.T) {
g := NewWithT(t)
ctx := context.Background()
b := cluster.NewDefaultConfigClientBuilder()
cluster := cloudStackCluster()
client := fake.NewClientBuilder().WithObjects().Build()
_, err := b.Build(ctx, clientutil.NewKubeClient(client), cluster)
g.Expect(err).To(MatchError(ContainSubstring("cloudstackdatacenterconfigs.anywhere.eks.amazonaws.com \"datacenter\" not found")))
}

func cloudStackCluster() *anywherev1.Cluster {
return &anywherev1.Cluster{
ObjectMeta: metav1.ObjectMeta{
Name: "my-cluster",
Namespace: "default",
Expand All @@ -49,32 +80,4 @@ func TestDefaultConfigClientBuilderCloudStackCluster(t *testing.T) {
},
},
}
datacenter := &anywherev1.CloudStackDatacenterConfig{
ObjectMeta: metav1.ObjectMeta{
Name: "datacenter",
Namespace: "default",
},
}

wantError := errors.New("test error")

client.EXPECT().Get(ctx, "datacenter", "default", &anywherev1.CloudStackDatacenterConfig{}).Return(wantError)

_, err := b.Build(ctx, client, cluster)
g.Expect(err).To(MatchError(ContainSubstring("building Config from a cluster client")))

client.EXPECT().Get(ctx, "datacenter", "default", &anywherev1.CloudStackDatacenterConfig{}).Return(nil).DoAndReturn(
func(ctx context.Context, name, namespace string, obj runtime.Object) error {
d := obj.(*anywherev1.CloudStackDatacenterConfig)
d.ObjectMeta = datacenter.ObjectMeta
d.Spec = datacenter.Spec
return nil
},
)

config, err := b.Build(ctx, client, cluster)
g.Expect(err).NotTo(HaveOccurred())
g.Expect(config).NotTo(BeNil())
g.Expect(config.Cluster).To(Equal(cluster))
g.Expect(config.CloudStackDatacenter).To(Equal(datacenter))
}

0 comments on commit 4ba2fd8

Please sign in to comment.