diff --git a/pkg/cluster/cloudstack_test.go b/pkg/cluster/cloudstack_test.go index a7250a1a70498..bf243534a3363 100644 --- a/pkg/cluster/cloudstack_test.go +++ b/pkg/cluster/cloudstack_test.go @@ -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) { @@ -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", @@ -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)) }