Skip to content

Commit

Permalink
chore(e2e-tests): account for upstream server TLS option changes MONG…
Browse files Browse the repository at this point in the history
…OSH-1718 (#1838)

The server will no longer start when TLS is enabled without a CA
certificate argument. This does not affect our tests’ functionality,
only how we start the server in them.
  • Loading branch information
addaleax authored Feb 26, 2024
1 parent 6146b88 commit e97b1e8
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 21 deletions.
12 changes: 10 additions & 2 deletions packages/e2e-tests/test/e2e-tls.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,8 @@ describe('e2e TLS', function () {
'--tlsCertificateKeyFile',
SERVER_KEY,
'--tlsAllowConnectionsWithoutCertificates',
'--tlsCAFile',
CA_CERT,
],
});

Expand Down Expand Up @@ -184,7 +186,9 @@ describe('e2e TLS', function () {
});
const result = await shell.waitForPromptOrExit();
expect(result.state).to.equal('exit');
shell.assertContainsOutput('unable to verify the first certificate');
shell.assertContainsOutput(
/unable to verify the first certificate|self[- ]signed certificate in certificate chain/
);
});

it('fails with invalid CA (connection string)', async function () {
Expand All @@ -199,7 +203,9 @@ describe('e2e TLS', function () {
});
const result = await shell.waitForPromptOrExit();
expect(result.state).to.equal('exit');
shell.assertContainsOutput('unable to verify the first certificate');
shell.assertContainsOutput(
/unable to verify the first certificate|self[- ]signed certificate in certificate chain/
);
});

it('fails when providing a CRL including the servers cert', async function () {
Expand Down Expand Up @@ -640,6 +646,8 @@ describe('e2e TLS', function () {
'requireTLS',
'--tlsCertificateKeyFile',
SERVER_INVALIDHOST_KEY,
'--tlsCAFile',
CA_CERT,
'--tlsAllowConnectionsWithoutCertificates',
],
});
Expand Down
47 changes: 28 additions & 19 deletions packages/e2e-tests/test/test-shell.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
import type Mocha from 'mocha';
import assert from 'assert';
import type { ChildProcess } from 'child_process';
import type {
ChildProcess,
ChildProcessWithoutNullStreams,
} from 'child_process';
import { spawn } from 'child_process';
import { once } from 'events';
import { inspect } from 'util';
import path from 'path';
import stripAnsi from 'strip-ansi';
import { eventually } from '../../../testing/eventually';
Expand All @@ -20,6 +24,12 @@ const PROMPT_PATTERN = /^([^<>]*> ?)+$/m;
const ERROR_PATTERN_1 = /Thrown:\n([^>]*)/gm; // node <= 12.14
const ERROR_PATTERN_2 = /Uncaught[:\n ]+([^>]*)/gm;

function matches(str: string, pattern: string | RegExp): boolean {
return typeof pattern === 'string'
? str.includes(pattern)
: pattern.test(str);
}

/**
* Test shell helper class.
*/
Expand All @@ -36,7 +46,7 @@ export class TestShell {
consumeStdio?: boolean;
} = { args: [] }
): TestShell {
let shellProcess: ChildProcess;
let shellProcess: ChildProcessWithoutNullStreams;

let env = options.env || process.env;
if (options.forceTerminal) {
Expand Down Expand Up @@ -88,7 +98,7 @@ export class TestShell {
static async killall(): Promise<void> {
const exitPromises: Promise<unknown>[] = [];
while (TestShell._openShells.length) {
const shell = TestShell._openShells.pop();
const shell = TestShell._openShells.pop()!;
shell.kill();
exitPromises.push(shell.waitForExit());
}
Expand All @@ -110,13 +120,16 @@ export class TestShell {
await TestShell.killall();
}

private _process: ChildProcess;
private _process: ChildProcessWithoutNullStreams;

private _output: string;
private _rawOutput: string;
private _onClose: Promise<number>;

constructor(shellProcess: ChildProcess, consumeStdio = true) {
constructor(
shellProcess: ChildProcessWithoutNullStreams,
consumeStdio = true
) {
this._process = shellProcess;
this._output = '';
this._rawOutput = '';
Expand Down Expand Up @@ -229,38 +242,34 @@ export class TestShell {
}
}

assertContainsOutput(expectedOutput: string): void {
assertContainsOutput(expectedOutput: string | RegExp): void {
const onlyOutputLines = this._getOutputLines();
if (!onlyOutputLines.join('\n').includes(expectedOutput)) {
if (!matches(onlyOutputLines.join('\n'), expectedOutput)) {
throw new assert.AssertionError({
message: `Expected shell output to include ${JSON.stringify(
expectedOutput
)}`,
message: `Expected shell output to include ${inspect(expectedOutput)}`,
actual: this._output,
expected: expectedOutput,
});
}
}

assertContainsError(expectedError: string): void {
assertContainsError(expectedError: string | RegExp): void {
const allErrors = this._getAllErrors();

if (!allErrors.find((error) => error.includes(expectedError))) {
if (!allErrors.find((error) => matches(error, expectedError))) {
throw new assert.AssertionError({
message: `Expected shell errors to include ${JSON.stringify(
expectedError
)}`,
message: `Expected shell errors to include ${inspect(expectedError)}`,
actual: this._output,
expected: expectedError,
});
}
}

assertNotContainsOutput(unexpectedOutput: string): void {
assertNotContainsOutput(unexpectedOutput: string | RegExp): void {
const onlyOutputLines = this._getOutputLines();
if (onlyOutputLines.join('\n').includes(unexpectedOutput)) {
if (matches(onlyOutputLines.join('\n'), unexpectedOutput)) {
throw new assert.AssertionError({
message: `Expected shell output not to include ${JSON.stringify(
message: `Expected shell output not to include ${inspect(
unexpectedOutput
)}`,
actual: this._output,
Expand Down Expand Up @@ -288,6 +297,6 @@ export class TestShell {
if (!match) {
return null;
}
return match.groups.logId;
return match.groups!.logId;
}
}

0 comments on commit e97b1e8

Please sign in to comment.