Skip to content

Commit

Permalink
additional test coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
tatlat committed Apr 1, 2024
1 parent 2aef03b commit 006453e
Show file tree
Hide file tree
Showing 3 changed files with 145 additions and 35 deletions.
15 changes: 13 additions & 2 deletions pkg/clustermanager/cluster_manager_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -898,7 +898,7 @@ func TestResumeEKSAControllerReconcileManagementCluster(t *testing.T) {
tt.Expect(tt.clusterManager.ResumeEKSAControllerReconcile(tt.ctx, tt.cluster, tt.clusterSpec, tt.mocks.provider)).To(Succeed())
}

func TestPauseEKSAControllerReconcileManagementClusterListObjectsError(t *testing.T) {
func TestResumeEKSAControllerReconcileManagementClusterListObjectsError(t *testing.T) {
tt := newTest(t, clustermanager.WithRetrier(retrier.NewWithMaxRetries(1, 0)))
tt.clusterSpec.Cluster = &v1alpha1.Cluster{
ObjectMeta: metav1.ObjectMeta{
Expand All @@ -911,9 +911,20 @@ func TestPauseEKSAControllerReconcileManagementClusterListObjectsError(t *testin
},
}

datacenterConfig := &v1alpha1.VSphereDatacenterConfig{
ObjectMeta: metav1.ObjectMeta{
Name: tt.clusterName,
},
Spec: v1alpha1.VSphereDatacenterConfigSpec{
Insecure: true,
},
}

tt.mocks.provider.EXPECT().DatacenterConfig(tt.clusterSpec).Return(datacenterConfig)

tt.mocks.client.EXPECT().ListObjects(tt.ctx, eksaClusterResourceType, "", "", &v1alpha1.ClusterList{}).Return(errors.New("list error"))

tt.Expect(tt.clusterManager.PauseEKSAControllerReconcile(tt.ctx, tt.cluster, tt.clusterSpec, tt.mocks.provider)).NotTo(Succeed())
tt.Expect(tt.clusterManager.ResumeEKSAControllerReconcile(tt.ctx, tt.cluster, tt.clusterSpec, tt.mocks.provider)).NotTo(Succeed())
}

func TestPauseEKSAControllerReconcileWorkloadClusterWithMachineConfig(t *testing.T) {
Expand Down
92 changes: 77 additions & 15 deletions pkg/workflows/management/create_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,7 @@ import (

"github.com/golang/mock/gomock"
corev1 "k8s.io/api/core/v1"
apierrors "k8s.io/apimachinery/pkg/api/errors"
v1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime/schema"

"github.com/aws/eks-anywhere/internal/test"
"github.com/aws/eks-anywhere/pkg/api/v1alpha1"
Expand Down Expand Up @@ -221,7 +219,7 @@ func (c *createTestSetup) expectMoveManagement(err error) {
c.ctx, c.bootstrapCluster, c.workloadCluster, c.workloadCluster.Name, c.clusterSpec, gomock.Any()).Return(err)
}

func (c *createTestSetup) expectInstallEksaComponentsWorkload(err1, err2, err3 error) {
func (c *createTestSetup) expectInstallEksaComponentsWorkload(err1, err2, err3, err4, err5 error) {
gomock.InOrder(

c.eksdInstaller.EXPECT().InstallEksdCRDs(c.ctx, c.clusterSpec, c.workloadCluster).Return(err1),
Expand All @@ -235,13 +233,13 @@ func (c *createTestSetup) expectInstallEksaComponentsWorkload(err1, err2, err3 e
c.eksdInstaller.EXPECT().InstallEksdManifest(
c.ctx, c.clusterSpec, c.workloadCluster),

c.clientFactory.EXPECT().BuildClientFromKubeconfig(c.bootstrapCluster.KubeconfigFile).Return(c.client, nil),
c.clientFactory.EXPECT().BuildClientFromKubeconfig(c.bootstrapCluster.KubeconfigFile).Return(c.client, err2),

c.clientFactory.EXPECT().BuildClientFromKubeconfig(c.workloadCluster.KubeconfigFile).Return(c.client, nil),
c.clientFactory.EXPECT().BuildClientFromKubeconfig(c.workloadCluster.KubeconfigFile).Return(c.client, err3).MaxTimes(1),

c.mover.EXPECT().Move(c.ctx, c.clusterSpec, c.client, c.client).Return(err2),
c.mover.EXPECT().Move(c.ctx, c.clusterSpec, c.client, c.client).Return(err4).MaxTimes(1),

c.clusterManager.EXPECT().ResumeEKSAControllerReconcile(c.ctx, c.workloadCluster, c.clusterSpec, c.provider).Return(err3).MaxTimes(1),
c.clusterManager.EXPECT().ResumeEKSAControllerReconcile(c.ctx, c.workloadCluster, c.clusterSpec, c.provider).Return(err5).MaxTimes(1),
)
}

Expand All @@ -264,7 +262,6 @@ func (c *createTestSetup) expectCreateNamespace() {
},
ObjectMeta: v1.ObjectMeta{Name: n},
}
c.client.EXPECT().Get(c.ctx, n, "", &corev1.Namespace{}).Return(apierrors.NewNotFound(schema.GroupResource{Group: "", Resource: ""}, "")).MaxTimes(2)
c.client.EXPECT().Create(c.ctx, ns).MaxTimes(2)
}

Expand Down Expand Up @@ -299,7 +296,7 @@ func TestCreateRunSuccess(t *testing.T) {
test.expectInstallResourcesOnManagementTask(nil)
test.expectPauseReconcile(nil)
test.expectMoveManagement(nil)
test.expectInstallEksaComponentsWorkload(nil, nil, nil)
test.expectInstallEksaComponentsWorkload(nil, nil, nil, nil, nil)
test.expectInstallGitOpsManager()
test.expectWriteClusterConfig()
test.expectDeleteBootstrap(nil)
Expand Down Expand Up @@ -742,7 +739,55 @@ func TestCreateEKSAWorkloadFailure(t *testing.T) {
test.expectInstallResourcesOnManagementTask(nil)
test.expectPauseReconcile(nil)
test.expectMoveManagement(nil)
test.expectInstallEksaComponentsWorkload(nil, fmt.Errorf("test"), nil)
test.expectInstallEksaComponentsWorkload(nil, nil, nil, fmt.Errorf("test"), nil)
test.expectCreateNamespace()

test.clusterManager.EXPECT().SaveLogsManagementCluster(test.ctx, test.clusterSpec, test.bootstrapCluster)

test.writer.EXPECT().Write(fmt.Sprintf("%s-checkpoint.yaml", test.clusterSpec.Cluster.Name), gomock.Any())

err := test.run()
if err == nil {
t.Fatalf("Create.Run() expected to return an error %v", err)
}
}

func TestCreateSrcClientFailure(t *testing.T) {
test := newCreateTest(t)
test.expectSetup()
test.expectPreflightValidationsToPass()
test.expectCreateBootstrap()
test.expectCAPIInstall(nil, nil, nil)
test.expectInstallEksaComponentsBootstrap(nil, nil, nil, nil)
test.expectCreateWorkload(nil, nil, nil, nil, nil, nil)
test.expectInstallResourcesOnManagementTask(nil)
test.expectPauseReconcile(nil)
test.expectMoveManagement(nil)
test.expectInstallEksaComponentsWorkload(nil, fmt.Errorf(""), nil, nil, nil)
test.expectCreateNamespace()

test.clusterManager.EXPECT().SaveLogsManagementCluster(test.ctx, test.clusterSpec, test.bootstrapCluster)

test.writer.EXPECT().Write(fmt.Sprintf("%s-checkpoint.yaml", test.clusterSpec.Cluster.Name), gomock.Any())

err := test.run()
if err == nil {
t.Fatalf("Create.Run() expected to return an error %v", err)
}
}

func TestCreateDstClientFailure(t *testing.T) {
test := newCreateTest(t)
test.expectSetup()
test.expectPreflightValidationsToPass()
test.expectCreateBootstrap()
test.expectCAPIInstall(nil, nil, nil)
test.expectInstallEksaComponentsBootstrap(nil, nil, nil, nil)
test.expectCreateWorkload(nil, nil, nil, nil, nil, nil)
test.expectInstallResourcesOnManagementTask(nil)
test.expectPauseReconcile(nil)
test.expectMoveManagement(nil)
test.expectInstallEksaComponentsWorkload(nil, nil, fmt.Errorf(""), nil, nil)
test.expectCreateNamespace()

test.clusterManager.EXPECT().SaveLogsManagementCluster(test.ctx, test.clusterSpec, test.bootstrapCluster)
Expand All @@ -766,7 +811,7 @@ func TestCreateEKSAResumeWorkloadFailure(t *testing.T) {
test.expectInstallResourcesOnManagementTask(nil)
test.expectPauseReconcile(nil)
test.expectMoveManagement(nil)
test.expectInstallEksaComponentsWorkload(nil, nil, fmt.Errorf("test"))
test.expectInstallEksaComponentsWorkload(nil, nil, nil, nil, fmt.Errorf("test"))
test.expectCreateNamespace()

test.clusterManager.EXPECT().SaveLogsManagementCluster(test.ctx, test.clusterSpec, test.bootstrapCluster)
Expand All @@ -790,7 +835,7 @@ func TestCreateGitOPsFailure(t *testing.T) {
test.expectInstallResourcesOnManagementTask(nil)
test.expectPauseReconcile(nil)
test.expectMoveManagement(nil)
test.expectInstallEksaComponentsWorkload(nil, nil, nil)
test.expectInstallEksaComponentsWorkload(nil, nil, nil, nil, nil)
test.expectCreateNamespace()
test.expectDatacenterConfig()
test.expectMachineConfigs()
Expand Down Expand Up @@ -819,7 +864,7 @@ func TestCreateWriteConfigFailure(t *testing.T) {
test.expectInstallResourcesOnManagementTask(nil)
test.expectPauseReconcile(nil)
test.expectMoveManagement(nil)
test.expectInstallEksaComponentsWorkload(nil, nil, nil)
test.expectInstallEksaComponentsWorkload(nil, nil, nil, nil, nil)
test.expectInstallGitOpsManager()
test.expectPreflightValidationsToPass()
test.expectCreateNamespace()
Expand Down Expand Up @@ -854,7 +899,7 @@ func TestCreateWriteConfigAWSIAMFailure(t *testing.T) {
test.expectInstallResourcesOnManagementTask(nil)
test.expectPauseReconcile(nil)
test.expectMoveManagement(nil)
test.expectInstallEksaComponentsWorkload(nil, nil, nil)
test.expectInstallEksaComponentsWorkload(nil, nil, nil, nil, nil)
test.expectInstallGitOpsManager()
test.expectPreflightValidationsToPass()
test.clusterSpec.AWSIamConfig = &v1alpha1.AWSIamConfig{}
Expand Down Expand Up @@ -890,7 +935,7 @@ func TestCreateRunDeleteBootstrapFailure(t *testing.T) {
test.expectInstallResourcesOnManagementTask(nil)
test.expectPauseReconcile(nil)
test.expectMoveManagement(nil)
test.expectInstallEksaComponentsWorkload(nil, nil, nil)
test.expectInstallEksaComponentsWorkload(nil, nil, nil, nil, nil)
test.expectInstallGitOpsManager()
test.expectWriteClusterConfig()
test.expectDeleteBootstrap(fmt.Errorf("test"))
Expand All @@ -906,3 +951,20 @@ func TestCreateRunDeleteBootstrapFailure(t *testing.T) {
t.Fatalf("Create.Run() err = %v, want err = nil", err)
}
}

func TestCreateNamespaceClientFailure(t *testing.T) {
test := newCreateTest(t)
test.expectSetup()
test.expectCreateBootstrap()
test.expectPreflightValidationsToPass()
test.expectCAPIInstall(nil, nil, nil)
test.expectInstallEksaComponentsBootstrap(nil, nil, nil, nil)
test.clientFactory.EXPECT().BuildClientFromKubeconfig(test.bootstrapCluster.KubeconfigFile).Return(test.client, fmt.Errorf(""))
test.clusterManager.EXPECT().SaveLogsManagementCluster(test.ctx, test.clusterSpec, test.bootstrapCluster)
test.writer.EXPECT().Write("test-cluster-checkpoint.yaml", gomock.Any(), gomock.Any())

err := test.run()
if err == nil {
t.Fatalf("Create.Run() err = %v, want err = nil", err)
}
}
73 changes: 55 additions & 18 deletions pkg/workflows/management/delete_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@ func (c *deleteTestSetup) expectMoveCAPI(err1, err2 error) {
c.clusterManager.EXPECT().MoveCAPI(c.ctx, c.workloadCluster, c.bootstrapCluster, c.workloadCluster.Name, c.clusterSpec, gomock.Any()).Return(err2)
}

func (c *deleteTestSetup) expectInstallEksaComponentsBootstrap(err1, err2, err3, err4, err5, err6 error) {
func (c *deleteTestSetup) expectInstallEksaComponentsBootstrap(err1, err2, err3, err4, err5, err6, err7, err8, err9 error) {
gomock.InOrder(
c.eksdInstaller.EXPECT().InstallEksdCRDs(c.ctx, c.clusterSpec, c.bootstrapCluster).Return(err1).AnyTimes(),

Expand All @@ -180,15 +180,15 @@ func (c *deleteTestSetup) expectInstallEksaComponentsBootstrap(err1, err2, err3,
c.eksdInstaller.EXPECT().InstallEksdManifest(
c.ctx, c.clusterSpec, c.bootstrapCluster).Return(err4).AnyTimes(),

c.clientFactory.EXPECT().BuildClientFromKubeconfig(c.workloadCluster.KubeconfigFile).Return(c.client, nil).AnyTimes(),
c.clientFactory.EXPECT().BuildClientFromKubeconfig(c.workloadCluster.KubeconfigFile).Return(c.client, err5).MaxTimes(1),

c.clientFactory.EXPECT().BuildClientFromKubeconfig(c.bootstrapCluster.KubeconfigFile).Return(c.client, nil).AnyTimes(),
c.clientFactory.EXPECT().BuildClientFromKubeconfig(c.bootstrapCluster.KubeconfigFile).Return(c.client, err6).MaxTimes(1),

c.client.EXPECT().Create(c.ctx, gomock.AssignableToTypeOf(&corev1.Namespace{})).AnyTimes(),
c.client.EXPECT().Create(c.ctx, gomock.AssignableToTypeOf(&corev1.Namespace{})).Return(err7).AnyTimes(),

c.mover.EXPECT().Move(c.ctx, c.clusterSpec, c.client, c.client).Return(err5).AnyTimes(),
c.mover.EXPECT().Move(c.ctx, c.clusterSpec, c.client, c.client).Return(err8).AnyTimes(),

c.clusterManager.EXPECT().AllowDeleteWhilePaused(c.ctx, c.bootstrapCluster, c.clusterSpec).Return(err6).AnyTimes(),
c.clusterManager.EXPECT().AllowDeleteWhilePaused(c.ctx, c.bootstrapCluster, c.clusterSpec).Return(err9).AnyTimes(),
)
}

Expand All @@ -214,7 +214,7 @@ func TestDeleteRunSuccess(t *testing.T) {
test.expectPreCAPI(nil)
test.expectInstallCAPI(nil)
test.expectMoveCAPI(nil, nil)
test.expectInstallEksaComponentsBootstrap(nil, nil, nil, nil, nil, nil)
test.expectInstallEksaComponentsBootstrap(nil, nil, nil, nil, nil, nil, nil, nil, nil)
test.expectApplyOnBootstrap(nil)
test.expectDeleteCluster(nil, nil)
test.expectCleanupGitRepo(nil)
Expand Down Expand Up @@ -350,7 +350,7 @@ func TestDeleteRunFailResumeReconcile(t *testing.T) {
test.expectPreCAPI(nil)
test.expectInstallCAPI(nil)
test.expectMoveCAPI(nil, nil)
test.expectInstallEksaComponentsBootstrap(fmt.Errorf(""), nil, nil, nil, nil, nil)
test.expectInstallEksaComponentsBootstrap(fmt.Errorf(""), nil, nil, nil, nil, nil, nil, nil, nil)
test.expectSaveLogsManagement()
test.expectSaveLogsWorkload()
err := test.run()
Expand All @@ -369,7 +369,7 @@ func TestDeleteRunFailAddAnnotation(t *testing.T) {
test.expectPreCAPI(nil)
test.expectInstallCAPI(nil)
test.expectMoveCAPI(nil, nil)
test.expectInstallEksaComponentsBootstrap(nil, fmt.Errorf(""), nil, nil, nil, nil)
test.expectInstallEksaComponentsBootstrap(nil, fmt.Errorf(""), nil, nil, nil, nil, nil, nil, nil)
test.expectSaveLogsManagement()
test.expectSaveLogsWorkload()

Expand All @@ -389,7 +389,7 @@ func TestDeleteRunFailProviderInstall(t *testing.T) {
test.expectPreCAPI(nil)
test.expectInstallCAPI(nil)
test.expectMoveCAPI(nil, nil)
test.expectInstallEksaComponentsBootstrap(nil, nil, fmt.Errorf(""), nil, nil, nil)
test.expectInstallEksaComponentsBootstrap(nil, nil, fmt.Errorf(""), nil, nil, nil, nil, nil, nil)
test.expectSaveLogsManagement()
test.expectSaveLogsWorkload()

Expand All @@ -409,7 +409,7 @@ func TestDeleteRunFailEksdInstall(t *testing.T) {
test.expectPreCAPI(nil)
test.expectInstallCAPI(nil)
test.expectMoveCAPI(nil, nil)
test.expectInstallEksaComponentsBootstrap(nil, nil, nil, fmt.Errorf(""), nil, nil)
test.expectInstallEksaComponentsBootstrap(nil, nil, nil, fmt.Errorf(""), nil, nil, nil, nil, nil)
test.expectSaveLogsManagement()
test.expectSaveLogsWorkload()

Expand All @@ -419,7 +419,7 @@ func TestDeleteRunFailEksdInstall(t *testing.T) {
}
}

func TestDeleteRunFailBuildClient(t *testing.T) {
func TestDeleteRunFailBuildSrcClient(t *testing.T) {
features.ClearCache()
os.Setenv(features.UseControllerForCli, "true")
test := newDeleteTest(t)
Expand All @@ -429,9 +429,46 @@ func TestDeleteRunFailBuildClient(t *testing.T) {
test.expectPreCAPI(nil)
test.expectInstallCAPI(nil)
test.expectMoveCAPI(nil, nil)
test.expectInstallEksaComponentsBootstrap(nil, nil, nil, nil, fmt.Errorf(""), nil)
test.expectInstallEksaComponentsBootstrap(nil, nil, nil, nil, fmt.Errorf(""), nil, nil, nil, nil)
test.expectSaveLogsManagement()

err := test.run()
if err == nil {
t.Fatalf("Delete.Run() err = %v, want err = nil", err)
}
}

func TestDeleteRunFailBuildDstClient(t *testing.T) {
features.ClearCache()
os.Setenv(features.UseControllerForCli, "true")
test := newDeleteTest(t)
test.expectSetup(nil)
test.expectBootstrapOpts(nil)
test.expectCreateBootstrap(nil)
test.expectPreCAPI(nil)
test.expectInstallCAPI(nil)
test.expectMoveCAPI(nil, nil)
test.expectInstallEksaComponentsBootstrap(nil, nil, nil, nil, nil, fmt.Errorf(""), nil, nil, nil)
test.expectSaveLogsManagement()

err := test.run()
if err == nil {
t.Fatalf("Delete.Run() err = %v, want err = nil", err)
}
}

func TestDeleteRunFailCreateNamespace(t *testing.T) {
features.ClearCache()
os.Setenv(features.UseControllerForCli, "true")
test := newDeleteTest(t)
test.expectSetup(nil)
test.expectBootstrapOpts(nil)
test.expectCreateBootstrap(nil)
test.expectPreCAPI(nil)
test.expectInstallCAPI(nil)
test.expectMoveCAPI(nil, nil)
test.expectInstallEksaComponentsBootstrap(nil, nil, nil, nil, nil, nil, fmt.Errorf(""), nil, nil)
test.expectSaveLogsManagement()
test.expectSaveLogsWorkload()

err := test.run()
if err == nil {
Expand All @@ -449,7 +486,7 @@ func TestDeleteRunFailAllowDeleteWhilePaused(t *testing.T) {
test.expectPreCAPI(nil)
test.expectInstallCAPI(nil)
test.expectMoveCAPI(nil, nil)
test.expectInstallEksaComponentsBootstrap(nil, nil, nil, nil, nil, fmt.Errorf(""))
test.expectInstallEksaComponentsBootstrap(nil, nil, nil, nil, nil, nil, nil, nil, fmt.Errorf(""))
test.expectSaveLogsManagement()

err := test.run()
Expand All @@ -468,7 +505,7 @@ func TestDeleteRunFailPostDelete(t *testing.T) {
test.expectPreCAPI(nil)
test.expectInstallCAPI(nil)
test.expectMoveCAPI(nil, nil)
test.expectInstallEksaComponentsBootstrap(nil, nil, nil, nil, nil, nil)
test.expectInstallEksaComponentsBootstrap(nil, nil, nil, nil, nil, nil, nil, nil, nil)
test.expectApplyOnBootstrap(nil)
test.expectDeleteCluster(nil, fmt.Errorf(""))
test.expectSaveLogsManagement()
Expand All @@ -489,7 +526,7 @@ func TestDeleteRunFailCleanupGit(t *testing.T) {
test.expectPreCAPI(nil)
test.expectInstallCAPI(nil)
test.expectMoveCAPI(nil, nil)
test.expectInstallEksaComponentsBootstrap(nil, nil, nil, nil, nil, nil)
test.expectInstallEksaComponentsBootstrap(nil, nil, nil, nil, nil, nil, nil, nil, nil)
test.expectApplyOnBootstrap(nil)
test.expectDeleteCluster(nil, nil)
test.expectCleanupGitRepo(fmt.Errorf(""))
Expand All @@ -512,7 +549,7 @@ func TestDeleteRunFailDeleteBootstrap(t *testing.T) {
test.expectPreCAPI(nil)
test.expectInstallCAPI(nil)
test.expectMoveCAPI(nil, nil)
test.expectInstallEksaComponentsBootstrap(nil, nil, nil, nil, nil, nil)
test.expectInstallEksaComponentsBootstrap(nil, nil, nil, nil, nil, nil, nil, nil, nil)
test.expectApplyOnBootstrap(nil)
test.expectDeleteCluster(nil, nil)
test.expectCleanupGitRepo(nil)
Expand Down

0 comments on commit 006453e

Please sign in to comment.