Skip to content

Commit

Permalink
Merge branch 'master' into drop_generatecontext_newclient
Browse files Browse the repository at this point in the history
  • Loading branch information
xcoulon authored Mar 21, 2024
2 parents 10dffc6 + 0f1c2f5 commit 7500083
Show file tree
Hide file tree
Showing 39 changed files with 302 additions and 314 deletions.
44 changes: 44 additions & 0 deletions .codecov.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
# Config can be validated before merging: curl -X POST --data-binary @.codecov.yaml https://codecov.io/validate
codecov:
max_report_age: "off" # see https://docs.codecov.io/docs/codecov-yaml#section-expired-reports

# See http://docs.codecov.io/docs/coverage-configuration
coverage:
precision: 2 # 2 = xx.xx%, 0 = xx%
round: down
# For example: 20...60 would result in any coverage less than 20%
# would have a red background. The color would gradually change to
# green approaching 60%. Any coverage over 60% would result in a
# solid green color.
range: "20...60"

# See https://docs.codecov.com/docs/commit-status
status:
# project will give us the diff in the total code coverage between a commit
# and its parent
project:
default:
# Allow the coverage to drop by 1%, and posting a success status.
threshold: 1%
# Patch gives just the coverage of the patch
patch: yes

Check warning on line 24 in .codecov.yaml

View workflow job for this annotation

GitHub Actions / YAML Lint

24:12 [truthy] truthy value should be one of [false, true]
# changes tells us if there are unexpected code coverage changes in other files
# which were not changed by the diff
changes: yes

Check warning on line 27 in .codecov.yaml

View workflow job for this annotation

GitHub Actions / YAML Lint

27:14 [truthy] truthy value should be one of [false, true]

# See http://docs.codecov.io/docs/ignoring-paths
ignore:
- "github/*"
- "cmd/*"
- "make/*"
- "resources/*"
- "test-resources/*"

# See https://docs.codecov.com/docs/pull-request-comments
comment:
layout: "header, diff, tree"
# default = posts once then update, posts new if delete
# once = post once then updates
# new = delete old, post new
# spammy = post new
behavior: "new"
31 changes: 15 additions & 16 deletions pkg/client/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,14 +26,14 @@ import (
func TestNewClientOK(t *testing.T) {
// given
t.Cleanup(gock.OffAll)
gock.New("https://example.com").
gock.New("http://example.com").
Get("api").
Persist().
Reply(200).
BodyString("{}")

// when
cl, err := client.NewClient("cool-token", "https://example.com")
cl, err := client.NewClient("cool-token", "http://example.com")

// then
require.NoError(t, err)
Expand All @@ -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) {
Expand All @@ -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) {
Expand All @@ -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) {
Expand All @@ -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) {
Expand All @@ -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) {
Expand All @@ -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) {
Expand All @@ -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) {
Expand Down
2 changes: 1 addition & 1 deletion pkg/cmd/add_space_users.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
},
Expand Down
38 changes: 19 additions & 19 deletions pkg/cmd/add_space_users_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"})
Expand All @@ -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"})
Expand All @@ -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"})
Expand All @@ -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"})
Expand All @@ -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"})
Expand All @@ -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"})
Expand All @@ -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
Expand All @@ -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"})
Expand All @@ -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)
Expand All @@ -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) {
Expand Down
2 changes: 1 addition & 1 deletion pkg/cmd/adm/register_member.go
Original file line number Diff line number Diff line change
Expand Up @@ -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...)
}
Expand Down
16 changes: 8 additions & 8 deletions pkg/cmd/adm/register_member_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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
Expand Down
2 changes: 1 addition & 1 deletion pkg/cmd/adm/restart.go
Original file line number Diff line number Diff line change
Expand Up @@ -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...)
},
}
Expand Down
Loading

0 comments on commit 7500083

Please sign in to comment.