Skip to content

Commit bfe1339

Browse files
ci: fix flaky tests (#12836)
1 parent b5425e1 commit bfe1339

File tree

5 files changed

+38
-13
lines changed

5 files changed

+38
-13
lines changed

t/APISIX.pm

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -380,9 +380,10 @@ _EOC_
380380
location /exec_request {
381381
content_by_lua_block {
382382
local shell = require("resty.shell")
383-
local ok, stdout, stderr, reason, status = shell.run([[ $exec_snippet ]], $stdin, @{[$timeout*1000]}, $max_size)
383+
-- timeout one second before the actual timeout to allow shell.run to finish and collect the stdout/stderr
384+
local ok, stdout, stderr, reason, status = shell.run([[ $exec_snippet ]], $stdin, @{[($timeout-1)*1000]}, $max_size)
384385
if not ok then
385-
ngx.log(ngx.WARN, "failed to execute the script with status: " .. status .. ", reason: " .. reason .. ", stderr: " .. stderr)
386+
ngx.log(ngx.WARN, "failed to execute the script with status: " .. (status or "nil ") .. ", reason: " .. (reason or "nil ") .. ", stderr: " .. (stderr or "nil "))
386387
ngx.print("stdout: ", stdout)
387388
ngx.print("stderr: ", stderr)
388389
return

t/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
"private": true,
44
"type": "module",
55
"scripts": {
6-
"test": "NODE_OPTIONS=--experimental-vm-modules jest"
6+
"test": "NODE_OPTIONS=--experimental-vm-modules jest --verbose"
77
},
88
"devDependencies": {
99
"@jest/globals": "^29.7.0",

t/plugin/lago.spec.mts

Lines changed: 32 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ import { existsSync } from 'node:fs';
1919
import { readFile, rm, writeFile } from 'node:fs/promises';
2020
import { promisify } from 'node:util';
2121

22-
import { afterAll, beforeAll, describe, expect, it } from '@jest/globals';
22+
import { afterAll, beforeAll, describe, expect, it, jest } from '@jest/globals';
2323
import axios from 'axios';
2424
import * as compose from 'docker-compose';
2525
import { gql, request } from 'graphql-request';
@@ -69,6 +69,7 @@ const launchLago = async () => {
6969
cwd: LAGO_PATH,
7070
log: true,
7171
env: {
72+
...process.env,
7273
LAGO_RSA_PRIVATE_KEY: Buffer.from(privateKey).toString('base64'),
7374
FRONT_PORT: `${LAGO_FRONT_PORT}`, // avoiding conflicts, tests do not require a front-end
7475
API_PORT: `${LAGO_API_PORT}`,
@@ -227,24 +228,31 @@ describe('Plugin - Lago', () => {
227228

228229
// set up
229230
beforeAll(async () => {
231+
console.log(`[${new Date().toLocaleTimeString()}] starting suite`)
230232
if (existsSync(LAGO_PATH)) await rm(LAGO_PATH, { recursive: true });
233+
console.log(`[${new Date().toLocaleTimeString()}] download compose file`)
231234
await downloadComposeFile();
235+
console.log(`[${new Date().toLocaleTimeString()}] launch lago`)
232236
await launchLago();
237+
console.log(`[${new Date().toLocaleTimeString()}] provision lago`)
233238
let res = await provisionLago();
234239
restAPIKey = res.apiKey;
235240
lagoClient = res.client;
236241
}, 120 * 1000);
237242

238243
// clean up
239244
afterAll(async () => {
245+
console.log(`[${new Date().toLocaleTimeString()}] cleaning up`)
240246
await compose.downAll({
241247
cwd: LAGO_PATH,
242248
commandOptions: ['--volumes'],
243249
});
250+
console.log(`[${new Date().toLocaleTimeString()}] cleaned up`)
244251
await rm(LAGO_PATH, { recursive: true });
245252
}, 30 * 1000);
246253

247254
it('should create route', async () => {
255+
console.log(`[${new Date().toLocaleTimeString()}] creating route`)
248256
await expect(
249257
requestAdminAPI('/apisix/admin/routes/1', 'PUT', {
250258
uri: '/hello',
@@ -269,6 +277,7 @@ describe('Plugin - Lago', () => {
269277
}),
270278
).resolves.not.toThrow();
271279

280+
console.log(`[${new Date().toLocaleTimeString()}] creating second route`)
272281
await expect(
273282
requestAdminAPI('/apisix/admin/routes/2', 'PUT', {
274283
uri: '/hello1',
@@ -293,53 +302,66 @@ describe('Plugin - Lago', () => {
293302
},
294303
}),
295304
).resolves.not.toThrow();
296-
});
305+
console.log(`[${new Date().toLocaleTimeString()}] created routes`)
306+
}, 5 * 1000);
297307

298-
it('should create consumer', async () =>
308+
it('should create consumer', async () => {
309+
console.log(`[${new Date().toLocaleTimeString()}] creating consumer`)
299310
expect(
300311
requestAdminAPI(`/apisix/admin/consumers/${JACK_USERNAME}`, 'PUT', {
301312
username: JACK_USERNAME,
302313
plugins: {
303314
'key-auth': { key: JACK_USERNAME },
304315
},
305316
}),
306-
).resolves.not.toThrow());
317+
).resolves.not.toThrow()
318+
console.log(`[${new Date().toLocaleTimeString()}] created consumer`)
319+
});
307320

308321
it('call API (without key)', async () => {
322+
console.log(`[${new Date().toLocaleTimeString()}] calling API without key`)
309323
const res = await client.get('/hello', { validateStatus: () => true });
310324
expect(res.status).toEqual(401);
311-
});
325+
console.log(`[${new Date().toLocaleTimeString()}] called API without key`)
326+
}, 5 * 1000);
312327

313328
it('call normal API', async () => {
329+
console.log(`[${new Date().toLocaleTimeString()}] calling normal API`)
314330
for (let i = 0; i < 3; i++) {
315331
await expect(
316332
client.get('/hello', { headers: { apikey: JACK_USERNAME } }),
317333
).resolves.not.toThrow();
318334
}
319335
await wait(500);
320-
});
336+
console.log(`[${new Date().toLocaleTimeString()}] called normal API`)
337+
}, 5 * 1000);
321338

322339
it('check Lago events (normal API)', async () => {
340+
console.log(`[${new Date().toLocaleTimeString()}] checking Lago events (normal API)`)
323341
const { data } = await lagoClient.events.findAllEvents({
324342
external_subscription_id: LAGO_EXTERNAL_SUBSCRIPTION_ID,
325343
});
326344

327345
expect(data.events).toHaveLength(3);
328346
expect(data.events[0].code).toEqual(LAGO_BILLABLE_METRIC_CODE);
329-
});
347+
console.log(`[${new Date().toLocaleTimeString()}] checked Lago events (normal API)`)
348+
}, 5 * 1000);
330349

331350
let expensiveStartAt: Date;
332351
it('call expensive API', async () => {
352+
console.log(`[${new Date().toLocaleTimeString()}] calling expensive API`)
333353
expensiveStartAt = new Date();
334354
for (let i = 0; i < 3; i++) {
335355
await expect(
336356
client.get('/hello1', { headers: { apikey: JACK_USERNAME } }),
337357
).resolves.not.toThrow();
338358
}
339359
await wait(500);
340-
});
360+
console.log(`[${new Date().toLocaleTimeString()}] called expensive API`)
361+
}, 5 * 1000);
341362

