Skip to content

Commit

Permalink
Add more test coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
jiayiwang7 committed Jun 14, 2022
1 parent d08cbe0 commit 621a4f4
Show file tree
Hide file tree
Showing 2 changed files with 166 additions and 1 deletion.
5 changes: 4 additions & 1 deletion pkg/clusterapi/fetch.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,10 @@ func MachineDeploymentInCluster(ctx context.Context, kubeclient kubernetes.Clien
if apierrors.IsNotFound(err) {
return nil, nil
}
return md, err
if err != nil {
return nil, err
}
return md, nil
}

func KubeadmConfigTemplateInCluster(ctx context.Context, kubeclient kubernetes.Client, clusterSpec *cluster.Spec, md *clusterv1.MachineDeployment) (*bootstrapv1.KubeadmConfigTemplate, error) {
Expand Down
162 changes: 162 additions & 0 deletions pkg/clusterapi/fetch_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,162 @@
package clusterapi_test

import (
"context"
"errors"
"testing"

"github.com/golang/mock/gomock"
. "github.com/onsi/gomega"
v1 "k8s.io/api/core/v1"
apierrors "k8s.io/apimachinery/pkg/api/errors"
"k8s.io/apimachinery/pkg/runtime/schema"
clusterv1 "sigs.k8s.io/cluster-api/api/v1beta1"
bootstrapv1 "sigs.k8s.io/cluster-api/bootstrap/kubeadm/api/v1beta1"

"github.com/aws/eks-anywhere/pkg/api/v1alpha1"
kubemock "github.com/aws/eks-anywhere/pkg/clients/kubernetes/mocks"
"github.com/aws/eks-anywhere/pkg/cluster"
"github.com/aws/eks-anywhere/pkg/clusterapi"
"github.com/aws/eks-anywhere/pkg/constants"
)

type fetchTest struct {
*WithT
ctx context.Context
kubeconfigClient *kubemock.MockClient
clusterSpec *cluster.Spec
workerNodeGroupConfig v1alpha1.WorkerNodeGroupConfiguration
machineDeployment *clusterv1.MachineDeployment
}

func newFetchTest(t *testing.T) fetchTest {
ctrl := gomock.NewController(t)
mockKubeconfigClient := kubemock.NewMockClient(ctrl)
wng := v1alpha1.WorkerNodeGroupConfiguration{
Name: "md-0",
}
md := &clusterv1.MachineDeployment{
Spec: clusterv1.MachineDeploymentSpec{
Template: clusterv1.MachineTemplateSpec{
Spec: clusterv1.MachineSpec{
Bootstrap: clusterv1.Bootstrap{
ConfigRef: &v1.ObjectReference{
Name: "snow-test-md-0-1",
},
},
},
},
},
}
return fetchTest{
WithT: NewWithT(t),
ctx: context.Background(),
kubeconfigClient: mockKubeconfigClient,
clusterSpec: givenClusterSpec(),
workerNodeGroupConfig: wng,
machineDeployment: md,
}
}

func TestMachineDeploymentInCluster(t *testing.T) {
g := newFetchTest(t)
g.kubeconfigClient.EXPECT().
Get(
g.ctx,
"snow-test-md-0",
constants.EksaSystemNamespace,
&clusterv1.MachineDeployment{},
).
Return(nil)

got, err := clusterapi.MachineDeploymentInCluster(g.ctx, g.kubeconfigClient, g.clusterSpec, g.workerNodeGroupConfig)
g.Expect(err).To(Succeed())
g.Expect(got).To(Equal(&clusterv1.MachineDeployment{}))
}

func TestMachineDeploymentInClusterNotExists(t *testing.T) {
g := newFetchTest(t)
g.kubeconfigClient.EXPECT().
Get(
g.ctx,
"snow-test-md-0",
constants.EksaSystemNamespace,
&clusterv1.MachineDeployment{},
).
Return(apierrors.NewNotFound(schema.GroupResource{Group: "", Resource: ""}, ""))

got, err := clusterapi.MachineDeploymentInCluster(g.ctx, g.kubeconfigClient, g.clusterSpec, g.workerNodeGroupConfig)
g.Expect(err).To(Succeed())
g.Expect(got).To(BeNil())
}

func TestMachineDeploymentInClusterError(t *testing.T) {
g := newFetchTest(t)
g.kubeconfigClient.EXPECT().
Get(
g.ctx,
"snow-test-md-0",
constants.EksaSystemNamespace,
&clusterv1.MachineDeployment{},
).
Return(errors.New("get md error"))

got, err := clusterapi.MachineDeploymentInCluster(g.ctx, g.kubeconfigClient, g.clusterSpec, g.workerNodeGroupConfig)
g.Expect(err).NotTo(Succeed())
g.Expect(got).To(BeNil())
}

func TestKubeadmConfigTemplateInCluster(t *testing.T) {
g := newFetchTest(t)
g.kubeconfigClient.EXPECT().
Get(
g.ctx,
"snow-test-md-0-1",
constants.EksaSystemNamespace,
&bootstrapv1.KubeadmConfigTemplate{},
).
Return(nil)

got, err := clusterapi.KubeadmConfigTemplateInCluster(g.ctx, g.kubeconfigClient, g.clusterSpec, g.machineDeployment)
g.Expect(err).To(Succeed())
g.Expect(got).To(Equal(&bootstrapv1.KubeadmConfigTemplate{}))
}

func TestKubeadmConfigTemplateInClusterMachineDeploymentNil(t *testing.T) {
g := newFetchTest(t)
got, err := clusterapi.KubeadmConfigTemplateInCluster(g.ctx, g.kubeconfigClient, g.clusterSpec, nil)
g.Expect(err).To(Succeed())
g.Expect(got).To(BeNil())
}

func TestKubeadmConfigTemplateInClusterNotExists(t *testing.T) {
g := newFetchTest(t)
g.kubeconfigClient.EXPECT().
Get(
g.ctx,
"snow-test-md-0-1",
constants.EksaSystemNamespace,
&bootstrapv1.KubeadmConfigTemplate{},
).
Return(apierrors.NewNotFound(schema.GroupResource{Group: "", Resource: ""}, ""))

got, err := clusterapi.KubeadmConfigTemplateInCluster(g.ctx, g.kubeconfigClient, g.clusterSpec, g.machineDeployment)
g.Expect(err).To(Succeed())
g.Expect(got).To(BeNil())
}

func TestKubeadmConfigTemplateInClusterError(t *testing.T) {
g := newFetchTest(t)
g.kubeconfigClient.EXPECT().
Get(
g.ctx,
"snow-test-md-0-1",
constants.EksaSystemNamespace,
&bootstrapv1.KubeadmConfigTemplate{},
).
Return(errors.New("get kct error"))

got, err := clusterapi.KubeadmConfigTemplateInCluster(g.ctx, g.kubeconfigClient, g.clusterSpec, g.machineDeployment)
g.Expect(err).NotTo(Succeed())
g.Expect(got).To(BeNil())
}

0 comments on commit 621a4f4

Please sign in to comment.