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+
36# This script helps to automatically provision Atlas cluster for running the e2e
47# tests against. In CI this will always create a new cluster and delete it when
58# the test run is finished. You can also use this script locally to run e2e
4144#
4245# COMPASS_E2E_ATLAS_CLOUD_SANDBOX_USERNAME Cloud user you created
4346# COMPASS_E2E_ATLAS_CLOUD_SANDBOX_PASSWORD Cloud user password
44- # COMPASS_E2E_ATLAS_CLOUD_SANDBOX_DBUSER_USERNAME Db user for the project
45- # COMPASS_E2E_ATLAS_CLOUD_SANDBOX_DBUSER_PASSWORD Db user password
4647#
4748# - Source the script followed by running the tests to make sure that some
4849# variables exported from this script are available for the test env:
@@ -56,10 +57,13 @@ _ATLAS_CLOUD_TEST_CLUSTER_NAME=${ATLAS_CLOUD_TEST_CLUSTER_NAME:-""}
5657# truncate if it's too long) so we're very limited in terms of how unique this
5758# name can be. Hopefully the epoch + part of git hash is enough for these to not
5859# overlap when tests are running
59- DEFAULT_ATLAS_CLOUD_TEST_CLUSTER_NAME=" e2e-$( date + " %s " ) - $( git rev-parse HEAD ) "
60+ DEFAULT_ATLAS_CLOUD_TEST_CLUSTER_NAME=" e2e-$RUN_ID "
6061
6162ATLAS_CLUSTER_NAME=" ${_ATLAS_CLOUD_TEST_CLUSTER_NAME:- $DEFAULT_ATLAS_CLOUD_TEST_CLUSTER_NAME } "
6263
64+ ATLAS_TEST_DB_USERNAME=" testuser-$RUN_ID "
65+ ATLAS_TEST_DB_PASSWORD=" $( head -c 32 /dev/urandom | base64 | tr -dc ' a-zA-Z0-9' ) "
66+
6367function atlascli() {
6468 docker run \
6569 -e MCLI_PUBLIC_API_KEY \
@@ -82,10 +86,26 @@ cleanup() {
8286 else
8387 echo " Custom cluster name provided ($_ATLAS_CLOUD_TEST_CLUSTER_NAME ), skipping cluster cleanup"
8488 fi
89+ echo " Deleting Atlas db user \` $ATLAS_TEST_DB_USERNAME \` ..."
90+ atlascli dbusers delete $ATLAS_TEST_DB_USERNAME --force
8591}
8692
8793trap cleanup EXIT
8894
95+ echo " Allowing access from current ip..."
96+ atlascli accessList create \
97+ --currentIp \
98+ --deleteAfter " $DELETE_AFTER "
99+
100+ echo " Creating Atlas db user \` $ATLAS_TEST_DB_USERNAME \` ..."
101+ atlascli dbusers create atlasAdmin \
102+ --username " $ATLAS_TEST_DB_USERNAME " \
103+ --password " $ATLAS_TEST_DB_PASSWORD " \
104+ --deleteAfter " $DELETE_AFTER " # so that it's autoremoved if cleaning up failed for some reason
105+
106+ export COMPASS_E2E_ATLAS_CLOUD_SANDBOX_DBUSER_USERNAME=" $ATLAS_TEST_DB_USERNAME "
107+ export COMPASS_E2E_ATLAS_CLOUD_SANDBOX_DBUSER_PASSWORD=" $ATLAS_TEST_DB_PASSWORD "
108+
89109echo " Creating Atlas deployment \` $ATLAS_CLUSTER_NAME \` to test against..."
90110atlascli clusters create $ATLAS_CLUSTER_NAME \
91111 --provider AWS \
0 commit comments