From a4b51983af3e953e26780aa7f5d56eaf24afaf4a Mon Sep 17 00:00:00 2001 From: Feny Mehta Date: Thu, 14 Nov 2024 17:21:47 +0530 Subject: [PATCH] code cov Signed-off-by: Feny Mehta --- pkg/cmd/adm/restart.go | 10 +++---- pkg/cmd/adm/restart_test.go | 52 ++++++++++++++++++++++++++++++++++++- 2 files changed, 56 insertions(+), 6 deletions(-) diff --git a/pkg/cmd/adm/restart.go b/pkg/cmd/adm/restart.go index cb62c04..d87dcdb 100644 --- a/pkg/cmd/adm/restart.go +++ b/pkg/cmd/adm/restart.go @@ -75,16 +75,16 @@ func restart(ctx *clicontext.CommandContext, clusterName string) error { kubeConfigFlags.KubeConfig = &kubeconfig factory := cmdutil.NewFactory(cmdutil.NewMatchVersionFlags(kubeConfigFlags)) - cl, err := ctx.NewClient(cfg.Token, cfg.ServerAPI) - if err != nil { - return err - } - if !ctx.AskForConfirmation( ioutils.WithMessagef("restart all the deployments in the cluster '%s' and namespace '%s' \n", clusterName, cfg.OperatorNamespace)) { return nil } + cl, err := ctx.NewClient(cfg.Token, cfg.ServerAPI) + if err != nil { + return err + } + //return restartDeployment(ctx, cl, cfg.OperatorNamespace, factory, ioStreams, checkRolloutStatus, restartNonOperatorDeployments) return restartDeployments(ctx, cl, cfg.OperatorNamespace, func(ctx *clicontext.CommandContext, labelSelector string) error { return checkRolloutStatus(ctx, factory, ioStreams, labelSelector) diff --git a/pkg/cmd/adm/restart_test.go b/pkg/cmd/adm/restart_test.go index 31043f3..81426c9 100644 --- a/pkg/cmd/adm/restart_test.go +++ b/pkg/cmd/adm/restart_test.go @@ -8,6 +8,7 @@ import ( "testing" "github.com/codeready-toolchain/toolchain-common/pkg/test" + "github.com/h2non/gock" clicontext "github.com/kubesaw/ksctl/pkg/context" . "github.com/kubesaw/ksctl/pkg/test" "github.com/stretchr/testify/assert" @@ -85,7 +86,7 @@ func TestKubectlRolloutFunctionality(t *testing.T) { hostDep.Labels = map[string]string{"kubesaw-control-plane": "kubesaw-controller-manager"} regDep.Labels = map[string]string{"toolchain.dev.openshift.com/provider": "codeready-toolchain"} - t.Run("Rollout Restart and Rollout Status works successfuly", func(t *testing.T) { + t.Run("Rollout Restart and Rollout Status works successfully", func(t *testing.T) { csCalls = 0 newClient, fakeClient := NewFakeClients(t, hostDep, regDep, pod) ctx := clicontext.NewCommandContext(term, newClient) @@ -323,6 +324,55 @@ func TestRestartAutoScalerDeployment(t *testing.T) { }) } +func TestRestart(t *testing.T) { + //given + t.Cleanup(gock.OffAll) + gock.New("https://cool-server.com"). + Get("api"). + Persist(). + Reply(200). + BodyString("{}") + SetFileConfig(t, Host(), Member()) + toolchainCluster := NewToolchainCluster(ToolchainClusterName("host")) + + ///OLM-deployments + //host + hostDeployment := newDeployment(test.NamespacedName("toolchain-host-operator", "host-operator-controller-manager"), 1) + hostDeployment.Labels = map[string]string{"kubesaw-control-plane": "kubesaw-controller-manager"} + hostPod := newPod(test.NamespacedName("toolchain-host-operator", "host-operator-controller-manager")) + + //Non-OLM deployments + //reg-svc + regServDeployment := newDeployment(test.NamespacedName("toolchain-host-operator", "registration-service"), 1) + regServDeployment.Labels = map[string]string{"toolchain.dev.openshift.com/provider": "codeready-toolchain"} + + t.Run("No restart when users says NO in confirmaion of restart", func(t *testing.T) { + term := NewFakeTerminalWithResponse("N") + //given + newClient, _ := NewFakeClients(t, toolchainCluster, hostDeployment, hostPod) + ctx := clicontext.NewCommandContext(term, newClient) + //when + err := restart(ctx, "host") + + //then + require.NoError(t, err) + require.NotContains(t, term.Output(), "Fetching the current OLM and non-OLM deployments of the operator in") + + }) + t.Run("fails when no factory and stream provided", func(t *testing.T) { + term := NewFakeTerminalWithResponse("Y") + //given + newClient, _ := NewFakeClients(t, toolchainCluster, hostDeployment, hostPod) + ctx := clicontext.NewCommandContext(term, newClient) + //when + err := restart(ctx, "host") + + //then + require.Error(t, err, "the server doesn't have a resource type deployment") + + }) +} + func newDeployment(namespacedName types.NamespacedName, replicas int32) *appsv1.Deployment { //nolint:unparam return &appsv1.Deployment{ ObjectMeta: metav1.ObjectMeta{