-
Notifications
You must be signed in to change notification settings - Fork 34
/
Copy pathtest.js
executable file
·397 lines (373 loc) · 11.9 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
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
#!/usr/bin/env node
import { fileURLToPath } from 'node:url';
import { dirname, join } from 'node:path';
import { cd, $ as zx, retry, expBackoff } from 'zx';
import { request } from 'undici';
import { compareDownstreamResponse } from './compare-downstream-response.js';
import { argv } from 'node:process';
import { existsSync } from 'node:fs';
import { copyFile, readFile, writeFile } from 'node:fs/promises';
import core from '@actions/core';
import TOML from '@iarna/toml';
async function killPortProcess(port) {
zx.verbose = false;
const pids = (await zx`lsof -ti:${port}`).stdout;
if (pids) {
for (const pid of pids.split('\n').reverse()) {
if (pid && pid != process.pid) {
await zx`kill -15 ${pid}`;
}
}
}
}
const startTime = Date.now();
const __dirname = dirname(fileURLToPath(import.meta.url));
async function sleep(seconds) {
return new Promise((resolve) => {
setTimeout(resolve, 1_000 * seconds);
});
}
let args = argv.slice(2);
const local = args.includes('--local');
const verbose = args.includes('--verbose');
const moduleMode = args.includes('--module-mode');
const aot = args.includes('--aot');
const debugBuild = args.includes('--debug-build');
const filter = args.filter((arg) => !arg.startsWith('--'));
async function $(...args) {
return await retry(10, () => zx(...args));
}
if (!local && process.env.FASTLY_API_TOKEN === undefined) {
try {
zx.verbose = false;
process.env.FASTLY_API_TOKEN = String(
await zx`fastly profile token --quiet`,
).trim();
} catch {
console.error(
'No environment variable named FASTLY_API_TOKEN has been set and no default fastly profile exists.',
);
console.error(
'In order to run the tests, either create a fastly profile using `fastly profile create` or export a fastly token under the name FASTLY_API_TOKEN',
);
process.exit(1);
}
}
const FASTLY_API_TOKEN = process.env.FASTLY_API_TOKEN;
zx.verbose = true;
const branchName = (await zx`git branch --show-current`).stdout
.trim()
.replace(/[^a-zA-Z0-9_-]/g, '_');
const fixture = moduleMode ? 'module-mode' : 'app';
const serviceName = `${fixture}--${branchName}${aot ? '--aot' : ''}${process.env.SUFFIX_STRING || ''}`;
let domain;
const fixturePath = join(__dirname, 'fixtures', fixture);
let localServer;
await cd(fixturePath);
await copyFile(
join(fixturePath, 'fastly.toml.in'),
join(fixturePath, 'fastly.toml'),
);
const config = TOML.parse(
await readFile(join(fixturePath, 'fastly.toml'), 'utf-8'),
);
config.name = serviceName;
if (aot) {
const buildArgs = config.scripts.build.split(' ');
buildArgs.splice(-1, null, '--enable-aot');
config.scripts.build = buildArgs.join(' ');
}
if (debugBuild) {
const buildArgs = config.scripts.build.split(' ');
buildArgs.splice(-1, null, '--debug-build');
config.scripts.build = buildArgs.join(' ');
}
await writeFile(
join(fixturePath, 'fastly.toml'),
TOML.stringify(config),
'utf-8',
);
if (!local) {
core.startGroup('Delete service if already exists');
try {
await zx`fastly service delete --quiet --service-name "${serviceName}" --force --token $FASTLY_API_TOKEN`;
} catch {}
core.endGroup();
core.startGroup('Build and deploy service');
await zx`npm i`;
await $`fastly compute publish -i --quiet --token $FASTLY_API_TOKEN --status-check-off`;
core.endGroup();
// get the public domain of the deployed application
domain =
'https://' +
JSON.parse(await $`fastly domain list --quiet --version latest --json`)[0]
.Name;
core.notice(`Service is running on ${domain}`);
const setupPath = join(fixturePath, 'setup.js');
if (existsSync(setupPath)) {
core.startGroup('Extra set-up steps for the service');
await zx`node ${setupPath} ${serviceName}`;
await sleep(60);
core.endGroup();
}
} else {
localServer = zx`fastly compute serve --verbose --viceroy-args="${verbose ? '-vv' : ''}"`;
domain = 'http://127.0.0.1:7676';
}
core.startGroup(`Check service is up and running on ${domain}`);
await retry(
27,
local
? [
// we expect it to take ~10 seconds to deploy, so focus on that time
6000, 3000, 1500, 500, 500, 500, 500, 500, 500, 500, 500, 500, 500, 500,
500, 500, 500, 500, 500, 500, 500, 500,
// after more than 20 seconds, means we have an unusually slow build, start backoff before timeout
1500,
3000, 6000, 12000, 24000,
].values()
: expBackoff('60s', '10s'),
async () => {
const response = await request(domain);
if (response.statusCode !== 200) {
throw new Error(
`Application "${fixture}" :: Not yet available on domain: ${domain}`,
);
}
},
);
core.endGroup();
let { default: tests } = await import(join(fixturePath, 'tests.json'), {
with: { type: 'json' },
});
core.startGroup('Running tests');
function chunks(arr, size) {
const output = [];
for (let i = 0; i < arr.length; i += size) {
output.push(arr.slice(i, i + size));
}
return output;
}
let results = [];
for (const chunk of chunks(Object.entries(tests), 100)) {
results.push(
...(await Promise.allSettled(
chunk.map(async ([title, test]) => {
// basic test filtering
if (!title.includes(filter)) {
return {
title,
test,
skipped: true,
};
}
async function getBodyChunks(response) {
const bodyChunks = [];
let downstreamTimeout;
await Promise.race([
(async () => {
// This body_streaming property allows us to test different cases
// of consumer streamining behaviours.
switch (test.body_streaming) {
case 'first-chunk-only':
for await (const chunk of response.body) {
bodyChunks.push(chunk);
response.body.on('error', () => {});
break;
}
break;
case 'none':
response.body.on('error', () => {});
break;
case 'full':
default:
for await (const chunk of response.body) {
bodyChunks.push(chunk);
}
}
})(),
new Promise((_, reject) => {
downstreamTimeout = setTimeout(() => {
reject(
new Error(`Test downstream response body chunk timeout`),
);
}, 30_000);
}),
]);
clearTimeout(downstreamTimeout);
return bodyChunks;
}
// default test options
if (!test.downstream_request) {
const [method, pathname, extra] = title.split(' ');
if (typeof extra === 'string')
throw new Error('Cannot infer downstream_request from title');
test.downstream_request = { method, pathname };
}
if (!test.downstream_response) {
test.downstream_response = {
status: 200,
};
}
if (!test.environments) {
test.environments = ['viceroy', 'compute'];
}
if (local) {
if (test.environments.includes('viceroy')) {
let path = test.downstream_request.pathname;
let url = `${domain}${path}`;
try {
const response = await request(url, {
method: test.downstream_request.method || 'GET',
headers: test.downstream_request.headers || undefined,
body: test.downstream_request.body || undefined,
});
const bodyChunks = await getBodyChunks(response);
await compareDownstreamResponse(
test.downstream_response,
response,
bodyChunks,
);
return {
title,
test,
skipped: false,
};
} catch (error) {
throw new Error(`${title} ${error.message}`, { cause: error });
}
} else {
return {
title,
test,
skipped: true,
};
}
} else {
if (test.environments.includes('compute')) {
// TODO: this just hides flakes, so we should remove this and fix the flakes.
return retry(10, expBackoff('60s', '10s'), async () => {
let path = test.downstream_request.pathname;
let url = `${domain}${path}`;
try {
const response = await request(url, {
method: test.downstream_request.method || 'GET',
headers: test.downstream_request.headers || undefined,
body: test.downstream_request.body || undefined,
});
const bodyChunks = await getBodyChunks(response);
await compareDownstreamResponse(
test.downstream_response,
response,
bodyChunks,
);
return {
title,
test,
skipped: false,
};
} catch (error) {
throw new Error(`${title} ${error.message}`);
}
});
} else {
return {
title,
test,
skipped: true,
};
}
}
}),
)),
);
}
core.endGroup();
console.log('Test results');
core.startGroup('Test results');
let passed = 0;
const failed = [];
const green = '\u001b[32m';
const red = '\u001b[31m';
const reset = '\u001b[0m';
const white = '\u001b[39m';
const info = '\u2139';
const tick = '\u2714';
const cross = '\u2716';
for (const result of results) {
if (result.status === 'fulfilled') {
passed += 1;
if (result.value.skipped) {
if (!result.value.title.includes(filter)) {
// console.log(white, info, `Skipped by test filter: ${result.value.title}`, reset);
} else if (local && !result.value.test.environments.includes('viceroy')) {
console.log(
white,
info,
`Skipped as test marked to only run on Fastly Compute: ${result.value.title}`,
reset,
);
} else if (
!local &&
!result.value.test.environments.includes('compute')
) {
console.log(
white,
info,
`Skipped as test marked to only run on local server: ${result.value.title}`,
reset,
);
} else {
console.log(
white,
info,
`Skipped due to no environments set: ${result.value.title}`,
reset,
);
}
} else {
console.log(green, tick, result.value.title, reset);
}
} else {
console.log(red, cross, result.reason, reset);
failed.push(result.reason);
}
}
core.endGroup();
if (failed.length) {
process.exitCode = 1;
core.startGroup('Failed tests');
for (const result of failed) {
console.log(red, cross, result, reset);
}
core.endGroup();
}
if (!local && failed.length) {
core.notice(`Tests failed, the service is named "${serviceName}"`);
if (domain) {
core.notice(`You can debug the service on ${domain}`);
}
}
if (!local && !failed.length) {
const teardownPath = join(fixturePath, 'teardown.js');
if (existsSync(teardownPath)) {
core.startGroup('Tear down the extra set-up for the service');
await zx`${teardownPath}`;
core.endGroup();
}
core.startGroup('Delete service');
// Delete the service now the tests have finished
await zx`fastly service delete --quiet --service-name "${serviceName}" --force --token $FASTLY_API_TOKEN`;
core.endGroup();
}
if (process.exitCode == undefined || process.exitCode == 0) {
console.log(
`All tests passed! Took ${(Date.now() - startTime) / 1000} seconds to complete`,
);
} else {
console.log(`Tests failed!`);
}
if (localServer) {
await killPortProcess(7676);
}
process.exit();