forked from researchsquare/run-ecs-task-action
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.test.js
175 lines (154 loc) · 6.42 KB
/
index.test.js
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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
const run = require('.');
const core = require('@actions/core');
jest.mock('@actions/core');
const mockEcsRunTask = jest.fn();
const mockEcsDescribeTaskDefinition = jest.fn();
const mockEcsDescribeTasks = jest.fn();
const mockEcsWaiter = jest.fn();
jest.mock('aws-sdk', () => {
return {
config: {
region: 'fake-region'
},
ECS: jest.fn(() => ({
describeTaskDefinition: mockEcsDescribeTaskDefinition,
describeTasks: mockEcsDescribeTasks,
runTask: mockEcsRunTask,
waitFor: mockEcsWaiter
})),
};
});
describe('Deploy to ECS', () => {
beforeEach(() => {
jest.clearAllMocks();
core.getInput = jest
.fn()
.mockReturnValueOnce('task-definition-arn') // task-definition-arn
.mockReturnValueOnce('cluster-789') // cluster
.mockReturnValueOnce('1'); // count
process.env = Object.assign(process.env, { GITHUB_WORKSPACE: __dirname });
mockEcsDescribeTaskDefinition.mockImplementation(() => {
return {
promise() {
return Promise.resolve({
taskDefinition: {
taskDefinitionArn: "task:def:arn"
}
});
}
};
});
mockEcsDescribeTasks.mockImplementation(() => {
return {
promise() {
return Promise.resolve({
failures: [],
tasks: [
{
containers: [
{
lastStatus: "RUNNING",
exitCode: 0,
reason: '',
taskArn: "arn:aws:ecs:fake-region:account_id:task/arn"
}
],
desiredStatus: "RUNNING",
lastStatus: "RUNNING",
taskArn: "arn:aws:ecs:fake-region:account_id:task/arn"
}
]
});
}
};
});
mockEcsRunTask.mockImplementation(() => {
return {
promise() {
return Promise.resolve({
failures: [],
tasks: [
{
containers: [
{
lastStatus: "RUNNING",
exitCode: 0,
reason: '',
taskArn: "arn:aws:ecs:fake-region:account_id:task/arn"
}
],
desiredStatus: "RUNNING",
lastStatus: "RUNNING",
taskArn: "arn:aws:ecs:fake-region:account_id:task/arn"
// taskDefinitionArn: "arn:aws:ecs:<region>:<aws_account_id>:task-definition/amazon-ecs-sample:1"
}
]
});
}
};
});
mockEcsWaiter.mockImplementation(() => {
return {
promise() {
return Promise.resolve({});
}
};
});
});
test('registers the task definition contents and runs the task', async () => {
await run();
expect(core.setFailed).toHaveBeenCalledTimes(0);
expect(core.setOutput).toHaveBeenNthCalledWith(1, 'task-definition-arn', 'task:def:arn');
expect(mockEcsRunTask).toHaveBeenNthCalledWith(1, {
cluster: 'cluster-789',
taskDefinition: 'task:def:arn',
count: 1,
networkConfiguration: {},
overrides: {}
});
expect(mockEcsWaiter).toHaveBeenCalledTimes(0);
expect(core.setOutput).toBeCalledWith('task-arn', ['arn:aws:ecs:fake-region:account_id:task/arn']);
});
test('registers the task definition contents and waits for tasks to finish successfully', async () => {
core.getInput = jest
.fn()
.mockReturnValueOnce('task-definition-arn') // task-definition
.mockReturnValueOnce('cluster-789') // cluster
.mockReturnValueOnce('1') // count
.mockReturnValueOnce("") // override
.mockReturnValueOnce("") // config?
.mockReturnValueOnce('true'); // wait-for-finish
await run();
expect(core.setFailed).toHaveBeenCalledTimes(0);
expect(core.setOutput).toHaveBeenNthCalledWith(1, 'task-definition-arn', 'task:def:arn');
expect(mockEcsWaiter).toHaveBeenCalledTimes(1);
expect(core.info).toBeCalledWith("All tasks have exited successfully.");
});
test('defaults to the default cluster', async () => {
core.getInput = jest
.fn()
.mockReturnValueOnce('task-definition-arn'); // task-definition
await run();
expect(core.setFailed).toHaveBeenCalledTimes(0);
expect(core.setOutput).toHaveBeenNthCalledWith(1, 'task-definition-arn', 'task:def:arn');
expect(mockEcsRunTask).toHaveBeenNthCalledWith(1, {
cluster: 'default',
taskDefinition: 'task:def:arn',
count: 1,
networkConfiguration: {},
overrides: {}
});
});
test('error is caught if task def registration fails', async () => {
core.getInput = jest
.fn()
.mockReturnValueOnce('task-definition-arn'); // task-definition
mockEcsDescribeTaskDefinition.mockImplementation(() => {
throw new Error("Could not parse");
});
await run();
expect(core.setFailed).toHaveBeenCalledTimes(2);
expect(core.setFailed).toHaveBeenNthCalledWith(1, 'Failed to describe task definition in ECS: Could not parse');
expect(core.setFailed).toHaveBeenNthCalledWith(2, 'Could not parse');
});
});