@@ -50,11 +50,26 @@ import (
50
50
)
51
51
52
52
const (
53
- cluster = "cluster"
54
- endpointId = "endpointId"
55
- ecsCallTimeout = 5 * time .Second
56
- taskARN = "taskARN"
57
- taskRoleCredsID = "taskRoleCredsID"
53
+ cluster = "cluster"
54
+ endpointId = "endpointId"
55
+ ecsCallTimeout = 5 * time .Second
56
+ taskARN = "taskARN"
57
+ taskRoleCredsID = "taskRoleCredsID"
58
+ updateTaskProtectionDecodeError = "UpdateTaskProtection: failed to decode request"
59
+ )
60
+
61
+ var (
62
+ taskMetadataErrorResponse = `{"error":{"Code":"ServerException","Message":"Failed to find a task for the request"}}`
63
+ noCredentialsErrorResponse = fmt .Sprintf (`{"error":{"Arn":"%s","Code":"AccessDeniedException","Message":"Invalid Request: no task IAM role credentials available for task"}}` , taskARN )
64
+ requestFailureErrorResponse = `{"requestID":"%s","error":{"Arn":"%s","Code":"AccessDeniedException","Message":"%s"}}`
65
+ timeoutErrorResponse = fmt .Sprintf (`{"error":{"Arn":"%s","Code":"RequestCanceled","Message":"Timed out calling ECS Task Protection API"}}` , taskARN )
66
+ nonRequestAWSErrorResponse = `{"error":{"Arn":"%s","Code":"InvalidParameterException","Message":"%s"}}`
67
+ nonAWSErrorResponse = `{"error":{"Arn":"%s","Code":"ServerException","Message":"%s"}}`
68
+ ecsErrorResponse = fmt .Sprintf (`{"failure":{"Arn":"%s","Detail":null,"Reason":"ecs failure 1"}}` , taskARN )
69
+ multipleECSErrorResponse = fmt .Sprintf (`{"error":{"Arn":"%s","Code":"ServerException","Message":"Unexpected error occurred"}}` , taskARN )
70
+ happyEnabledTaskProtectionResponse = fmt .Sprintf (`{"protection":{"ExpirationDate":null,"ProtectionEnabled":true,"TaskArn":"%s"}}` , taskARN )
71
+ malformedRequestResponse = `{"error":{"Code":"InvalidParameterException","Message":"%s"}}`
72
+ missingTaskProtectionFieldResponse = fmt .Sprintf (`{"error":{"Arn":"%s","Code":"InvalidParameterException","Message":"Invalid request: does not contain 'ProtectionEnabled' field"}}` , taskARN )
58
73
)
59
74
60
75
// Tests the path for UpdateTaskProtection API
@@ -70,6 +85,7 @@ type TestCase struct {
70
85
setMetricsExpectations func (ctrl * gomock.Controller , metricsFactory * mock_metrics.MockEntryFactory )
71
86
expectedStatusCode int
72
87
expectedResponseBody types.TaskProtectionResponse
88
+ expectedResonseBodyJSON string
73
89
}
74
90
75
91
func testTaskProtectionRequest (t * testing.T , tc TestCase ) {
@@ -127,6 +143,7 @@ func testTaskProtectionRequest(t *testing.T, tc TestCase) {
127
143
var actualResponseBody types.TaskProtectionResponse
128
144
err = json .Unmarshal (recorder .Body .Bytes (), & actualResponseBody )
129
145
require .NoError (t , err )
146
+ assert .Equal (t , tc .expectedResonseBodyJSON , recorder .Body .String ())
130
147
131
148
// Assert status code and body
132
149
assert .Equal (t , tc .expectedStatusCode , recorder .Code )
@@ -162,12 +179,12 @@ func TestGetTaskProtection(t *testing.T) {
162
179
testTaskProtectionRequest (t , taskMetadataFetchErrorCase (
163
180
state .NewErrorMetadataFetchFailure ("" ), metricName , nil ))
164
181
})
165
- t .Run ("task metadata uknown error" , func (t * testing.T ) {
182
+ t .Run ("task metadata unknown error" , func (t * testing.T ) {
166
183
testTaskProtectionRequest (t , taskMetadataFetchErrorCase (
167
184
errors .New ("unknown" ), metricName , nil ))
168
185
})
169
186
t .Run ("task role creds not found" , func (t * testing.T ) {
170
- testTaskProtectionRequest (t , taskRoleCredsNotFoundCase (metricName , nil ))
187
+ testTaskProtectionRequest (t , taskRoleCredsNotFoundCase (metricName , nil , noCredentialsErrorResponse ))
171
188
})
172
189
t .Run ("request failure" , func (t * testing.T ) {
173
190
ecsRequestID := "reqID"
@@ -198,6 +215,7 @@ func TestGetTaskProtection(t *testing.T) {
198
215
Message : ecsErrMessage ,
199
216
},
200
217
},
218
+ expectedResonseBodyJSON : fmt .Sprintf (requestFailureErrorResponse , ecsRequestID , taskARN , ecsErrMessage ),
201
219
})
202
220
})
203
221
t .Run ("agent timeout" , func (t * testing.T ) {
@@ -216,6 +234,7 @@ func TestGetTaskProtection(t *testing.T) {
216
234
Message : "Timed out calling ECS Task Protection API" ,
217
235
},
218
236
},
237
+ expectedResonseBodyJSON : timeoutErrorResponse ,
219
238
})
220
239
})
221
240
t .Run ("non-request-failure aws error" , func (t * testing.T ) {
@@ -234,6 +253,7 @@ func TestGetTaskProtection(t *testing.T) {
234
253
Message : ecsErrMessage ,
235
254
},
236
255
},
256
+ expectedResonseBodyJSON : fmt .Sprintf (nonRequestAWSErrorResponse , taskARN , ecsErrMessage ),
237
257
})
238
258
})
239
259
t .Run ("non-aws error" , func (t * testing.T ) {
@@ -249,6 +269,7 @@ func TestGetTaskProtection(t *testing.T) {
249
269
Arn : taskARN , Code : apierrors .ErrCodeServerException , Message : err .Error (),
250
270
},
251
271
},
272
+ expectedResonseBodyJSON : fmt .Sprintf (nonAWSErrorResponse , taskARN , err .Error ()),
252
273
})
253
274
})
254
275
t .Run ("ecs failure" , func (t * testing.T ) {
@@ -262,8 +283,9 @@ func TestGetTaskProtection(t *testing.T) {
262
283
setMetricsExpectations : metricsExpectations (metricName , 0 ),
263
284
expectedStatusCode : http .StatusOK ,
264
285
expectedResponseBody : types.TaskProtectionResponse {
265
- Failure : ecsFailure ,
286
+ Failure : & ecsFailure ,
266
287
},
288
+ expectedResonseBodyJSON : ecsErrorResponse ,
267
289
})
268
290
})
269
291
t .Run ("more than one ecs failure" , func (t * testing.T ) {
@@ -282,6 +304,7 @@ func TestGetTaskProtection(t *testing.T) {
282
304
Message : "Unexpected error occurred" ,
283
305
},
284
306
},
307
+ expectedResonseBodyJSON : multipleECSErrorResponse ,
285
308
})
286
309
})
287
310
t .Run ("happy case" , func (t * testing.T ) {
@@ -292,9 +315,10 @@ func TestGetTaskProtection(t *testing.T) {
292
315
setFactoryExpectations : factoryExpectations (happyECSInput , & ecs.GetTaskProtectionOutput {
293
316
ProtectedTasks : []ecstypes.ProtectedTask {protectedTask },
294
317
}, nil ),
295
- setMetricsExpectations : metricsExpectations (metricName , 1 ),
296
- expectedStatusCode : http .StatusOK ,
297
- expectedResponseBody : types.TaskProtectionResponse {Protection : protectedTask },
318
+ setMetricsExpectations : metricsExpectations (metricName , 1 ),
319
+ expectedStatusCode : http .StatusOK ,
320
+ expectedResponseBody : types.TaskProtectionResponse {Protection : & protectedTask },
321
+ expectedResonseBodyJSON : happyEnabledTaskProtectionResponse ,
298
322
})
299
323
})
300
324
}
@@ -351,9 +375,10 @@ func TestUpdateTaskProtection(t *testing.T) {
351
375
expectedResponseBody : types.TaskProtectionResponse {
352
376
Error : & types.ErrorResponse {
353
377
Code : apierrors .ErrCodeInvalidParameterException ,
354
- Message : "UpdateTaskProtection: failed to decode request" ,
378
+ Message : updateTaskProtectionDecodeError ,
355
379
},
356
380
},
381
+ expectedResonseBodyJSON : fmt .Sprintf (malformedRequestResponse , updateTaskProtectionDecodeError ),
357
382
})
358
383
})
359
384
t .Run ("invalid type in the request" , func (t * testing.T ) {
@@ -364,9 +389,10 @@ func TestUpdateTaskProtection(t *testing.T) {
364
389
expectedResponseBody : types.TaskProtectionResponse {
365
390
Error : & types.ErrorResponse {
366
391
Code : apierrors .ErrCodeInvalidParameterException ,
367
- Message : "UpdateTaskProtection: failed to decode request" ,
392
+ Message : updateTaskProtectionDecodeError ,
368
393
},
369
394
},
395
+ expectedResonseBodyJSON : fmt .Sprintf (malformedRequestResponse , updateTaskProtectionDecodeError ),
370
396
})
371
397
})
372
398
t .Run ("ProtectionEnabled field not found on the request" , func (t * testing.T ) {
@@ -386,10 +412,11 @@ func TestUpdateTaskProtection(t *testing.T) {
386
412
Message : "Invalid request: does not contain 'ProtectionEnabled' field" ,
387
413
},
388
414
},
415
+ expectedResonseBodyJSON : missingTaskProtectionFieldResponse ,
389
416
})
390
417
})
391
418
t .Run ("task role creds not found" , func (t * testing.T ) {
392
- testTaskProtectionRequest (t , taskRoleCredsNotFoundCase (metricName , happyRequestBody ))
419
+ testTaskProtectionRequest (t , taskRoleCredsNotFoundCase (metricName , happyRequestBody , noCredentialsErrorResponse ))
393
420
})
394
421
t .Run ("request failure" , func (t * testing.T ) {
395
422
ecsRequestID := "reqID"
@@ -421,6 +448,7 @@ func TestUpdateTaskProtection(t *testing.T) {
421
448
Message : ecsErrMessage ,
422
449
},
423
450
},
451
+ expectedResonseBodyJSON : fmt .Sprintf (requestFailureErrorResponse , ecsRequestID , taskARN , ecsErrMessage ),
424
452
})
425
453
})
426
454
t .Run ("agent timeout" , func (t * testing.T ) {
@@ -440,6 +468,7 @@ func TestUpdateTaskProtection(t *testing.T) {
440
468
Message : "Timed out calling ECS Task Protection API" ,
441
469
},
442
470
},
471
+ expectedResonseBodyJSON : timeoutErrorResponse ,
443
472
})
444
473
})
445
474
t .Run ("non-request-failure aws error" , func (t * testing.T ) {
@@ -459,6 +488,7 @@ func TestUpdateTaskProtection(t *testing.T) {
459
488
Message : ecsErrMessage ,
460
489
},
461
490
},
491
+ expectedResonseBodyJSON : fmt .Sprintf (nonRequestAWSErrorResponse , taskARN , ecsErrMessage ),
462
492
})
463
493
})
464
494
t .Run ("non-aws error" , func (t * testing.T ) {
@@ -475,6 +505,7 @@ func TestUpdateTaskProtection(t *testing.T) {
475
505
Arn : taskARN , Code : apierrors .ErrCodeServerException , Message : err .Error (),
476
506
},
477
507
},
508
+ expectedResonseBodyJSON : fmt .Sprintf (nonAWSErrorResponse , taskARN , err .Error ()),
478
509
})
479
510
})
480
511
t .Run ("ecs failure" , func (t * testing.T ) {
@@ -489,8 +520,9 @@ func TestUpdateTaskProtection(t *testing.T) {
489
520
setMetricsExpectations : metricsExpectations (metricName , 0 ),
490
521
expectedStatusCode : http .StatusOK ,
491
522
expectedResponseBody : types.TaskProtectionResponse {
492
- Failure : ecsFailure ,
523
+ Failure : & ecsFailure ,
493
524
},
525
+ expectedResonseBodyJSON : ecsErrorResponse ,
494
526
})
495
527
})
496
528
t .Run ("more than one ecs failure" , func (t * testing.T ) {
@@ -510,6 +542,7 @@ func TestUpdateTaskProtection(t *testing.T) {
510
542
Message : "Unexpected error occurred" ,
511
543
},
512
544
},
545
+ expectedResonseBodyJSON : multipleECSErrorResponse ,
513
546
})
514
547
})
515
548
t .Run ("happy case" , func (t * testing.T ) {
@@ -521,9 +554,10 @@ func TestUpdateTaskProtection(t *testing.T) {
521
554
setFactoryExpectations : factoryExpectations (happyECSInput , & ecs.UpdateTaskProtectionOutput {
522
555
ProtectedTasks : []ecstypes.ProtectedTask {protectedTask },
523
556
}, nil ),
524
- setMetricsExpectations : metricsExpectations (metricName , 1 ),
525
- expectedStatusCode : http .StatusOK ,
526
- expectedResponseBody : types.TaskProtectionResponse {Protection : protectedTask },
557
+ setMetricsExpectations : metricsExpectations (metricName , 1 ),
558
+ expectedStatusCode : http .StatusOK ,
559
+ expectedResponseBody : types.TaskProtectionResponse {Protection : & protectedTask },
560
+ expectedResonseBodyJSON : happyEnabledTaskProtectionResponse ,
527
561
})
528
562
})
529
563
}
@@ -582,6 +616,7 @@ func taskMetadataFetchErrorCase(err error, metricName string, reqBody interface{
582
616
Message : "Failed to find a task for the request" ,
583
617
},
584
618
},
619
+ expectedResonseBodyJSON : taskMetadataErrorResponse ,
585
620
}
586
621
}
587
622
@@ -604,11 +639,12 @@ func taskMetadataLookupFailureCase(metricName string, reqBody interface{}) TestC
604
639
Message : "Failed to find a task for the request" ,
605
640
},
606
641
},
642
+ expectedResonseBodyJSON : `{"error":{"Code":"ResourceNotFoundException","Message":"Failed to find a task for the request"}}` ,
607
643
}
608
644
}
609
645
610
646
// Creates a test case for Task Role credentials not found case.
611
- func taskRoleCredsNotFoundCase (metricName string , reqBody interface {}) TestCase {
647
+ func taskRoleCredsNotFoundCase (metricName string , reqBody interface {}, expectedResponseJSON string ) TestCase {
612
648
return TestCase {
613
649
setAgentStateExpectations : happyStateExpectations ,
614
650
setCredsManagerExpectations : func (credsManager * mock_credentials.MockManager ) {
@@ -625,6 +661,7 @@ func taskRoleCredsNotFoundCase(metricName string, reqBody interface{}) TestCase
625
661
Message : "Invalid Request: no task IAM role credentials available for task" ,
626
662
},
627
663
},
664
+ expectedResonseBodyJSON : expectedResponseJSON ,
628
665
}
629
666
}
630
667
0 commit comments