-
Notifications
You must be signed in to change notification settings - Fork 12
/
test.js
238 lines (199 loc) · 7.91 KB
/
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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
'use strict'
const expect = require('expect.js');
const {
ECS,
DescribeServicesCommand,
ListTaskDefinitionsCommand,
DescribeTaskDefinitionCommand,
RegisterTaskDefinitionCommand,
UpdateServiceCommand
} = require('@aws-sdk/client-ecs');
const { mockClient } = require('aws-sdk-client-mock');
const updater = require('./');
describe('ECS Service Image Updater', function () {
const ecsMock = mockClient(ECS);
const oldGetServiceTaskDefinition = updater.getServiceTaskDefinition;
const oldGetLatestActiveTaskDefinition = updater.getLatestActiveTaskDefinition;
const oldGetTaskDefinition = updater.getTaskDefinition;
afterEach(() => {
ecsMock.reset();
updater.getServiceTaskDefinition = oldGetServiceTaskDefinition;
updater.getLatestActiveTaskDefinition = oldGetLatestActiveTaskDefinition;
updater.getTaskDefinition = oldGetTaskDefinition;
});
it('currentTaskDefinition should return the current task definition from in Service', function (done) {
const serviceName = 'planet-express';
const taskDefinitionArn = 'arn::good-news:96';
updater.getServiceTaskDefinition = () => Promise.resolve(taskDefinitionArn);
updater.getTaskDefinition = () => Promise.resolve({ taskDefinitionArn: taskDefinitionArn });
updater.currentTaskDefinition({ serviceName: serviceName })
.then((taskDefinition) => {
expect(taskDefinition.taskDefinitionArn).to.equal(taskDefinitionArn);
done();
});
});
it('currentTaskDefinition should return the current task definition in a Task Definition Family', function (done) {
const family = 'simpsons';
const taskDefinitionArn = 'arn::good-news:96';
updater.getLatestActiveTaskDefinition = () => Promise.resolve(taskDefinitionArn);
updater.getTaskDefinition = _taskDefinitionArnSupplied => Promise.resolve({ taskDefinitionArn: taskDefinitionArn });
updater.currentTaskDefinition({ taskDefinitionFamily: family })
.then((taskDefinition) => {
expect(taskDefinition.taskDefinitionArn).to.equal(taskDefinitionArn);
done();
});
});
it('getServiceTaskDefinition should get the active task definition in Service', function (done) {
const serviceName = 'planet-express';
const taskDefinitionArn = 'arn::good-news:96';
ecsMock.on(DescribeServicesCommand).callsFake((params) => {
expect(params.services).to.eql([serviceName]);
const data = {
services: [
{ serviceName: '1', taskDefinition: 'arn' },
{ serviceName: serviceName, taskDefinition: taskDefinitionArn }
]
}
return Promise.resolve(data);
});
updater.getServiceTaskDefinition({ serviceName: serviceName })
.then((taskDefinitionArnReturned) => {
expect(taskDefinitionArnReturned).to.equal(taskDefinitionArn);
done();
});
});
it('getLatestActiveTaskDefinition should get the latest task definition in a Task Definition Family', function (done) {
const family = 'simpsons';
ecsMock.on(ListTaskDefinitionsCommand).callsFake((params) => {
expect(params).to.eql({
familyPrefix: family,
sort: 'DESC',
status: 'ACTIVE'
});
const data = {
taskDefinitionArns: [
"arn:2",
"arn:1",
]
}
return Promise.resolve(data);
});
updater.getLatestActiveTaskDefinition({ taskDefinitionFamily: family })
.then((taskDefinitionArnReturned) => {
expect(taskDefinitionArnReturned).to.equal("arn:2");
done();
});
});
it('getTaskDefinition should return a task definition', function (done) {
const taskDefinitionArn = 'arn::good-news:96';
ecsMock.on(DescribeTaskDefinitionCommand).callsFake((params) => {
expect(params.taskDefinition).to.equal(taskDefinitionArn);
return Promise.resolve({ taskDefinition: { taskDefinitionArn: taskDefinitionArn } });
});
updater.getTaskDefinition(taskDefinitionArn)
.then((taskDefinition) => {
expect(taskDefinition.taskDefinitionArn).to.equal(taskDefinitionArn);
done();
});
});
it('updateTaskDefinitionImage should update a task definition with a new image', function () {
const container = 'app';
const image = 'image:2';
const taskDefinition = {
taskDefinitionArn: 'arn',
executionRoleArn: 'arn:role',
containerDefinitions: [
{
name: container,
image: 'image:1'
}
]
};
const updatedTaskDefinition = updater.updateTaskDefinitionImage(taskDefinition, container, image);
expect(updatedTaskDefinition['containerDefinitions'][0]['image']).to.equal(image);
expect(updatedTaskDefinition['executionRoleArn']).to.equal(taskDefinition.executionRoleArn);
});
it('updateTaskDefinitionImage should update a task definition with a new image in multiple containers', function () {
const oldImage = 'image:1';
const newImage = 'image:2';
const taskDefinition = {
taskDefinitionArn: 'arn',
containerDefinitions: [
{
name: 'app',
image: oldImage
},
{
name: 'worker',
image: oldImage
},
{
name: 'db',
image: oldImage
}
]
};
const updatedTaskDefinition = updater.updateTaskDefinitionImage(taskDefinition, ['app', 'worker'], newImage);
expect(updatedTaskDefinition['containerDefinitions'][0]['image']).to.equal(newImage);
expect(updatedTaskDefinition['containerDefinitions'][1]['image']).to.equal(newImage);
expect(updatedTaskDefinition['containerDefinitions'][2]['image']).to.equal(oldImage);
});
it('updateService should update Service to use new Task Definition', function (done) {
ecsMock.on(UpdateServiceCommand).callsFake((params) => {
expect(params).to.eql({
cluster: 'arn:cluster',
service: 'serviceName',
taskDefinition: 'arn:taskDefinition'
});
return Promise.resolve({ 'service': { serviceName: 'serviceName' } });
});
const options = {
clusterArn: 'arn:cluster',
serviceName: 'serviceName',
};
updater.updateService(options, 'arn:taskDefinition')
.then((service) => {
expect(service).to.eql({ serviceName: 'serviceName' });
done();
});
});
describe('Wrap up', function () {
const oldCurrentTaskDefinitionFn = updater.currentTaskDefinition;
const oldUpdateTaskDefinitionImageFn = updater.updateTaskDefinitionImage;
const oldUpdateServiceFn = updater.updateService;
const options = {
clusterArn: 'arn:cluster',
serviceName: 'serviceName',
containerNames: ['containerName'],
image: 'image:1'
};
after(() => {
updater.currentTaskDefinition = oldCurrentTaskDefinitionFn;
updater.updateTaskDefinitionImage = oldUpdateTaskDefinitionImageFn;
updater.updateService = oldUpdateServiceFn;
});
it('should do it all more good', function (done) {
updater.currentTaskDefinition = (optionsSupplied) => {
expect(optionsSupplied).to.eql(options);
return Promise.resolve({ taskDefinitionArn: 'arn' });
};
updater.updateTaskDefinitionImage = function (taskDefinition, containerName, image) {
expect(taskDefinition.taskDefinitionArn).to.equal('arn');
expect(containerName).to.eql(['containerName']);
expect(image).to.equal('image:1');
return { taskDefinitionArn: 'arn:updated' };
};
ecsMock.on(RegisterTaskDefinitionCommand).callsFake((taskDefinition) => {
expect(taskDefinition.taskDefinitionArn).to.equal('arn:updated');
return Promise.resolve({ taskDefinition: { taskDefinitionArn: 'arn:created' } });
});
updater.updateService = (optionsSupplied, taskDefinitionArn) => {
expect(optionsSupplied).to.eql(options);
expect(taskDefinitionArn).to.equal('arn:created');
return Promise.resolve({ taskDefinition: 'arn:created' });
}
updater(options)
.then(() => done());
});
});
});