/
lifecycle.go
95 lines (81 loc) · 2.36 KB
/
lifecycle.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
package taskmock
import (
"fmt"
"path"
"path/filepath"
"sync"
"time"
"ecsdeployer.com/ecsdeployer/internal/testutil"
"github.com/webdestroya/awsmocker"
)
type lifecycle struct {
taskId string
options *Options
callCount int
mu sync.Mutex
muStopped sync.Once
_stoppedAt int64
}
func (lc *lifecycle) stoppedAt() int64 {
lc.muStopped.Do(func() {
lc._stoppedAt = time.Now().UTC().Unix()
})
return lc._stoppedAt
}
func (lc *lifecycle) describeTaskMock() *awsmocker.MockedEndpoint {
runningCallStart := lc.options.PendingCount
stoppedCallStart := runningCallStart + lc.options.RunningCount
return &awsmocker.MockedEndpoint{
Request: describeTaskRequest(lc.taskId),
Response: &awsmocker.MockedResponse{
Body: func(rr *awsmocker.ReceivedRequest) string {
lc.mu.Lock()
lc.callCount++
defer lc.mu.Unlock()
clusterName := path.Base(testutil.JmesPathSearch(rr.JsonPayload, "cluster").(string))
taskArn := testutil.JmesPathSearch(rr.JsonPayload, "tasks[0]").(string)
clusterArn := fmt.Sprintf("arn:aws:ecs:%s:%s:cluster/%s", rr.Region, awsmocker.DefaultAccountId, clusterName)
taskResult := map[string]any{
"taskArn": taskArn,
"clusterArn": clusterArn,
"desiredStatus": "RUNNING",
"lastStatus": "PENDING",
}
switch {
case lc.callCount > stoppedCallStart:
taskResult["lastStatus"] = "STOPPED"
taskResult["desiredStatus"] = "STOPPED"
taskResult["stoppedAt"] = lc.stoppedAt()
taskResult["stopCode"] = lc.options.StopReason
taskResult["stoppedReason"] = "aws stoppedReason here"
taskResult["containers"] = []interface{}{
map[string]interface{}{
"name": "primary",
"exitCode": lc.options.ExitCode,
},
}
case lc.callCount > runningCallStart:
taskResult["lastStatus"] = "RUNNING"
}
return jsonify(map[string]interface{}{
"failures": []interface{}{},
"tasks": []interface{}{taskResult},
})
},
},
}
}
func describeTaskRequest(taskId string) *awsmocker.MockedRequest {
return &awsmocker.MockedRequest{
Service: "ecs",
Action: "DescribeTasks",
Matcher: func(rr *awsmocker.ReceivedRequest) bool {
taskArnRaw := testutil.JmesSearchOrNil(rr.JsonPayload, "tasks[0]")
if taskArnRaw == nil {
return false
}
taskArn := taskArnRaw.(string)
return taskId == filepath.Base(taskArn)
},
}
}