342363
it('check Lago events (expensive API)', async () => {
364+
console.log(`[${new Date().toLocaleTimeString()}] checking Lago events (expensive API)`)
343365
const { data } = await lagoClient.events.findAllEvents({
344366
external_subscription_id: LAGO_EXTERNAL_SUBSCRIPTION_ID,
345367
timestamp_from: expensiveStartAt.toISOString(),
@@ -348,5 +370,6 @@ describe('Plugin - Lago', () => {
348370
expect(data.events).toHaveLength(3);
349371
expect(data.events[0].code).toEqual(LAGO_BILLABLE_METRIC_CODE);
350372
expect(data.events[1].properties).toEqual({ tier: 'expensive' });
351-
});
373+
console.log(`[${new Date().toLocaleTimeString()}] checked Lago events (expensive API)`)
374+
}, 5 * 1000);
352375
});

t/plugin/lago.t

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ property "event_properties" validation failed: wrong type: expected object, got
6767
6868
6969
=== TEST 2: test
70-
--- timeout: 300
70+
--- timeout: 302
7171
--- max_size: 2048000
7272
--- exec
7373
cd t && pnpm test plugin/lago.spec.mts 2>&1

t/plugin/sls-logger.t

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -486,6 +486,7 @@ hello world
486486
log_format = "bad plugin metadata"
487487
}
488488
local _, err = core.etcd.set(key, val)
489+
ngx.sleep(1)
489490
if err then
490491
ngx.say(err)
491492
return

0 commit comments

Comments
 (0)