From 1e745c3c29308313265c67c6b1eed2b2042faa5f Mon Sep 17 00:00:00 2001 From: Xavier Coulon Date: Tue, 19 Mar 2024 10:30:23 +0100 Subject: [PATCH] remove unused RestClient param not used anymore Signed-off-by: Xavier Coulon --- pkg/client/client_test.go | 27 ++++---- pkg/cmd/add_space_users.go | 2 +- pkg/cmd/add_space_users_test.go | 38 ++++++------ pkg/cmd/adm/register_member.go | 2 +- pkg/cmd/adm/register_member_test.go | 16 ++--- pkg/cmd/adm/restart.go | 2 +- pkg/cmd/adm/restart_test.go | 32 +++++----- pkg/cmd/adm/unregister_member.go | 2 +- pkg/cmd/adm/unregister_member_test.go | 20 +++--- pkg/cmd/approve.go | 2 +- pkg/cmd/approve_test.go | 62 +++++++++---------- pkg/cmd/ban.go | 2 +- pkg/cmd/ban_test.go | 42 ++++++------- pkg/cmd/create_social_event.go | 2 +- pkg/cmd/create_social_event_test.go | 28 ++++----- pkg/cmd/deactivate.go | 2 +- pkg/cmd/deactivate_test.go | 12 ++-- pkg/cmd/delete.go | 2 +- pkg/cmd/delete_test.go | 20 +++--- pkg/cmd/disable_user.go | 2 +- pkg/cmd/disable_user_test.go | 12 ++-- pkg/cmd/promote_space.go | 2 +- pkg/cmd/promote_space_test.go | 16 ++--- pkg/cmd/promote_user.go | 2 +- pkg/cmd/promote_user_test.go | 16 ++--- pkg/cmd/remove_space_users.go | 2 +- pkg/cmd/remove_space_users_test.go | 24 +++---- pkg/cmd/retarget.go | 2 +- pkg/cmd/retarget_test.go | 38 ++++++------ pkg/cmd/status.go | 2 +- pkg/cmd/status_test.go | 20 +++--- pkg/context/clusterconfig_command_context.go | 7 +-- .../clusterconfig_command_context_test.go | 4 +- pkg/context/command_context.go | 16 ++--- pkg/context/command_context_test.go | 8 +-- pkg/test/client.go | 5 +- 36 files changed, 241 insertions(+), 252 deletions(-) diff --git a/pkg/client/client_test.go b/pkg/client/client_test.go index b3cc140..c2daa65 100644 --- a/pkg/client/client_test.go +++ b/pkg/client/client_test.go @@ -56,9 +56,9 @@ func TestPatchUserSignup(t *testing.T) { t.Run("update is successful", func(t *testing.T) { //given userSignup := NewUserSignup() - newClient, newRESTClient, fakeClient := NewFakeClients(t, userSignup) + newClient, fakeClient := NewFakeClients(t, userSignup) term := NewFakeTerminal() - ctx := clicontext.NewCommandContext(term, newClient, newRESTClient) + ctx := clicontext.NewCommandContext(term, newClient) // when err := client.PatchUserSignup(ctx, userSignup.Name, func(signup *toolchainv1alpha1.UserSignup) (bool, error) { @@ -75,9 +75,9 @@ func TestPatchUserSignup(t *testing.T) { t.Run("UserSignup should not be updated", func(t *testing.T) { //given userSignup := NewUserSignup() - newClient, newRESTClient, fakeClient := NewFakeClients(t, userSignup) + newClient, fakeClient := NewFakeClients(t, userSignup) term := NewFakeTerminal() - ctx := clicontext.NewCommandContext(term, newClient, newRESTClient) + ctx := clicontext.NewCommandContext(term, newClient) // when err := client.PatchUserSignup(ctx, userSignup.Name, func(signup *toolchainv1alpha1.UserSignup) (bool, error) { @@ -93,9 +93,9 @@ func TestPatchUserSignup(t *testing.T) { t.Run("change UserSignup func returns error", func(t *testing.T) { //given userSignup := NewUserSignup() - newClient, newRESTClient, fakeClient := NewFakeClients(t, userSignup) + newClient, fakeClient := NewFakeClients(t, userSignup) term := NewFakeTerminal() - ctx := clicontext.NewCommandContext(term, newClient, newRESTClient) + ctx := clicontext.NewCommandContext(term, newClient) // when err := client.PatchUserSignup(ctx, userSignup.Name, func(signup *toolchainv1alpha1.UserSignup) (bool, error) { @@ -111,12 +111,12 @@ func TestPatchUserSignup(t *testing.T) { t.Run("get of UserSignup fails", func(t *testing.T) { //given userSignup := NewUserSignup() - newClient, newRESTClient, fakeClient := NewFakeClients(t, userSignup) + newClient, fakeClient := NewFakeClients(t, userSignup) fakeClient.MockGet = func(ctx context.Context, key runtimeclient.ObjectKey, obj runtimeclient.Object, opts ...runtimeclient.GetOption) error { return fmt.Errorf("some error") } term := NewFakeTerminal() - ctx := clicontext.NewCommandContext(term, newClient, newRESTClient) + ctx := clicontext.NewCommandContext(term, newClient) // when err := client.PatchUserSignup(ctx, userSignup.Name, func(signup *toolchainv1alpha1.UserSignup) (bool, error) { @@ -131,12 +131,12 @@ func TestPatchUserSignup(t *testing.T) { t.Run("update of UserSignup fails", func(t *testing.T) { //given userSignup := NewUserSignup() - newClient, newRESTClient, fakeClient := NewFakeClients(t, userSignup) + newClient, fakeClient := NewFakeClients(t, userSignup) fakeClient.MockPatch = func(ctx context.Context, obj runtimeclient.Object, patch runtimeclient.Patch, opts ...runtimeclient.PatchOption) error { return fmt.Errorf("some error") } term := NewFakeTerminal() - ctx := clicontext.NewCommandContext(term, newClient, newRESTClient) + ctx := clicontext.NewCommandContext(term, newClient) // when err := client.PatchUserSignup(ctx, userSignup.Name, func(signup *toolchainv1alpha1.UserSignup) (bool, error) { @@ -157,8 +157,7 @@ func TestPatchUserSignup(t *testing.T) { newClient := func(_, _ string) (runtimeclient.Client, error) { return nil, fmt.Errorf("some error") } - newRESTClient := client.DefaultNewRESTClient - ctx := clicontext.NewCommandContext(term, newClient, newRESTClient) + ctx := clicontext.NewCommandContext(term, newClient) // when err := client.PatchUserSignup(ctx, userSignup.Name, func(signup *toolchainv1alpha1.UserSignup) (bool, error) { @@ -177,9 +176,9 @@ func TestUpdateUserSignupLacksPermissions(t *testing.T) { SetFileConfig(t, Host(NoToken())) userSignup := NewUserSignup() - newClient, newRESTClient, fakeClient := NewFakeClients(t, userSignup) + newClient, fakeClient := NewFakeClients(t, userSignup) term := NewFakeTerminal() - ctx := clicontext.NewCommandContext(term, newClient, newRESTClient) + ctx := clicontext.NewCommandContext(term, newClient) // when err := client.PatchUserSignup(ctx, userSignup.Name, func(signup *toolchainv1alpha1.UserSignup) (bool, error) { diff --git a/pkg/cmd/add_space_users.go b/pkg/cmd/add_space_users.go index 6827d07..231713b 100644 --- a/pkg/cmd/add_space_users.go +++ b/pkg/cmd/add_space_users.go @@ -28,7 +28,7 @@ one or more users specified by their MasterUserRecord name. One SpaceBinding wil Args: cobra.ExactArgs(0), RunE: func(cmd *cobra.Command, args []string) error { term := ioutils.NewTerminal(cmd.InOrStdin, cmd.OutOrStdout) - ctx := clicontext.NewCommandContext(term, client.DefaultNewClient, client.DefaultNewRESTClient) + ctx := clicontext.NewCommandContext(term, client.DefaultNewClient) return AddSpaceUsers(ctx, spaceName, role, users) }, diff --git a/pkg/cmd/add_space_users_test.go b/pkg/cmd/add_space_users_test.go index 1b62d7d..5d0bb68 100644 --- a/pkg/cmd/add_space_users_test.go +++ b/pkg/cmd/add_space_users_test.go @@ -25,11 +25,11 @@ func TestAddSpaceUsers(t *testing.T) { // given mur1 := masteruserrecord.NewMasterUserRecord(t, "alice", masteruserrecord.TierName("deactivate30")) mur2 := masteruserrecord.NewMasterUserRecord(t, "bob", masteruserrecord.TierName("deactivate30")) - newClient, newRESTClient, fakeClient := initAddSpaceUsersTest(t, mur1, mur2) + newClient, fakeClient := initAddSpaceUsersTest(t, mur1, mur2) SetFileConfig(t, Host()) term := NewFakeTerminalWithResponse("Y") - ctx := clicontext.NewCommandContext(term, newClient, newRESTClient) + ctx := clicontext.NewCommandContext(term, newClient) // when err := cmd.AddSpaceUsers(ctx, "testspace", "admin", []string{"alice", "bob"}) @@ -47,11 +47,11 @@ func TestAddSpaceUsers(t *testing.T) { // given mur1 := masteruserrecord.NewMasterUserRecord(t, "alice", masteruserrecord.TierName("deactivate30")) mur2 := masteruserrecord.NewMasterUserRecord(t, "bob", masteruserrecord.TierName("deactivate30")) - newClient, newRESTClient, fakeClient := initAddSpaceUsersTest(t, mur1, mur2) + newClient, fakeClient := initAddSpaceUsersTest(t, mur1, mur2) SetFileConfig(t, Host()) term := NewFakeTerminalWithResponse("Y") - ctx := clicontext.NewCommandContext(term, newClient, newRESTClient) + ctx := clicontext.NewCommandContext(term, newClient) // when err := cmd.AddSpaceUsers(ctx, "testspace", "viewer", []string{"alice", "bob"}) @@ -69,11 +69,11 @@ func TestAddSpaceUsers(t *testing.T) { // given mur1 := masteruserrecord.NewMasterUserRecord(t, "alice", masteruserrecord.TierName("deactivate30")) mur2 := masteruserrecord.NewMasterUserRecord(t, "bob", masteruserrecord.TierName("deactivate30")) - newClient, newRESTClient, fakeClient := initAddSpaceUsersTest(t, mur1, mur2) + newClient, fakeClient := initAddSpaceUsersTest(t, mur1, mur2) SetFileConfig(t, Host()) term := NewFakeTerminalWithResponse("N") - ctx := clicontext.NewCommandContext(term, newClient, newRESTClient) + ctx := clicontext.NewCommandContext(term, newClient) // when err := cmd.AddSpaceUsers(ctx, "testspace", "admin", []string{"alice", "bob"}) @@ -91,11 +91,11 @@ func TestAddSpaceUsers(t *testing.T) { // given mur1 := masteruserrecord.NewMasterUserRecord(t, "alice", masteruserrecord.TierName("deactivate30")) mur2 := masteruserrecord.NewMasterUserRecord(t, "bob", masteruserrecord.TierName("deactivate30")) - newClient, newRESTClient, fakeClient := NewFakeClients(t, mur1, mur2) // no space + newClient, fakeClient := NewFakeClients(t, mur1, mur2) // no space SetFileConfig(t, Host()) term := NewFakeTerminalWithResponse("N") - ctx := clicontext.NewCommandContext(term, newClient, newRESTClient) + ctx := clicontext.NewCommandContext(term, newClient) // when err := cmd.AddSpaceUsers(ctx, "testspace", "admin", []string{"alice", "bob"}) @@ -111,11 +111,11 @@ func TestAddSpaceUsers(t *testing.T) { t.Run("when first mur not found", func(t *testing.T) { // given - newClient, newRESTClient, fakeClient := initAddSpaceUsersTest(t) // no murs + newClient, fakeClient := initAddSpaceUsersTest(t) // no murs SetFileConfig(t, Host()) term := NewFakeTerminalWithResponse("N") - ctx := clicontext.NewCommandContext(term, newClient, newRESTClient) + ctx := clicontext.NewCommandContext(term, newClient) // when err := cmd.AddSpaceUsers(ctx, "testspace", "admin", []string{"alice", "bob"}) @@ -132,11 +132,11 @@ func TestAddSpaceUsers(t *testing.T) { t.Run("when second mur not found", func(t *testing.T) { // given mur1 := masteruserrecord.NewMasterUserRecord(t, "alice", masteruserrecord.TierName("deactivate30")) - newClient, newRESTClient, fakeClient := initAddSpaceUsersTest(t, mur1) // mur2 missing + newClient, fakeClient := initAddSpaceUsersTest(t, mur1) // mur2 missing SetFileConfig(t, Host()) term := NewFakeTerminalWithResponse("N") - ctx := clicontext.NewCommandContext(term, newClient, newRESTClient) + ctx := clicontext.NewCommandContext(term, newClient) // when err := cmd.AddSpaceUsers(ctx, "testspace", "admin", []string{"alice", "bob"}) @@ -153,11 +153,11 @@ func TestAddSpaceUsers(t *testing.T) { t.Run("when role is invalid", func(t *testing.T) { // given mur1 := masteruserrecord.NewMasterUserRecord(t, "alice", masteruserrecord.TierName("deactivate30")) - newClient, newRESTClient, fakeClient := initAddSpaceUsersTest(t, mur1) // mur2 missing + newClient, fakeClient := initAddSpaceUsersTest(t, mur1) // mur2 missing SetFileConfig(t, Host()) term := NewFakeTerminalWithResponse("N") - ctx := clicontext.NewCommandContext(term, newClient, newRESTClient) + ctx := clicontext.NewCommandContext(term, newClient) // when err := cmd.AddSpaceUsers(ctx, "testspace", "badrole", []string{"alice", "bob"}) // invalid role @@ -177,14 +177,14 @@ func TestAddSpaceUsers(t *testing.T) { // given mur1 := masteruserrecord.NewMasterUserRecord(t, "alice", masteruserrecord.TierName("deactivate30")) mur2 := masteruserrecord.NewMasterUserRecord(t, "bob", masteruserrecord.TierName("deactivate30")) - newClient, newRESTClient, fakeClient := initAddSpaceUsersTest(t, mur1, mur2) + newClient, fakeClient := initAddSpaceUsersTest(t, mur1, mur2) fakeClient.MockGet = func(ctx context.Context, key runtimeclient.ObjectKey, obj runtimeclient.Object, opts ...runtimeclient.GetOption) error { return fmt.Errorf("client error") } SetFileConfig(t, Host()) term := NewFakeTerminalWithResponse("Y") - ctx := clicontext.NewCommandContext(term, newClient, newRESTClient) + ctx := clicontext.NewCommandContext(term, newClient) // when err := cmd.AddSpaceUsers(ctx, "testspace", "admin", []string{"alice", "bob"}) @@ -199,7 +199,7 @@ func TestAddSpaceUsers(t *testing.T) { }) } -func initAddSpaceUsersTest(t *testing.T, murs ...*toolchainv1alpha1.MasterUserRecord) (clicontext.NewClientFunc, clicontext.NewRESTClientFunc, *test.FakeClient) { +func initAddSpaceUsersTest(t *testing.T, murs ...*toolchainv1alpha1.MasterUserRecord) (clicontext.NewClientFunc, *test.FakeClient) { space := newSpace() nsTemplateTier := newNSTemplateTier("base") roles := make(map[string]toolchainv1alpha1.NSTemplateTierSpaceRole) @@ -214,8 +214,8 @@ func initAddSpaceUsersTest(t *testing.T, murs ...*toolchainv1alpha1.MasterUserRe for _, mur := range murs { objs = append(objs, mur) } - newClient, newRESTClient, fakeClient := NewFakeClients(t, objs...) - return newClient, newRESTClient, fakeClient + newClient, fakeClient := NewFakeClients(t, objs...) + return newClient, fakeClient } func assertSpaceBindings(t *testing.T, fakeClient *test.FakeClient, expectedMurs []string, expectedRole string) { diff --git a/pkg/cmd/adm/register_member.go b/pkg/cmd/adm/register_member.go index 2ec04b6..a983835 100644 --- a/pkg/cmd/adm/register_member.go +++ b/pkg/cmd/adm/register_member.go @@ -37,7 +37,7 @@ func NewRegisterMemberCmd() *cobra.Command { Long: `Downloads the 'add-cluster.sh' script from the 'toolchain-cicd' repo and calls it twice: once to register the Host cluster in the Member cluster and once to register the Member cluster in the host cluster.`, RunE: func(cmd *cobra.Command, args []string) error { term := ioutils.NewTerminal(cmd.InOrStdin, cmd.OutOrStdout) - ctx := clicontext.NewCommandContext(term, client.DefaultNewClient, client.DefaultNewRESTClient) + ctx := clicontext.NewCommandContext(term, client.DefaultNewClient) newCommand := func(name string, args ...string) *exec.Cmd { return exec.Command(name, args...) } diff --git a/pkg/cmd/adm/register_member_test.go b/pkg/cmd/adm/register_member_test.go index 0b87ea8..7aed996 100644 --- a/pkg/cmd/adm/register_member_test.go +++ b/pkg/cmd/adm/register_member_test.go @@ -55,10 +55,10 @@ func TestRegisterMember(t *testing.T) { t.Run("When automatic approval is enabled", func(t *testing.T) { term := NewFakeTerminalWithResponse("Y") toolchainConfig := config.NewToolchainConfigObj(t, config.AutomaticApproval().Enabled(true)) - newClient, newRESTClient, fakeClient := NewFakeClients(t, toolchainConfig, deployment) + newClient, fakeClient := NewFakeClients(t, toolchainConfig, deployment) numberOfUpdateCalls := 0 fakeClient.MockUpdate = whenDeploymentThenUpdated(t, fakeClient, hostDeploymentName, 1, &numberOfUpdateCalls) - ctx := clicontext.NewCommandContext(term, newClient, newRESTClient) + ctx := clicontext.NewCommandContext(term, newClient) counter = 0 // when @@ -87,14 +87,14 @@ func TestRegisterMember(t *testing.T) { t.Run("When toolchainConfig is not present", func(t *testing.T) { term := NewFakeTerminalWithResponse("Y") - newClient, newRESTClient, fakeClient := NewFakeClients(t, deployment) + newClient, fakeClient := NewFakeClients(t, deployment) fakeClient.MockUpdate = func(ctx context.Context, obj runtimeclient.Object, opts ...runtimeclient.UpdateOption) error { if _, ok := obj.(*toolchainv1alpha1.ToolchainConfig); ok { return fmt.Errorf("should not be called") } return fakeClient.Client.Update(ctx, obj, opts...) } - ctx := clicontext.NewCommandContext(term, newClient, newRESTClient) + ctx := clicontext.NewCommandContext(term, newClient) counter = 0 // when @@ -113,14 +113,14 @@ func TestRegisterMember(t *testing.T) { t.Run("When automatic approval is disabled", func(t *testing.T) { term := NewFakeTerminalWithResponse("Y") toolchainConfig := config.NewToolchainConfigObj(t, config.AutomaticApproval().Enabled(false)) - newClient, newRESTClient, fakeClient := NewFakeClients(t, toolchainConfig, deployment) + newClient, fakeClient := NewFakeClients(t, toolchainConfig, deployment) fakeClient.MockUpdate = func(ctx context.Context, obj runtimeclient.Object, opts ...runtimeclient.UpdateOption) error { if _, ok := obj.(*toolchainv1alpha1.ToolchainConfig); ok { return fmt.Errorf("should not be called") } return fakeClient.Client.Update(ctx, obj, opts...) } - ctx := clicontext.NewCommandContext(term, newClient, newRESTClient) + ctx := clicontext.NewCommandContext(term, newClient) counter = 0 // when @@ -143,11 +143,11 @@ func TestRegisterMember(t *testing.T) { toolchainConfig := config.NewToolchainConfigObj(t, config.AutomaticApproval().Enabled(false)) toolchainConfig2 := config.NewToolchainConfigObj(t, config.AutomaticApproval().Enabled(true)) toolchainConfig2.Name = "config2" - newClient, newRESTClient, fakeClient := NewFakeClients(t, toolchainConfig, toolchainConfig2, deployment) + newClient, fakeClient := NewFakeClients(t, toolchainConfig, toolchainConfig2, deployment) fakeClient.MockUpdate = func(ctx context.Context, obj runtimeclient.Object, opts ...runtimeclient.UpdateOption) error { return fmt.Errorf("should not be called") } - ctx := clicontext.NewCommandContext(term, newClient, newRESTClient) + ctx := clicontext.NewCommandContext(term, newClient) counter = 0 // when diff --git a/pkg/cmd/adm/restart.go b/pkg/cmd/adm/restart.go index cd10361..2b12c5b 100644 --- a/pkg/cmd/adm/restart.go +++ b/pkg/cmd/adm/restart.go @@ -29,7 +29,7 @@ If no deployment name is provided, then it lists all existing deployments in the Args: cobra.RangeArgs(0, 1), RunE: func(cmd *cobra.Command, args []string) error { term := ioutils.NewTerminal(cmd.InOrStdin, cmd.OutOrStdout) - ctx := clicontext.NewCommandContext(term, client.DefaultNewClient, client.DefaultNewRESTClient) + ctx := clicontext.NewCommandContext(term, client.DefaultNewClient) return restart(ctx, targetCluster, args...) }, } diff --git a/pkg/cmd/adm/restart_test.go b/pkg/cmd/adm/restart_test.go index 10f21e7..aeb694a 100644 --- a/pkg/cmd/adm/restart_test.go +++ b/pkg/cmd/adm/restart_test.go @@ -37,10 +37,10 @@ func TestRestartDeployment(t *testing.T) { t.Run("restart is successful for "+clusterName, func(t *testing.T) { // given deployment := newDeployment(namespacedName, 3) - newClient, newRESTClient, fakeClient := NewFakeClients(t, deployment) + newClient, fakeClient := NewFakeClients(t, deployment) numberOfUpdateCalls := 0 fakeClient.MockUpdate = requireDeploymentBeingUpdated(t, fakeClient, namespacedName, 3, &numberOfUpdateCalls) - ctx := clicontext.NewCommandContext(term, newClient, newRESTClient) + ctx := clicontext.NewCommandContext(term, newClient) // when err := restart(ctx, clusterName, "cool-deployment") @@ -55,11 +55,11 @@ func TestRestartDeployment(t *testing.T) { t.Run("list deployments when no deployment name is provided for "+clusterName, func(t *testing.T) { // given deployment := newDeployment(namespacedName, 3) - newClient, newRESTClient, fakeClient := NewFakeClients(t, deployment) + newClient, fakeClient := NewFakeClients(t, deployment) numberOfUpdateCalls := 0 fakeClient.MockUpdate = requireDeploymentBeingUpdated(t, fakeClient, namespacedName, 3, &numberOfUpdateCalls) term := NewFakeTerminalWithResponse("Y") - ctx := clicontext.NewCommandContext(term, newClient, newRESTClient) + ctx := clicontext.NewCommandContext(term, newClient) // when err := restart(ctx, clusterName) @@ -75,13 +75,13 @@ func TestRestartDeployment(t *testing.T) { t.Run("restart fails - cannot get the deployment for "+clusterName, func(t *testing.T) { // given deployment := newDeployment(namespacedName, 3) - newClient, newRESTClient, fakeClient := NewFakeClients(t, deployment) + newClient, fakeClient := NewFakeClients(t, deployment) numberOfUpdateCalls := 0 fakeClient.MockUpdate = requireDeploymentBeingUpdated(t, fakeClient, namespacedName, 3, &numberOfUpdateCalls) fakeClient.MockGet = func(ctx context.Context, key runtimeclient.ObjectKey, obj runtimeclient.Object, opts ...runtimeclient.GetOption) error { return fmt.Errorf("some error") } - ctx := clicontext.NewCommandContext(term, newClient, newRESTClient) + ctx := clicontext.NewCommandContext(term, newClient) // when err := restart(ctx, clusterName, "cool-deployment") @@ -96,11 +96,11 @@ func TestRestartDeployment(t *testing.T) { t.Run("restart fails - deployment not found for "+clusterName, func(t *testing.T) { // given deployment := newDeployment(namespacedName, 3) - newClient, newRESTClient, fakeClient := NewFakeClients(t, deployment) + newClient, fakeClient := NewFakeClients(t, deployment) numberOfUpdateCalls := 0 fakeClient.MockUpdate = requireDeploymentBeingUpdated(t, fakeClient, namespacedName, 3, &numberOfUpdateCalls) term := NewFakeTerminalWithResponse("Y") - ctx := clicontext.NewCommandContext(term, newClient, newRESTClient) + ctx := clicontext.NewCommandContext(term, newClient) // when err := restart(ctx, clusterName, "wrong-deployment") @@ -131,11 +131,11 @@ func TestRestartDeploymentWithInsufficientPermissions(t *testing.T) { Name: "cool-deployment", } deployment := newDeployment(namespacedName, 3) - newClient, newRESTClient, fakeClient := NewFakeClients(t, deployment) + newClient, fakeClient := NewFakeClients(t, deployment) numberOfUpdateCalls := 0 fakeClient.MockUpdate = requireDeploymentBeingUpdated(t, fakeClient, namespacedName, 3, &numberOfUpdateCalls) term := NewFakeTerminalWithResponse("Y") - ctx := clicontext.NewCommandContext(term, newClient, newRESTClient) + ctx := clicontext.NewCommandContext(term, newClient) // when err := restart(ctx, clusterName, "cool-deployment") @@ -162,10 +162,10 @@ func TestRestartHostOperator(t *testing.T) { // given deployment := newDeployment(namespacedName, 1) deployment.Labels = map[string]string{"olm.owner.namespace": "toolchain-host-operator"} - newClient, newRESTClient, fakeClient := NewFakeClients(t, deployment) + newClient, fakeClient := NewFakeClients(t, deployment) numberOfUpdateCalls := 0 fakeClient.MockUpdate = requireDeploymentBeingUpdated(t, fakeClient, namespacedName, 1, &numberOfUpdateCalls) - ctx := clicontext.NewCommandContext(term, newClient, newRESTClient) + ctx := clicontext.NewCommandContext(term, newClient) // when err := restartHostOperator(ctx, fakeClient, cfg) @@ -179,10 +179,10 @@ func TestRestartHostOperator(t *testing.T) { t.Run("host deployment with the label is not present - restart fails", func(t *testing.T) { // given deployment := newDeployment(namespacedName, 1) - newClient, newRESTClient, fakeClient := NewFakeClients(t, deployment) + newClient, fakeClient := NewFakeClients(t, deployment) numberOfUpdateCalls := 0 fakeClient.MockUpdate = requireDeploymentBeingUpdated(t, fakeClient, namespacedName, 1, &numberOfUpdateCalls) - ctx := clicontext.NewCommandContext(term, newClient, newRESTClient) + ctx := clicontext.NewCommandContext(term, newClient) // when err := restartHostOperator(ctx, fakeClient, cfg) @@ -199,10 +199,10 @@ func TestRestartHostOperator(t *testing.T) { deployment.Labels = map[string]string{"olm.owner.namespace": "toolchain-host-operator"} deployment2 := deployment.DeepCopy() deployment2.Name = "another" - newClient, newRESTClient, fakeClient := NewFakeClients(t, deployment, deployment2) + newClient, fakeClient := NewFakeClients(t, deployment, deployment2) numberOfUpdateCalls := 0 fakeClient.MockUpdate = requireDeploymentBeingUpdated(t, fakeClient, namespacedName, 1, &numberOfUpdateCalls) - ctx := clicontext.NewCommandContext(term, newClient, newRESTClient) + ctx := clicontext.NewCommandContext(term, newClient) // when err := restartHostOperator(ctx, fakeClient, cfg) diff --git a/pkg/cmd/adm/unregister_member.go b/pkg/cmd/adm/unregister_member.go index 674a0cf..7f1ae0e 100644 --- a/pkg/cmd/adm/unregister_member.go +++ b/pkg/cmd/adm/unregister_member.go @@ -22,7 +22,7 @@ func NewUnregisterMemberCmd() *cobra.Command { Args: cobra.ExactArgs(1), RunE: func(cmd *cobra.Command, args []string) error { term := ioutils.NewTerminal(cmd.InOrStdin, cmd.OutOrStdout) - ctx := clicontext.NewCommandContext(term, client.DefaultNewClient, client.DefaultNewRESTClient) + ctx := clicontext.NewCommandContext(term, client.DefaultNewClient) return UnregisterMemberCluster(ctx, args[0]) }, } diff --git a/pkg/cmd/adm/unregister_member_test.go b/pkg/cmd/adm/unregister_member_test.go index 163041a..72b7b87 100644 --- a/pkg/cmd/adm/unregister_member_test.go +++ b/pkg/cmd/adm/unregister_member_test.go @@ -17,13 +17,13 @@ func TestUnregisterMemberWhenAnswerIsY(t *testing.T) { deployment := newDeployment(hostDeploymentName, 1) deployment.Labels = map[string]string{"olm.owner.namespace": "toolchain-host-operator"} - newClient, newRESTClient, fakeClient := NewFakeClients(t, toolchainCluster, deployment) + newClient, fakeClient := NewFakeClients(t, toolchainCluster, deployment) numberOfUpdateCalls := 0 fakeClient.MockUpdate = whenDeploymentThenUpdated(t, fakeClient, hostDeploymentName, 1, &numberOfUpdateCalls) SetFileConfig(t, Host(), Member()) term := NewFakeTerminalWithResponse("y") - ctx := clicontext.NewCommandContext(term, newClient, newRESTClient) + ctx := clicontext.NewCommandContext(term, newClient) // when err := UnregisterMemberCluster(ctx, "member1") @@ -44,10 +44,10 @@ func TestUnregisterMemberWhenAnswerIsY(t *testing.T) { func TestUnregisterMemberWhenAnswerIsN(t *testing.T) { // given toolchainCluster := NewToolchainCluster(ToolchainClusterName("member-cool-server.com")) - newClient, newRESTClient, fakeClient := NewFakeClients(t, toolchainCluster) + newClient, fakeClient := NewFakeClients(t, toolchainCluster) SetFileConfig(t, Host(), Member()) term := NewFakeTerminalWithResponse("n") - ctx := clicontext.NewCommandContext(term, newClient, newRESTClient) + ctx := clicontext.NewCommandContext(term, newClient) // when err := UnregisterMemberCluster(ctx, "member1") @@ -65,10 +65,10 @@ func TestUnregisterMemberWhenAnswerIsN(t *testing.T) { func TestUnregisterMemberWhenNotFound(t *testing.T) { // given toolchainCluster := NewToolchainCluster(ToolchainClusterName("another-cool-server.com")) - newClient, newRESTClient, fakeClient := NewFakeClients(t, toolchainCluster) + newClient, fakeClient := NewFakeClients(t, toolchainCluster) SetFileConfig(t, Host(), Member()) term := NewFakeTerminalWithResponse("n") - ctx := clicontext.NewCommandContext(term, newClient, newRESTClient) + ctx := clicontext.NewCommandContext(term, newClient) // when err := UnregisterMemberCluster(ctx, "member1") @@ -86,10 +86,10 @@ func TestUnregisterMemberWhenNotFound(t *testing.T) { func TestUnregisterMemberWhenUnknownClusterName(t *testing.T) { // given toolchainCluster := NewToolchainCluster(ToolchainClusterName("member-cool-server.com")) - newClient, newRESTClient, fakeClient := NewFakeClients(t, toolchainCluster) + newClient, fakeClient := NewFakeClients(t, toolchainCluster) SetFileConfig(t, Host(), Member()) term := NewFakeTerminalWithResponse("n") - ctx := clicontext.NewCommandContext(term, newClient, newRESTClient) + ctx := clicontext.NewCommandContext(term, newClient) // when err := UnregisterMemberCluster(ctx, "some") @@ -110,9 +110,9 @@ func TestUnregisterMemberLacksPermissions(t *testing.T) { SetFileConfig(t, Host(NoToken()), Member(NoToken())) toolchainCluster := NewToolchainCluster(ToolchainClusterName("member-cool-server.com")) - newClient, newRESTClient, fakeClient := NewFakeClients(t, toolchainCluster) + newClient, fakeClient := NewFakeClients(t, toolchainCluster) term := NewFakeTerminal() - ctx := clicontext.NewCommandContext(term, newClient, newRESTClient) + ctx := clicontext.NewCommandContext(term, newClient) // when err := UnregisterMemberCluster(ctx, "member1") diff --git a/pkg/cmd/approve.go b/pkg/cmd/approve.go index 91e80f6..5c5970d 100644 --- a/pkg/cmd/approve.go +++ b/pkg/cmd/approve.go @@ -39,7 +39,7 @@ only one parameter which is the name of the UserSignup to be approved`, }, RunE: func(cmd *cobra.Command, args []string) error { term := ioutils.NewTerminal(cmd.InOrStdin, cmd.OutOrStdout) - ctx := clicontext.NewCommandContext(term, client.DefaultNewClient, client.DefaultNewRESTClient) + ctx := clicontext.NewCommandContext(term, client.DefaultNewClient) switch { case usersignupName != "": return Approve(ctx, ByName(usersignupName), skipPhone, targetCluster) diff --git a/pkg/cmd/approve_test.go b/pkg/cmd/approve_test.go index ab361ae..764aa64 100644 --- a/pkg/cmd/approve_test.go +++ b/pkg/cmd/approve_test.go @@ -23,10 +23,10 @@ func TestApprove(t *testing.T) { t.Run("when answer is Y", func(t *testing.T) { // given userSignup := NewUserSignup() - newClient, newRESTClient, fakeClient := NewFakeClients(t, userSignup) + newClient, fakeClient := NewFakeClients(t, userSignup) SetFileConfig(t, Host()) term := NewFakeTerminalWithResponse("Y") - ctx := clicontext.NewCommandContext(term, newClient, newRESTClient) + ctx := clicontext.NewCommandContext(term, newClient) // when err := cmd.Approve(ctx, dummyGet(userSignup), false, "") @@ -46,10 +46,10 @@ func TestApprove(t *testing.T) { t.Run("when answer is N", func(t *testing.T) { // given userSignup := NewUserSignup() - newClient, newRESTClient, fakeClient := NewFakeClients(t, userSignup) + newClient, fakeClient := NewFakeClients(t, userSignup) SetFileConfig(t, Host()) term := NewFakeTerminalWithResponse("n") - ctx := clicontext.NewCommandContext(term, newClient, newRESTClient) + ctx := clicontext.NewCommandContext(term, newClient) // when err := cmd.Approve(ctx, dummyGet(userSignup), false, "") @@ -66,10 +66,10 @@ func TestApprove(t *testing.T) { t.Run("reactivate deactivated user", func(t *testing.T) { // given userSignup := NewUserSignup(UserSignupDeactivated(true)) - newClient, newRESTClient, fakeClient := NewFakeClients(t, userSignup) + newClient, fakeClient := NewFakeClients(t, userSignup) SetFileConfig(t, Host()) term := NewFakeTerminalWithResponse("Y") - ctx := clicontext.NewCommandContext(term, newClient, newRESTClient) + ctx := clicontext.NewCommandContext(term, newClient) // when err := cmd.Approve(ctx, dummyGet(userSignup), false, "") @@ -85,10 +85,10 @@ func TestApprove(t *testing.T) { t.Run("user is already active - automatically approved", func(t *testing.T) { // given userSignup := NewUserSignup(UserSignupAutomaticallyApproved(true)) - newClient, newRESTClient, fakeClient := NewFakeClients(t, userSignup) + newClient, fakeClient := NewFakeClients(t, userSignup) SetFileConfig(t, Host()) term := NewFakeTerminalWithResponse("Y") - ctx := clicontext.NewCommandContext(term, newClient, newRESTClient) + ctx := clicontext.NewCommandContext(term, newClient) // when err := cmd.Approve(ctx, dummyGet(userSignup), false, "") @@ -101,10 +101,10 @@ func TestApprove(t *testing.T) { t.Run("user is already active - approved by admin", func(t *testing.T) { // given userSignup := NewUserSignup(UserSignupApprovedByAdmin(true)) - newClient, newRESTClient, fakeClient := NewFakeClients(t, userSignup) + newClient, fakeClient := NewFakeClients(t, userSignup) SetFileConfig(t, Host()) term := NewFakeTerminalWithResponse("Y") - ctx := clicontext.NewCommandContext(term, newClient, newRESTClient) + ctx := clicontext.NewCommandContext(term, newClient) // when err := cmd.Approve(ctx, dummyGet(userSignup), false, "") @@ -118,10 +118,10 @@ func TestApprove(t *testing.T) { t.Run("when usersignup is already approved", func(t *testing.T) { // given userSignup := NewUserSignup(UserSignupAutomaticallyApproved(true)) - newClient, newRESTClient, fakeClient := NewFakeClients(t, userSignup) + newClient, fakeClient := NewFakeClients(t, userSignup) SetFileConfig(t, Host()) term := NewFakeTerminalWithResponse("Y") - ctx := clicontext.NewCommandContext(term, newClient, newRESTClient) + ctx := clicontext.NewCommandContext(term, newClient) // when err := cmd.Approve(ctx, dummyGet(userSignup), false, "") @@ -138,10 +138,10 @@ func TestApprove(t *testing.T) { t.Run("when getting usersignup failed", func(t *testing.T) { // given userSignup := NewUserSignup() - newClient, newRESTClient, fakeClient := NewFakeClients(t, userSignup) + newClient, fakeClient := NewFakeClients(t, userSignup) SetFileConfig(t, Host()) term := NewFakeTerminalWithResponse("Y") - ctx := clicontext.NewCommandContext(term, newClient, newRESTClient) + ctx := clicontext.NewCommandContext(term, newClient) // when err := cmd.Approve(ctx, func(configuration.ClusterConfig, runtimeclient.Client) (*toolchainv1alpha1.UserSignup, error) { @@ -162,10 +162,10 @@ func TestApprove(t *testing.T) { t.Run("when usersignup has phone hash", func(t *testing.T) { // given userSignup := NewUserSignup() - newClient, newRESTClient, fakeClient := NewFakeClients(t, userSignup) + newClient, fakeClient := NewFakeClients(t, userSignup) SetFileConfig(t, Host()) term := NewFakeTerminalWithResponse("Y") - ctx := clicontext.NewCommandContext(term, newClient, newRESTClient) + ctx := clicontext.NewCommandContext(term, newClient) // when err := cmd.Approve(ctx, dummyGet(userSignup), false, "") @@ -185,10 +185,10 @@ func TestApprove(t *testing.T) { t.Run("when usersignup doesn't have phone hash but skip phone verification flag is set", func(t *testing.T) { // given userSignup := NewUserSignup(UserSignupRemoveLabel(toolchainv1alpha1.UserSignupUserPhoneHashLabelKey)) - newClient, newRESTClient, fakeClient := NewFakeClients(t, userSignup) + newClient, fakeClient := NewFakeClients(t, userSignup) SetFileConfig(t, Host()) term := NewFakeTerminalWithResponse("Y") - ctx := clicontext.NewCommandContext(term, newClient, newRESTClient) + ctx := clicontext.NewCommandContext(term, newClient) // when err := cmd.Approve(ctx, dummyGet(userSignup), true, "") @@ -208,10 +208,10 @@ func TestApprove(t *testing.T) { t.Run("when usersignup doesn't have phone hash", func(t *testing.T) { // given userSignup := NewUserSignup(UserSignupRemoveLabel(toolchainv1alpha1.UserSignupUserPhoneHashLabelKey)) - newClient, newRESTClient, fakeClient := NewFakeClients(t, userSignup) + newClient, fakeClient := NewFakeClients(t, userSignup) SetFileConfig(t, Host()) term := NewFakeTerminalWithResponse("Y") - ctx := clicontext.NewCommandContext(term, newClient, newRESTClient) + ctx := clicontext.NewCommandContext(term, newClient) // when err := cmd.Approve(ctx, dummyGet(userSignup), false, "") @@ -230,13 +230,13 @@ func TestApprove(t *testing.T) { t.Run("when targetCluster is valid", func(t *testing.T) { // given userSignup := NewUserSignup() - newClient, newRESTClient, fakeClient := NewFakeClients(t, userSignup) + newClient, fakeClient := NewFakeClients(t, userSignup) SetFileConfig(t, Host(), Member(ClusterName("member1"), ServerName("m1.devcluster.openshift.com")), Member(ClusterName("member2"), ServerName("m2.devcluster.openshift.com"))) term := NewFakeTerminalWithResponse("Y") - ctx := clicontext.NewCommandContext(term, newClient, newRESTClient) + ctx := clicontext.NewCommandContext(term, newClient) // when err := cmd.Approve(ctx, dummyGet(userSignup), false, "member1") @@ -258,13 +258,13 @@ func TestApprove(t *testing.T) { t.Run("when targetCluster is invalid", func(t *testing.T) { // given userSignup := NewUserSignup() - newClient, newRESTClient, _ := NewFakeClients(t, userSignup) + newClient, _ := NewFakeClients(t, userSignup) SetFileConfig(t, Host(), Member(ClusterName("member1"), ServerName("m1.devcluster.openshift.com")), Member(ClusterName("member2"), ServerName("m2.devcluster.openshift.com"))) term := NewFakeTerminalWithResponse("Y") - ctx := clicontext.NewCommandContext(term, newClient, newRESTClient) + ctx := clicontext.NewCommandContext(term, newClient) // when err := cmd.Approve(ctx, dummyGet(userSignup), false, "non-existent-member") @@ -286,7 +286,7 @@ func TestLookupUserSignupByName(t *testing.T) { t.Run("when user is found", func(t *testing.T) { userSignup := NewUserSignup() - _, _, fakeClient := NewFakeClients(t, userSignup) + _, fakeClient := NewFakeClients(t, userSignup) SetFileConfig(t, Host()) term := NewFakeTerminalWithResponse("Y") cfg, err := configuration.LoadClusterConfig(term, "host") @@ -301,7 +301,7 @@ func TestLookupUserSignupByName(t *testing.T) { }) t.Run("when user is unknown", func(t *testing.T) { - _, _, fakeClient := NewFakeClients(t) + _, fakeClient := NewFakeClients(t) SetFileConfig(t, Host()) term := NewFakeTerminalWithResponse("Y") cfg, err := configuration.LoadClusterConfig(term, "host") @@ -317,7 +317,7 @@ func TestLookupUserSignupByName(t *testing.T) { t.Run("when error occurrs", func(t *testing.T) { userSignup := NewUserSignup() - _, _, fakeClient := NewFakeClients(t, userSignup) + _, fakeClient := NewFakeClients(t, userSignup) fakeClient.MockGet = func(ctx context.Context, key runtimeclient.ObjectKey, obj runtimeclient.Object, opts ...runtimeclient.GetOption) error { return fmt.Errorf("mock error") } @@ -338,7 +338,7 @@ func TestLookupUserSignupByEmailAddress(t *testing.T) { t.Run("when user is found", func(t *testing.T) { userSignup := NewUserSignup() - _, _, fakeClient := NewFakeClients(t, userSignup) + _, fakeClient := NewFakeClients(t, userSignup) SetFileConfig(t, Host()) term := NewFakeTerminalWithResponse("Y") cfg, err := configuration.LoadClusterConfig(term, "host") @@ -354,7 +354,7 @@ func TestLookupUserSignupByEmailAddress(t *testing.T) { t.Run("when no match found", func(t *testing.T) { userSignup := NewUserSignup() - _, _, fakeClient := NewFakeClients(t, userSignup) + _, fakeClient := NewFakeClients(t, userSignup) SetFileConfig(t, Host()) term := NewFakeTerminalWithResponse("Y") cfg, err := configuration.LoadClusterConfig(term, "host") @@ -370,7 +370,7 @@ func TestLookupUserSignupByEmailAddress(t *testing.T) { t.Run("when too many matches found", func(t *testing.T) { userSignup1 := NewUserSignup() userSignup2 := NewUserSignup() // same email address as userSignup1 - _, _, fakeClient := NewFakeClients(t, userSignup1, userSignup2) + _, fakeClient := NewFakeClients(t, userSignup1, userSignup2) SetFileConfig(t, Host()) term := NewFakeTerminalWithResponse("Y") cfg, err := configuration.LoadClusterConfig(term, "host") @@ -385,7 +385,7 @@ func TestLookupUserSignupByEmailAddress(t *testing.T) { t.Run("when error occurrs", func(t *testing.T) { userSignup := NewUserSignup() - _, _, fakeClient := NewFakeClients(t, userSignup) + _, fakeClient := NewFakeClients(t, userSignup) fakeClient.MockList = func(ctx context.Context, list runtimeclient.ObjectList, opts ...runtimeclient.ListOption) error { return fmt.Errorf("mock error") } diff --git a/pkg/cmd/ban.go b/pkg/cmd/ban.go index f6b0318..38a6462 100644 --- a/pkg/cmd/ban.go +++ b/pkg/cmd/ban.go @@ -24,7 +24,7 @@ only one parameter which is the name of the UserSignup to be used for banning`, Args: cobra.ExactArgs(1), RunE: func(cmd *cobra.Command, args []string) error { term := ioutils.NewTerminal(cmd.InOrStdin, cmd.OutOrStdout) - ctx := clicontext.NewCommandContext(term, client.DefaultNewClient, client.DefaultNewRESTClient) + ctx := clicontext.NewCommandContext(term, client.DefaultNewClient) return Ban(ctx, args...) }, } diff --git a/pkg/cmd/ban_test.go b/pkg/cmd/ban_test.go index 2550755..44f6a01 100644 --- a/pkg/cmd/ban_test.go +++ b/pkg/cmd/ban_test.go @@ -7,7 +7,6 @@ import ( toolchainv1alpha1 "github.com/codeready-toolchain/api/api/v1alpha1" "github.com/codeready-toolchain/toolchain-common/pkg/test" - "github.com/kubesaw/ksctl/pkg/client" "github.com/kubesaw/ksctl/pkg/cmd" clicontext "github.com/kubesaw/ksctl/pkg/context" . "github.com/kubesaw/ksctl/pkg/test" @@ -20,10 +19,10 @@ import ( func TestBanCmdWhenAnswerIsY(t *testing.T) { // given userSignup := NewUserSignup() - newClient, newRESTClient, fakeClient := NewFakeClients(t, userSignup) + newClient, fakeClient := NewFakeClients(t, userSignup) SetFileConfig(t, Host()) term := NewFakeTerminalWithResponse("y") - ctx := clicontext.NewCommandContext(term, newClient, newRESTClient) + ctx := clicontext.NewCommandContext(term, newClient) // when err := cmd.Ban(ctx, userSignup.Name) @@ -39,7 +38,7 @@ func TestBanCmdWhenAnswerIsY(t *testing.T) { t.Run("don't ban twice", func(t *testing.T) { // given term := NewFakeTerminalWithResponse("y") - ctx := clicontext.NewCommandContext(term, newClient, newRESTClient) + ctx := clicontext.NewCommandContext(term, newClient) // when err := cmd.Ban(ctx, userSignup.Name) @@ -55,10 +54,10 @@ func TestBanCmdWhenAnswerIsY(t *testing.T) { func TestBanCmdWhenAnswerIsN(t *testing.T) { // given userSignup := NewUserSignup() - newClient, newRESTClient, fakeClient := NewFakeClients(t, userSignup) + newClient, fakeClient := NewFakeClients(t, userSignup) SetFileConfig(t, Host()) term := NewFakeTerminalWithResponse("n") - ctx := clicontext.NewCommandContext(term, newClient, newRESTClient) + ctx := clicontext.NewCommandContext(term, newClient) // when err := cmd.Ban(ctx, userSignup.Name) @@ -75,10 +74,10 @@ func TestBanCmdWhenAnswerIsN(t *testing.T) { func TestBanCmdWhenNotFound(t *testing.T) { // given userSignup := NewUserSignup() - newClient, newRESTClient, fakeClient := NewFakeClients(t, userSignup) + newClient, fakeClient := NewFakeClients(t, userSignup) SetFileConfig(t, Host()) term := NewFakeTerminalWithResponse("n") - ctx := clicontext.NewCommandContext(term, newClient, newRESTClient) + ctx := clicontext.NewCommandContext(term, newClient) // when err := cmd.Ban(ctx, "some") @@ -99,9 +98,9 @@ func TestCreateBannedUser(t *testing.T) { t.Run("BannedUser creation is successful", func(t *testing.T) { //given userSignup := NewUserSignup() - newClient, newRESTClient, fakeClient := NewFakeClients(t, userSignup) + newClient, fakeClient := NewFakeClients(t, userSignup) term := NewFakeTerminal() - ctx := clicontext.NewCommandContext(term, newClient, newRESTClient) + ctx := clicontext.NewCommandContext(term, newClient) // when err := cmd.CreateBannedUser(ctx, userSignup.Name, func(signup *toolchainv1alpha1.UserSignup, bannedUser *toolchainv1alpha1.BannedUser) (bool, error) { @@ -116,9 +115,9 @@ func TestCreateBannedUser(t *testing.T) { t.Run("BannedUser should not be created", func(t *testing.T) { //given userSignup := NewUserSignup() - newClient, newRESTClient, fakeClient := NewFakeClients(t, userSignup) + newClient, fakeClient := NewFakeClients(t, userSignup) term := NewFakeTerminal() - ctx := clicontext.NewCommandContext(term, newClient, newRESTClient) + ctx := clicontext.NewCommandContext(term, newClient) // when err := cmd.CreateBannedUser(ctx, userSignup.Name, func(signup *toolchainv1alpha1.UserSignup, bannedUser *toolchainv1alpha1.BannedUser) (bool, error) { @@ -133,9 +132,9 @@ func TestCreateBannedUser(t *testing.T) { t.Run("confirmation func returns error", func(t *testing.T) { //given userSignup := NewUserSignup() - newClient, newRESTClient, fakeClient := NewFakeClients(t, userSignup) + newClient, fakeClient := NewFakeClients(t, userSignup) term := NewFakeTerminal() - ctx := clicontext.NewCommandContext(term, newClient, newRESTClient) + ctx := clicontext.NewCommandContext(term, newClient) // when err := cmd.CreateBannedUser(ctx, userSignup.Name, func(signup *toolchainv1alpha1.UserSignup, bannedUser *toolchainv1alpha1.BannedUser) (bool, error) { @@ -150,12 +149,12 @@ func TestCreateBannedUser(t *testing.T) { t.Run("get of UserSignup fails", func(t *testing.T) { //given userSignup := NewUserSignup() - newClient, newRESTClient, fakeClient := NewFakeClients(t, userSignup) + newClient, fakeClient := NewFakeClients(t, userSignup) fakeClient.MockGet = func(ctx context.Context, key runtimeclient.ObjectKey, obj runtimeclient.Object, opts ...runtimeclient.GetOption) error { return fmt.Errorf("some error") } term := NewFakeTerminal() - ctx := clicontext.NewCommandContext(term, newClient, newRESTClient) + ctx := clicontext.NewCommandContext(term, newClient) // when err := cmd.CreateBannedUser(ctx, userSignup.Name, func(signup *toolchainv1alpha1.UserSignup, bannedUser *toolchainv1alpha1.BannedUser) (bool, error) { @@ -170,12 +169,12 @@ func TestCreateBannedUser(t *testing.T) { t.Run("creation of BannedUser fails", func(t *testing.T) { //given userSignup := NewUserSignup() - newClient, newRESTClient, fakeClient := NewFakeClients(t, userSignup) + newClient, fakeClient := NewFakeClients(t, userSignup) fakeClient.MockCreate = func(ctx context.Context, obj runtimeclient.Object, opts ...runtimeclient.CreateOption) error { return fmt.Errorf("some error") } term := NewFakeTerminal() - ctx := clicontext.NewCommandContext(term, newClient, newRESTClient) + ctx := clicontext.NewCommandContext(term, newClient) // when err := cmd.CreateBannedUser(ctx, userSignup.Name, func(signup *toolchainv1alpha1.UserSignup, bannedUser *toolchainv1alpha1.BannedUser) (bool, error) { @@ -195,8 +194,7 @@ func TestCreateBannedUser(t *testing.T) { newClient := func(token, apiEndpoint string) (runtimeclient.Client, error) { return nil, fmt.Errorf("some error") } - newRESTClient := client.DefaultNewRESTClient - ctx := clicontext.NewCommandContext(term, newClient, newRESTClient) + ctx := clicontext.NewCommandContext(term, newClient) // when err := cmd.CreateBannedUser(ctx, userSignup.Name, func(signup *toolchainv1alpha1.UserSignup, bannedUser *toolchainv1alpha1.BannedUser) (bool, error) { @@ -214,9 +212,9 @@ func TestCreateBannedUserLacksPermissions(t *testing.T) { SetFileConfig(t, Host(NoToken())) userSignup := NewUserSignup() - newClient, newRESTClient, fakeClient := NewFakeClients(t, userSignup) + newClient, fakeClient := NewFakeClients(t, userSignup) term := NewFakeTerminal() - ctx := clicontext.NewCommandContext(term, newClient, newRESTClient) + ctx := clicontext.NewCommandContext(term, newClient) // when err := cmd.CreateBannedUser(ctx, userSignup.Name, func(signup *toolchainv1alpha1.UserSignup, bannedUser *toolchainv1alpha1.BannedUser) (bool, error) { diff --git a/pkg/cmd/create_social_event.go b/pkg/cmd/create_social_event.go index fc79aff..151a8dc 100644 --- a/pkg/cmd/create_social_event.go +++ b/pkg/cmd/create_social_event.go @@ -36,7 +36,7 @@ func NewCreateSocialEventCmd() *cobra.Command { Args: cobra.ExactArgs(0), RunE: func(cmd *cobra.Command, args []string) error { term := ioutils.NewTerminal(cmd.InOrStdin, cmd.OutOrStdout) - ctx := clicontext.NewCommandContext(term, client.DefaultNewClient, client.DefaultNewRESTClient) + ctx := clicontext.NewCommandContext(term, client.DefaultNewClient) return CreateSocialEvent(ctx, startDate, endDate, description, userTier, spaceTier, maxAttendees, preferSameCluster) }, } diff --git a/pkg/cmd/create_social_event_test.go b/pkg/cmd/create_social_event_test.go index 9e65c81..a210068 100644 --- a/pkg/cmd/create_social_event_test.go +++ b/pkg/cmd/create_social_event_test.go @@ -27,8 +27,8 @@ func TestCreateSocialEvent(t *testing.T) { t.Run("1-day event without description", func(t *testing.T) { // given - newClient, newRESTClient, fakeClient := NewFakeClients(t, userTier, spaceTier) - ctx := clicontext.NewCommandContext(term, newClient, newRESTClient) + newClient, fakeClient := NewFakeClients(t, userTier, spaceTier) + ctx := clicontext.NewCommandContext(term, newClient) startDate := "2022-06-21" // summer 🏝 endDate := "2022-06-21" // ends same day description := "" @@ -55,8 +55,8 @@ func TestCreateSocialEvent(t *testing.T) { t.Run("2-day event", func(t *testing.T) { // given - newClient, newRESTClient, fakeClient := NewFakeClients(t, userTier, spaceTier) - ctx := clicontext.NewCommandContext(term, newClient, newRESTClient) + newClient, fakeClient := NewFakeClients(t, userTier, spaceTier) + ctx := clicontext.NewCommandContext(term, newClient) startDate := "2022-06-21" // summer 🏝 endDate := "2022-06-22" description := "summer workshop" @@ -81,8 +81,8 @@ func TestCreateSocialEvent(t *testing.T) { t.Run("invalid start date", func(t *testing.T) { // given - newClient, newRESTClient, _ := NewFakeClients(t, userTier, spaceTier) - ctx := clicontext.NewCommandContext(term, newClient, newRESTClient) + newClient, _ := NewFakeClients(t, userTier, spaceTier) + ctx := clicontext.NewCommandContext(term, newClient) startDate := "2022-06-xx" // invalid! endDate := "2022-06-22" description := "summer workshop" @@ -97,8 +97,8 @@ func TestCreateSocialEvent(t *testing.T) { t.Run("invalid end date", func(t *testing.T) { // given - newClient, newRESTClient, _ := NewFakeClients(t, userTier, spaceTier) - ctx := clicontext.NewCommandContext(term, newClient, newRESTClient) + newClient, _ := NewFakeClients(t, userTier, spaceTier) + ctx := clicontext.NewCommandContext(term, newClient) startDate := "2022-06-21" endDate := "2022-06-32" // invalid value! description := "summer workshop" @@ -113,8 +113,8 @@ func TestCreateSocialEvent(t *testing.T) { t.Run("end date before start date", func(t *testing.T) { // given - newClient, newRESTClient, _ := NewFakeClients(t, userTier, spaceTier) - ctx := clicontext.NewCommandContext(term, newClient, newRESTClient) + newClient, _ := NewFakeClients(t, userTier, spaceTier) + ctx := clicontext.NewCommandContext(term, newClient) startDate := "2022-06-21" endDate := "2022-06-11" // before start date! description := "summer workshop" @@ -129,8 +129,8 @@ func TestCreateSocialEvent(t *testing.T) { t.Run("usertier does not exist", func(t *testing.T) { // given - newClient, newRESTClient, _ := NewFakeClients(t, spaceTier) // no user tier - ctx := clicontext.NewCommandContext(term, newClient, newRESTClient) + newClient, _ := NewFakeClients(t, spaceTier) // no user tier + ctx := clicontext.NewCommandContext(term, newClient) startDate := "2022-06-21" endDate := "2022-06-22" description := "summer workshop" @@ -145,8 +145,8 @@ func TestCreateSocialEvent(t *testing.T) { t.Run("nstemplatetier does not exist", func(t *testing.T) { // given - newClient, newRESTClient, _ := NewFakeClients(t, userTier) // no space tier - ctx := clicontext.NewCommandContext(term, newClient, newRESTClient) + newClient, _ := NewFakeClients(t, userTier) // no space tier + ctx := clicontext.NewCommandContext(term, newClient) startDate := "2022-06-21" endDate := "2022-06-22" description := "summer workshop" diff --git a/pkg/cmd/deactivate.go b/pkg/cmd/deactivate.go index aef1e1b..73b5828 100644 --- a/pkg/cmd/deactivate.go +++ b/pkg/cmd/deactivate.go @@ -19,7 +19,7 @@ only one parameter which is the name of the UserSignup to be deactivated`, Args: cobra.ExactArgs(1), RunE: func(cmd *cobra.Command, args []string) error { term := ioutils.NewTerminal(cmd.InOrStdin, cmd.OutOrStdout) - ctx := clicontext.NewCommandContext(term, client.DefaultNewClient, client.DefaultNewRESTClient) + ctx := clicontext.NewCommandContext(term, client.DefaultNewClient) return Deactivate(ctx, args...) }, } diff --git a/pkg/cmd/deactivate_test.go b/pkg/cmd/deactivate_test.go index f8f2efc..bfaad90 100644 --- a/pkg/cmd/deactivate_test.go +++ b/pkg/cmd/deactivate_test.go @@ -16,10 +16,10 @@ import ( func TestDeactivateCmdWhenAnswerIsY(t *testing.T) { // given userSignup := NewUserSignup() - newClient, newRESTClient, fakeClient := NewFakeClients(t, userSignup) + newClient, fakeClient := NewFakeClients(t, userSignup) SetFileConfig(t, Host()) term := NewFakeTerminalWithResponse("y") - ctx := clicontext.NewCommandContext(term, newClient, newRESTClient) + ctx := clicontext.NewCommandContext(term, newClient) // when err := cmd.Deactivate(ctx, userSignup.Name) @@ -37,10 +37,10 @@ func TestDeactivateCmdWhenAnswerIsY(t *testing.T) { func TestDeactivateCmdWhenAnswerIsN(t *testing.T) { // given userSignup := NewUserSignup() - newClient, newRESTClient, fakeClient := NewFakeClients(t, userSignup) + newClient, fakeClient := NewFakeClients(t, userSignup) SetFileConfig(t, Host()) term := NewFakeTerminalWithResponse("n") - ctx := clicontext.NewCommandContext(term, newClient, newRESTClient) + ctx := clicontext.NewCommandContext(term, newClient) // when err := cmd.Deactivate(ctx, userSignup.Name) @@ -57,10 +57,10 @@ func TestDeactivateCmdWhenAnswerIsN(t *testing.T) { func TestDeactivateCmdWhenNotFound(t *testing.T) { // given userSignup := NewUserSignup() - newClient, newRESTClient, fakeClient := NewFakeClients(t, userSignup) + newClient, fakeClient := NewFakeClients(t, userSignup) SetFileConfig(t, Host()) term := NewFakeTerminalWithResponse("n") - ctx := clicontext.NewCommandContext(term, newClient, newRESTClient) + ctx := clicontext.NewCommandContext(term, newClient) // when err := cmd.Deactivate(ctx, "some") diff --git a/pkg/cmd/delete.go b/pkg/cmd/delete.go index cbdbba5..938cb0f 100644 --- a/pkg/cmd/delete.go +++ b/pkg/cmd/delete.go @@ -22,7 +22,7 @@ only one parameter which is the name of the UserSignup to be deleted`, Args: cobra.ExactArgs(1), RunE: func(cmd *cobra.Command, args []string) error { term := ioutils.NewTerminal(cmd.InOrStdin, cmd.OutOrStdout) - ctx := clicontext.NewCommandContext(term, client.DefaultNewClient, client.DefaultNewRESTClient) + ctx := clicontext.NewCommandContext(term, client.DefaultNewClient) return Delete(ctx, args...) }, } diff --git a/pkg/cmd/delete_test.go b/pkg/cmd/delete_test.go index b27dd9e..4673c46 100644 --- a/pkg/cmd/delete_test.go +++ b/pkg/cmd/delete_test.go @@ -17,10 +17,10 @@ import ( func TestDeleteCmdWhenAnswerIsY(t *testing.T) { // given userSignup := NewUserSignup() - newClient, newRESTClient, fakeClient := NewFakeClients(t, userSignup) + newClient, fakeClient := NewFakeClients(t, userSignup) SetFileConfig(t, Host()) term := NewFakeTerminalWithResponse("y") - ctx := clicontext.NewCommandContext(term, newClient, newRESTClient) + ctx := clicontext.NewCommandContext(term, newClient) // when err := cmd.Delete(ctx, userSignup.Name) @@ -38,10 +38,10 @@ func TestDeleteCmdWhenAnswerIsY(t *testing.T) { func TestDeleteCmdWhenAnswerIsN(t *testing.T) { // given userSignup := NewUserSignup() - newClient, newRESTClient, fakeClient := NewFakeClients(t, userSignup) + newClient, fakeClient := NewFakeClients(t, userSignup) SetFileConfig(t, Host()) term := NewFakeTerminalWithResponse("n") - ctx := clicontext.NewCommandContext(term, newClient, newRESTClient) + ctx := clicontext.NewCommandContext(term, newClient) // when err := cmd.Delete(ctx, userSignup.Name) @@ -59,10 +59,10 @@ func TestDeleteCmdWhenAnswerIsN(t *testing.T) { func TestDeleteCmdWhenNotFound(t *testing.T) { // given userSignup := NewUserSignup() - newClient, newRESTClient, fakeClient := NewFakeClients(t, userSignup) + newClient, fakeClient := NewFakeClients(t, userSignup) SetFileConfig(t, Host()) term := NewFakeTerminalWithResponse("n") - ctx := clicontext.NewCommandContext(term, newClient, newRESTClient) + ctx := clicontext.NewCommandContext(term, newClient) // when err := cmd.Delete(ctx, "some") @@ -81,9 +81,9 @@ func TestDeleteLacksPermissions(t *testing.T) { SetFileConfig(t, Host(NoToken())) userSignup := NewUserSignup() - newClient, newRESTClient, fakeClient := NewFakeClients(t, userSignup) + newClient, fakeClient := NewFakeClients(t, userSignup) term := NewFakeTerminal() - ctx := clicontext.NewCommandContext(term, newClient, newRESTClient) + ctx := clicontext.NewCommandContext(term, newClient) // when err := cmd.Delete(ctx, userSignup.Name) @@ -96,7 +96,7 @@ func TestDeleteLacksPermissions(t *testing.T) { func TestDeleteHasPropagationPolicy(t *testing.T) { // given userSignup := NewUserSignup() - newClient, newRESTClient, fakeClient := NewFakeClients(t, userSignup) + newClient, fakeClient := NewFakeClients(t, userSignup) SetFileConfig(t, Host()) term := NewFakeTerminalWithResponse("y") @@ -111,7 +111,7 @@ func TestDeleteHasPropagationPolicy(t *testing.T) { assert.Equal(t, metav1.DeletePropagationForeground, *deleteOptions.PropagationPolicy) return nil } - ctx := clicontext.NewCommandContext(term, newClient, newRESTClient) + ctx := clicontext.NewCommandContext(term, newClient) // when err := cmd.Delete(ctx, userSignup.Name) diff --git a/pkg/cmd/disable_user.go b/pkg/cmd/disable_user.go index f0fce34..d64730c 100644 --- a/pkg/cmd/disable_user.go +++ b/pkg/cmd/disable_user.go @@ -17,7 +17,7 @@ only one parameter which is the name of the MasterUserRecord to be disabled`, Args: cobra.ExactArgs(1), RunE: func(cmd *cobra.Command, args []string) error { term := ioutils.NewTerminal(cmd.InOrStdin, cmd.OutOrStdout) - ctx := clicontext.NewCommandContext(term, client.DefaultNewClient, client.DefaultNewRESTClient) + ctx := clicontext.NewCommandContext(term, client.DefaultNewClient) return DisableUser(ctx, args...) }, } diff --git a/pkg/cmd/disable_user_test.go b/pkg/cmd/disable_user_test.go index 74bf213..4ebc9b4 100644 --- a/pkg/cmd/disable_user_test.go +++ b/pkg/cmd/disable_user_test.go @@ -17,10 +17,10 @@ func TestDisableUserCmdWhenAnswerIsY(t *testing.T) { // given // this mur will be disabled mur1 := masteruserrecord.NewMasterUserRecord(t, "alice", masteruserrecord.TierName("deactivate30")) - newClient, newRESTClient, fakeClient := NewFakeClients(t, mur1) + newClient, fakeClient := NewFakeClients(t, mur1) SetFileConfig(t, Host()) term := NewFakeTerminalWithResponse("y") - ctx := clicontext.NewCommandContext(term, newClient, newRESTClient) + ctx := clicontext.NewCommandContext(term, newClient) // when err := cmd.DisableUser(ctx, mur1.Name) @@ -39,10 +39,10 @@ func TestDisableUserCmdWhenAnswerIsY(t *testing.T) { func TestDisableUserCmdWhenAnswerIsN(t *testing.T) { // given mur := masteruserrecord.NewMasterUserRecord(t, "alice", masteruserrecord.TierName("deactivate30")) - newClient, newRESTClient, fakeClient := NewFakeClients(t, mur) + newClient, fakeClient := NewFakeClients(t, mur) SetFileConfig(t, Host()) term := NewFakeTerminalWithResponse("n") - ctx := clicontext.NewCommandContext(term, newClient, newRESTClient) + ctx := clicontext.NewCommandContext(term, newClient) // when err := cmd.DisableUser(ctx, mur.Name) @@ -59,10 +59,10 @@ func TestDisableUserCmdWhenAnswerIsN(t *testing.T) { func TestDisableUserCmdWhenNotFound(t *testing.T) { // given mur := masteruserrecord.NewMasterUserRecord(t, "alice", masteruserrecord.TierName("deactivate30")) - newClient, newRESTClient, fakeClient := NewFakeClients(t, mur) + newClient, fakeClient := NewFakeClients(t, mur) SetFileConfig(t, Host()) term := NewFakeTerminalWithResponse("n") - ctx := clicontext.NewCommandContext(term, newClient, newRESTClient) + ctx := clicontext.NewCommandContext(term, newClient) // when err := cmd.DisableUser(ctx, "some") diff --git a/pkg/cmd/promote_space.go b/pkg/cmd/promote_space.go index 578f26f..0fd530e 100644 --- a/pkg/cmd/promote_space.go +++ b/pkg/cmd/promote_space.go @@ -19,7 +19,7 @@ parameters - first one is Space name and second is the name of the target NSTemp Args: cobra.ExactArgs(2), RunE: func(cmd *cobra.Command, args []string) error { term := ioutils.NewTerminal(cmd.InOrStdin, cmd.OutOrStdout) - ctx := clicontext.NewCommandContext(term, client.DefaultNewClient, client.DefaultNewRESTClient) + ctx := clicontext.NewCommandContext(term, client.DefaultNewClient) return PromoteSpace(ctx, args[0], args[1]) }, } diff --git a/pkg/cmd/promote_space_test.go b/pkg/cmd/promote_space_test.go index c1da85a..7d85089 100644 --- a/pkg/cmd/promote_space_test.go +++ b/pkg/cmd/promote_space_test.go @@ -18,10 +18,10 @@ import ( func TestPromoteSpaceCmdWhenAnswerIsY(t *testing.T) { // given space := newSpace() - newClient, newRESTClient, fakeClient := NewFakeClients(t, space, newNSTemplateTier("advanced")) + newClient, fakeClient := NewFakeClients(t, space, newNSTemplateTier("advanced")) SetFileConfig(t, Host()) term := NewFakeTerminalWithResponse("Y") - ctx := clicontext.NewCommandContext(term, newClient, newRESTClient) + ctx := clicontext.NewCommandContext(term, newClient) // when err := cmd.PromoteSpace(ctx, space.Name, "advanced") @@ -39,10 +39,10 @@ func TestPromoteSpaceCmdWhenAnswerIsY(t *testing.T) { func TestPromoteSpaceCmdWhenAnswerIsN(t *testing.T) { // given space := newSpace() - newClient, newRESTClient, fakeClient := NewFakeClients(t, space, newNSTemplateTier("advanced")) + newClient, fakeClient := NewFakeClients(t, space, newNSTemplateTier("advanced")) SetFileConfig(t, Host()) term := NewFakeTerminalWithResponse("n") - ctx := clicontext.NewCommandContext(term, newClient, newRESTClient) + ctx := clicontext.NewCommandContext(term, newClient) // when err := cmd.PromoteSpace(ctx, space.Name, "advanced") @@ -59,10 +59,10 @@ func TestPromoteSpaceCmdWhenAnswerIsN(t *testing.T) { func TestPromoteSpaceCmdWhenSpaceNotFound(t *testing.T) { // given space := newSpace() - newClient, newRESTClient, fakeClient := NewFakeClients(t, space, newNSTemplateTier("advanced")) + newClient, fakeClient := NewFakeClients(t, space, newNSTemplateTier("advanced")) SetFileConfig(t, Host()) term := NewFakeTerminalWithResponse("Y") - ctx := clicontext.NewCommandContext(term, newClient, newRESTClient) + ctx := clicontext.NewCommandContext(term, newClient) // when err := cmd.PromoteSpace(ctx, "another", "advanced") // attempt to promote a space that does not exist @@ -79,10 +79,10 @@ func TestPromoteSpaceCmdWhenSpaceNotFound(t *testing.T) { func TestPromoteSpaceCmdWhenNSTemplateTierNotFound(t *testing.T) { // given space := newSpace() - newClient, newRESTClient, fakeClient := NewFakeClients(t, space) + newClient, fakeClient := NewFakeClients(t, space) SetFileConfig(t, Host()) term := NewFakeTerminalWithResponse("Y") - ctx := clicontext.NewCommandContext(term, newClient, newRESTClient) + ctx := clicontext.NewCommandContext(term, newClient) // when err := cmd.PromoteSpace(ctx, space.Name, "advanced") diff --git a/pkg/cmd/promote_user.go b/pkg/cmd/promote_user.go index 1621675..5210859 100644 --- a/pkg/cmd/promote_user.go +++ b/pkg/cmd/promote_user.go @@ -19,7 +19,7 @@ parameters - first one is MasterUserRecord name and second is the name of the ta Args: cobra.ExactArgs(2), RunE: func(cmd *cobra.Command, args []string) error { term := ioutils.NewTerminal(cmd.InOrStdin, cmd.OutOrStdout) - ctx := clicontext.NewCommandContext(term, client.DefaultNewClient, client.DefaultNewRESTClient) + ctx := clicontext.NewCommandContext(term, client.DefaultNewClient) return PromoteUser(ctx, args[0], args[1]) }, } diff --git a/pkg/cmd/promote_user_test.go b/pkg/cmd/promote_user_test.go index 452af74..c700b5d 100644 --- a/pkg/cmd/promote_user_test.go +++ b/pkg/cmd/promote_user_test.go @@ -19,10 +19,10 @@ import ( func TestPromoteUserCmdWhenAnswerIsY(t *testing.T) { // given mur := masteruserrecord.NewMasterUserRecord(t, "testmur", masteruserrecord.TierName("deactivate30")) - newClient, newRESTClient, fakeClient := NewFakeClients(t, mur, newUserTier("deactivate180")) + newClient, fakeClient := NewFakeClients(t, mur, newUserTier("deactivate180")) SetFileConfig(t, Host()) term := NewFakeTerminalWithResponse("Y") - ctx := clicontext.NewCommandContext(term, newClient, newRESTClient) + ctx := clicontext.NewCommandContext(term, newClient) // when err := cmd.PromoteUser(ctx, mur.Name, "deactivate180") @@ -40,10 +40,10 @@ func TestPromoteUserCmdWhenAnswerIsY(t *testing.T) { func TestPromoteUserCmdWhenAnswerIsN(t *testing.T) { // given mur := masteruserrecord.NewMasterUserRecord(t, "testmur", masteruserrecord.TierName("deactivate30")) - newClient, newRESTClient, fakeClient := NewFakeClients(t, mur, newUserTier("deactivate180")) + newClient, fakeClient := NewFakeClients(t, mur, newUserTier("deactivate180")) SetFileConfig(t, Host()) term := NewFakeTerminalWithResponse("n") - ctx := clicontext.NewCommandContext(term, newClient, newRESTClient) + ctx := clicontext.NewCommandContext(term, newClient) // when err := cmd.PromoteUser(ctx, mur.Name, "deactivate180") @@ -60,10 +60,10 @@ func TestPromoteUserCmdWhenAnswerIsN(t *testing.T) { func TestPromoteUserCmdWhenMasterUserRecordNotFound(t *testing.T) { // given mur := masteruserrecord.NewMasterUserRecord(t, "testmur", masteruserrecord.TierName("deactivate30")) - newClient, newRESTClient, fakeClient := NewFakeClients(t, mur, newUserTier("deactivate180")) + newClient, fakeClient := NewFakeClients(t, mur, newUserTier("deactivate180")) SetFileConfig(t, Host()) term := NewFakeTerminalWithResponse("Y") - ctx := clicontext.NewCommandContext(term, newClient, newRESTClient) + ctx := clicontext.NewCommandContext(term, newClient) // when err := cmd.PromoteUser(ctx, "another", "deactivate180") // attempt to promote a mur that does not exist @@ -80,10 +80,10 @@ func TestPromoteUserCmdWhenMasterUserRecordNotFound(t *testing.T) { func TestPromoteUserCmdWhenUserTierNotFound(t *testing.T) { // given mur := masteruserrecord.NewMasterUserRecord(t, "testmur", masteruserrecord.TierName("deactivate30")) - newClient, newRESTClient, fakeClient := NewFakeClients(t, mur) + newClient, fakeClient := NewFakeClients(t, mur) SetFileConfig(t, Host()) term := NewFakeTerminalWithResponse("Y") - ctx := clicontext.NewCommandContext(term, newClient, newRESTClient) + ctx := clicontext.NewCommandContext(term, newClient) // when err := cmd.PromoteUser(ctx, mur.Name, "deactivate180") diff --git a/pkg/cmd/remove_space_users.go b/pkg/cmd/remove_space_users.go index a16e243..1af8117 100644 --- a/pkg/cmd/remove_space_users.go +++ b/pkg/cmd/remove_space_users.go @@ -25,7 +25,7 @@ one or more users specified by their MasterUserRecord name.`, Args: cobra.ExactArgs(0), RunE: func(cmd *cobra.Command, args []string) error { term := ioutils.NewTerminal(cmd.InOrStdin, cmd.OutOrStdout) - ctx := clicontext.NewCommandContext(term, client.DefaultNewClient, client.DefaultNewRESTClient) + ctx := clicontext.NewCommandContext(term, client.DefaultNewClient) return RemoveSpaceUsers(ctx, spaceName, users) }, diff --git a/pkg/cmd/remove_space_users_test.go b/pkg/cmd/remove_space_users_test.go index 53c4d4d..144b04a 100644 --- a/pkg/cmd/remove_space_users_test.go +++ b/pkg/cmd/remove_space_users_test.go @@ -28,11 +28,11 @@ func TestRemoveSpaceUsers(t *testing.T) { sb1 := spacebinding.NewSpaceBinding(mur1, space, "alice") mur2 := masteruserrecord.NewMasterUserRecord(t, "bob", masteruserrecord.TierName("deactivate30")) sb2 := spacebinding.NewSpaceBinding(mur2, space, "bob") - newClient, newRESTClient, fakeClient := NewFakeClients(t, space, sb1, sb2) + newClient, fakeClient := NewFakeClients(t, space, sb1, sb2) SetFileConfig(t, Host()) term := NewFakeTerminalWithResponse("Y") - ctx := clicontext.NewCommandContext(term, newClient, newRESTClient) + ctx := clicontext.NewCommandContext(term, newClient) // when err := cmd.RemoveSpaceUsers(ctx, "testspace", []string{"alice", "bob"}) @@ -53,11 +53,11 @@ func TestRemoveSpaceUsers(t *testing.T) { sb1 := spacebinding.NewSpaceBinding(mur1, space, "alice") mur2 := masteruserrecord.NewMasterUserRecord(t, "bob", masteruserrecord.TierName("deactivate30")) sb2 := spacebinding.NewSpaceBinding(mur2, space, "bob") - newClient, newRESTClient, fakeClient := NewFakeClients(t, space, sb1, sb2) + newClient, fakeClient := NewFakeClients(t, space, sb1, sb2) SetFileConfig(t, Host()) term := NewFakeTerminalWithResponse("Y") - ctx := clicontext.NewCommandContext(term, newClient, newRESTClient) + ctx := clicontext.NewCommandContext(term, newClient) // when err := cmd.RemoveSpaceUsers(ctx, "testspace", []string{"alice"}) @@ -79,11 +79,11 @@ func TestRemoveSpaceUsers(t *testing.T) { sb1 := spacebinding.NewSpaceBinding(mur1, space, "alice") mur2 := masteruserrecord.NewMasterUserRecord(t, "bob", masteruserrecord.TierName("deactivate30")) sb2 := spacebinding.NewSpaceBinding(mur2, space, "bob") - newClient, newRESTClient, fakeClient := NewFakeClients(t, space, sb1, sb2) + newClient, fakeClient := NewFakeClients(t, space, sb1, sb2) SetFileConfig(t, Host()) term := NewFakeTerminalWithResponse("N") - ctx := clicontext.NewCommandContext(term, newClient, newRESTClient) + ctx := clicontext.NewCommandContext(term, newClient) // when err := cmd.RemoveSpaceUsers(ctx, "testspace", []string{"alice", "bob"}) @@ -104,11 +104,11 @@ func TestRemoveSpaceUsers(t *testing.T) { sb1 := spacebinding.NewSpaceBinding(mur1, space, "alice") mur2 := masteruserrecord.NewMasterUserRecord(t, "bob", masteruserrecord.TierName("deactivate30")) sb2 := spacebinding.NewSpaceBinding(mur2, space, "bob") - newClient, newRESTClient, fakeClient := NewFakeClients(t, sb1, sb2) // no space + newClient, fakeClient := NewFakeClients(t, sb1, sb2) // no space SetFileConfig(t, Host()) term := NewFakeTerminalWithResponse("N") - ctx := clicontext.NewCommandContext(term, newClient, newRESTClient) + ctx := clicontext.NewCommandContext(term, newClient) // when err := cmd.RemoveSpaceUsers(ctx, "testspace", []string{"alice", "bob"}) @@ -129,11 +129,11 @@ func TestRemoveSpaceUsers(t *testing.T) { sb1 := spacebinding.NewSpaceBinding(mur1, space, "alice") mur2 := masteruserrecord.NewMasterUserRecord(t, "bob", masteruserrecord.TierName("deactivate30")) sb2 := spacebinding.NewSpaceBinding(mur2, space, "bob") - newClient, newRESTClient, fakeClient := NewFakeClients(t, space, sb1, sb2) + newClient, fakeClient := NewFakeClients(t, space, sb1, sb2) SetFileConfig(t, Host()) term := NewFakeTerminalWithResponse("N") - ctx := clicontext.NewCommandContext(term, newClient, newRESTClient) + ctx := clicontext.NewCommandContext(term, newClient) // when err := cmd.RemoveSpaceUsers(ctx, "testspace", []string{"alice", "notexist"}) @@ -154,14 +154,14 @@ func TestRemoveSpaceUsers(t *testing.T) { sb1 := spacebinding.NewSpaceBinding(mur1, space, "alice") mur2 := masteruserrecord.NewMasterUserRecord(t, "bob", masteruserrecord.TierName("deactivate30")) sb2 := spacebinding.NewSpaceBinding(mur2, space, "bob") - newClient, newRESTClient, fakeClient := NewFakeClients(t, space, sb1, sb2) + newClient, fakeClient := NewFakeClients(t, space, sb1, sb2) fakeClient.MockGet = func(ctx context.Context, key runtimeclient.ObjectKey, obj runtimeclient.Object, opts ...runtimeclient.GetOption) error { return fmt.Errorf("client error") } SetFileConfig(t, Host()) term := NewFakeTerminalWithResponse("Y") - ctx := clicontext.NewCommandContext(term, newClient, newRESTClient) + ctx := clicontext.NewCommandContext(term, newClient) // when err := cmd.RemoveSpaceUsers(ctx, "testspace", []string{"alice", "bob"}) diff --git a/pkg/cmd/retarget.go b/pkg/cmd/retarget.go index 84d58a4..53b2a7d 100644 --- a/pkg/cmd/retarget.go +++ b/pkg/cmd/retarget.go @@ -21,7 +21,7 @@ func NewRetargetCmd() *cobra.Command { Args: cobra.ExactArgs(2), RunE: func(cmd *cobra.Command, args []string) error { term := ioutils.NewTerminal(cmd.InOrStdin, cmd.OutOrStdout) - ctx := clicontext.NewCommandContext(term, client.DefaultNewClient, client.DefaultNewRESTClient) + ctx := clicontext.NewCommandContext(term, client.DefaultNewClient) return Retarget(ctx, args[0], args[1]) }, } diff --git a/pkg/cmd/retarget_test.go b/pkg/cmd/retarget_test.go index 8322785..0444f32 100644 --- a/pkg/cmd/retarget_test.go +++ b/pkg/cmd/retarget_test.go @@ -25,8 +25,8 @@ func TestRetarget(t *testing.T) { // given term := NewFakeTerminalWithResponse("y") space := testspace.NewSpace(test.HostOperatorNs, "john-dev", testspace.WithCreatorLabel("john")) - newClient, newRESTClient, fakeClient := prepareRetargetSpace(t, space, userSignup) - ctx := clicontext.NewCommandContext(term, newClient, newRESTClient) + newClient, fakeClient := prepareRetargetSpace(t, space, userSignup) + ctx := clicontext.NewCommandContext(term, newClient) // when err := cmd.Retarget(ctx, space.Name, "member2") @@ -47,8 +47,8 @@ func TestRetarget(t *testing.T) { t.Run("no space found", func(t *testing.T) { // given term := NewFakeTerminalWithResponse("y") - newClient, newRESTClient, _ := prepareRetargetSpace(t) // no usersignup created - ctx := clicontext.NewCommandContext(term, newClient, newRESTClient) + newClient, _ := prepareRetargetSpace(t) // no usersignup created + ctx := clicontext.NewCommandContext(term, newClient) // when err := cmd.Retarget(ctx, "space-that-doesnt-exist", "member1") @@ -61,8 +61,8 @@ func TestRetarget(t *testing.T) { // given term := NewFakeTerminalWithResponse("y") space := testspace.NewSpace(test.HostOperatorNs, "john-dev", testspace.WithCreatorLabel("john"), testspace.WithSpecTargetCluster("member-m2.devcluster.openshift.com")) - newClient, newRESTClient, _ := prepareRetargetSpace(t, space, userSignup) - ctx := clicontext.NewCommandContext(term, newClient, newRESTClient) + newClient, _ := prepareRetargetSpace(t, space, userSignup) + ctx := clicontext.NewCommandContext(term, newClient) // when err := cmd.Retarget(ctx, space.Name, "member2") @@ -75,8 +75,8 @@ func TestRetarget(t *testing.T) { // given term := NewFakeTerminalWithResponse("y") space := testspace.NewSpace(test.HostOperatorNs, "john-dev") - newClient, newRESTClient, _ := prepareRetargetSpace(t, space) - ctx := clicontext.NewCommandContext(term, newClient, newRESTClient) + newClient, _ := prepareRetargetSpace(t, space) + ctx := clicontext.NewCommandContext(term, newClient) // when err := cmd.Retarget(ctx, space.Name, "non-existent-member") // bad member name @@ -90,7 +90,7 @@ func TestRetarget(t *testing.T) { // given term := NewFakeTerminalWithResponse("y") space := testspace.NewSpace(test.HostOperatorNs, "john-dev", testspace.WithCreatorLabel("john")) - newClient, newRESTClient, fakeClient := prepareRetargetSpace(t, space, userSignup) + newClient, fakeClient := prepareRetargetSpace(t, space, userSignup) fakeClient.MockPatch = func(ctx context.Context, obj runtimeclient.Object, patch runtimeclient.Patch, opts ...runtimeclient.PatchOption) error { if testSignup, ok := obj.(*toolchainv1alpha1.Space); ok { if testSignup.Spec.TargetCluster != "" { @@ -99,7 +99,7 @@ func TestRetarget(t *testing.T) { } return fakeClient.Client.Patch(ctx, obj, patch, opts...) } - ctx := clicontext.NewCommandContext(term, newClient, newRESTClient) + ctx := clicontext.NewCommandContext(term, newClient) // when err := cmd.Retarget(ctx, space.Name, "member2") @@ -113,8 +113,8 @@ func TestRetarget(t *testing.T) { // given term := NewFakeTerminalWithResponse("y") space := testspace.NewSpace(test.HostOperatorNs, "john-dev") - newClient, newRESTClient, _ := prepareRetargetSpace(t, space, userSignup) - ctx := clicontext.NewCommandContext(term, newClient, newRESTClient) + newClient, _ := prepareRetargetSpace(t, space, userSignup) + ctx := clicontext.NewCommandContext(term, newClient) // when err := cmd.Retarget(ctx, space.Name, "member2") @@ -128,8 +128,8 @@ func TestRetarget(t *testing.T) { // given term := NewFakeTerminalWithResponse("y") space := testspace.NewSpace(test.HostOperatorNs, "john-dev", testspace.WithCreatorLabel("john")) - newClient, newRESTClient, _ := prepareRetargetSpace(t, space) - ctx := clicontext.NewCommandContext(term, newClient, newRESTClient) + newClient, _ := prepareRetargetSpace(t, space) + ctx := clicontext.NewCommandContext(term, newClient) // when err := cmd.Retarget(ctx, space.Name, "member2") @@ -144,8 +144,8 @@ func TestRetarget(t *testing.T) { // given term := NewFakeTerminalWithResponse("n") space := testspace.NewSpace(test.HostOperatorNs, "john-dev", testspace.WithCreatorLabel("john"), testspace.WithSpecTargetCluster("member-m1.devcluster.openshift.com")) - newClient, newRESTClient, fakeClient := prepareRetargetSpace(t, space, userSignup) - ctx := clicontext.NewCommandContext(term, newClient, newRESTClient) + newClient, fakeClient := prepareRetargetSpace(t, space, userSignup) + ctx := clicontext.NewCommandContext(term, newClient) // when err := cmd.Retarget(ctx, space.Name, "member2") @@ -163,12 +163,12 @@ func TestRetarget(t *testing.T) { }) } -func prepareRetargetSpace(t *testing.T, initObjs ...runtime.Object) (clicontext.NewClientFunc, clicontext.NewRESTClientFunc, *test.FakeClient) { - newClient, newRESTClient, fakeClient := NewFakeClients(t, initObjs...) +func prepareRetargetSpace(t *testing.T, initObjs ...runtime.Object) (clicontext.NewClientFunc, *test.FakeClient) { + newClient, fakeClient := NewFakeClients(t, initObjs...) SetFileConfig(t, Host(), Member(ClusterName("member1"), ServerName("m1.devcluster.openshift.com")), Member(ClusterName("member2"), ServerName("m2.devcluster.openshift.com"))) - return newClient, newRESTClient, fakeClient + return newClient, fakeClient } diff --git a/pkg/cmd/status.go b/pkg/cmd/status.go index 96ef06b..2a3e48d 100644 --- a/pkg/cmd/status.go +++ b/pkg/cmd/status.go @@ -23,7 +23,7 @@ func NewStatusCmd() *cobra.Command { Args: cobra.MaximumNArgs(0), RunE: func(cmd *cobra.Command, _ []string) error { term := ioutils.NewTerminal(cmd.InOrStdin, cmd.OutOrStdout) - ctx := clicontext.NewCommandContext(term, client.DefaultNewClient, client.DefaultNewRESTClient) + ctx := clicontext.NewCommandContext(term, client.DefaultNewClient) return Status(ctx) }, } diff --git a/pkg/cmd/status_test.go b/pkg/cmd/status_test.go index 0efe9b6..9f284dd 100644 --- a/pkg/cmd/status_test.go +++ b/pkg/cmd/status_test.go @@ -21,10 +21,10 @@ import ( func TestStatusCmdWhenIsReady(t *testing.T) { // given toolchainStatus := NewToolchainStatus(ToBeReady()) - newClient, newRESTClient, _ := NewFakeClients(t, toolchainStatus) + newClient, _ := NewFakeClients(t, toolchainStatus) SetFileConfig(t, Host()) term := NewFakeTerminal() - ctx := clicontext.NewCommandContext(term, newClient, newRESTClient) + ctx := clicontext.NewCommandContext(term, newClient) // when err := cmd.Status(ctx) @@ -39,10 +39,10 @@ func TestStatusCmdWhenIsReady(t *testing.T) { func TestStatusCmdWhenIsNotReady(t *testing.T) { // given toolchainStatus := NewToolchainStatus(ToBeNotReady()) - newClient, newRESTClient, _ := NewFakeClients(t, toolchainStatus) + newClient, _ := NewFakeClients(t, toolchainStatus) SetFileConfig(t, Host()) term := NewFakeTerminal() - ctx := clicontext.NewCommandContext(term, newClient, newRESTClient) + ctx := clicontext.NewCommandContext(term, newClient) // when err := cmd.Status(ctx) @@ -57,10 +57,10 @@ func TestStatusCmdWhenIsNotReady(t *testing.T) { func TestStatusCmdWhenConditionNotFound(t *testing.T) { // given toolchainStatus := NewToolchainStatus(toolchainv1alpha1.Condition{}) - newClient, newRESTClient, _ := NewFakeClients(t, toolchainStatus) + newClient, _ := NewFakeClients(t, toolchainStatus) SetFileConfig(t, Host()) term := NewFakeTerminal() - ctx := clicontext.NewCommandContext(term, newClient, newRESTClient) + ctx := clicontext.NewCommandContext(term, newClient) // when err := cmd.Status(ctx) @@ -75,10 +75,10 @@ func TestStatusCmdWhenConditionNotFound(t *testing.T) { func TestStatusCmdWithInsufficientPermissions(t *testing.T) { // given toolchainStatus := NewToolchainStatus(toolchainv1alpha1.Condition{}) - newClient, newRESTClient, _ := NewFakeClients(t, toolchainStatus) + newClient, _ := NewFakeClients(t, toolchainStatus) SetFileConfig(t, Host(NoToken())) term := NewFakeTerminal() - ctx := clicontext.NewCommandContext(term, newClient, newRESTClient) + ctx := clicontext.NewCommandContext(term, newClient) // when err := cmd.Status(ctx) @@ -93,13 +93,13 @@ func TestStatusCmdWithInsufficientPermissions(t *testing.T) { func TestStatusCmdWhenGetFailed(t *testing.T) { // given toolchainStatus := NewToolchainStatus(toolchainv1alpha1.Condition{}) - newClient, newRESTClient, fakeClient := NewFakeClients(t, toolchainStatus) + newClient, fakeClient := NewFakeClients(t, toolchainStatus) SetFileConfig(t, Host()) fakeClient.MockGet = func(ctx context.Context, key runtimeclient.ObjectKey, obj runtimeclient.Object, opts ...runtimeclient.GetOption) error { return fmt.Errorf("some error") } term := NewFakeTerminal() - ctx := clicontext.NewCommandContext(term, newClient, newRESTClient) + ctx := clicontext.NewCommandContext(term, newClient) // when err := cmd.Status(ctx) diff --git a/pkg/context/clusterconfig_command_context.go b/pkg/context/clusterconfig_command_context.go index aa1a1cc..251eb94 100644 --- a/pkg/context/clusterconfig_command_context.go +++ b/pkg/context/clusterconfig_command_context.go @@ -17,12 +17,11 @@ type ClusterConfigCommandContext struct { } // NewClusterConfigCommandContext returns the context of the admin command to run -func NewClusterConfigCommandContext(term ioutils.Terminal, cfg configuration.ClusterConfig, newClient NewClientFunc, newRESTClient NewRESTClientFunc, files assets.FS, clusterConfigName string) *ClusterConfigCommandContext { +func NewClusterConfigCommandContext(term ioutils.Terminal, cfg configuration.ClusterConfig, newClient NewClientFunc, files assets.FS, clusterConfigName string) *ClusterConfigCommandContext { return &ClusterConfigCommandContext{ CommandContext: CommandContext{ - Terminal: term, - NewClient: newClient, - NewRESTClient: newRESTClient, + Terminal: term, + NewClient: newClient, }, Files: files, ClusterConfig: cfg, diff --git a/pkg/context/clusterconfig_command_context_test.go b/pkg/context/clusterconfig_command_context_test.go index 448a0a6..68ba1d1 100644 --- a/pkg/context/clusterconfig_command_context_test.go +++ b/pkg/context/clusterconfig_command_context_test.go @@ -31,7 +31,7 @@ func TestConfigPath(t *testing.T) { t.Run("with explicit clusterName", func(t *testing.T) { // given - ctx := clicontext.NewClusterConfigCommandContext(term, cfg, nil, nil, resources.Resources, "custom_path") + ctx := clicontext.NewClusterConfigCommandContext(term, cfg, nil, resources.Resources, "custom_path") // when path, err := cfg.ConfigurePath(ctx, ctx.ClusterConfigName, "component") @@ -43,7 +43,7 @@ func TestConfigPath(t *testing.T) { t.Run("without explicit clusterName", func(t *testing.T) { // given - ctx := clicontext.NewClusterConfigCommandContext(term, cfg, nil, nil, resources.Resources, "") // default path + ctx := clicontext.NewClusterConfigCommandContext(term, cfg, nil, resources.Resources, "") // default path // when path, err := cfg.ConfigurePath(ctx, ctx.ClusterConfigName, "component") diff --git a/pkg/context/command_context.go b/pkg/context/command_context.go index c1dde15..e8c71cd 100644 --- a/pkg/context/command_context.go +++ b/pkg/context/command_context.go @@ -2,9 +2,9 @@ package context import ( "context" + "github.com/kubesaw/ksctl/pkg/ioutils" - "k8s.io/client-go/rest" runtimeclient "sigs.k8s.io/controller-runtime/pkg/client" ) @@ -12,21 +12,17 @@ import ( type CommandContext struct { context.Context ioutils.Terminal - NewClient NewClientFunc - NewRESTClient NewRESTClientFunc + NewClient NewClientFunc } // NewClientFunc a function to create a `client.Client` with the given token and API endpoint type NewClientFunc func(string, string) (runtimeclient.Client, error) -type NewRESTClientFunc func(token, apiEndpoint string) (*rest.RESTClient, error) - // NewCommandContext returns the context of the command to run -func NewCommandContext(term ioutils.Terminal, newClient NewClientFunc, newRESTClient NewRESTClientFunc) *CommandContext { +func NewCommandContext(term ioutils.Terminal, newClient NewClientFunc) *CommandContext { return &CommandContext{ - Context: context.Background(), - Terminal: term, - NewClient: newClient, - NewRESTClient: newRESTClient, + Context: context.Background(), + Terminal: term, + NewClient: newClient, } } diff --git a/pkg/context/command_context_test.go b/pkg/context/command_context_test.go index 36fda81..967466b 100644 --- a/pkg/context/command_context_test.go +++ b/pkg/context/command_context_test.go @@ -15,9 +15,9 @@ func TestLoadClusterConfig(t *testing.T) { SetFileConfig(t, Host()) t.Run("success", func(t *testing.T) { - newClient, newRESTClient, _ := NewFakeClients(t) + newClient, _ := NewFakeClients(t) term := NewFakeTerminalWithResponse("Y") - ctx := clicontext.NewCommandContext(term, newClient, newRESTClient) + ctx := clicontext.NewCommandContext(term, newClient) // when _, err := configuration.LoadClusterConfig(ctx, "host") @@ -29,9 +29,9 @@ func TestLoadClusterConfig(t *testing.T) { t.Run("fail", func(t *testing.T) { // given SetFileConfig(t, Host(NoToken()), Member(NoToken())) - newClient, newRESTClient, _ := NewFakeClients(t) + newClient, _ := NewFakeClients(t) term := NewFakeTerminalWithResponse("Y") - ctx := clicontext.NewCommandContext(term, newClient, newRESTClient) + ctx := clicontext.NewCommandContext(term, newClient) // when _, err := configuration.LoadClusterConfig(ctx, "host") diff --git a/pkg/test/client.go b/pkg/test/client.go index 6b4e4ff..4d15496 100644 --- a/pkg/test/client.go +++ b/pkg/test/client.go @@ -18,7 +18,7 @@ import ( runtimeclient "sigs.k8s.io/controller-runtime/pkg/client" ) -func NewFakeClients(t *testing.T, initObjs ...runtime.Object) (clicontext.NewClientFunc, clicontext.NewRESTClientFunc, *test.FakeClient) { +func NewFakeClients(t *testing.T, initObjs ...runtime.Object) (clicontext.NewClientFunc, *test.FakeClient) { fakeClient := test.NewFakeClient(t, initObjs...) fakeClient.MockCreate = func(ctx context.Context, obj runtimeclient.Object, opts ...runtimeclient.CreateOption) error { stringDataToData(obj) @@ -35,9 +35,6 @@ func NewFakeClients(t *testing.T, initObjs ...runtime.Object) (clicontext.NewCli assert.Contains(t, apiEndpoint, ".com") return fakeClient, nil }, - func(token string, apiEndpoint string) (*rest.RESTClient, error) { - return NewFakeExternalClient(t, token, apiEndpoint), nil - }, fakeClient }