Skip to content

Commit 477eab3

Browse files
committed
chore(e2e): adjust the scripts to make it easier to run atlas cloud e2e tests locally
1 parent 44b9988 commit 477eab3

File tree

2 files changed

+97
-47
lines changed

2 files changed

+97
-47
lines changed
Lines changed: 79 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,61 @@
11
#!/bin/bash
22

3+
# This script helps to automatically provision Atlas cluster for running the e2e
4+
# tests against. In CI this will always create a new cluster and delete it when
5+
# the test run is finished. You can also use this script locally to run e2e
6+
# tests against a "logged in" Atlas Cloud experience in compass-web sandbox.
7+
#
8+
# While the provisioning of clusters is automated, you should be aware that it
9+
# requires some extra environmental variables to be available when you are
10+
# running it. If you want to be able to run these e2e tests locally, following
11+
# steps are required:
12+
#
13+
# - Create a test Atlas user on one of the testing environments (-dev / -qa).
14+
# You can only use your work emails with a subaddress to create those (e.g,
15+
16+
#
17+
# - Setup a new org and project. Save the org id and project id for later.
18+
#
19+
# - Create new db user with username / password auth and admin role. This user
20+
# will be used to prepopulate dbs with data in tests. Save the credentials.
21+
#
22+
# - Create a new API key (Access Manager > Project Access > Create Application >
23+
# API Key) for the project you created and save the public and private keys.
24+
#
25+
# - (Optional) Deploy a cluster with a required configuration through Atlas
26+
# Cloud UI. If you skip the step, the script will deploy a default cluster for
27+
# you.
28+
#
29+
# - Make sure that you have the following environmental variables provided to
30+
# the script below:
31+
#
32+
# MCLI_OPS_MANAGER_URL API base url matching the environment you used to
33+
# create your user (https://cloud{-dev,-qa}.mongodb.com/)
34+
# MCLI_PUBLIC_API_KEY Public API key
35+
# MCLI_PRIVATE_API_KEY Private API key
36+
# MCLI_ORG_ID Org ID
37+
# MCLI_PROJECT_ID Project ID
38+
#
39+
# COMPASS_E2E_ATLAS_CLOUD_SANDBOX_USERNAME Cloud user you created
40+
# COMPASS_E2E_ATLAS_CLOUD_SANDBOX_PASSWORD Cloud user password
41+
# COMPASS_E2E_ATLAS_CLOUD_SANDBOX_DBUSER_USERNAME Db user for the project
42+
# COMPASS_E2E_ATLAS_CLOUD_SANDBOX_DBUSER_PASSWORD Db user password
43+
#
44+
# - Source the script followed by running the tests to make sure that some
45+
# variables exported from this script are available for the test env:
46+
#
47+
# (ATLAS_CLOUD_TEST_CLUSTER_NAME="TestCluster" source .evergreen/start-atlas-cloud-cluster.sh \
48+
# && npm run -w compass-e2e-tests test web -- --test-atlas-cloud-sandbox --test-filter="atlas-cloud/**/*")
49+
50+
_ATLAS_CLOUD_TEST_CLUSTER_NAME=${ATLAS_CLOUD_TEST_CLUSTER_NAME:-""}
51+
352
# Atlas limits the naming to something like /^[\w\d-]{,23}$/ (and will auto
453
# truncate if it's too long) so we're very limited in terms of how unique this
554
# name can be. Hopefully the epoch + part of git hash is enough for these to not
655
# overlap when tests are running
7-
ATLAS_CLOUD_TEST_CLUSTER_NAME="e2e-$(date +"%s")-$(git rev-parse HEAD)"
56+
DEFAULT_ATLAS_CLOUD_TEST_CLUSTER_NAME="e2e-$(date +"%s")-$(git rev-parse HEAD)"
57+
58+
ATLAS_CLUSTER_NAME="${_ATLAS_CLOUD_TEST_CLUSTER_NAME:-$DEFAULT_ATLAS_CLOUD_TEST_CLUSTER_NAME}"
859

