From da57803b2bd94439e56732b2cc2f096ae59dea5b Mon Sep 17 00:00:00 2001 From: Feny Mehta Date: Fri, 13 Sep 2024 16:20:06 +0530 Subject: [PATCH] some checking Signed-off-by: Feny Mehta --- pkg/cmd/adm/restart.go | 1 + pkg/cmd/adm/restart_test.go | 105 ++++++++++++------------------- pkg/cmd/adm/unregister_member.go | 3 +- 3 files changed, 41 insertions(+), 68 deletions(-) diff --git a/pkg/cmd/adm/restart.go b/pkg/cmd/adm/restart.go index 60ed006..c3738dd 100644 --- a/pkg/cmd/adm/restart.go +++ b/pkg/cmd/adm/restart.go @@ -103,6 +103,7 @@ func deletePods(ctx *clicontext.CommandContext, cl runtimeclient.Client, deploym if err := cl.List(ctx, &pods, runtimeclient.MatchingLabelsSelector{Selector: selector}); err != nil { return err } + //delete pods for _, pod := range pods.Items { if err := cl.Delete(ctx, &pod); err != nil { diff --git a/pkg/cmd/adm/restart_test.go b/pkg/cmd/adm/restart_test.go index b01ca35..e7e4b99 100644 --- a/pkg/cmd/adm/restart_test.go +++ b/pkg/cmd/adm/restart_test.go @@ -48,7 +48,6 @@ func TestRestartDeployment(t *testing.T) { // then require.NoError(t, err) AssertDeploymentHasReplicas(t, fakeClient, namespacedName, 3) - assert.Equal(t, 2, numberOfUpdateCalls) }) t.Run("list deployments when no deployment name is provided for "+clusterName, func(t *testing.T) { @@ -64,11 +63,9 @@ func TestRestartDeployment(t *testing.T) { err := restart(ctx, clusterName) // then - require.EqualError(t, err, "at least one deployment name is required, include one or more of the above deployments to restart") + require.EqualError(t, err, "please mention one of the following operator names to restart: host | member-1 | member-2") AssertDeploymentHasReplicas(t, fakeClient, namespacedName, 3) assert.Equal(t, 0, numberOfUpdateCalls) - assert.Contains(t, term.Output(), fmt.Sprintf("Existing deployments in toolchain-%s-operator namespace", clusterType)) - assert.Contains(t, term.Output(), "cool-deployment") }) t.Run("restart fails - cannot get the deployment for "+clusterName, func(t *testing.T) { @@ -109,8 +106,6 @@ func TestRestartDeployment(t *testing.T) { AssertDeploymentHasReplicas(t, fakeClient, namespacedName, 3) assert.Equal(t, 0, numberOfUpdateCalls) assert.Contains(t, term.Output(), "ERROR: The given deployment 'wrong-deployment' wasn't found.") - assert.Contains(t, term.Output(), fmt.Sprintf("Existing deployments in toolchain-%s-operator namespace", clusterType)) - assert.Contains(t, term.Output(), "cool-deployment") }) } } @@ -150,67 +145,45 @@ func TestRestartHostOperator(t *testing.T) { // given SetFileConfig(t, Host()) term := NewFakeTerminalWithResponse("") // it should not read the input - _, err := configuration.LoadClusterConfig(term, "host") + cfg, err := configuration.LoadClusterConfig(term, "host") require.NoError(t, err) - // namespacedName := types.NamespacedName{ - // Namespace: "toolchain-host-operator", - // Name: "host-operator-controller-manager", - // } - - // t.Run("host deployment is present and restart successful", func(t *testing.T) { - // // given - // deployment := newDeployment(namespacedName, 1) - // deployment.Labels = map[string]string{"olm.owner.namespace": "toolchain-host-operator"} - // newClient, fakeClient := NewFakeClients(t, deployment) - // numberOfUpdateCalls := 0 - // fakeClient.MockUpdate = requireDeploymentBeingUpdated(t, fakeClient, namespacedName, 1, &numberOfUpdateCalls) - // ctx := clicontext.NewCommandContext(term, newClient) - - // // when - // err := restartHostOperator(ctx, fakeClient, cfg.OperatorNamespace) - - // // then - // require.NoError(t, err) - // AssertDeploymentHasReplicas(t, fakeClient, namespacedName, 1) - // assert.Equal(t, 2, numberOfUpdateCalls) - // }) - - // t.Run("host deployment with the label is not present - restart fails", func(t *testing.T) { - // // given - // deployment := newDeployment(namespacedName, 1) - // newClient, fakeClient := NewFakeClients(t, deployment) - // numberOfUpdateCalls := 0 - // fakeClient.MockUpdate = requireDeploymentBeingUpdated(t, fakeClient, namespacedName, 1, &numberOfUpdateCalls) - // ctx := clicontext.NewCommandContext(term, newClient) - - // // when - // err := restartHostOperator(ctx, fakeClient, cfg.OperatorNamespace) - - // // then - // require.Error(t, err) - // AssertDeploymentHasReplicas(t, fakeClient, namespacedName, 1) - // assert.Equal(t, 0, numberOfUpdateCalls) - // }) - - // t.Run("there are more deployments with the host operator label - restart fails", func(t *testing.T) { - // // given - // deployment := newDeployment(namespacedName, 1) - // deployment.Labels = map[string]string{"olm.owner.namespace": "toolchain-host-operator"} - // deployment2 := deployment.DeepCopy() - // deployment2.Name = "another" - // newClient, fakeClient := NewFakeClients(t, deployment, deployment2) - // numberOfUpdateCalls := 0 - // fakeClient.MockUpdate = requireDeploymentBeingUpdated(t, fakeClient, namespacedName, 1, &numberOfUpdateCalls) - // ctx := clicontext.NewCommandContext(term, newClient) - - // // when - // err := restartHostOperator(ctx, fakeClient, cfg.OperatorNamespace) - - // // then - // require.Error(t, err) - // AssertDeploymentHasReplicas(t, fakeClient, namespacedName, 1) - // assert.Equal(t, 0, numberOfUpdateCalls) - // }) + namespacedName := types.NamespacedName{ + Namespace: "toolchain-host-operator", + Name: "host-operator-controller-manager", + } + + t.Run("host deployment is present and restart successful", func(t *testing.T) { + // given + deployment := newDeployment(namespacedName, 1) + deployment.Labels = map[string]string{"olm.owner.kind": "ClusterServiceVersion"} + newClient, fakeClient := NewFakeClients(t, deployment) + numberOfUpdateCalls := 0 + fakeClient.MockUpdate = requireDeploymentBeingUpdated(t, fakeClient, namespacedName, 1, &numberOfUpdateCalls) + ctx := clicontext.NewCommandContext(term, newClient) + + // when + err := restartDeployment(ctx, fakeClient, cfg.OperatorNamespace) + + // then + require.NoError(t, err) + AssertDeploymentHasReplicas(t, fakeClient, namespacedName, 1) + }) + + t.Run("host deployment with the label is not present - restart fails", func(t *testing.T) { + // given + deployment := newDeployment(namespacedName, 1) + newClient, fakeClient := NewFakeClients(t, deployment) + numberOfUpdateCalls := 0 + fakeClient.MockUpdate = requireDeploymentBeingUpdated(t, fakeClient, namespacedName, 1, &numberOfUpdateCalls) + ctx := clicontext.NewCommandContext(term, newClient) + + // when + err := restartDeployment(ctx, fakeClient, cfg.OperatorNamespace) + + // then + require.NoError(t, err) + + }) } func newDeployment(namespacedName types.NamespacedName, replicas int32) *appsv1.Deployment { //nolint:unparam diff --git a/pkg/cmd/adm/unregister_member.go b/pkg/cmd/adm/unregister_member.go index dc1557b..2fb3af7 100644 --- a/pkg/cmd/adm/unregister_member.go +++ b/pkg/cmd/adm/unregister_member.go @@ -62,6 +62,5 @@ func UnregisterMemberCluster(ctx *clicontext.CommandContext, clusterName string) } ctx.Printlnf("\nThe deletion of the Toolchain member cluster from the Host cluster has been triggered") - //return restartHostOperator(ctx, hostClusterClient, hostClusterConfig.OperatorNamespace) - return nil + return restartDeployment(ctx, hostClusterClient, hostClusterConfig.OperatorNamespace) }