Skip to content

Commit add5de6

Browse files
authored
chore(e2e): enable e2e tests to run directly against Atlas (#6381)
* enable test against atlas * use const * update function * comments * fix if check * comments * comments
1 parent c78c97d commit add5de6

File tree

3 files changed

+156
-82
lines changed

3 files changed

+156
-82
lines changed

packages/compass-e2e-tests/helpers/commands/connect-form.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import {
88
DEFAULT_CONNECTION_NAME_2,
99
DEFAULT_CONNECTION_STRING_1,
1010
DEFAULT_CONNECTION_STRING_2,
11+
TEST_ATLAS_CLOUD_EXTERNAL_URL,
1112
TEST_MULTIPLE_CONNECTIONS,
1213
} from '../compass';
1314
import Debug from 'debug';
@@ -986,6 +987,12 @@ export async function setupDefaultConnections(browser: CompassBrowser) {
986987
whereas we do have some tests that try and use those. We can easily change
987988
this in future if needed, though.
988989
*/
990+
991+
// no need to setup connections if we are running against Atlas
992+
if (TEST_ATLAS_CLOUD_EXTERNAL_URL) {
993+
return;
994+
}
995+
989996
for (const connectionName of [
990997
DEFAULT_CONNECTION_NAME_1,
991998
DEFAULT_CONNECTION_NAME_2,

packages/compass-e2e-tests/helpers/compass.ts

Lines changed: 67 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,10 @@ let MONGODB_USE_ENTERPRISE =
4646

4747
// should we test compass-web (true) or compass electron (false)?
4848
export const TEST_COMPASS_WEB = process.argv.includes('--test-compass-web');
49+
export const TEST_ATLAS_CLOUD_EXTERNAL_URL =
50+
process.env.TEST_ATLAS_CLOUD_EXTERNAL_URL;
51+
export const TEST_ATLAS_CLOUD_EXTERNAL_GROUP_ID =
52+
process.env.TEST_ATLAS_CLOUD_EXTERNAL_GROUP_ID;
4953
// multiple connections is now the default
5054
export const TEST_MULTIPLE_CONNECTIONS = true;
5155

@@ -75,19 +79,22 @@ export const MONGODB_TEST_SERVER_PORT = Number(
7579
process.env.MONGODB_TEST_SERVER_PORT ?? 27091
7680
);
7781

78-
export const DEFAULT_CONNECTION_STRING_1 = `mongodb://127.0.0.1:${MONGODB_TEST_SERVER_PORT}/test`;
82+
export const DEFAULT_CONNECTION_STRING_1 =
83+
process.env.TEST_ATLAS_CLOUD_EXTERNAL_CONNECTION_STRING_1 ||
84+
`mongodb://127.0.0.1:${MONGODB_TEST_SERVER_PORT}/test`;
7985
// NOTE: in browser.setupDefaultConnections() we don't give the first connection an
8086
// explicit name, so it gets a calculated one based off the connection string
81-
export const DEFAULT_CONNECTION_NAME_1 = connectionNameFromString(
82-
DEFAULT_CONNECTION_STRING_1
83-
);
87+
export const DEFAULT_CONNECTION_NAME_1 =
88+
process.env.TEST_ATLAS_CLOUD_EXTERNAL_CONNECTION_NAME_1 ||
89+
connectionNameFromString(DEFAULT_CONNECTION_STRING_1);
8490

8591
// for testing multiple connections
86-
export const DEFAULT_CONNECTION_STRING_2 = `mongodb://127.0.0.1:${
87-
MONGODB_TEST_SERVER_PORT + 1
88-
}/test`;
92+
export const DEFAULT_CONNECTION_STRING_2 =
93+
process.env.TEST_ATLAS_CLOUD_EXTERNAL_CONNECTION_STRING_2 ||
94+
`mongodb://127.0.0.1:${MONGODB_TEST_SERVER_PORT + 1}/test`;
8995
// NOTE: in browser.setupDefaultConnections() the second connection gets given an explicit name
90-
export const DEFAULT_CONNECTION_NAME_2 = 'connection-2';
96+
export const DEFAULT_CONNECTION_NAME_2 =
97+
process.env.TEST_ATLAS_CLOUD_EXTERNAL_CONNECTION_NAME_2 || 'connection-2';
9198

9299
export function updateMongoDBServerInfo() {
93100
try {
@@ -106,7 +113,7 @@ export function updateMongoDBServerInfo() {
106113
'server-info',
107114
'--',
108115
'--connectionString',
109-
`mongodb://127.0.0.1:${String(MONGODB_TEST_SERVER_PORT)}`,
116+
DEFAULT_CONNECTION_STRING_1,
110117
],
111118
{ encoding: 'utf-8' }
112119
);
@@ -761,6 +768,16 @@ async function startCompassElectron(
761768
return compass;
762769
}
763770

771+
export type StoredAtlasCloudCookies = {
772+
name: string;
773+
value: string;
774+
domain: string;
775+
path: string;
776+
secure: boolean;
777+
httpOnly: boolean;
778+
expirationDate: number;
779+
}[];
780+
764781
export async function startBrowser(
765782
name: string,
766783
// eslint-disable-next-line @typescript-eslint/no-unused-vars
@@ -787,7 +804,47 @@ export async function startBrowser(
787804
...webdriverOptions,
788805
...wdioOptions,
789806
})) as CompassBrowser;
790-
await browser.navigateTo('http://localhost:7777/');
807+
808+
if (TEST_ATLAS_CLOUD_EXTERNAL_URL) {
809+
// Navigate to a 404 page to set cookies
810+
await browser.navigateTo(`https://${TEST_ATLAS_CLOUD_EXTERNAL_URL}/404`);
811+
812+
const cookiesFile = process.env.TEST_ATLAS_CLOUD_EXTERNAL_COOKIES_FILE;
813+
if (!cookiesFile) {
814+
throw new Error(
815+
'TEST_ATLAS_CLOUD_EXTERNAL_URL is set but TEST_ATLAS_CLOUD_EXTERNAL_COOKIES_FILE is not. Please set TEST_ATLAS_CLOUD_EXTERNAL_COOKIES_FILE to the path of the cookies file.'
816+
);
817+
}
818+
const cookies: StoredAtlasCloudCookies = JSON.parse(
819+
await fs.readFile(cookiesFile, 'utf8')
820+
);
821+
822+
// These are the relevant cookies for auth:
823+
// https://github.com/10gen/mms/blob/6d27992a6ab9ab31471c8bcdaa4e347aa39f4013/server/src/features/com/xgen/svc/cukes/helpers/Client.java#L122-L130
824+
await browser.setCookies(
825+
cookies
826+
.filter((cookie) => {
827+
cookie.name.includes('mmsa-') ||
828+
cookie.name.includes('mdb-sat') ||
829+
cookie.name.includes('mdb-srt');
830+
})
831+
.map((cookie) => ({
832+
name: cookie.name,
833+
value: cookie.value,
834+
domain: cookie.domain,
835+
path: cookie.path,
836+
secure: cookie.secure,
837+
httpOnly: cookie.httpOnly,
838+
}))
839+
);
840+
841+
await browser.navigateTo(
842+
`https://${TEST_ATLAS_CLOUD_EXTERNAL_URL}/v2/${TEST_ATLAS_CLOUD_EXTERNAL_GROUP_ID}#/explorer`
843+
);
844+
} else {
845+
await browser.navigateTo('http://localhost:7777/');
846+
}
847+
791848
const compass = new Compass(name, browser, {
792849
mode: 'web',
793850
writeCoverage: false,

packages/compass-e2e-tests/index.ts

Lines changed: 82 additions & 72 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import {
1818
LOG_PATH,
1919
removeUserDataDir,
2020
updateMongoDBServerInfo,
21+
TEST_ATLAS_CLOUD_EXTERNAL_URL,
2122
} from './helpers/compass';
2223
import ResultLogger from './helpers/result-logger';
2324

@@ -60,52 +61,54 @@ async function setup() {
6061
const disableStartStop = process.argv.includes('--disable-start-stop');
6162
const shouldTestCompassWeb = process.argv.includes('--test-compass-web');
6263

63-
// When working on the tests it is faster to just keep the server running.
64-
if (!disableStartStop) {
65-
debug('Starting MongoDB server');
66-
crossSpawn.sync('npm', ['run', 'start-servers'], { stdio: 'inherit' });
67-
68-
if (shouldTestCompassWeb) {
69-
debug('Starting Compass Web');
70-
compassWeb = crossSpawn.spawn(
71-
'npm',
72-
['run', '--unsafe-perm', 'start-web'],
73-
{
74-
cwd: path.resolve(__dirname, '..', '..'),
75-
env: {
76-
...process.env,
77-
OPEN_BROWSER: 'false', // tell webpack dev server not to open the default browser
78-
DISABLE_DEVSERVER_OVERLAY: 'true',
79-
APP_ENV: 'webdriverio',
80-
},
64+
if (!TEST_ATLAS_CLOUD_EXTERNAL_URL) {
65+
// When working on the tests it is faster to just keep the server running.
66+
if (!disableStartStop) {
67+
debug('Starting MongoDB server');
68+
crossSpawn.sync('npm', ['run', 'start-servers'], { stdio: 'inherit' });
69+
70+
if (shouldTestCompassWeb) {
71+
debug('Starting Compass Web');
72+
compassWeb = crossSpawn.spawn(
73+
'npm',
74+
['run', '--unsafe-perm', 'start-web'],
75+
{
76+
cwd: path.resolve(__dirname, '..', '..'),
77+
env: {
78+
...process.env,
79+
OPEN_BROWSER: 'false', // tell webpack dev server not to open the default browser
80+
DISABLE_DEVSERVER_OVERLAY: 'true',
81+
APP_ENV: 'webdriverio',
82+
},
83+
}
84+
);
85+
86+
compassWeb.stdout.pipe(process.stdout);
87+
compassWeb.stderr.pipe(process.stderr);
88+
89+
let serverReady = false;
90+
const start = Date.now();
91+
while (!serverReady) {
92+
if (Date.now() - start >= 120_000) {
93+
throw new Error(
94+
'The compass-web sandbox is still not running after 120000ms'
95+
);
96+
}
97+
try {
98+
const res = await fetch('http://localhost:7777');
99+
serverReady = res.ok;
100+
debug('Web server ready: %s', serverReady);
101+
} catch (err) {
102+
debug('Failed to connect to dev server: %s', (err as any).message);
103+
}
104+
await wait(1000);
81105
}
82-
);
83-
84-
compassWeb.stdout.pipe(process.stdout);
85-
compassWeb.stderr.pipe(process.stderr);
86-
87-
let serverReady = false;
88-
const start = Date.now();
89-
while (!serverReady) {
90-
if (Date.now() - start >= 120_000) {
91-
throw new Error(
92-
'The compass-web sandbox is still not running after 120000ms'
93-
);
94-
}
95-
try {
96-
const res = await fetch('http://localhost:7777');
97-
serverReady = res.ok;
98-
debug('Web server ready: %s', serverReady);
99-
} catch (err) {
100-
debug('Failed to connect to dev server: %s', (err as any).message);
101-
}
102-
await wait(1000);
106+
} else {
107+
debug('Writing electron-versions.json');
108+
crossSpawn.sync('scripts/write-electron-versions.sh', [], {
109+
stdio: 'inherit',
110+
});
103111
}
104-
} else {
105-
debug('Writing electron-versions.json');
106-
crossSpawn.sync('scripts/write-electron-versions.sh', [], {
107-
stdio: 'inherit',
108-
});
109112
}
110113
}
111114

@@ -139,34 +142,36 @@ function cleanup() {
139142
const disableStartStop = process.argv.includes('--disable-start-stop');
140143
const shouldTestCompassWeb = process.argv.includes('--test-compass-web');
141144

142-
if (!disableStartStop) {
143-
if (shouldTestCompassWeb) {
144-
debug('Stopping compass-web');
145-
try {
146-
if (compassWeb.pid) {
147-
debug(`killing compass-web [${compassWeb.pid}]`);
148-
kill(compassWeb.pid, 'SIGINT');
149-
} else {
150-
debug('no pid for compass-web');
145+
if (!TEST_ATLAS_CLOUD_EXTERNAL_URL) {
146+
if (!disableStartStop) {
147+
if (shouldTestCompassWeb) {
148+
debug('Stopping compass-web');
149+
try {
150+
if (compassWeb.pid) {
151+
debug(`killing compass-web [${compassWeb.pid}]`);
152+
kill(compassWeb.pid, 'SIGINT');
153+
} else {
154+
debug('no pid for compass-web');
155+
}
156+
} catch (e) {
157+
debug('Failed to stop compass-web', e);
151158
}
152-
} catch (e) {
153-
debug('Failed to stop compass-web', e);
154159
}
155-
}
156160

157-
debug('Stopping MongoDB server');
158-
try {
159-
crossSpawn.sync('npm', ['run', 'stop-servers'], {
160-
// If it's taking too long we might as well kill the process and move on,
161-
// mongodb-runner is flaky sometimes and in ci `posttest-ci` script will
162-
// take care of additional clean up anyway
163-
timeout: 120_000,
164-
stdio: 'inherit',
165-
});
166-
} catch (e) {
167-
debug('Failed to stop MongoDB Server', e);
161+
debug('Stopping MongoDB server');
162+
try {
163+
crossSpawn.sync('npm', ['run', 'stop-servers'], {
164+
// If it's taking too long we might as well kill the process and move on,
165+
// mongodb-runner is flaky sometimes and in ci `posttest-ci` script will
166+
// take care of additional clean up anyway
167+
timeout: 120_000,
168+
stdio: 'inherit',
169+
});
170+
} catch (e) {
171+
debug('Failed to stop MongoDB Server', e);
172+
}
173+
debug('Done stopping');
168174
}
169-
debug('Done stopping');
170175
}
171176

172177
// Since the webdriverio update something is messing with the terminal's
@@ -258,9 +263,10 @@ async function main() {
258263

259264
const e2eTestGroupsAmount = parseInt(process.env.E2E_TEST_GROUPS || '1');
260265
const e2eTestGroup = parseInt(process.env.E2E_TEST_GROUP || '1');
266+
const e2eTestFilter = process.env.E2E_TEST_FILTER || '*';
261267

262-
const rawTests = (
263-
await glob('tests/**/*.{test,spec}.ts', {
268+
const tests = (
269+
await glob(`tests/**/${e2eTestFilter}.{test,spec}.ts`, {
264270
cwd: __dirname,
265271
})
266272
).filter((value, index, array) => {
@@ -271,15 +277,19 @@ async function main() {
271277
return index >= minGroupIndex && index <= maxGroupIndex;
272278
});
273279

274-
console.info('Test files:', rawTests);
280+
console.info('Test files:', tests);
275281

276282
// The only test file that's interested in the first-run experience (at the
277283
// time of writing) is time-to-first-query.ts and that happens to be
278284
// alphabetically right at the end. Which is fine, but the first test to run
279285
// will also get the slow first run experience for no good reason unless it is
280286
// the time-to-first-query.ts test.
281287
// So yeah.. this is a bit of a micro optimisation.
282-
const tests = [FIRST_TEST, ...rawTests.filter((t) => t !== FIRST_TEST)];
288+
tests.sort((a, b) => {
289+
if (a === FIRST_TEST) return -1;
290+
else if (b === FIRST_TEST) return 1;
291+
else return 0;
292+
});
283293

284294
// Ensure the insert-data mocha hooks are run.
285295
tests.unshift(path.join('helpers', 'insert-data.ts'));

0 commit comments

Comments
 (0)