Skip to content

Commit

Permalink
unit test case for restart
Browse files Browse the repository at this point in the history
Signed-off-by: Feny Mehta <[email protected]>
  • Loading branch information
fbm3307 committed Sep 25, 2024
1 parent c0332b1 commit 83e99b5
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 5 deletions.
1 change: 0 additions & 1 deletion pkg/cmd/adm/restart.go
Original file line number Diff line number Diff line change
Expand Up @@ -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

Check warning on line 74 in pkg/cmd/adm/restart.go

View check run for this annotation

Codecov / codecov/patch

pkg/cmd/adm/restart.go#L74

Added line #L74 was not covered by tests
}
Expand Down
54 changes: 50 additions & 4 deletions pkg/cmd/adm/restart_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"

Expand All @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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{
Expand Down

0 comments on commit 83e99b5

Please sign in to comment.