Skip to content

Commit

Permalink
E2E: Support factory reset in logging
Browse files Browse the repository at this point in the history
When we do a factory reset, all of the log files must be reopened.

Signed-off-by: Mark Yen <mark.yen@suse.com>
  • Loading branch information
mook-as committed Jun 28, 2023
1 parent c6bf66c commit c4c2242
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 14 deletions.
3 changes: 0 additions & 3 deletions e2e/extensions.e2e.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
* An E2E test is required to have access to the web page context.
*/

import fs from 'fs';
import os from 'os';
import path from 'path';

Expand Down Expand Up @@ -31,8 +30,6 @@ const rdctl = getFullPathForTool('rdctl');
// and we don't have to deal with unintended escape-sequence processing.
const execPath = process.execPath.replace(/\\/g, '/');

fs.mkdirSync(reportAsset(__filename, 'log'), { recursive: true });

const console = new Log(path.basename(__filename, '.ts'), reportAsset(__filename, 'log'));
const NAMESPACE = 'rancher-desktop-extensions';

Expand Down
6 changes: 5 additions & 1 deletion e2e/lockedFields.e2e.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ import {
import type { DeploymentProfileType } from '@pkg/config/settings';
import { readDeploymentProfiles } from '@pkg/main/deploymentProfiles';
import { spawnFile } from '@pkg/utils/childProcess';
import { reopenLogs } from '@pkg/utils/logging';

import type { ElectronApplication, BrowserContext, Page } from '@playwright/test';

Expand Down Expand Up @@ -70,7 +71,10 @@ test.describe('Locked fields', () => {

test.describe.configure({ mode: 'serial' });

test.beforeAll(() => tool('rdctl', 'factory-reset'));
test.beforeAll(async() => {
await tool('rdctl', 'factory-reset', '--verbose');
reopenLogs();
});

test.afterAll(async() => {
await restoreUserProfile();
Expand Down
2 changes: 2 additions & 0 deletions e2e/utils/TestUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,8 @@ export async function teardown(app: ElectronApplication, filename: string) {
const string = min ? `${ min } min ${ sec } sec` : `${ sec } seconds`;

console.log(`Test ${ path.basename(filename) } took ${ string }.`);
} else {
console.log(`Test ${ path.basename(filename) } did not have a start time.`);
}
}

Expand Down
49 changes: 39 additions & 10 deletions pkg/rancher-desktop/utils/logging.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,40 @@ export function setLogLevel(level: logLevel): void {
export class Log {
constructor(topic: string, directory = paths.logs) {
this.path = path.join(directory, `${ topic }.log`);
this.stream = fs.createWriteStream(this.path, { flags: 'w', mode: 0o600 });
this.reopen();
// The following lines only exist because TypeScript can't reason about
// the call to this.reopen() correctly. They are unused.
this.realStream ??= fs.createWriteStream(this.path, { flags: 'ERROR' });
this.console ??= globalThis.console;
this.fdPromise ??= Promise.reject();
}

/** The path to the log file. */
readonly path: string;

/** A stream to write to the log file. */
get stream(): fs.WriteStream {
return this.realStream;
}

/** The underlying console stream. */
protected console: Console;

protected realStream: fs.WriteStream;

protected reopen(mode = 'w') {
if (process.env.RD_TEST === 'e2e') {
// If we're running E2E tests, we may need to create the log directory.
// We don't do this normally because it's synchronous and slow.
fs.mkdirSync(path.dirname(this.path), { recursive: true });
}
this.realStream?.close();
this.realStream = fs.createWriteStream(this.path, { flags: mode, mode: 0o600 });
this.fdPromise = new Promise((resolve) => {
this.stream.on('open', resolve);
});
delete this._fdStream;

// If we're running unit tests, output to the console rather than file.
// However, _don't_ do so for end-to-end tests in Playwright.
// We detect Playwright via an environment variable we set in scripts/e2e.ts
Expand All @@ -52,15 +82,6 @@ export class Log {
}
}

/** The path to the log file. */
readonly path: string;

/** A stream to write to the log file. */
readonly stream: fs.WriteStream;

/** The underlying console stream. */
protected readonly console: Console;

protected fdPromise: Promise<number>;

_fdStream: Promise<stream.Writable> | undefined;
Expand Down Expand Up @@ -184,4 +205,12 @@ export function clearLoggingDirectory(): void {
}
}

export function reopenLogs() {
for (const log of logs.values()) {
log['reopen']('a');
// Trigger making the stream
((_: any) => {})(log.fdStream);
}
}

fs.mkdirSync(paths.logs, { recursive: true });

0 comments on commit c4c2242

Please sign in to comment.