Skip to content

Commit 005752b

Browse files
test(iam): fix tests crashing in case of get api-key failure (scaleway#5075)
1 parent ce16e26 commit 005752b

8 files changed

+432
-503
lines changed

internal/namespaces/iam/v1alpha1/custom_iam_test.go

Lines changed: 91 additions & 93 deletions
Original file line numberDiff line numberDiff line change
@@ -31,103 +31,101 @@ func Test_iamAPIKeyGet(t *testing.T) {
3131
create a member API Key from the organization owner (no impersonation).
3232
*/
3333

34-
userResulter := func(result any) any {
35-
users := result.([]*iamSdk.User)
36-
if users == nil {
37-
panic("users is nil")
38-
}
39-
if len(users) == 0 {
40-
panic("no user found")
41-
}
42-
43-
return users[0].ID
44-
}
45-
46-
apiKeyResulter := func(result any) any {
47-
apiKeys := result.([]*iamSdk.APIKey)
48-
if apiKeys == nil {
49-
panic("apiKeys is nil")
50-
}
51-
if len(apiKeys) == 0 {
52-
panic("no api key found")
53-
}
54-
55-
return apiKeys[0].AccessKey
56-
}
57-
58-
applicationResulter := func(result any) any {
59-
applications := result.([]*iamSdk.Application)
60-
if applications == nil {
61-
panic("applications is nil")
62-
}
63-
if len(applications) == 0 {
64-
panic("no application found")
65-
}
66-
67-
return applications[0].ID
68-
}
69-
70-
t.Run("GetOwnerAPIKey", core.Test(&core.TestConfig{
71-
Commands: commands,
72-
BeforeFunc: core.BeforeFuncCombine(
73-
core.ExecStoreBeforeCmdWithResulter(
74-
"owner",
75-
"scw iam user list type=owner",
76-
userResulter,
34+
t.Run("GetOwnerAPIKey", func(t *testing.T) {
35+
userResulter := newSliceResulter(t, "*iamSdk.User", func(users []*iamSdk.User) any {
36+
return users[0].ID
37+
})
38+
39+
apiKeyResulter := newSliceResulter(t, "*iamSdk.APIKey", func(keys []*iamSdk.APIKey) any {
40+
return keys[0].AccessKey
41+
})
42+
43+
core.Test(&core.TestConfig{
44+
Commands: commands,
45+
BeforeFunc: core.BeforeFuncCombine(
46+
core.ExecStoreBeforeCmdWithResulter(
47+
"owner",
48+
"scw iam user list type=owner",
49+
userResulter,
50+
),
51+
52+
core.ExecStoreBeforeCmdWithResulter(
53+
"ownerAPIKey",
54+
"scw iam api-key list bearer-id={{ .owner }}",
55+
apiKeyResulter,
56+
),
7757
),
78-
79-
core.ExecStoreBeforeCmdWithResulter(
80-
"ownerAPIKey",
81-
"scw iam api-key list bearer-id={{ .owner }}",
82-
apiKeyResulter,
58+
Cmd: `scw iam api-key get {{ .ownerAPIKey }}`,
59+
Check: core.TestCheckCombine(
60+
core.TestCheckGolden(),
61+
core.TestCheckExitCode(0),
8362
),
84-
),
85-
Cmd: `scw iam api-key get {{ .ownerAPIKey }}`,
86-
Check: core.TestCheckCombine(
87-
core.TestCheckGolden(),
88-
core.TestCheckExitCode(0),
89-
),
90-
}))
91-
92-
t.Run("GetMemberAPIKey", core.Test(&core.TestConfig{
93-
Commands: commands,
94-
BeforeFunc: core.BeforeFuncCombine(
95-
core.ExecStoreBeforeCmdWithResulter(
96-
"member",
97-
"scw iam user list type=member",
98-
userResulter,
63+
})(t)
64+
})
65+
66+
t.Run("GetMemberAPIKey", func(t *testing.T) {
67+
userResulter := newSliceResulter(t, "*iamSdk.User", func(users []*iamSdk.User) any {
68+
return users[0].ID
69+
})
70+
71+
apiKeyResulter := newSliceResulter(t, "*iamSdk.APIKey", func(keys []*iamSdk.APIKey) any {
72+
return keys[0].AccessKey
73+
})
74+
75+
core.Test(&core.TestConfig{
76+
Commands: commands,
77+
BeforeFunc: core.BeforeFuncCombine(
78+
core.ExecStoreBeforeCmdWithResulter(
79+
"member",
80+
"scw iam user list type=member",
81+
userResulter,
82+
),
83+
core.ExecStoreBeforeCmdWithResulter(
84+
"memberAPIKey",
85+
"scw iam api-key list bearer-id={{ .member }}",
86+
apiKeyResulter,
87+
),
9988
),
100-
core.ExecStoreBeforeCmdWithResulter(
101-
"memberAPIKey",
102-
"scw iam api-key list bearer-id={{ .member }}",
103-
apiKeyResulter,
89+
Cmd: `scw iam api-key get {{ .memberAPIKey }}`,
90+
Check: core.TestCheckCombine(
91+
core.TestCheckGolden(),
92+
core.TestCheckExitCode(0),
10493
),
105-
),
106-
Cmd: `scw iam api-key get {{ .memberAPIKey }}`,
107-
Check: core.TestCheckCombine(
108-
core.TestCheckGolden(),
109-
core.TestCheckExitCode(0),
110-
),
111-
}))
112-
113-
t.Run("GetApplicationAPIKey", core.Test(&core.TestConfig{
114-
Commands: commands,
115-
BeforeFunc: core.BeforeFuncCombine(
116-
core.ExecStoreBeforeCmdWithResulter(
117-
"application",
118-
"scw iam application list",
119-
applicationResulter,
94+
})(t)
95+
})
96+
97+
t.Run("GetApplicationAPIKey", func(t *testing.T) {
98+
appResulter := newSliceResulter(
99+
t,
100+
"*iamSdk.Application",
101+
func(apps []*iamSdk.Application) any {
102+
return apps[0].ID
103+
},
104+
)
105+
106+
apiKeyResulter := newSliceResulter(t, "*iamSdk.APIKey", func(keys []*iamSdk.APIKey) any {
107+
return keys[0].AccessKey
108+
})
109+
110+
core.Test(&core.TestConfig{
111+
Commands: commands,
112+
BeforeFunc: core.BeforeFuncCombine(
113+
core.ExecStoreBeforeCmdWithResulter(
114+
"application",
115+
"scw iam application list",
116+
appResulter,
117+
),
118+
core.ExecStoreBeforeCmdWithResulter(
119+
"applicationAPIKey",
120+
"scw iam api-key list bearer-id={{ .application }}",
121+
apiKeyResulter,
122+
),
120123
),
121-
core.ExecStoreBeforeCmdWithResulter(
122-
"applicationAPIKey",
123-
"scw iam api-key list bearer-id={{ .application }}",
124-
apiKeyResulter,
124+
Cmd: `scw iam api-key get {{ .applicationAPIKey }}`,
125+
Check: core.TestCheckCombine(
126+
core.TestCheckGolden(),
127+
core.TestCheckExitCode(0),
125128
),
126-
),
127-
Cmd: `scw iam api-key get {{ .applicationAPIKey }}`,
128-
Check: core.TestCheckCombine(
129-
core.TestCheckGolden(),
130-
core.TestCheckExitCode(0),
131-
),
132-
}))
129+
})(t)
130+
})
133131
}

internal/namespaces/iam/v1alpha1/helpers_test.go

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
package iam_test
22

3-
import "github.com/scaleway/scaleway-cli/v2/core"
3+
import (
4+
"testing"
5+
6+
"github.com/scaleway/scaleway-cli/v2/core"
7+
)
48

59
func addSSHKey(metaKey string, key string) core.BeforeFunc {
610
return func(ctx *core.BeforeFuncCtx) error {
@@ -11,3 +15,26 @@ func addSSHKey(metaKey string, key string) core.BeforeFunc {
1115
return nil
1216
}
1317
}
18+
19+
func newSliceResulter[T any](
20+
t *testing.T,
21+
sliceTypeName string,
22+
extractor func([]T) any,
23+
) func(any) any {
24+
t.Helper()
25+
26+
return func(result any) any {
27+
slice, ok := result.([]T)
28+
if !ok {
29+
t.Fatalf("expected []%s, got %T", sliceTypeName, result)
30+
}
31+
if slice == nil {
32+
t.Fatalf("%s slice is nil", sliceTypeName)
33+
}
34+
if len(slice) == 0 {
35+
t.Fatalf("no %s found", sliceTypeName)
36+
}
37+
38+
return extractor(slice)
39+
}
40+
}

0 commit comments

Comments
 (0)