Skip to content

Commit

Permalink
some checking
Browse files Browse the repository at this point in the history
Signed-off-by: Feny Mehta <[email protected]>
  • Loading branch information
fbm3307 committed Sep 13, 2024
1 parent 2748b57 commit da57803
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 68 deletions.
1 change: 1 addition & 0 deletions pkg/cmd/adm/restart.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {

Check failure on line 109 in pkg/cmd/adm/restart.go

View workflow job for this annotation

GitHub Actions / GolangCI Lint

G601: Implicit memory aliasing in for loop. (gosec)
Expand Down
105 changes: 39 additions & 66 deletions pkg/cmd/adm/restart_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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) {
Expand Down Expand Up @@ -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")
})
}
}
Expand Down Expand Up @@ -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
Expand Down
3 changes: 1 addition & 2 deletions pkg/cmd/adm/unregister_member.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}

0 comments on commit da57803

Please sign in to comment.