-
Notifications
You must be signed in to change notification settings - Fork 32
/
test.js
361 lines (335 loc) · 13.3 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
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
const fs = require('fs');
const should = require('should');
const async = require('async');
const sinon = require('sinon');
const Octokit = require('@octokit/rest');
const lib = require('./index.js');
const base64Build = 'eyJpZCI6IjE3NDBjZTJhLTYxZDktNGE1OC1iM2M3LWNmYWQ5OWRiOGQwYSIsInByb2plY3RJZCI6Im5vZGUtZXhhbXBsZS1na2UiLCJzdGF0dXMiOiJTVUNDRVNTIiwic291cmNlIjp7InJlcG9Tb3VyY2UiOnsicHJvamVjdElkIjoibm9kZ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y1jZmFkOTlkYjhkMGEiLCJzdWJzdGl0dXRpb25zIjp7IkJSQU5DSF9OQU1FIjoibWFzdGVyIiwiQ09NTUlUX1NIQSI6Im40ODk5MWI0YTdiNzQxOGEzNWQwMGVkZWQwMjhhZTFmYzA2YTRmYzc1IiwiUkVQT19OQU1FIjoibm9kZS1leGFtcGxlLWZyb250ZW5kIiwiUkVWSVNJT05fSUQiOiI0ODk5MWI0YTdiNzQxOGEzNWQwMGVkZWQwMjhhZTFmYzA2YTRmYzc1IiwiU0hPUlRfU0hBIjoiNDg5OTFiNGEiLCJUQUdfTkFNRSI6IiJ9LCJ0YWdzIjpbInRyaWdnZXItZGVmYXVsdC1naXRodWItY2hlY2tzIl19Cg==';
const nbCommonFields = 2;
const MS_PER_MINUTE = 60000;
describe('eventToBuild', () => {
it('should transform a base64 build to an object', () => {
const build = lib.eventToBuild(base64Build);
should.exist(build.projectId);
build.projectId.should.equal('node-example-gke');
});
});
describe('createSlackMessage', () => {
it('should create a slack message', async () => {
const build = {
id: 'build-id',
logUrl: 'https://logurl.com',
status: 'SUCCESS',
finishTime: '2017-03-19T00:08:12.220502Z',
source: {},
};
const message = await lib.createSlackMessage(build);
message.text.should.equal(`Build \`${build.id}\` finished`);
should.exist(message.attachments);
message.attachments.should.have.length(1);
const attachment = message.attachments[0];
attachment.title_link.should.equal(build.logUrl);
attachment.ts.should.equal(1489882092);
attachment.fields.should.have.length(nbCommonFields);
attachment.fields[0].value.should.equal(build.status);
});
it('should create a slack message saying the build started, with start timestamp if status WORKING', async () => {
const build = {
id: 'build-id',
logUrl: 'https://logurl.com',
status: 'WORKING',
startTime: '2017-03-19T00:08:12.220502Z',
finishTime: null,
source: {},
};
const message = await lib.createSlackMessage(build);
message.text.should.equal('Build `build-id` started');
should.exist(message.attachments);
message.attachments.should.have.length(1);
const attachment = message.attachments[0];
attachment.title_link.should.equal(build.logUrl);
attachment.ts.should.equal(1489882092);
attachment.fields.should.have.length(nbCommonFields - 1);
attachment.fields[0].value.should.equal(build.status);
});
it('should include the build duration as a field', async () => {
const now = Date.now();
const deltaInMinutes = 11;
const build = {
id: 'build-id',
logUrl: 'https://logurl.com',
status: 'SUCCESS',
startTime: new Date(now - deltaInMinutes * MS_PER_MINUTE),
finishTime: now,
source: {},
};
const message = await lib.createSlackMessage(build);
const attachment = message.attachments[0];
attachment.fields[1].value.should.equal(`${deltaInMinutes} minutes`);
});
it('should not include build duration as a field for start notifications', async () => {
const now = Date.now();
const deltaInMinutes = 11;
const build = {
id: 'build-id',
logUrl: 'https://logurl.com',
status: 'WORKING',
startTime: new Date(now - deltaInMinutes * MS_PER_MINUTE),
finishTime: null,
source: {},
};
const message = await lib.createSlackMessage(build);
const attachment = message.attachments[0];
attachment.fields.should.not.containEql({ title: 'Duration', value: `${deltaInMinutes} minutes` });
});
it('should create a slack message with images', async () => {
const build = {
id: 'build-id',
logUrl: 'https://logurl.com',
status: 'SUCCESS',
finishTime: Date.now(),
images: ['image-1', 'image-2'],
source: {},
};
const message = await lib.createSlackMessage(build);
const attachment = message.attachments[0];
attachment.fields.should.have.length(nbCommonFields + 1);
attachment.fields[nbCommonFields].value.should.equal('image-1\nimage-2');
});
it('should include the source info in the message', async () => {
const build = lib.eventToBuild(base64Build);
const message = await lib.createSlackMessage(build);
const attachment = message.attachments[0];
attachment.fields[2].value.should.equal('node-example-frontend');
attachment.fields[3].value.should.equal('master');
});
it('should include the commit author in the message if a github commit is retrieved', async () => {
const build = lib.eventToBuild(base64Build);
build.source.repoSource.repoName = 'github_artofwar_chapter1';
const octokit = new Octokit({
auth: 'token sjhfgsa',
});
const stub = sinon.stub(octokit.git, 'getCommit')
.returns({
data: {
author: {
name: 'Sun Tzu',
},
},
});
const githubCommit = await lib.getGithubCommit(build, octokit);
const message = await lib.createSlackMessage(build, githubCommit);
const attachment = message.attachments[0];
attachment.fields[4].value.should.equal('Sun Tzu');
stub.reset();
});
it('should include the source info in the message, for default trigger', async () => {
const build = lib.eventToBuild(base64BuildDefaultTrigger);
const message = await lib.createSlackMessage(build);
const attachment = message.attachments[0];
attachment.fields[2].value.should.equal('node-example-frontend');
attachment.fields[3].value.should.equal('master');
});
it('should use the right color depending on the status', () => {
const build = {
id: 'build-id',
finishTime: Date.now(),
};
const testCases = [
{
status: 'QUEUED',
want: '#4285F4',
},
{
status: 'WORKING',
want: '#4285F4',
},
{
status: 'SUCCESS',
want: '#34A853',
},
{
status: 'FAILURE',
want: '#EA4335',
},
{
status: 'INTERNAL_ERROR',
want: '#EA4335',
},
{
status: 'TIMEOUT',
want: '#FBBC05',
},
];
testCases.forEach(async (tc) => {
build.status = tc.status;
const message = await lib.createSlackMessage(build);
message.attachments[0].color.should.equal(tc.want, tc.status);
});
});
});
function cleanConfig(callback) {
const config = {
SLACK_WEBHOOK_URL: 'https://hooks.slack.com/services/XXX',
};
fs.writeFile('config.json', JSON.stringify(config), 'utf8', callback);
}
describe('subscribe', () => {
beforeEach(cleanConfig);
afterEach(cleanConfig);
beforeEach(() => {
this.webhookCalled = false;
lib.webhook.send = (message) => {
this.webhookCalled = true;
this.message = message;
};
});
it('should subscribe to pubsub message and send a slack message', async () => {
const event = {
data: base64Build,
};
await lib.subscribe(event);
this.webhookCalled.should.be.true();
});
it('should not send a message for non final status (by default)', () => {
const testCases = [
{
status: 'QUEUED',
want: false,
},
{
status: 'WORKING',
want: false,
},
{
status: 'SUCCESS',
want: true,
},
{
status: 'FAILURE',
want: true,
},
{
status: 'INTERNAL_ERROR',
want: true,
},
{
status: 'TIMEOUT',
want: true,
},
];
async.forEach(testCases, async (tc, doneEach) => {
this.webhookCalled = false;
const event = {
data: Buffer.from(JSON.stringify({
status: tc.status,
})).toString('base64'),
};
lib.subscribe(event, () => {
this.webhookCalled.should.equal(tc.want);
doneEach();
});
});
});
it('should send a message only for specified status', () => {
lib.status = ['FAILURE', 'INTERNAL_ERROR'];
const testCases = [
{
status: 'QUEUED',
want: false,
},
{
status: 'WORKING',
want: false,
},
{
status: 'SUCCESS',
want: false,
},
{
status: 'FAILURE',
want: true,
},
{
status: 'INTERNAL_ERROR',
want: true,
},
{
status: 'TIMEOUT',
want: false,
},
];
async.forEach(testCases, async (tc, doneEach) => {
this.webhookCalled = false;
const event = {
data: Buffer.from(JSON.stringify({
status: tc.status,
})).toString('base64'),
};
lib.subscribe(event, () => {
this.webhookCalled.should.equal(tc.want, tc.status);
doneEach();
});
}, () => {
// clean the status list.
lib.GC_SLACK_STATUS = null;
});
});
it('should send a message at start of build if WORKING is in status', () => {
lib.status = ['WORKING', 'SUCCESS', 'FAILURE', 'TIMEOUT', 'INTERNAL_ERROR'];
const testCases = [
{
status: 'QUEUED',
want: false,
},
{
status: 'WORKING',
want: true,
},
{
status: 'SUCCESS',
want: true,
},
{
status: 'FAILURE',
want: true,
},
{
status: 'INTERNAL_ERROR',
want: true,
},
{
status: 'TIMEOUT',
want: true,
},
];
async.forEach(testCases, async (tc, doneEach) => {
this.webhookCalled = false;
const event = {
data: Buffer.from(JSON.stringify({
status: tc.status,
})).toString('base64'),
};
lib.subscribe(event, () => {
this.webhookCalled.should.equal(tc.want, tc.status);
doneEach();
});
}, () => {
// clean the status list.
lib.GC_SLACK_STATUS = null;
});
});
it('octokit should be defined if token is set', async () => {
const event = {
data: base64Build,
};
await lib.subscribe(event, 'kuhasdkasjhd');
this.webhookCalled.should.be.true();
});
it('should send error when something goes wrong', async () => {
await lib.subscribe('testError');
this.webhookCalled.should.be.true();
this.message.should.containEql('Error');
});
});