From d5a211c774081639846efa226d0541532df1a56b Mon Sep 17 00:00:00 2001 From: Michael Telatynski <7t3chguy@gmail.com> Date: Thu, 14 Dec 2023 13:49:35 +0000 Subject: [PATCH] Attach synapse logs to failed Playwright test runs (#12027) * Attach synapse logs to failed Playwright test runs Signed-off-by: Michael Telatynski <7t3chguy@gmail.com> * Update playwright/plugins/homeserver/index.ts --------- Signed-off-by: Michael Telatynski <7t3chguy@gmail.com> --- playwright/element-web-test.ts | 14 ++++++++++++-- playwright/plugins/homeserver/dendrite/index.ts | 4 +++- playwright/plugins/homeserver/index.ts | 7 ++++++- playwright/plugins/homeserver/synapse/index.ts | 4 +++- 4 files changed, 24 insertions(+), 5 deletions(-) diff --git a/playwright/element-web-test.ts b/playwright/element-web-test.ts index cb8638ebbf..d34406b54d 100644 --- a/playwright/element-web-test.ts +++ b/playwright/element-web-test.ts @@ -17,6 +17,7 @@ limitations under the License. import { test as base, expect as baseExpect, Locator, Page, ExpectMatcherState, ElementHandle } from "@playwright/test"; import AxeBuilder from "@axe-core/playwright"; import _ from "lodash"; +import { basename } from "node:path"; import type mailhog from "mailhog"; import type { IConfigOptions } from "../src/IConfigOptions"; @@ -108,7 +109,7 @@ export const test = base.extend< }, startHomeserverOpts: "default", - homeserver: async ({ request, startHomeserverOpts: opts }, use) => { + homeserver: async ({ request, startHomeserverOpts: opts }, use, testInfo) => { if (typeof opts === "string") { opts = { template: opts }; } @@ -127,7 +128,16 @@ export const test = base.extend< } await use(await server.start(opts)); - await server.stop(); + const logs = await server.stop(); + + if (testInfo.status !== "passed") { + for (const path of logs) { + await testInfo.attach(`homeserver-${basename(path)}`, { + path, + contentType: "text/plain", + }); + } + } }, // eslint-disable-next-line no-empty-pattern oAuthServer: async ({}, use) => { diff --git a/playwright/plugins/homeserver/dendrite/index.ts b/playwright/plugins/homeserver/dendrite/index.ts index 2080534041..a9823f5b05 100644 --- a/playwright/plugins/homeserver/dendrite/index.ts +++ b/playwright/plugins/homeserver/dendrite/index.ts @@ -81,7 +81,7 @@ export class Dendrite extends Synapse implements Homeserver, HomeserverInstance return this; } - public async stop(): Promise { + public async stop(): Promise { if (!this.config) throw new Error("Missing existing dendrite instance, did you call stop() before start()?"); const dendriteLogsPath = path.join("playwright", "dendritelogs", this.config.serverId); @@ -97,6 +97,8 @@ export class Dendrite extends Synapse implements Homeserver, HomeserverInstance await fse.remove(this.config.configDir); console.log(`Stopped dendrite id ${this.config.serverId}.`); + + return [path.join(dendriteLogsPath, "stdout.log"), path.join(dendriteLogsPath, "stderr.log")]; } } diff --git a/playwright/plugins/homeserver/index.ts b/playwright/plugins/homeserver/index.ts index bd01f0e555..a4b9cdf98b 100644 --- a/playwright/plugins/homeserver/index.ts +++ b/playwright/plugins/homeserver/index.ts @@ -53,7 +53,12 @@ export interface StartHomeserverOpts { export interface Homeserver { start(opts: StartHomeserverOpts): Promise; - stop(): Promise; + /** + * Stop this test homeserver instance. + * + * @returns A list of paths relative to the cwd for logfiles generated during this test run. + */ + stop(): Promise; } export interface Credentials { diff --git a/playwright/plugins/homeserver/synapse/index.ts b/playwright/plugins/homeserver/synapse/index.ts index 78a37d3a17..ffa0205c69 100644 --- a/playwright/plugins/homeserver/synapse/index.ts +++ b/playwright/plugins/homeserver/synapse/index.ts @@ -150,7 +150,7 @@ export class Synapse implements Homeserver, HomeserverInstance { return this; } - public async stop(): Promise { + public async stop(): Promise { if (!this.config) throw new Error("Missing existing synapse instance, did you call stop() before start()?"); const id = this.config.serverId; const synapseLogsPath = path.join("playwright", "synapselogs", id); @@ -162,6 +162,8 @@ export class Synapse implements Homeserver, HomeserverInstance { await this.docker.stop(); await fse.remove(this.config.configDir); console.log(`Stopped synapse id ${id}.`); + + return [path.join(synapseLogsPath, "stdout.log"), path.join(synapseLogsPath, "stderr.log")]; } public async registerUser(username: string, password: string, displayName?: string): Promise {