960
function atlascli() {
1061
docker run \
@@ -17,23 +68,43 @@ function atlascli() {
1768
}
1869

1970
cleanup() {
20-
echo "Scheduling Atlas deployment \`$ATLAS_CLOUD_TEST_CLUSTER_NAME\` for deletion..."
21-
atlascli clusters delete $ATLAS_CLOUD_TEST_CLUSTER_NAME --force
71+
# Assuming that we want to preserve the cluster if the name was provided
72+
# outside of script. Helpful when trying to run the tests locally, you can
73+
# automatically create a cluster with a custom name for the first time, but
74+
# then re-use it when running the tests again. Don't forget to clean it up
75+
# after you're done!
76+
if [ -z "$_ATLAS_CLOUD_TEST_CLUSTER_NAME" ]; then
77+
echo "Scheduling Atlas deployment \`$ATLAS_CLUSTER_NAME\` for deletion..."
78+
atlascli clusters delete $ATLAS_CLUSTER_NAME --force
79+
else
80+
echo "Custom cluster name provided ($_ATLAS_CLOUD_TEST_CLUSTER_NAME), skipping cluster cleanup"
81+
fi
2282
}
2383

2484
trap cleanup EXIT
2585

26-
echo "Creating Atlas deployment \`$ATLAS_CLOUD_TEST_CLUSTER_NAME\` to test against..."
27-
atlascli clusters create $ATLAS_CLOUD_TEST_CLUSTER_NAME \
86+
echo "Creating Atlas deployment \`$ATLAS_CLUSTER_NAME\` to test against..."
87+
atlascli clusters create $ATLAS_CLUSTER_NAME \
2888
--provider AWS \
2989
--region US_EAST_1 \
3090
--tier M10
3191

3292
echo "Waiting for the deployment to be provisioned..."
33-
atlascli clusters watch "$ATLAS_CLOUD_TEST_CLUSTER_NAME"
93+
atlascli clusters watch $ATLAS_CLUSTER_NAME
3494

3595
echo "Getting connection string for provisioned cluster..."
36-
ATLAS_CLOUD_TEST_CLUSTER_CONNECTION_STRING_JSON="$(atlascli clusters connectionStrings describe $ATLAS_CLOUD_TEST_CLUSTER_NAME -o json)"
96+
CONNECTION_STRINGS_JSON="$(atlascli clusters connectionStrings describe $ATLAS_CLUSTER_NAME -o json)"
97+
98+
export COMPASS_E2e_ATLAS_CLOUD_SANDBOX_CLOUD_CONFIG=$(
99+
if [[ "$MCLI_OPS_MANAGER_URL" =~ "-dev" ]]; then
100+
echo "dev"
101+
elif [[ "$MCLI_OPS_MANAGER_URL" =~ "-qa" ]]; then
102+
echo "qa"
103+
else
104+
echo "prod"
105+
fi
106+
)
107+
echo "Cloud config: $COMPASS_E2e_ATLAS_CLOUD_SANDBOX_CLOUD_CONFIG"
37108

38-
export COMPASS_E2E_ATLAS_CLOUD_SANDBOX_DEFAULT_CONNECTIONS="{\"$ATLAS_CLOUD_TEST_CLUSTER_NAME\": $ATLAS_CLOUD_TEST_CLUSTER_CONNECTION_STRING_JSON}"
109+
export COMPASS_E2E_ATLAS_CLOUD_SANDBOX_DEFAULT_CONNECTIONS="{\"$ATLAS_CLUSTER_NAME\": $CONNECTION_STRINGS_JSON}"
39110
echo "Cluster connections: $COMPASS_E2E_ATLAS_CLOUD_SANDBOX_DEFAULT_CONNECTIONS"

packages/compass-e2e-tests/helpers/test-runner-context.ts

Lines changed: 18 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import {
44
} from '@mongodb-js/connection-info';
55
import type { MongoClusterOptions } from 'mongodb-runner';
66
import yargs from 'yargs';
7-
import type { Argv } from 'yargs';
7+
import type { Argv, CamelCase } from 'yargs';
88
import { hideBin } from 'yargs/helpers';
99
import Debug from 'debug';
1010
import fs from 'fs';
@@ -106,15 +106,17 @@ function buildDesktopArgs(yargs: Argv) {
106106
* make sure that the tests in mms are also updated to account for that
107107
*/
108108
const atlasCloudExternalArgs = [
109-
'test-atlas-cloud-external',
110109
'atlas-cloud-external-url',
111110
'atlas-cloud-external-project-id',
112111
'atlas-cloud-external-cookies-file',
113112
'atlas-cloud-external-default-connections-file',
114113
] as const;
115114

115+
type AtlasCloudExternalArgs =
116+
| typeof atlasCloudExternalArgs[number]
117+
| CamelCase<typeof atlasCloudExternalArgs[number]>;
118+
116119
const atlasCloudSandboxArgs = [
117-
'test-atlas-cloud-sandbox',
118120
'atlas-cloud-sandbox-cloud-config',
119121
'atlas-cloud-sandbox-username',
120122
'atlas-cloud-sandbox-password',
@@ -123,6 +125,10 @@ const atlasCloudSandboxArgs = [
123125
'atlas-cloud-sandbox-default-connections',
124126
] as const;
125127

128+
type AtlasCloudSandboxArgs =
129+
| typeof atlasCloudSandboxArgs[number]
130+
| CamelCase<typeof atlasCloudSandboxArgs[number]>;
131+
126132
let testEnv: 'desktop' | 'web' | undefined;
127133

128134
function buildWebArgs(yargs: Argv) {
@@ -191,13 +197,9 @@ function buildWebArgs(yargs: Argv) {
191197
description:
192198
'Stringified JSON with connections that are expected to be available in the Atlas project',
193199
})
194-
.implies(
195-
Object.fromEntries(
196-
atlasCloudSandboxArgs.map((arg) => {
197-
return [arg, atlasCloudSandboxArgs];
198-
})
199-
)
200-
)
200+
.implies({
201+
'test-atlas-cloud-sandbox': atlasCloudSandboxArgs,
202+
})
201203
.option('test-atlas-cloud-external', {
202204
type: 'boolean',
203205
description:
@@ -221,13 +223,9 @@ function buildWebArgs(yargs: Argv) {
221223
description:
222224
'File with JSON array of connections (following ConnectionInfo schema) that are expected to be available in the Atlas project',
223225
})
224-
.implies(
225-
Object.fromEntries(
226-
atlasCloudExternalArgs.map((arg) => {
227-
return [arg, atlasCloudExternalArgs];
228-
})
229-
)
230-
)
226+
.implies({
227+
'test-atlas-cloud-external': atlasCloudExternalArgs,
228+
})
231229
.conflicts({
232230
'test-atlas-cloud-external': 'test-atlas-cloud-sandbox',
233231
'test-atlas-cloud-sandbox': 'test-atlas-cloud-external',
@@ -310,42 +308,23 @@ export function assertTestingWeb(ctx = context): asserts ctx is WebParsedArgs {
310308
export function isTestingAtlasCloudExternal(
311309
ctx = context
312310
): ctx is WebParsedArgs & {
313-
[K in
314-
| 'testAtlasCloudExternal'
315-
| 'atlasCloudExternalUrl'
316-
| 'atlasCloudExternalProjectId'
317-
| 'atlasCloudExternalCookiesFile'
318-
| 'atlasCloudExternalDefaultConnectionsFile']: NonNullable<
319-
WebParsedArgs[K]
320-
>;
311+
[K in AtlasCloudExternalArgs]: NonNullable<WebParsedArgs[K]>;
321312
} {
322313
return isTestingWeb(ctx) && !!ctx.testAtlasCloudExternal;
323314
}
324315

325316
export function isTestingAtlasCloudSandbox(
326317
ctx = context
327318
): ctx is WebParsedArgs & {
328-
[K in
329-
| 'testAtlasCloudSandbox'
330-
| 'atlasCloudSandboxUsername'
331-
| 'atlasCloudSandboxPassword'
332-
| 'atlasCloudSandboxDbuserUsername'
333-
| 'atlasCloudSandboxDbuserPassword'
334-
| 'atlasCloudSandboxDefaultConnections']: NonNullable<WebParsedArgs[K]>;
319+
[K in AtlasCloudSandboxArgs]: NonNullable<WebParsedArgs[K]>;
335320
} {
336321
return isTestingWeb(ctx) && !!ctx.testAtlasCloudSandbox;
337322
}
338323

339324
export function assertTestingAtlasCloudSandbox(
340325
ctx = context
341326
): asserts ctx is WebParsedArgs & {
342-
[K in
343-
| 'testAtlasCloudSandbox'
344-
| 'atlasCloudSandboxUsername'
345-
| 'atlasCloudSandboxPassword'
346-
| 'atlasCloudSandboxDbuserUsername'
347-
| 'atlasCloudSandboxDbuserPassword'
348-
| 'atlasCloudSandboxDefaultConnections']: NonNullable<WebParsedArgs[K]>;
327+
[K in AtlasCloudSandboxArgs]: NonNullable<WebParsedArgs[K]>;
349328
} {
350329
if (!isTestingAtlasCloudSandbox(ctx)) {
351330
throw new Error(`Expected tested runtime to be web w/ Atlas Cloud account`);

0 commit comments

Comments
 (0)