Skip to content

Commit 6942b5c

Browse files
authored
chore(e2e-tests): consistently wait for successful exits of shell MONGOSH-1943 (#2301)
Do not use unqualified `shell.waitForExit()` in cases where we do not validate the exit code or check that no errors are displayed in the output, just assuming that the shell exited successfully, but instead intentionally always validate that these conditions are true in the cases in which we expect them to be.
1 parent fdf6b6d commit 6942b5c

File tree

9 files changed

+59
-67
lines changed

9 files changed

+59
-67
lines changed

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -998,7 +998,7 @@ describe('Auth e2e', function () {
998998
],
999999
});
10001000
if (
1001-
(await preTestShell.waitForExit()) === 1 &&
1001+
(await preTestShell.waitForAnyExit()) === 1 &&
10021002
preTestShell.output.match(
10031003
/digital envelope routines::unsupported|SSL routines::library has no ciphers/
10041004
)
@@ -1022,7 +1022,7 @@ describe('Auth e2e', function () {
10221022
'SCRAM-SHA-1',
10231023
],
10241024
});
1025-
await shell.waitForExit();
1025+
await shell.waitForAnyExit();
10261026
try {
10271027
shell.assertContainsOutput(
10281028
'Auth mechanism SCRAM-SHA-1 is not supported in FIPS mode'
@@ -1109,7 +1109,7 @@ describe('Auth e2e', function () {
11091109
'GSSAPI',
11101110
],
11111111
});
1112-
await shell.waitForExit();
1112+
await shell.waitForAnyExit();
11131113
// Failing to auth with kerberos fails with different error messages on each OS.
11141114
// Sometimes in CI, it also fails because the server received kerberos
11151115
// credentials, most likely because of a successful login by another

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -344,7 +344,7 @@ describe('e2e direct connection', function () {
344344
const shell = this.startTestShell({
345345
args: ['--host', hostlist, 'admin'],
346346
});
347-
await shell.waitForExit();
347+
await shell.waitForAnyExit();
348348
shell.assertContainsOutput('MongoServerSelectionError');
349349
});
350350

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

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -249,7 +249,7 @@ describe('OIDC auth e2e', function () {
249249
'--browser=false',
250250
],
251251
});
252-
await shell.waitForExit();
252+
await shell.waitForAnyExit();
253253
shell.assertContainsOutput(
254254
'Consider specifying --oidcFlows=auth-code,device-auth if you are running mongosh in an environment without browser access'
255255
);
@@ -405,7 +405,7 @@ describe('OIDC auth e2e', function () {
405405
MONGOSH_E2E_TEST_CURL_ALLOW_INVALID_TLS: '1',
406406
},
407407
});
408-
await shell.waitForExit();
408+
await shell.waitForAnyExit();
409409
// We cannot make the mongod server accept the mock IdP's certificate,
410410
// so the best we can verify here is that auth failed *on the server*
411411
shell.assertContainsOutput(/MongoServerError: Authentication failed/);
@@ -435,7 +435,7 @@ describe('OIDC auth e2e', function () {
435435
},
436436
});
437437

438-
await shell.waitForExit();
438+
await shell.waitForAnyExit();
439439
// We cannot make the mongod server accept the mock IdP's certificate,
440440
// so the best we can verify here is that auth failed *on the server*
441441
shell.assertContainsOutput(/MongoServerError: Authentication failed/);
@@ -499,7 +499,7 @@ describe('OIDC auth e2e', function () {
499499
'--eval=42',
500500
],
501501
});
502-
await shell.waitForExit();
502+
await shell.waitForSuccessfulExit();
503503

504504
shell.assertContainsOutput('BEGIN OIDC TOKEN DUMP');
505505
shell.assertContainsOutput('"tokenType": "Bearer"');
@@ -519,7 +519,7 @@ describe('OIDC auth e2e', function () {
519519
'--eval=42',
520520
],
521521
});
522-
await shell.waitForExit();
522+
await shell.waitForSuccessfulExit();
523523

524524
shell.assertContainsOutput('BEGIN OIDC TOKEN DUMP');
525525
shell.assertContainsOutput('"tokenType": "Bearer"');

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -271,7 +271,7 @@ describe('e2e proxy support', function () {
271271
},
272272
});
273273

274-
const code = await shell.waitForExit();
274+
const code = await shell.waitForAnyExit();
275275
expect(code).to.equal(1);
276276
});
277277

