Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore(e2e): automatically create db user and add ip address to the access list before running tests #6432

Merged
merged 1 commit into from
Nov 1, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 0 additions & 2 deletions .evergreen/functions.yml
Original file line number Diff line number Diff line change
Expand Up @@ -685,8 +685,6 @@ functions:
DEBUG: ${debug|}
COMPASS_E2E_ATLAS_CLOUD_SANDBOX_USERNAME: ${e2e_tests_compass_web_atlas_username}
COMPASS_E2E_ATLAS_CLOUD_SANDBOX_PASSWORD: ${e2e_tests_compass_web_atlas_password}
COMPASS_E2E_ATLAS_CLOUD_SANDBOX_DBUSER_USERNAME: ${e2e_tests_compass_web_atlas_db_username}
COMPASS_E2E_ATLAS_CLOUD_SANDBOX_DBUSER_PASSWORD: ${e2e_tests_compass_web_atlas_password}
MCLI_PUBLIC_API_KEY: ${e2e_tests_mcli_public_api_key}
MCLI_PRIVATE_API_KEY: ${e2e_tests_mcli_private_api_key}
MCLI_ORG_ID: ${e2e_tests_mcli_org_id}
Expand Down
29 changes: 23 additions & 6 deletions .evergreen/start-atlas-cloud-cluster.sh
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
#!/bin/bash

RUN_ID="$(date +"%s")-$(git rev-parse --short HEAD)"
DELETE_AFTER="$(date -u -Iseconds -d '+2 hours' 2>/dev/null || date -u -Iseconds -v '+2H')"

# This script helps to automatically provision Atlas cluster for running the e2e
# tests against. In CI this will always create a new cluster and delete it when
# the test run is finished. You can also use this script locally to run e2e
Expand All @@ -16,9 +19,6 @@
#
# - Setup a new org and project. Save the org id and project id for later.
#
# - Create new db user with username / password auth and admin role. This user
# will be used to prepopulate dbs with data in tests. Save the credentials.
#
# - Create a new API key (Access Manager > Project Access > Create Application >
# API Key) for the project you created and save the public and private keys.
#
Expand All @@ -38,8 +38,6 @@
#
# COMPASS_E2E_ATLAS_CLOUD_SANDBOX_USERNAME Cloud user you created
# COMPASS_E2E_ATLAS_CLOUD_SANDBOX_PASSWORD Cloud user password
# COMPASS_E2E_ATLAS_CLOUD_SANDBOX_DBUSER_USERNAME Db user for the project
# COMPASS_E2E_ATLAS_CLOUD_SANDBOX_DBUSER_PASSWORD Db user password
#
# - Source the script followed by running the tests to make sure that some
# variables exported from this script are available for the test env:
Expand All @@ -53,10 +51,13 @@ _ATLAS_CLOUD_TEST_CLUSTER_NAME=${ATLAS_CLOUD_TEST_CLUSTER_NAME:-""}
# truncate if it's too long) so we're very limited in terms of how unique this
# name can be. Hopefully the epoch + part of git hash is enough for these to not
# overlap when tests are running
DEFAULT_ATLAS_CLOUD_TEST_CLUSTER_NAME="e2e-$(date +"%s")-$(git rev-parse HEAD)"
DEFAULT_ATLAS_CLOUD_TEST_CLUSTER_NAME="e2e-$RUN_ID"

ATLAS_CLUSTER_NAME="${_ATLAS_CLOUD_TEST_CLUSTER_NAME:-$DEFAULT_ATLAS_CLOUD_TEST_CLUSTER_NAME}"

ATLAS_TEST_DB_USERNAME="testuser-$RUN_ID"
ATLAS_TEST_DB_PASSWORD="$(head -c 32 /dev/urandom | base64 | tr -dc 'a-zA-Z0-9')"

function atlascli() {
docker run \
-e MCLI_PUBLIC_API_KEY \
Expand All @@ -79,10 +80,26 @@ cleanup() {
else
echo "Custom cluster name provided ($_ATLAS_CLOUD_TEST_CLUSTER_NAME), skipping cluster cleanup"
fi
echo "Deleting Atlas db user \`$ATLAS_TEST_DB_USERNAME\`..."
atlascli dbusers delete $ATLAS_TEST_DB_USERNAME --force
}

trap cleanup EXIT

echo "Allowing access from current ip..."
atlascli accessList create \
--currentIp \
--deleteAfter "$DELETE_AFTER"
Comment on lines +90 to +92
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

❤️


echo "Creating Atlas db user \`$ATLAS_TEST_DB_USERNAME\`..."
atlascli dbusers create atlasAdmin \
--username "$ATLAS_TEST_DB_USERNAME" \
--password "$ATLAS_TEST_DB_PASSWORD" \
--deleteAfter "$DELETE_AFTER" # so that it's autoremoved if cleaning up failed for some reason

export COMPASS_E2E_ATLAS_CLOUD_SANDBOX_DBUSER_USERNAME="$ATLAS_TEST_DB_USERNAME"
export COMPASS_E2E_ATLAS_CLOUD_SANDBOX_DBUSER_PASSWORD="$ATLAS_TEST_DB_PASSWORD"

echo "Creating Atlas deployment \`$ATLAS_CLUSTER_NAME\` to test against..."
atlascli clusters create $ATLAS_CLUSTER_NAME \
--provider AWS \
Expand Down
Loading