@@ -16,6 +16,7 @@ package regcreds
16
16
import (
17
17
"testing"
18
18
19
+ "github.com/aws/amazon-ecs-cli/ecs-cli/modules/utils/regcreds"
19
20
"github.com/aws/aws-sdk-go/aws"
20
21
"github.com/aws/aws-sdk-go/aws/awserr"
21
22
"github.com/aws/aws-sdk-go/service/iam"
@@ -28,8 +29,8 @@ func TestCreateTaskExecutionRole(t *testing.T) {
28
29
testRegistry := "myreg.test.io"
29
30
testRegCredARN := "arn:aws:secret/some-test-arn"
30
31
testRegKMSKey := "arn:aws:kms:key/67yt-756yth"
31
- testCreds := make (map [string ]CredsOutputEntry )
32
- testCreds [testRegistry ] = CredsOutputEntry {
32
+ testCreds := make (map [string ]readers. CredsOutputEntry )
33
+ testCreds [testRegistry ] = readers. CredsOutputEntry {
33
34
CredentialARN : testRegCredARN ,
34
35
KMSKeyID : testRegKMSKey ,
35
36
}
@@ -57,15 +58,16 @@ func TestCreateTaskExecutionRole(t *testing.T) {
57
58
Region : "us-west-2" ,
58
59
}
59
60
60
- err := createTaskExecutionRole (testParams , mocks .MockIAM , mocks .MockKMS )
61
+ policyCreateTime , err := createTaskExecutionRole (testParams , mocks .MockIAM , mocks .MockKMS )
61
62
assert .NoError (t , err , "Unexpected error when creating task execution role" )
63
+ assert .NotNil (t , policyCreateTime , "Expected policy create time to be non-nil" )
62
64
}
63
65
64
66
func TestCreateTaskExecutionRole_NoKMSKey (t * testing.T ) {
65
67
testRegistry := "myreg.test.io"
66
68
testRegCredARN := "arn:aws:secret/some-test-arn"
67
- testCreds := make (map [string ]CredsOutputEntry )
68
- testCreds [testRegistry ] = CredsOutputEntry {CredentialARN : testRegCredARN }
69
+ testCreds := make (map [string ]readers. CredsOutputEntry )
70
+ testCreds [testRegistry ] = readers. CredsOutputEntry {CredentialARN : testRegCredARN }
69
71
testRoleName := "myNginxProjectRole"
70
72
71
73
testPolicyArn := aws .String ("arn:aws:iam::policy/" + testRoleName + "-policy" )
@@ -88,15 +90,16 @@ func TestCreateTaskExecutionRole_NoKMSKey(t *testing.T) {
88
90
Region : "us-west-2" ,
89
91
}
90
92
91
- err := createTaskExecutionRole (testParams , mocks .MockIAM , mocks .MockKMS )
93
+ policyCreateTime , err := createTaskExecutionRole (testParams , mocks .MockIAM , mocks .MockKMS )
92
94
assert .NoError (t , err , "Unexpected error when creating task execution role" )
95
+ assert .NotNil (t , policyCreateTime , "Expected policy create time to be non-nil" )
93
96
}
94
97
95
98
func TestCreateTaskExecutionRole_RoleExists (t * testing.T ) {
96
99
testRegistry := "myreg.test.io"
97
100
testRegCredARN := "arn:aws:secret/some-test-arn"
98
- testCreds := make (map [string ]CredsOutputEntry )
99
- testCreds [testRegistry ] = CredsOutputEntry {CredentialARN : testRegCredARN }
101
+ testCreds := make (map [string ]readers. CredsOutputEntry )
102
+ testCreds [testRegistry ] = readers. CredsOutputEntry {CredentialARN : testRegCredARN }
100
103
testRoleName := "myNginxProjectRole"
101
104
102
105
testPolicyArn := aws .String ("arn:aws:iam::policy/" + testRoleName + "-policy" )
@@ -120,49 +123,16 @@ func TestCreateTaskExecutionRole_RoleExists(t *testing.T) {
120
123
Region : "us-west-2" ,
121
124
}
122
125
123
- err := createTaskExecutionRole (testParams , mocks .MockIAM , mocks .MockKMS )
124
- assert .NoError (t , err , "Unexpected error when creating task execution role" )
125
- }
126
-
127
- func TestCreateTaskExecutionRole_RoleAndPolicyAlreadyExists (t * testing.T ) {
128
- testRegistry := "myreg.test.io"
129
- testRegCredARN := "arn:aws:secret/some-test-arn"
130
- testCreds := make (map [string ]CredsOutputEntry )
131
- testCreds [testRegistry ] = CredsOutputEntry {CredentialARN : testRegCredARN }
132
- testRoleName := "myNginxProjectRole"
133
-
134
- testPolicySecondArn := aws .String ("arn:aws:iam::policy/" + testRoleName + "-policy-20181010222222" )
135
- entityExistsError := awserr .New ("EntityAlreadyExists" , "Didn't you see the error code? This resource already exists." , errors .New ("something went wrong" ))
136
-
137
- mocks := setupTestController (t )
138
- gomock .InOrder (
139
- // CreateOrFindRole should return nil if given role already exists
140
- mocks .MockIAM .EXPECT ().CreateOrFindRole (testRoleName , roleDescriptionString , assumeRolePolicyDocString ).Return ("" , nil ),
141
- mocks .MockIAM .EXPECT ().CreateRole (gomock .Any ()).Return (nil , entityExistsError ),
142
- )
143
- gomock .InOrder (
144
- // CreatePolicy should be called again if the generated policy name already exists
145
- mocks .MockIAM .EXPECT ().CreatePolicy (gomock .Any ()).Return (nil , entityExistsError ),
146
- mocks .MockIAM .EXPECT ().CreatePolicy (gomock .Any ()).Return (& iam.CreatePolicyOutput {Policy : & iam.Policy {Arn : testPolicySecondArn }}, nil ),
147
- mocks .MockIAM .EXPECT ().AttachRolePolicy (getExecutionRolePolicyARN ("us-west-2" ), testRoleName ).Return (nil , nil ),
148
- mocks .MockIAM .EXPECT ().AttachRolePolicy (* testPolicySecondArn , testRoleName ).Return (nil , nil ),
149
- )
150
-
151
- testParams := executionRoleParams {
152
- CredEntries : testCreds ,
153
- RoleName : testRoleName ,
154
- Region : "us-west-2" ,
155
- }
156
-
157
- err := createTaskExecutionRole (testParams , mocks .MockIAM , mocks .MockKMS )
126
+ policyCreateTime , err := createTaskExecutionRole (testParams , mocks .MockIAM , mocks .MockKMS )
158
127
assert .NoError (t , err , "Unexpected error when creating task execution role" )
128
+ assert .NotNil (t , policyCreateTime , "Expected policy create time to be non-nil" )
159
129
}
160
130
161
131
func TestCreateTaskExecutionRole_ErrorOnCreateRoleFails (t * testing.T ) {
162
132
testRegistry := "myreg.test.io"
163
133
testRegCredARN := "arn:aws:secret/some-test-arn"
164
- testCreds := make (map [string ]CredsOutputEntry )
165
- testCreds [testRegistry ] = CredsOutputEntry {CredentialARN : testRegCredARN }
134
+ testCreds := make (map [string ]readers. CredsOutputEntry )
135
+ testCreds [testRegistry ] = readers. CredsOutputEntry {CredentialARN : testRegCredARN }
166
136
testRoleName := "myNginxProjectRole"
167
137
168
138
mocks := setupTestController (t )
@@ -177,18 +147,17 @@ func TestCreateTaskExecutionRole_ErrorOnCreateRoleFails(t *testing.T) {
177
147
Region : "us-west-2" ,
178
148
}
179
149
180
- err := createTaskExecutionRole (testParams , mocks .MockIAM , mocks .MockKMS )
150
+ _ , err := createTaskExecutionRole (testParams , mocks .MockIAM , mocks .MockKMS )
181
151
assert .Error (t , err , "Expected error when CreateRole fails" )
182
152
}
183
153
184
154
func TestCreateTaskExecutionRole_ErrorOnCreatePolicyFails (t * testing.T ) {
185
155
testRegistry := "myreg.test.io"
186
156
testRegCredARN := "arn:aws:secret/some-test-arn"
187
- testCreds := make (map [string ]CredsOutputEntry )
188
- testCreds [testRegistry ] = CredsOutputEntry {CredentialARN : testRegCredARN }
157
+ testCreds := make (map [string ]readers. CredsOutputEntry )
158
+ testCreds [testRegistry ] = readers. CredsOutputEntry {CredentialARN : testRegCredARN }
189
159
testRoleName := "myNginxProjectRole"
190
160
191
- //testPolicyArn := aws.String("arn:aws:iam::policy/" + testRoleName + "-policy")
192
161
testRoleArn := aws .String ("arn:aws:iam::role/" + testRoleName )
193
162
194
163
mocks := setupTestController (t )
@@ -206,6 +175,6 @@ func TestCreateTaskExecutionRole_ErrorOnCreatePolicyFails(t *testing.T) {
206
175
Region : "us-west-2" ,
207
176
}
208
177
209
- err := createTaskExecutionRole (testParams , mocks .MockIAM , mocks .MockKMS )
178
+ _ , err := createTaskExecutionRole (testParams , mocks .MockIAM , mocks .MockKMS )
210
179
assert .Error (t , err , "Expected error when CreatePolicy fails" )
211
180
}
0 commit comments