@@ -622,7 +622,7 @@ describe('e2e proxy support', function () {
622622
},
623623
});
624624

625-
await shell.waitForExit();
625+
await shell.waitForAnyExit();
626626
// We cannot make the mongod server accept the mock IdP's certificate,
627627
// so the best we can verify here is that auth failed *on the server*
628628
shell.assertContainsOutput(/MongoServerError: Authentication failed/);

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ describe('snippet integration tests', function () {
8787
'config.set("snippetIndexSourceURLs", "http://localhost:1/")'
8888
);
8989
shell.writeInputLine('exit');
90-
await shell.waitForExit();
90+
await shell.waitForSuccessfulExit();
9191

9292
shell = makeTestShell();
9393
await shell.waitForPrompt();

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -92,8 +92,8 @@ describe('e2e TLS', function () {
9292
});
9393
await shell.waitForPrompt();
9494
await shell.executeLine('db.shutdownServer({ force: true })');
95-
shell.kill();
96-
await shell.waitForExit();
95+
shell.writeInputLine('exit');
96+
await shell.waitForAnyExit(); // closing the server may lead to an error being displayed
9797
});
9898

9999
const server = startTestServer('e2e-tls-no-cli-valid-srv', {
@@ -399,8 +399,8 @@ describe('e2e TLS', function () {
399399
});
400400
await shell.waitForPrompt();
401401
await shell.executeLine('db.shutdownServer({ force: true })');
402-
shell.kill();
403-
await shell.waitForExit();
402+
shell.writeInputLine('exit');
403+
await shell.waitForAnyExit(); // closing the server may lead to an error being displayed
404404
});
405405

