Skip to content

Commit 40d6f41

Browse files
authored
chore(e2e-tests): refactor TestShell to ensure killAll gets called (#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"
1 parent 24d76dc commit 40d6f41

18 files changed

+390
-253
lines changed

packages/e2e-tests/package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
"url": "git://github.com/mongodb-js/mongosh.git"
1212
},
1313
"scripts": {
14-
"test": "mocha -r \"../../scripts/import-expansions.js\" --timeout 15000 --colors -r ts-node/register \"./test/e2e*.spec.ts\"",
14+
"test": "mocha -r ts-node/register -r \"../../scripts/import-expansions.js\" -r \"./test/test-shell-context.ts\" --timeout 15000 --colors \"./test/*.spec.ts\"",
1515
"test-ci": "node ../../scripts/run-if-package-requested.js npm test",
1616
"test-coverage": "nyc --no-clean --cwd ../.. --reporter=none npm run test",
1717
"test-ci-coverage": "nyc --no-clean --cwd ../.. --reporter=none npm run test-ci",

packages/e2e-tests/test/e2e-analytics.spec.ts

+4-7
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import { expect } from 'chai';
22
import { startTestCluster } from '../../../testing/integration-testing-hooks';
33
import { eventually } from '../../../testing/eventually';
4-
import { TestShell } from './test-shell';
54

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

16-
afterEach(TestShell.cleanup);
17-
1815
before(async function () {
1916
if (process.env.MONGOSH_TEST_FORCE_API_STRICT) {
2017
return this.skip();
@@ -36,7 +33,7 @@ describe('e2e Analytics Node', function () {
3633
],
3734
};
3835

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

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

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

77-
const directConnectionToAnalyticsShell = TestShell.start({
74+
const directConnectionToAnalyticsShell = this.startTestShell({
7875
args: [`${await rs3.connectionString()}?directConnection=true`],
7976
});
8077
await Promise.all([

packages/e2e-tests/test/e2e-auth.spec.ts

+12-14
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { expect } from 'chai';
22
import type { Db, Document, MongoClientOptions } from 'mongodb';
33
import { MongoClient } from 'mongodb';
44
import { eventually } from '../../../testing/eventually';
5-
import { TestShell } from './test-shell';
5+
import type { TestShell } from './test-shell';
66
import {
77
skipIfApiStrict,
88
startSharedTestServer,
@@ -110,7 +110,7 @@ describe('Auth e2e', function () {
110110
beforeEach(async function () {
111111
const connectionString = await testServer.connectionString();
112112
dbName = `test-${Date.now()}`;
113-
shell = TestShell.start({ args: [connectionString] });
113+
shell = this.startTestShell({ args: [connectionString] });
114114

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

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

138138
await client.close();
139139
});
140-
afterEach(TestShell.cleanup);
141140

142141
describe('user management', function () {
143142
describe('createUser', function () {
@@ -879,7 +878,7 @@ describe('Auth e2e', function () {
879878
pathname: `/${dbName}`,
880879
}
881880
);
882-
shell = TestShell.start({ args: [authConnectionString] });
881+
shell = this.startTestShell({ args: [authConnectionString] });
883882
await shell.waitForPrompt();
884883
shell.assertNoErrors();
885884
await shell.executeLine(`use ${dbName}`);
@@ -903,7 +902,7 @@ describe('Auth e2e', function () {
903902
pathname: `/${dbName}`,
904903
}
905904
);
906-
shell = TestShell.start({ args: [authConnectionString] });
905+
shell = this.startTestShell({ args: [authConnectionString] });
907906
await shell.waitForPrompt();
908907
shell.assertNoErrors();
909908
await shell.executeLine(`use ${dbName}`);
@@ -930,7 +929,7 @@ describe('Auth e2e', function () {
930929
});
931930
it('can auth when there is -u and -p', async function () {
932931
const connectionString = await testServer.connectionString();
933-
shell = TestShell.start({
932+
shell = this.startTestShell({
934933
args: [
935934
connectionString,
936935
'-u',
@@ -965,7 +964,7 @@ describe('Auth e2e', function () {
965964
return this.skip(); // No SCRAM-SHA-1 in FIPS mode
966965
}
967966
const connectionString = await testServer.connectionString();
968-
shell = TestShell.start({
967+
shell = this.startTestShell({
969968
args: [
970969
connectionString,
971970
'-u',
@@ -989,7 +988,7 @@ describe('Auth e2e', function () {
989988
// This test is not particularly meaningful if we're using the system OpenSSL installation
990989
// and it is not properly configured for FIPS to begin with. This is the case on e.g.
991990
// Ubuntu 22.04 in evergreen CI.
992-
const preTestShell = TestShell.start({
991+
const preTestShell = this.startTestShell({
993992
args: [
994993
'--quiet',
995994
'--nodb',
@@ -1009,7 +1008,7 @@ describe('Auth e2e', function () {
10091008
}
10101009

10111010
const connectionString = await testServer.connectionString();
1012-
shell = TestShell.start({
1011+
shell = this.startTestShell({
10131012
args: [
10141013
connectionString,
10151014
'--tlsFIPSMode',
@@ -1034,7 +1033,7 @@ describe('Auth e2e', function () {
10341033
});
10351034
it('can auth with SCRAM-SHA-256', async function () {
10361035
const connectionString = await testServer.connectionString();
1037-
shell = TestShell.start({
1036+
shell = this.startTestShell({
10381037
args: [
10391038
connectionString,
10401039
'-u',
@@ -1055,7 +1054,7 @@ describe('Auth e2e', function () {
10551054
});
10561055
it('cannot auth when authenticationMechanism mismatches (sha256 -> sha1)', async function () {
10571056
const connectionString = await testServer.connectionString();
1058-
shell = TestShell.start({
1057+
shell = this.startTestShell({
10591058
args: [
10601059
connectionString,
10611060
'-u',
@@ -1076,7 +1075,7 @@ describe('Auth e2e', function () {
10761075
});
10771076
it('cannot auth when authenticationMechanism mismatches (sha1 -> sha256)', async function () {
10781077
const connectionString = await testServer.connectionString();
1079-
shell = TestShell.start({
1078+
shell = this.startTestShell({
10801079
args: [
10811080
connectionString,
10821081
'-u',
@@ -1097,7 +1096,7 @@ describe('Auth e2e', function () {
10971096
});
10981097
it('does not fail with kerberos not found for GSSAPI', async function () {
10991098
const connectionString = await testServer.connectionString();
1100-
shell = TestShell.start({
1099+
shell = this.startTestShell({
11011100
args: [
11021101
connectionString,
11031102
'-u',
@@ -1144,6 +1143,5 @@ describe('Auth e2e', function () {
11441143

11451144
await client.close();
11461145
});
1147-
afterEach(TestShell.cleanup);
11481146
});
11491147
});

packages/e2e-tests/test/e2e-aws.spec.ts

+8-11
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import { expect } from 'chai';
22
import { spawnSync } from 'child_process';
3-
import { TestShell } from './test-shell';
43

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

120-
afterEach(TestShell.cleanup);
121-
122119
context('without environment variables being present', function () {
123120
context('specifying explicit parameters', function () {
124121
it('connects with access key and secret', async function () {
125-
const shell = TestShell.start({
122+
const shell = this.startTestShell({
126123
args: [
127124
getConnectionString(),
128125
'--username',
@@ -142,7 +139,7 @@ describe('e2e AWS AUTH', function () {
142139

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

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

180177
it('connects with access key, secret, and session token for IAM role', async function () {
181178
const tokenDetails = generateIamSessionToken();
182-
const shell = TestShell.start({
179+
const shell = this.startTestShell({
183180
args: [
184181
`${getConnectionString(
185182
tokenDetails.key,
@@ -203,7 +200,7 @@ describe('e2e AWS AUTH', function () {
203200
context('with AWS environment variables', function () {
204201
context('without any other parameters', function () {
205202
it('connects for the IAM user', async function () {
206-
const shell = TestShell.start({
203+
const shell = this.startTestShell({
207204
args: [getConnectionString()],
208205
env: {
209206
...process.env,
@@ -222,7 +219,7 @@ describe('e2e AWS AUTH', function () {
222219

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

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

269266
it('connects for the IAM role session', async function () {
270267
const tokenDetails = generateIamSessionToken();
271-
const shell = TestShell.start({
268+
const shell = this.startTestShell({
272269
args: [
273270
getConnectionString(),
274271
'--username',

packages/e2e-tests/test/e2e-banners.spec.ts

+4-5
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,16 @@ import {
22
skipIfApiStrict,
33
startSharedTestServer,
44
} from '../../../testing/integration-testing-hooks';
5-
import { TestShell } from './test-shell';
5+
import type { TestShell } from './test-shell';
66

77
describe('e2e startup banners', function () {
88
skipIfApiStrict();
9-
afterEach(TestShell.cleanup);
109

1110
const testServer = startSharedTestServer();
1211

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

3231
beforeEach(async function () {
33-
helperShell = TestShell.start({
32+
helperShell = this.startTestShell({
3433
args: [await testServer.connectionString()],
3534
});
3635
await helperShell.waitForPrompt();
@@ -47,7 +46,7 @@ describe('e2e startup banners', function () {
4746
});
4847

4948
it('shows automation notices', async function () {
50-
const shell = TestShell.start({
49+
const shell = this.startTestShell({
5150
args: [await testServer.connectionString()],
5251
});
5352
await shell.waitForPrompt();

packages/e2e-tests/test/e2e-bson.spec.ts

+3-3
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { expect } from 'chai';
22
import type { Db } from 'mongodb';
33
import { MongoClient } from 'mongodb';
44
import { bson } from '@mongosh/service-provider-core';
5-
import { TestShell } from './test-shell';
5+
import type { TestShell } from './test-shell';
66
import { startSharedTestServer } from '../../../testing/integration-testing-hooks';
77

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

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

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

3131
await client.close();
3232
});
33-
afterEach(TestShell.cleanup);
33+
3434
describe('printed BSON', function () {
3535
const outputDoc = {
3636
ObjectId: "ObjectId('5f16b8bebe434dc98cdfc9ca')",

0 commit comments

Comments
 (0)