11#! /bin/bash
22
3+ RUN_ID=" $( date +" %s" ) -$( git rev-parse --short HEAD) "
4+ DELETE_AFTER=" $( date -u -Iseconds -d ' +2 hours' 2> /dev/null || date -u -Iseconds -v ' +2H' ) "
5+
6+ # This script helps to automatically provision Atlas cluster for running the e2e
7+ # tests against. In CI this will always create a new cluster and delete it when
8+ # the test run is finished. You can also use this script locally to run e2e
9+ # tests against a "logged in" Atlas Cloud experience in compass-web sandbox.
10+ #
11+ # While the provisioning of clusters is automated, you should be aware that it
12+ # requires some extra environmental variables to be available when you are
13+ # running it. If you want to be able to run these e2e tests locally, following
14+ # steps are required:
15+ #
16+ # - Create a test Atlas user on one of the testing environments (-dev / -qa).
17+ # You can only use your work emails with a subaddress to create those (e.g,
18+ 19+ #
20+ # - Setup a new org and project. Save the org id and project id for later.
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+ #
42+ # - Source the script followed by running the tests to make sure that some
43+ # variables exported from this script are available for the test env:
44+ #
45+ # (ATLAS_CLOUD_TEST_CLUSTER_NAME="TestCluster" source .evergreen/start-atlas-cloud-cluster.sh \
46+ # && npm run -w compass-e2e-tests test web -- --test-atlas-cloud-sandbox --test-filter="atlas-cloud/**/*")
47+
48+ _ATLAS_CLOUD_TEST_CLUSTER_NAME=${ATLAS_CLOUD_TEST_CLUSTER_NAME:- " " }
49+
350# Atlas limits the naming to something like /^[\w\d-]{,23}$/ (and will auto
451# truncate if it's too long) so we're very limited in terms of how unique this
552# name can be. Hopefully the epoch + part of git hash is enough for these to not
653# overlap when tests are running
7- ATLAS_CLOUD_TEST_CLUSTER_NAME=" e2e-$( date +" %s" ) -$( git rev-parse HEAD) "
54+ DEFAULT_ATLAS_CLOUD_TEST_CLUSTER_NAME=" e2e-$RUN_ID "
55+
56+ ATLAS_CLUSTER_NAME=" ${_ATLAS_CLOUD_TEST_CLUSTER_NAME:- $DEFAULT_ATLAS_CLOUD_TEST_CLUSTER_NAME } "
57+
58+ ATLAS_TEST_DB_USERNAME=" testuser-$RUN_ID "
59+ ATLAS_TEST_DB_PASSWORD=" $( head -c 32 /dev/urandom | base64 | tr -dc ' a-zA-Z0-9' ) "
860
961function atlascli() {
1062 docker run \
@@ -17,23 +69,59 @@ function atlascli() {
1769}
1870
1971cleanup () {
20- echo " Scheduling Atlas deployment \` $ATLAS_CLOUD_TEST_CLUSTER_NAME \` for deletion..."
21- atlascli clusters delete $ATLAS_CLOUD_TEST_CLUSTER_NAME --force
72+ # Assuming that we want to preserve the cluster if the name was provided
73+ # outside of script. Helpful when trying to run the tests locally, you can
74+ # automatically create a cluster with a custom name for the first time, but
75+ # then re-use it when running the tests again. Don't forget to clean it up
76+ # after you're done!
77+ if [ -z " $_ATLAS_CLOUD_TEST_CLUSTER_NAME " ]; then
78+ echo " Scheduling Atlas deployment \` $ATLAS_CLUSTER_NAME \` for deletion..."
79+ atlascli clusters delete $ATLAS_CLUSTER_NAME --force
80+ else
81+ echo " Custom cluster name provided ($_ATLAS_CLOUD_TEST_CLUSTER_NAME ), skipping cluster cleanup"
82+ fi
83+ echo " Deleting Atlas db user \` $ATLAS_TEST_DB_USERNAME \` ..."
84+ atlascli dbusers delete $ATLAS_TEST_DB_USERNAME --force
2285}
2386
2487trap cleanup EXIT
2588
26- echo " Creating Atlas deployment \` $ATLAS_CLOUD_TEST_CLUSTER_NAME \` to test against..."
27- atlascli clusters create $ATLAS_CLOUD_TEST_CLUSTER_NAME \
89+ echo " Allowing access from current ip..."
90+ atlascli accessList create \
91+ --currentIp \
92+ --deleteAfter " $DELETE_AFTER "
93+
94+ echo " Creating Atlas db user \` $ATLAS_TEST_DB_USERNAME \` ..."
95+ atlascli dbusers create atlasAdmin \
96+ --username " $ATLAS_TEST_DB_USERNAME " \
97+ --password " $ATLAS_TEST_DB_PASSWORD " \
98+ --deleteAfter " $DELETE_AFTER " # so that it's autoremoved if cleaning up failed for some reason
99+
100+ export COMPASS_E2E_ATLAS_CLOUD_SANDBOX_DBUSER_USERNAME=" $ATLAS_TEST_DB_USERNAME "
101+ export COMPASS_E2E_ATLAS_CLOUD_SANDBOX_DBUSER_PASSWORD=" $ATLAS_TEST_DB_PASSWORD "
102+
103+ echo " Creating Atlas deployment \` $ATLAS_CLUSTER_NAME \` to test against..."
104+ atlascli clusters create $ATLAS_CLUSTER_NAME \
28105 --provider AWS \
29106 --region US_EAST_1 \
30107 --tier M10
31108
32109echo " Waiting for the deployment to be provisioned..."
33- atlascli clusters watch " $ATLAS_CLOUD_TEST_CLUSTER_NAME "
110+ atlascli clusters watch $ATLAS_CLUSTER_NAME
34111
35112echo " 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) "
113+ CONNECTION_STRINGS_JSON=" $( atlascli clusters connectionStrings describe $ATLAS_CLUSTER_NAME -o json) "
114+
115+ export COMPASS_E2E_ATLAS_CLOUD_SANDBOX_CLOUD_CONFIG=$(
116+ if [[ " $MCLI_OPS_MANAGER_URL " =~ " -dev" ]]; then
117+ echo " dev"
118+ elif [[ " $MCLI_OPS_MANAGER_URL " =~ " -qa" ]]; then
119+ echo " qa"
120+ else
121+ echo " prod"
122+ fi
123+ )
124+ echo " Cloud config: $COMPASS_E2E_ATLAS_CLOUD_SANDBOX_CLOUD_CONFIG "
37125
38- export COMPASS_E2E_ATLAS_CLOUD_SANDBOX_DEFAULT_CONNECTIONS=" {\" $ATLAS_CLOUD_TEST_CLUSTER_NAME \" : $ATLAS_CLOUD_TEST_CLUSTER_CONNECTION_STRING_JSON }"
126+ export COMPASS_E2E_ATLAS_CLOUD_SANDBOX_DEFAULT_CONNECTIONS=" {\" $ATLAS_CLUSTER_NAME \" : $CONNECTION_STRINGS_JSON }"
39127echo " Cluster connections: $COMPASS_E2E_ATLAS_CLOUD_SANDBOX_DEFAULT_CONNECTIONS "
0 commit comments