406406
const server = startTestServer('e2e-tls-valid-cli-valid-srv', {

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

Lines changed: 30 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -35,19 +35,17 @@ describe('e2e', function () {
3535
describe('--version', function () {
3636
it('shows version', async function () {
3737
const shell = this.startTestShell({ args: ['--version'] });
38-
await shell.waitForExit();
38+
await shell.waitForSuccessfulExit();
3939

40-
shell.assertNoErrors();
4140
shell.assertContainsOutput(require('../package.json').version);
4241
});
4342
});
4443

4544
describe('--build-info', function () {
4645
it('shows build info in JSON format', async function () {
4746
const shell = this.startTestShell({ args: ['--build-info'] });
48-
await shell.waitForExit();
47+
await shell.waitForSuccessfulExit();
4948

50-
shell.assertNoErrors();
5149
const data = JSON.parse(shell.output);
5250
expect(Object.keys(data)).to.deep.equal([
5351
'version',
@@ -101,7 +99,7 @@ describe('e2e', function () {
10199
'process.report.getReport()',
102100
],
103101
});
104-
await shell.waitForExit();
102+
await shell.waitForSuccessfulExit();
105103
processReport = JSON.parse(shell.output);
106104
}
107105
expect(data.runtimeGlibcVersion).to.equal(
@@ -118,8 +116,7 @@ describe('e2e', function () {
118116
'--nodb',
119117
],
120118
});
121-
await shell.waitForExit();
122-
shell.assertNoErrors();
119+
await shell.waitForSuccessfulExit();
123120
const deps = JSON.parse(shell.output);
124121
expect(deps.nodeDriverVersion).to.be.a('string');
125122
expect(deps.libmongocryptVersion).to.be.a('string');
@@ -176,34 +173,28 @@ describe('e2e', function () {
176173
});
177174
});
178175
it('closes the shell when "exit" is entered', async function () {
179-
const onExit = shell.waitForExit();
180176
shell.writeInputLine('exit');
181-
expect(await onExit).to.equal(0);
177+
await shell.waitForSuccessfulExit();
182178
});
183179
it('closes the shell when "quit" is entered', async function () {
184-
const onExit = shell.waitForExit();
185180
shell.writeInputLine('quit');
186-
expect(await onExit).to.equal(0);
181+
await shell.waitForSuccessfulExit();
187182
});
188183
it('closes the shell with the specified exit code when "exit(n)" is entered', async function () {
189-
const onExit = shell.waitForExit();
190184
shell.writeInputLine('exit(42)');
191-
expect(await onExit).to.equal(42);
185+
expect(await shell.waitForAnyExit()).to.equal(42);
192186
});
193187
it('closes the shell with the specified exit code when "quit(n)" is entered', async function () {
194-
const onExit = shell.waitForExit();
195188
shell.writeInputLine('quit(42)');
196-
expect(await onExit).to.equal(42);
189+
expect(await shell.waitForAnyExit()).to.equal(42);
197190
});
198191
it('closes the shell with the pre-specified exit code when "exit" is entered', async function () {
199-
const onExit = shell.waitForExit();
200192
shell.writeInputLine('process.exitCode = 42; exit()');
201-
expect(await onExit).to.equal(42);
193+
expect(await shell.waitForAnyExit()).to.equal(42);
202194
});
203195
it('closes the shell with the pre-specified exit code when "quit" is entered', async function () {
204-
const onExit = shell.waitForExit();
205196
shell.writeInputLine('process.exitCode = 42; quit()');
206-
expect(await onExit).to.equal(42);
197+
expect(await shell.waitForAnyExit()).to.equal(42);
207198
});
208199
it('decorates internal errors with bug reporting information', async function () {
209200
const err = await shell.executeLine(
@@ -300,10 +291,8 @@ describe('e2e', function () {
300291
'sleep(100);print([1,2,3,4,5,6,7,8,9,10].reduce(\n(a,b) => { return a*b; }, 1))\n\n\n\n',
301292
{ end: true }
302293
);
303-
const exitCode = await shell.waitForExit();
304-
expect(exitCode).to.equal(0);
294+
await shell.waitForSuccessfulExit();
305295
shell.assertContainsOutput('3628800');
306-
shell.assertNoErrors();
307296
});
308297
});
309298

@@ -1095,9 +1084,8 @@ describe('e2e', function () {
10951084
'[db.hello()].reduce(\n() => { return 11111*11111; },0)\n\n\n',
10961085
{ end: true }
10971086
);
1098-
await shell.waitForExit();
1087+
await shell.waitForSuccessfulExit();
10991088
shell.assertContainsOutput('123454321');
1100-
shell.assertNoErrors();
11011089
});
11021090
});
11031091

@@ -1162,8 +1150,7 @@ describe('e2e', function () {
11621150
// when run under coverage, as we currently specify the location of
11631151
// coverage files via a relative path and nyc fails to write to that
11641152
// when started from a changed cwd.
1165-
await shell.waitForExit();
1166-
shell.assertNoErrors();
1153+
await shell.waitForSuccessfulExit();
11671154
});
11681155

11691156
if (!jsContextFlags.includes('--jsContext=plain-vm')) {
@@ -1202,7 +1189,7 @@ describe('e2e', function () {
12021189
await eventually(() => {
12031190
shell.assertContainsOutput('Error: uh oh');
12041191
});
1205-
expect(await shell.waitForExit()).to.equal(1);
1192+
expect(await shell.waitForAnyExit()).to.equal(1);
12061193
});
12071194
}
12081195
});
@@ -1216,8 +1203,7 @@ describe('e2e', function () {
12161203
await eventually(() => {
12171204
shell.assertContainsOutput('hello one');
12181205
});
1219-
expect(await shell.waitForExit()).to.equal(0);
1220-
shell.assertNoErrors();
1206+
await shell.waitForSuccessfulExit();
12211207
});
12221208

12231209
if (!jsContextFlags.includes('--jsContext=plain-vm')) {
@@ -1244,7 +1230,7 @@ describe('e2e', function () {
12441230
await eventually(() => {
12451231
shell.assertContainsOutput('Error: uh oh');
12461232
});
1247-
expect(await shell.waitForExit()).to.equal(1);
1233+
expect(await shell.waitForAnyExit()).to.equal(1);
12481234
});
12491235

12501236
it('fails with the error if the loaded script throws asynchronously (setImmediate)', async function () {
@@ -1259,7 +1245,7 @@ describe('e2e', function () {
12591245
await eventually(() => {
12601246
shell.assertContainsOutput('Error: uh oh');
12611247
});
1262-
expect(await shell.waitForExit()).to.equal(
1248+
expect(await shell.waitForAnyExit()).to.equal(
12631249
jsContextFlags.includes('--jsContext=repl') ? 0 : 1
12641250
);
12651251
});
@@ -1276,7 +1262,7 @@ describe('e2e', function () {
12761262
await eventually(() => {
12771263
shell.assertContainsOutput('Error: uh oh');
12781264
});
1279-
expect(await shell.waitForExit()).to.equal(
1265+
expect(await shell.waitForAnyExit()).to.equal(
12801266
jsContextFlags.includes('--jsContext=repl') ? 0 : 1
12811267
);
12821268
});
@@ -1299,7 +1285,7 @@ describe('e2e', function () {
12991285
script,
13001286
],
13011287
});
1302-
expect(await shell.waitForExit()).to.equal(0);
1288+
await shell.waitForSuccessfulExit();
13031289

13041290
// Check that:
13051291
// - the script runs in the expected environment
@@ -1512,7 +1498,7 @@ describe('e2e', function () {
15121498
}
15131499
await shell.executeLine('a = 42');
15141500
shell.writeInput('.exit\n');
1515-
await shell.waitForExit();
1501+
await shell.waitForSuccessfulExit();
15161502

15171503
shell = await startTestShell();
15181504
// Arrow up twice to skip the .exit line
@@ -1521,7 +1507,7 @@ describe('e2e', function () {
15211507
expect(shell.output).to.include('a = 42');
15221508
});
15231509
shell.writeInput('\n.exit\n');
1524-
await shell.waitForExit();
1510+
await shell.waitForSuccessfulExit();
15251511

15261512
expect(await fs.readFile(historyPath, 'utf8')).to.match(/^a = 42$/m);
15271513
});
@@ -1533,7 +1519,7 @@ describe('e2e', function () {
15331519

15341520
await shell.executeLine('a = 42');
15351521
shell.writeInput('.exit\n');
1536-
await shell.waitForExit();
1522+
await shell.waitForSuccessfulExit();
15371523

15381524
expect((await fs.stat(historyPath)).mode & 0o077).to.equal(0);
15391525
});
@@ -1544,7 +1530,7 @@ describe('e2e', function () {
15441530
await shell.executeLine('a = 42');
15451531
await shell.executeLine('foo = "bar"');
15461532
shell.writeInput('.exit\n');
1547-
await shell.waitForExit();
1533+
await shell.waitForAnyExit(); // db.auth() call fails because of --nodb
15481534

15491535
const contents = await fs.readFile(historyPath, 'utf8');
15501536
expect(contents).to.not.match(/mypassword/);
@@ -1603,7 +1589,7 @@ describe('e2e', function () {
16031589
`config.set("updateURL", ${JSON.stringify(httpServerUrl)})`
16041590
);
16051591
shell.writeInputLine('exit');
1606-
await shell.waitForExit();
1592+
await shell.waitForSuccessfulExit();
16071593
}
16081594

16091595
delete env.CI;
@@ -1623,13 +1609,13 @@ describe('e2e', function () {
16231609
).to.be.a('string');
16241610
});
16251611
shell.writeInputLine('exit');
1626-
await shell.waitForExit();
1612+
await shell.waitForSuccessfulExit();
16271613
}
16281614

16291615
{
16301616
const shell = await startTestShell();
16311617
shell.writeInputLine('exit');
1632-
await shell.waitForExit();
1618+
await shell.waitForSuccessfulExit();
16331619
shell.assertContainsOutput(
16341620
'mongosh 2023.4.15 is available for download: https://www.mongodb.com/try/download/shell'
16351621
);
@@ -1799,7 +1785,7 @@ describe('e2e', function () {
17991785
'.mongodb.net/',
18001786
],
18011787
});
1802-
const exitCode = await shell.waitForExit();
1788+
const exitCode = await shell.waitForAnyExit();
18031789
expect(exitCode).to.equal(1);
18041790
});
18051791

@@ -1813,7 +1799,7 @@ describe('e2e', function () {
18131799
'.mongodb.net/',
18141800
],
18151801
});
1816-
const exitCode = await shell.waitForExit();
1802+
const exitCode = await shell.waitForAnyExit();
18171803
expect(exitCode).to.equal(1);
18181804
});
18191805

@@ -1904,7 +1890,7 @@ describe('e2e', function () {
19041890
);
19051891

19061892
shell.writeInputLine('exit');
1907-
await shell.waitForExit();
1893+
await shell.waitForSuccessfulExit();
19081894
shell.assertNoErrors();
19091895
});
19101896
});
@@ -1937,7 +1923,7 @@ describe('e2e', function () {
19371923
args: [filename],
19381924
env: { ...process.env, MONGOSH_RUN_NODE_SCRIPT: '1' },
19391925
});
1940-
expect(await shell.waitForExit()).to.equal(0);
1926+
await shell.waitForSuccessfulExit();
19411927
shell.assertContainsOutput('610');
19421928
});
19431929
});

0 commit comments

Comments
 (0)