@@ -29,38 +29,108 @@ import (
2929 "github.com/stretchr/testify/assert"
3030)
3131
32+ // AWS SDK mocks
33+
3234type mockGetRoleAPI func (ctx context.Context , params * iam.GetRoleInput , optFns ... func (* iam.Options )) (* iam.GetRoleOutput , error )
3335
3436func (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+
3858func 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