diff --git a/pkg/cmd/adm/restart.go b/pkg/cmd/adm/restart.go index a5f5271..9ce25fe 100644 --- a/pkg/cmd/adm/restart.go +++ b/pkg/cmd/adm/restart.go @@ -70,7 +70,6 @@ func restart(ctx *clicontext.CommandContext, clusterNames ...string) error { kubeConfigFlags.KubeConfig = &kubeconfig cl, err := ctx.NewClient(cfg.Token, cfg.ServerAPI) - if err != nil { return err } diff --git a/pkg/cmd/adm/restart_test.go b/pkg/cmd/adm/restart_test.go index 36a42cc..04b958e 100644 --- a/pkg/cmd/adm/restart_test.go +++ b/pkg/cmd/adm/restart_test.go @@ -6,6 +6,7 @@ import ( "net/http" "testing" + "github.com/codeready-toolchain/toolchain-common/pkg/test" clicontext "github.com/kubesaw/ksctl/pkg/context" . "github.com/kubesaw/ksctl/pkg/test" @@ -24,7 +25,7 @@ import ( "k8s.io/kubectl/pkg/scheme" ) -func TestRestart(t *testing.T) { +func TestRestartDeployment(t *testing.T) { // given tests := map[string]struct { namespace string @@ -127,10 +128,8 @@ func TestRestart(t *testing.T) { }) streams, _, buf, _ := genericclioptions.NewTestIOStreams() - deployment := newDeployment(namespacedName, 1) - deployment.Labels = map[string]string{tc.labelKey: tc.labelValue} term := NewFakeTerminalWithResponse("Y") - newClient, fakeClient := NewFakeClients(t, deployment) + newClient, fakeClient := NewFakeClients(t, deployment1) ctx := clicontext.NewCommandContext(term, newClient) //when @@ -153,6 +152,53 @@ func TestRestart(t *testing.T) { } } +func TestRestart(t *testing.T) { + t.Run("restart should fail if more than one clustername", func(t *testing.T) { + //given + toolchainCluster := NewToolchainCluster(ToolchainClusterName("host-cool-server.com")) + deployment := newDeployment(test.NamespacedName("toolchain-host-operator", "host-operator-controller-manager"), 1) + term := NewFakeTerminalWithResponse("Y") + newClient, _ := NewFakeClients(t, toolchainCluster, deployment) + ctx := clicontext.NewCommandContext(term, newClient) + + //when + err := restart(ctx, "host-cool-server.com", "member") + + //then + require.Error(t, err, "please provide 1 cluster name to restart the operator e.g `ksctl adm restart host`") + }) + t.Run("restart should fail if zero clustername", func(t *testing.T) { + //given + toolchainCluster := NewToolchainCluster(ToolchainClusterName("host-cool-server.com")) + deployment := newDeployment(test.NamespacedName("toolchain-host-operator", "host-operator-controller-manager"), 1) + term := NewFakeTerminalWithResponse("Y") + newClient, _ := NewFakeClients(t, toolchainCluster, deployment) + ctx := clicontext.NewCommandContext(term, newClient) + + //when + err := restart(ctx) + + //then + require.Error(t, err, "please provide 1 cluster name to restart the operator e.g `ksctl adm restart host`") + }) + t.Run("restart should succeed with 1 clustername", func(t *testing.T) { + //given + SetFileConfig(t, Host()) + toolchainCluster := NewToolchainCluster(ToolchainClusterName("host")) + deployment := newDeployment(test.NamespacedName("toolchain-host-operator", "host-operator-controller-manager"), 1) + term := NewFakeTerminalWithResponse("Y") + newClient, _ := NewFakeClients(t, toolchainCluster, deployment) + ctx := clicontext.NewCommandContext(term, newClient) + + //when + err := restart(ctx, "host") + + //then + require.NoError(t, err) + }) + +} + func newDeployment(namespacedName types.NamespacedName, replicas int32) *appsv1.Deployment { //nolint:unparam return &appsv1.Deployment{ ObjectMeta: metav1.ObjectMeta{