Skip to content

Commit

Permalink
code cov
Browse files Browse the repository at this point in the history
Signed-off-by: Feny Mehta <fbm3307@gmail.com>
  • Loading branch information
fbm3307 committed Nov 14, 2024
1 parent 17da571 commit a4b5198
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 6 deletions.
10 changes: 5 additions & 5 deletions pkg/cmd/adm/restart.go
Original file line number Diff line number Diff line change
Expand Up @@ -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

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

View check run for this annotation

Codecov / codecov/patch

pkg/cmd/adm/restart.go#L85

Added line #L85 was not covered by tests
}

//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)
Expand Down
52 changes: 51 additions & 1 deletion pkg/cmd/adm/restart_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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{
Expand Down

0 comments on commit a4b5198

Please sign in to comment.