Skip to content

Commit 7460c80

Browse files
authored
test(cli): mock awsiam.go clients (#1124)
Fixes RAIN-47429 Signed-off-by: nschmeller <[email protected]>
1 parent 7b5334b commit 7460c80

File tree

2 files changed

+84
-9
lines changed

2 files changed

+84
-9
lines changed

cli/cmd/awsiam.go

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -173,11 +173,11 @@ func setupSSMRole(c *iam.Client, roleName string) (types.Role, error) {
173173
}
174174
}
175175

176-
type IAMGetRoleAPI interface {
176+
type iamGetRoleFromNameAPI interface {
177177
GetRole(ctx context.Context, params *iam.GetRoleInput, optFns ...func(*iam.Options)) (*iam.GetRoleOutput, error)
178178
}
179179

180-
func getRoleFromName(c IAMGetRoleAPI, roleName string) (types.Role, error) {
180+
func getRoleFromName(c iamGetRoleFromNameAPI, roleName string) (types.Role, error) {
181181
cli.Log.Debug("fetching info about role", roleName)
182182
output, err := c.GetRole(
183183
context.Background(),
@@ -192,9 +192,14 @@ func getRoleFromName(c IAMGetRoleAPI, roleName string) (types.Role, error) {
192192
return *output.Role, nil
193193
}
194194

195+
type iamCreateSSMRoleAPI interface {
196+
GetRole(ctx context.Context, params *iam.GetRoleInput, optFns ...func(*iam.Options)) (*iam.GetRoleOutput, error)
197+
CreateRole(ctx context.Context, params *iam.CreateRoleInput, optFns ...func(*iam.Options)) (*iam.CreateRoleOutput, error)
198+
}
199+
195200
// createSSMRole makes a call to the AWS API to create an IAM role.
196201
// Returns information about the newly created role and any errors.
197-
func createSSMRole(c *iam.Client) (types.Role, error) {
202+
func createSSMRole(c iamCreateSSMRoleAPI) (types.Role, error) {
198203
cli.Log.Debug("check if role already exists") // intended for after interrupt or error
199204
getOutput, err := c.GetRole(
200205
context.Background(),

cli/cmd/awsiam_test.go

Lines changed: 76 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -29,38 +29,108 @@ import (
2929
"github.com/stretchr/testify/assert"
3030
)
3131

32+
// AWS SDK mocks
33+
3234
type mockGetRoleAPI func(ctx context.Context, params *iam.GetRoleInput, optFns ...func(*iam.Options)) (*iam.GetRoleOutput, error)
3335

3436
func (m mockGetRoleAPI) GetRole(ctx context.Context, params *iam.GetRoleInput, optFns ...func(*iam.Options)) (*iam.GetRoleOutput, error) {
3537
return m(ctx, params, optFns...)
3638
}
3739

40+
type mockCreateRoleAPI func(ctx context.Context, params *iam.CreateRoleInput, optFns ...func(*iam.Options)) (*iam.CreateRoleOutput, error)
41+
42+
func (m mockCreateRoleAPI) CreateRole(ctx context.Context, params *iam.CreateRoleInput, optFns ...func(*iam.Options)) (*iam.CreateRoleOutput, error) {
43+
return m(ctx, params, optFns...)
44+
}
45+
46+
// ------------------------------------------------------------
47+
48+
// getRoleFromName tests
49+
50+
type mockGetRoleFromNameClient struct {
51+
getRoleMethod mockGetRoleAPI
52+
}
53+
54+
func (m mockGetRoleFromNameClient) GetRole(ctx context.Context, params *iam.GetRoleInput, optFns ...func(*iam.Options)) (*iam.GetRoleOutput, error) {
55+
return m.getRoleMethod(ctx, params, optFns...)
56+
}
57+
3858
func TestGetRoleFromName(t *testing.T) {
3959
testRoleName := "test_role_name"
4060
cases := []struct {
41-
client func(t *testing.T) IAMGetRoleAPI
61+
client iamGetRoleFromNameAPI
4262
roleName string
4363
}{
4464
{
45-
client: func(t *testing.T) IAMGetRoleAPI {
46-
return mockGetRoleAPI(func(ctx context.Context, params *iam.GetRoleInput, optFns ...func(*iam.Options)) (*iam.GetRoleOutput, error) {
65+
client: mockGetRoleFromNameClient{
66+
getRoleMethod: mockGetRoleAPI(func(ctx context.Context, params *iam.GetRoleInput, optFns ...func(*iam.Options)) (*iam.GetRoleOutput, error) {
4767
return &iam.GetRoleOutput{
4868
Role: &types.Role{
4969
RoleName: aws.String(testRoleName),
5070
},
5171
}, nil
72+
}),
73+
},
74+
roleName: testRoleName,
75+
},
76+
}
77+
78+
for i, tt := range cases {
79+
t.Run(strconv.Itoa(i), func(t *testing.T) {
80+
role, err := getRoleFromName(tt.client, tt.roleName)
81+
assert.NoError(t, err)
82+
assert.Equal(t, *role.RoleName, tt.roleName)
83+
})
84+
}
85+
}
5286

53-
})
87+
// createSSMRole tests
88+
89+
type mockCreateSSMRoleClient struct {
90+
getRoleMethod mockGetRoleAPI
91+
createRoleMethod mockCreateRoleAPI
92+
}
93+
94+
func (m mockCreateSSMRoleClient) GetRole(ctx context.Context, params *iam.GetRoleInput, optFns ...func(*iam.Options)) (*iam.GetRoleOutput, error) {
95+
return m.getRoleMethod(ctx, params, optFns...)
96+
}
97+
98+
func (m mockCreateSSMRoleClient) CreateRole(ctx context.Context, params *iam.CreateRoleInput, optFns ...func(*iam.Options)) (*iam.CreateRoleOutput, error) {
99+
return m.createRoleMethod(ctx, params, optFns...)
100+
}
101+
102+
func TestCreateSSMRole(t *testing.T) {
103+
testRoleName := "test_role_name"
104+
cases := []struct {
105+
client iamCreateSSMRoleAPI
106+
roleName string
107+
}{
108+
{
109+
client: mockCreateSSMRoleClient{
110+
getRoleMethod: mockGetRoleAPI(func(ctx context.Context, params *iam.GetRoleInput, optFns ...func(*iam.Options)) (*iam.GetRoleOutput, error) {
111+
return &iam.GetRoleOutput{
112+
Role: &types.Role{
113+
RoleName: aws.String(testRoleName),
114+
},
115+
}, nil
116+
}),
117+
createRoleMethod: mockCreateRoleAPI(func(ctx context.Context, params *iam.CreateRoleInput, optFns ...func(*iam.Options)) (*iam.CreateRoleOutput, error) {
118+
return &iam.CreateRoleOutput{
119+
Role: &types.Role{
120+
RoleName: aws.String(testRoleName),
121+
},
122+
}, nil
123+
}),
54124
},
55125
roleName: testRoleName,
56126
},
57127
}
58128

59129
for i, tt := range cases {
60130
t.Run(strconv.Itoa(i), func(t *testing.T) {
61-
role, err := getRoleFromName(tt.client(t), tt.roleName)
131+
role, err := createSSMRole(tt.client)
62132
assert.NoError(t, err)
63-
assert.Equal(t, *role.RoleName, testRoleName)
133+
assert.Equal(t, *role.RoleName, tt.roleName)
64134
})
65135
}
66136
}

0 commit comments

Comments
 (0)