Skip to content

Commit ad7f207

Browse files
Add trace logs for E2E tests (#1745)
* Add optional trace logs for E2E tests * Fix paths * changelog * Always trace and always clear up before running * Fix missing files * Add logging to debug why we only have one trace file in artefacts * Ensure tracing is only cleaned on startup * Derace powerlevels test * Review comments * Fix message * Reorder joins * Update spec/e2e/setup.ts Co-authored-by: Andrew Ferrazzutti <[email protected]> --------- Co-authored-by: Andrew Ferrazzutti <[email protected]>
1 parent 49abb79 commit ad7f207

File tree

6 files changed

+68
-6
lines changed

6 files changed

+68
-6
lines changed

.github/workflows/e2e-test.yml

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,12 @@ jobs:
138138
yarn --strict-semver --frozen-lockfile
139139
HOMERUNNER_SPAWN_HS_TIMEOUT_SECS=100 ../homerunner &
140140
bash -ic 'yarn test:e2e'
141+
- name: Archive trace logs
142+
if: always()
143+
uses: actions/upload-artifact@v3
144+
with:
145+
name: e2e-traces
146+
path: matrix-appservice-irc/.e2e-traces/*
141147

142148
integration-test-pool:
143149
runs-on: ubuntu-latest
@@ -207,4 +213,10 @@ jobs:
207213
cd matrix-appservice-irc
208214
yarn --strict-semver --frozen-lockfile
209215
HOMERUNNER_SPAWN_HS_TIMEOUT_SECS=100 ../homerunner &
210-
bash -ic 'yarn test:e2e'
216+
bash -ic 'yarn test:e2e'
217+
- name: Archive trace logs
218+
if: always()
219+
uses: actions/upload-artifact@v3
220+
with:
221+
name: e2e-traces-pool
222+
path: matrix-appservice-irc/.e2e-traces/*

changelog.d/1745.misc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Add tracing for Matrix and IRC clients when running E2E tests.

spec/e2e/jest.config.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,4 +14,5 @@ module.exports = {
1414
},
1515
],
1616
},
17+
globalSetup: './setup.ts'
1718
};

spec/e2e/powerlevels.spec.ts

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
/* eslint-disable @typescript-eslint/no-non-null-assertion */
22
import { TestIrcServer } from "matrix-org-irc";
33
import { IrcBridgeE2ETest } from "../util/e2e-test";
4-
import { describe, expect, it } from "@jest/globals";
5-
import { PowerLevelContent } from "matrix-appservice-bridge";
4+
import { describe, it } from "@jest/globals";
65

76

87
describe('Ensure powerlevels are appropriately applied', () => {
@@ -28,10 +27,18 @@ describe('Ensure powerlevels are appropriately applied', () => {
2827
// Create the channel
2928
await bob.join(channel);
3029

30+
const aliceJoin = bob.waitForEvent('join');
3131
const cRoomId = await testEnv.joinChannelHelper(alice, await testEnv.createAdminRoomHelper(alice), channel);
32+
// Trigger a join on IRC.
33+
await alice.sendText(cRoomId, 'Hello world!');
34+
// Wait for alice to join.
35+
await aliceJoin;
3236

3337
// Now have charlie join and be opped.
38+
const charlieJoin = bob.waitForEvent('join');
3439
await charlie.join(channel);
40+
await charlieJoin;
41+
3542
const operatorPL = testEnv.ircBridge.config.ircService.servers.localhost.modePowerMap!.o;
3643
const plEvent = alice.waitForPowerLevel(
3744
cRoomId, {
@@ -43,6 +50,9 @@ describe('Ensure powerlevels are appropriately applied', () => {
4350
}
4451
);
4552

53+
// Wait for charlie to join
54+
await charlieJoin;
55+
4656
await bob.send('MODE', channel, '+o', charlie.nick);
4757
await plEvent;
4858
});

spec/e2e/setup.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
import * as fs from "node:fs/promises";
2+
3+
export default async function() {
4+
console.log('Cleaning .e2e-traces')
5+
await fs.rm('.e2e-traces', { recursive: true, force: true })
6+
await fs.mkdir('.e2e-traces');
7+
}
8+

spec/util/e2e-test.ts

Lines changed: 33 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,9 @@ import { IrcServer } from "../../src/irc/IrcServer";
77
import { MatrixClient } from "matrix-bot-sdk";
88
import { TestIrcServer } from "matrix-org-irc";
99
import { IrcConnectionPool } from "../../src/pool-service/IrcConnectionPool";
10-
import dns from 'node:dns';
10+
import { expect } from "@jest/globals";
11+
import * as dns from 'node:dns';
12+
import { WriteStream, createWriteStream } from "node:fs";
1113
// Needed to make tests work on GitHub actions. Node 17+ defaults
1214
// to IPv6, and the homerunner domain resolves to IPv6, but the
1315
// runtime doesn't actually support IPv6 🤦
@@ -24,8 +26,11 @@ interface Opts {
2426
ircNicks?: string[];
2527
timeout?: number;
2628
config?: Partial<BridgeConfig>,
29+
traceToFile?: boolean,
2730
}
2831

32+
const traceFilePath = '.e2e-traces';
33+
2934
export class E2ETestMatrixClient extends MatrixClient {
3035

3136
public async waitForPowerLevel(
@@ -160,6 +165,11 @@ export class IrcBridgeE2ETest {
160165
}
161166

162167
static async createTestEnv(opts: Opts = {}): Promise<IrcBridgeE2ETest> {
168+
const testName = expect.getState().currentTestName?.replace(/[^a-zA-Z]/g, '-');
169+
const tracePath = `${traceFilePath}/${testName}.log`;
170+
console.log('Opening new trace file', tracePath);
171+
const traceLog = createWriteStream(tracePath, 'utf-8');
172+
163173
const workerID = parseInt(process.env.JEST_WORKER_ID ?? '0');
164174
const { matrixLocalparts, config } = opts;
165175
const ircTest = new TestIrcServer();
@@ -280,7 +290,7 @@ export class IrcBridgeE2ETest {
280290
}
281291
}),
282292
}, registration);
283-
return new IrcBridgeE2ETest(homeserver, ircBridge, registration, postgresDb, ircTest, redisPool)
293+
return new IrcBridgeE2ETest(homeserver, ircBridge, registration, postgresDb, ircTest, traceLog, redisPool)
284294
}
285295

286296
private constructor(
@@ -289,7 +299,24 @@ export class IrcBridgeE2ETest {
289299
public readonly registration: AppServiceRegistration,
290300
readonly postgresDb: string,
291301
public readonly ircTest: TestIrcServer,
292-
public readonly pool?: IrcConnectionPool) {
302+
private traceLog: WriteStream,
303+
public readonly pool?: IrcConnectionPool,
304+
) {
305+
const startTime = Date.now();
306+
for (const [clientId, client] of Object.entries(ircTest.clients)) {
307+
client.on('raw', (msg) => {
308+
traceLog.write(
309+
`${Date.now() - startTime}ms [IRC:${clientId}] ${JSON.stringify(msg)} \n`
310+
);
311+
})
312+
}
313+
for (const {client, userId} of Object.values(homeserver.users)) {
314+
client.on('room.event', (roomId, eventData) => {
315+
traceLog.write(
316+
`${Date.now() - startTime}ms [Matrix:${userId}] ${roomId} ${JSON.stringify(eventData)}\n`
317+
);
318+
})
319+
}
293320
}
294321

295322
public async recreateBridge() {
@@ -317,6 +344,9 @@ export class IrcBridgeE2ETest {
317344
}
318345

319346
public async tearDown(): Promise<void> {
347+
if (this.traceLog) {
348+
this.traceLog.close();
349+
}
320350
await Promise.allSettled([
321351
this.ircBridge?.kill(),
322352
this.ircTest.tearDown(),

0 commit comments

Comments
 (0)