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>
pull/28217/head
Michael Telatynski 2023-12-14 13:49:35 +00:00 committed by GitHub
parent 18f11b8024
commit d5a211c774
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 24 additions and 5 deletions

View File

@ -17,6 +17,7 @@ limitations under the License.
import { test as base, expect as baseExpect, Locator, Page, ExpectMatcherState, ElementHandle } from "@playwright/test"; import { test as base, expect as baseExpect, Locator, Page, ExpectMatcherState, ElementHandle } from "@playwright/test";
import AxeBuilder from "@axe-core/playwright"; import AxeBuilder from "@axe-core/playwright";
import _ from "lodash"; import _ from "lodash";
import { basename } from "node:path";
import type mailhog from "mailhog"; import type mailhog from "mailhog";
import type { IConfigOptions } from "../src/IConfigOptions"; import type { IConfigOptions } from "../src/IConfigOptions";
@ -108,7 +109,7 @@ export const test = base.extend<
}, },
startHomeserverOpts: "default", startHomeserverOpts: "default",
homeserver: async ({ request, startHomeserverOpts: opts }, use) => { homeserver: async ({ request, startHomeserverOpts: opts }, use, testInfo) => {
if (typeof opts === "string") { if (typeof opts === "string") {
opts = { template: opts }; opts = { template: opts };
} }
@ -127,7 +128,16 @@ export const test = base.extend<
} }
await use(await server.start(opts)); 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 // eslint-disable-next-line no-empty-pattern
oAuthServer: async ({}, use) => { oAuthServer: async ({}, use) => {

View File

@ -81,7 +81,7 @@ export class Dendrite extends Synapse implements Homeserver, HomeserverInstance
return this; return this;
} }
public async stop(): Promise<void> { public async stop(): Promise<string[]> {
if (!this.config) throw new Error("Missing existing dendrite instance, did you call stop() before start()?"); 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); 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); await fse.remove(this.config.configDir);
console.log(`Stopped dendrite id ${this.config.serverId}.`); console.log(`Stopped dendrite id ${this.config.serverId}.`);
return [path.join(dendriteLogsPath, "stdout.log"), path.join(dendriteLogsPath, "stderr.log")];
} }
} }

View File

@ -53,7 +53,12 @@ export interface StartHomeserverOpts {
export interface Homeserver { export interface Homeserver {
start(opts: StartHomeserverOpts): Promise<HomeserverInstance>; start(opts: StartHomeserverOpts): Promise<HomeserverInstance>;
stop(): Promise<void>; /**
* Stop this test homeserver instance.
*
* @returns A list of paths relative to the cwd for logfiles generated during this test run.
*/
stop(): Promise<string[]>;
} }
export interface Credentials { export interface Credentials {

View File

@ -150,7 +150,7 @@ export class Synapse implements Homeserver, HomeserverInstance {
return this; return this;
} }
public async stop(): Promise<void> { public async stop(): Promise<string[]> {
if (!this.config) throw new Error("Missing existing synapse instance, did you call stop() before start()?"); if (!this.config) throw new Error("Missing existing synapse instance, did you call stop() before start()?");
const id = this.config.serverId; const id = this.config.serverId;
const synapseLogsPath = path.join("playwright", "synapselogs", id); const synapseLogsPath = path.join("playwright", "synapselogs", id);
@ -162,6 +162,8 @@ export class Synapse implements Homeserver, HomeserverInstance {
await this.docker.stop(); await this.docker.stop();
await fse.remove(this.config.configDir); await fse.remove(this.config.configDir);
console.log(`Stopped synapse id ${id}.`); 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<Credentials> { public async registerUser(username: string, password: string, displayName?: string): Promise<Credentials> {