Skip to content

Commit

Permalink
chore(e2e-tests): refactor TestShell to ensure killAll gets called (
Browse files Browse the repository at this point in the history
#2170)

* Refactored TestShell to ensure killAll is called

* Avoid asserting test shell enabled in after as some "before" hook might have failed

* Remove open shells as they get killed

* Kill shells before removing homedir

* Add a startTestShell onto the Mocha.Context

* Require test-shell.ts before tests

* Update tests to start shells from context

* Export and test ensureTestShellAfterHook

* Use ensureTestShellAfterHook to ensure order of hooks

* Adding back the after block in e2e-direct tests

* Split test-shell.ts to separate concerns

* Add assertNoOpenShells to e2e-editor

* Using a doc comment instead of a @deprecated tag

* Refactored "runAndGetOutputWithoutErrors" into "waitForCleanOutout"
  • Loading branch information
kraenhansen authored Oct 1, 2024
1 parent 24d76dc commit 40d6f41
Show file tree
Hide file tree
Showing 18 changed files with 390 additions and 253 deletions.
2 changes: 1 addition & 1 deletion packages/e2e-tests/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
"url": "git://github.com/mongodb-js/mongosh.git"
},
"scripts": {
"test": "mocha -r \"../../scripts/import-expansions.js\" --timeout 15000 --colors -r ts-node/register \"./test/e2e*.spec.ts\"",
"test": "mocha -r ts-node/register -r \"../../scripts/import-expansions.js\" -r \"./test/test-shell-context.ts\" --timeout 15000 --colors \"./test/*.spec.ts\"",
"test-ci": "node ../../scripts/run-if-package-requested.js npm test",
"test-coverage": "nyc --no-clean --cwd ../.. --reporter=none npm run test",
"test-ci-coverage": "nyc --no-clean --cwd ../.. --reporter=none npm run test-ci",
Expand Down
11 changes: 4 additions & 7 deletions packages/e2e-tests/test/e2e-analytics.spec.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import { expect } from 'chai';
import { startTestCluster } from '../../../testing/integration-testing-hooks';
import { eventually } from '../../../testing/eventually';
import { TestShell } from './test-shell';

describe('e2e Analytics Node', function () {
const replSetName = 'replicaSet';
Expand All @@ -13,8 +12,6 @@ describe('e2e Analytics Node', function () {
{ args: ['--replSet', replSetName] }
);

afterEach(TestShell.cleanup);

before(async function () {
if (process.env.MONGOSH_TEST_FORCE_API_STRICT) {
return this.skip();
Expand All @@ -36,7 +33,7 @@ describe('e2e Analytics Node', function () {
],
};

const shell = TestShell.start({
const shell = this.startTestShell({
args: [await rs0.connectionString()],
});
await shell.waitForPrompt();
Expand All @@ -55,7 +52,7 @@ describe('e2e Analytics Node', function () {

context('without readPreference', function () {
it('a direct connection ends up at primary', async function () {
const shell = TestShell.start({
const shell = this.startTestShell({
args: [await rs0.connectionString()],
});
await shell.waitForPrompt();
Expand All @@ -68,13 +65,13 @@ describe('e2e Analytics Node', function () {

context('specifying readPreference and tags', function () {
it('ends up at the ANALYTICS node', async function () {
const shell = TestShell.start({
const shell = this.startTestShell({
args: [
`${await rs0.connectionString()}?replicaSet=${replSetName}&readPreference=secondary&readPreferenceTags=nodeType:ANALYTICS`,
],
});

const directConnectionToAnalyticsShell = TestShell.start({
const directConnectionToAnalyticsShell = this.startTestShell({
args: [`${await rs3.connectionString()}?directConnection=true`],
});
await Promise.all([
Expand Down
26 changes: 12 additions & 14 deletions packages/e2e-tests/test/e2e-auth.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { expect } from 'chai';
import type { Db, Document, MongoClientOptions } from 'mongodb';
import { MongoClient } from 'mongodb';
import { eventually } from '../../../testing/eventually';
import { TestShell } from './test-shell';
import type { TestShell } from './test-shell';
import {
skipIfApiStrict,
startSharedTestServer,
Expand Down Expand Up @@ -110,7 +110,7 @@ describe('Auth e2e', function () {
beforeEach(async function () {
const connectionString = await testServer.connectionString();
dbName = `test-${Date.now()}`;
shell = TestShell.start({ args: [connectionString] });
shell = this.startTestShell({ args: [connectionString] });

client = await MongoClient.connect(connectionString, {});

Expand All @@ -137,7 +137,6 @@ describe('Auth e2e', function () {

await client.close();
});
afterEach(TestShell.cleanup);

describe('user management', function () {
describe('createUser', function () {
Expand Down Expand Up @@ -879,7 +878,7 @@ describe('Auth e2e', function () {
pathname: `/${dbName}`,
}
);
shell = TestShell.start({ args: [authConnectionString] });
shell = this.startTestShell({ args: [authConnectionString] });
await shell.waitForPrompt();
shell.assertNoErrors();
await shell.executeLine(`use ${dbName}`);
Expand All @@ -903,7 +902,7 @@ describe('Auth e2e', function () {
pathname: `/${dbName}`,
}
);
shell = TestShell.start({ args: [authConnectionString] });
shell = this.startTestShell({ args: [authConnectionString] });
await shell.waitForPrompt();
shell.assertNoErrors();
await shell.executeLine(`use ${dbName}`);
Expand All @@ -930,7 +929,7 @@ describe('Auth e2e', function () {
});
it('can auth when there is -u and -p', async function () {
const connectionString = await testServer.connectionString();
shell = TestShell.start({
shell = this.startTestShell({
args: [
connectionString,
'-u',
Expand Down Expand Up @@ -965,7 +964,7 @@ describe('Auth e2e', function () {
return this.skip(); // No SCRAM-SHA-1 in FIPS mode
}
const connectionString = await testServer.connectionString();
shell = TestShell.start({
shell = this.startTestShell({
args: [
connectionString,
'-u',
Expand All @@ -989,7 +988,7 @@ describe('Auth e2e', function () {
// This test is not particularly meaningful if we're using the system OpenSSL installation
// and it is not properly configured for FIPS to begin with. This is the case on e.g.
// Ubuntu 22.04 in evergreen CI.
const preTestShell = TestShell.start({
const preTestShell = this.startTestShell({
args: [
'--quiet',
'--nodb',
Expand All @@ -1009,7 +1008,7 @@ describe('Auth e2e', function () {
}

const connectionString = await testServer.connectionString();
shell = TestShell.start({
shell = this.startTestShell({
args: [
connectionString,
'--tlsFIPSMode',
Expand All @@ -1034,7 +1033,7 @@ describe('Auth e2e', function () {
});
it('can auth with SCRAM-SHA-256', async function () {
const connectionString = await testServer.connectionString();
shell = TestShell.start({
shell = this.startTestShell({
args: [
connectionString,
'-u',
Expand All @@ -1055,7 +1054,7 @@ describe('Auth e2e', function () {
});
it('cannot auth when authenticationMechanism mismatches (sha256 -> sha1)', async function () {
const connectionString = await testServer.connectionString();
shell = TestShell.start({
shell = this.startTestShell({
args: [
connectionString,
'-u',
Expand All @@ -1076,7 +1075,7 @@ describe('Auth e2e', function () {
});
it('cannot auth when authenticationMechanism mismatches (sha1 -> sha256)', async function () {
const connectionString = await testServer.connectionString();
shell = TestShell.start({
shell = this.startTestShell({
args: [
connectionString,
'-u',
Expand All @@ -1097,7 +1096,7 @@ describe('Auth e2e', function () {
});
it('does not fail with kerberos not found for GSSAPI', async function () {
const connectionString = await testServer.connectionString();
shell = TestShell.start({
shell = this.startTestShell({
args: [
connectionString,
'-u',
Expand Down Expand Up @@ -1144,6 +1143,5 @@ describe('Auth e2e', function () {

await client.close();
});
afterEach(TestShell.cleanup);
});
});
19 changes: 8 additions & 11 deletions packages/e2e-tests/test/e2e-aws.spec.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { expect } from 'chai';
import { spawnSync } from 'child_process';
import { TestShell } from './test-shell';

function assertEnvVariable(variableName: string): string {
if (process.env.MONGOSH_TEST_FORCE_API_STRICT) {
Expand Down Expand Up @@ -117,12 +116,10 @@ describe('e2e AWS AUTH', function () {
).replace('arn:aws:iam::', 'arn:aws:sts::')}/*`;
});

afterEach(TestShell.cleanup);

context('without environment variables being present', function () {
context('specifying explicit parameters', function () {
it('connects with access key and secret', async function () {
const shell = TestShell.start({
const shell = this.startTestShell({
args: [
getConnectionString(),
'--username',
Expand All @@ -142,7 +139,7 @@ describe('e2e AWS AUTH', function () {

it('connects with access key, secret, and session token for IAM role', async function () {
const tokenDetails = generateIamSessionToken();
const shell = TestShell.start({
const shell = this.startTestShell({
args: [
getConnectionString(),
'--username',
Expand All @@ -165,7 +162,7 @@ describe('e2e AWS AUTH', function () {

context('specifying connection string parameters', function () {
it('connects with access key and secret', async function () {
const shell = TestShell.start({
const shell = this.startTestShell({
args: [getConnectionString(AWS_ACCESS_KEY_ID, AWS_SECRET_ACCESS_KEY)],
});
const result = await shell.waitForPromptOrExit();
Expand All @@ -179,7 +176,7 @@ describe('e2e AWS AUTH', function () {

it('connects with access key, secret, and session token for IAM role', async function () {
const tokenDetails = generateIamSessionToken();
const shell = TestShell.start({
const shell = this.startTestShell({
args: [
`${getConnectionString(
tokenDetails.key,
Expand All @@ -203,7 +200,7 @@ describe('e2e AWS AUTH', function () {
context('with AWS environment variables', function () {
context('without any other parameters', function () {
it('connects for the IAM user', async function () {
const shell = TestShell.start({
const shell = this.startTestShell({
args: [getConnectionString()],
env: {
...process.env,
Expand All @@ -222,7 +219,7 @@ describe('e2e AWS AUTH', function () {

it('connects for the IAM role session', async function () {
const tokenDetails = generateIamSessionToken();
const shell = TestShell.start({
const shell = this.startTestShell({
args: [getConnectionString()],
env: {
...process.env,
Expand All @@ -243,7 +240,7 @@ describe('e2e AWS AUTH', function () {

context('with invalid environment but valid parameters', function () {
it('connects for the IAM user', async function () {
const shell = TestShell.start({
const shell = this.startTestShell({
args: [
getConnectionString(),
'--username',
Expand All @@ -268,7 +265,7 @@ describe('e2e AWS AUTH', function () {

it('connects for the IAM role session', async function () {
const tokenDetails = generateIamSessionToken();
const shell = TestShell.start({
const shell = this.startTestShell({
args: [
getConnectionString(),
'--username',
Expand Down
9 changes: 4 additions & 5 deletions packages/e2e-tests/test/e2e-banners.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,16 @@ import {
skipIfApiStrict,
startSharedTestServer,
} from '../../../testing/integration-testing-hooks';
import { TestShell } from './test-shell';
import type { TestShell } from './test-shell';

describe('e2e startup banners', function () {
skipIfApiStrict();
afterEach(TestShell.cleanup);

const testServer = startSharedTestServer();

context('without special configuration', function () {
it('shows startup warnings', async function () {
const shell = TestShell.start({
const shell = this.startTestShell({
args: [await testServer.connectionString()],
});
await shell.waitForPrompt();
Expand All @@ -30,7 +29,7 @@ describe('e2e startup banners', function () {
let helperShell: TestShell;

beforeEach(async function () {
helperShell = TestShell.start({
helperShell = this.startTestShell({
args: [await testServer.connectionString()],
});
await helperShell.waitForPrompt();
Expand All @@ -47,7 +46,7 @@ describe('e2e startup banners', function () {
});

it('shows automation notices', async function () {
const shell = TestShell.start({
const shell = this.startTestShell({
args: [await testServer.connectionString()],
});
await shell.waitForPrompt();
Expand Down
6 changes: 3 additions & 3 deletions packages/e2e-tests/test/e2e-bson.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { expect } from 'chai';
import type { Db } from 'mongodb';
import { MongoClient } from 'mongodb';
import { bson } from '@mongosh/service-provider-core';
import { TestShell } from './test-shell';
import type { TestShell } from './test-shell';
import { startSharedTestServer } from '../../../testing/integration-testing-hooks';

describe('BSON e2e', function () {
Expand All @@ -15,7 +15,7 @@ describe('BSON e2e', function () {
beforeEach(async function () {
const connectionString = await testServer.connectionString();
dbName = `test-${Date.now()}`;
shell = TestShell.start({ args: [connectionString] });
shell = this.startTestShell({ args: [connectionString] });

client = await MongoClient.connect(connectionString, {});

Expand All @@ -30,7 +30,7 @@ describe('BSON e2e', function () {

await client.close();
});
afterEach(TestShell.cleanup);

describe('printed BSON', function () {
const outputDoc = {
ObjectId: "ObjectId('5f16b8bebe434dc98cdfc9ca')",
Expand Down
Loading

0 comments on commit 40d6f41

Please sign in to comment.