From 2b95279d205cd12d3acb3938fe436151b9170453 Mon Sep 17 00:00:00 2001 From: minnakt <47064971+minnakt@users.noreply.github.com> Date: Mon, 10 Feb 2025 20:55:58 -0500 Subject: [PATCH] DEVPROD-13428: Reset Amboy database in e2e tests when mutations enqueue jobs (#616) --- apps/parsley/cypress/support/index.ts | 4 +- apps/spruce/cypress/support/e2e.ts | 27 +++++---- scripts/evg-db-ops.sh | 86 ++++++++++++++++----------- 3 files changed, 70 insertions(+), 47 deletions(-) diff --git a/apps/parsley/cypress/support/index.ts b/apps/parsley/cypress/support/index.ts index 825b8e223..4a45c4a58 100644 --- a/apps/parsley/cypress/support/index.ts +++ b/apps/parsley/cypress/support/index.ts @@ -142,8 +142,8 @@ declare global { afterEach(() => { if (mutationDispatched) { - cy.log("A mutation was detected. Restoring EVG."); - cy.exec("yarn evg-db-ops --restore"); + cy.log("A mutation was detected. Restoring Evergreen."); + cy.exec("yarn evg-db-ops --restore evergreen"); } }); })(); diff --git a/apps/spruce/cypress/support/e2e.ts b/apps/spruce/cypress/support/e2e.ts index e8187a54d..6d51303f4 100644 --- a/apps/spruce/cypress/support/e2e.ts +++ b/apps/spruce/cypress/support/e2e.ts @@ -22,7 +22,7 @@ import { SEEN_TEST_ANALYSIS_TAB_GUIDE_CUE, SEEN_WATERFALL_ONBOARDING_TUTORIAL, } from "constants/cookies"; -import { isMutation } from "../utils/graphql-test-utils"; +import { hasOperationName, isMutation } from "../utils/graphql-test-utils"; // Alternatively you can use CommonJS syntax: // require('./commands') @@ -159,17 +159,12 @@ declare global { } } -before(() => { - cy.exec("yarn evg-db-ops --restore").then((result) => { - if (result.code !== 0) { - throw new Error("EVG DB restoration failed during setup."); - } - }); -}); +const hostMutations = ["ReprovisionToNew", "RestartJasper", "UpdateHostStatus"]; // Close over beforeEach and afterEach to encapsulate mutationDispatched (() => { let mutationDispatched: boolean; + let clearAmboyDB: boolean; beforeEach(() => { cy.login(); cy.setCookie(bannerCookie, "true"); @@ -179,17 +174,29 @@ before(() => { cy.setCookie(SEEN_TEST_ANALYSIS_TAB_GUIDE_CUE, "true"); cy.setCookie(SEEN_WATERFALL_ONBOARDING_TUTORIAL, "true"); mutationDispatched = false; + clearAmboyDB = false; cy.intercept("POST", "/graphql/query", (req) => { if (isMutation(req)) { mutationDispatched = true; + hostMutations.forEach((m) => { + if (hasOperationName(req, m)) { + clearAmboyDB = true; + } + }); } }); }); afterEach(() => { if (mutationDispatched) { - cy.log("A mutation was detected. Restoring EVG."); - cy.exec("yarn evg-db-ops --restore"); + if (clearAmboyDB) { + cy.log( + "A mutation that creates an Amboy job was detected. Restoring Amboy.", + ); + cy.exec("yarn evg-db-ops --restore amboy"); + } + cy.log("A mutation was detected. Restoring Evergreen."); + cy.exec("yarn evg-db-ops --restore evergreen"); } }); })(); diff --git a/scripts/evg-db-ops.sh b/scripts/evg-db-ops.sh index d0e2f982d..b4405953a 100755 --- a/scripts/evg-db-ops.sh +++ b/scripts/evg-db-ops.sh @@ -1,11 +1,14 @@ #!/bin/bash -DB_NAME="evergreen_local" -URI="mongodb://localhost:27017/$DB_NAME" - -# Define paths for storing database dumps. DUMP_ROOT=${TMPDIR}evg_dump -DUMP_FOLDER="$DUMP_ROOT/$DB_NAME" + +EVERGREEN_DB_NAME="evergreen_local" +EVERGREEN_URI="mongodb://localhost:27017/$EVERGREEN_DB_NAME" +EVERGREEN_DUMP_FOLDER="$DUMP_ROOT/$EVERGREEN_DB_NAME" + +AMBOY_DB_NAME="amboy_local" +AMBOY_URI="mongodb://localhost:27017/$AMBOY_DB_NAME" +AMBOY_DUMP_FOLDER="$DUMP_ROOT/$AMBOY_DB_NAME" RUNTIME_ENVIRONMENTS_SCRIPT_PATH="$(dirname "${BASH_SOURCE[0]}")/add-runtime-environments-creds.js" @@ -15,8 +18,8 @@ clean_up() { echo "Cleaned up $DUMP_ROOT." } -# Function to reseed the database with smoke test data. -reseed_database() { +# Function to reseed databases with smoke test data. +reseed_databases() { # Change the current directory sdlschema symlink. if ! cd -- "$(dirname -- "$(readlink -- "sdlschema")")"; then echo "Unable to find Evergreen directory from the sdlschema symlink" @@ -24,56 +27,64 @@ reseed_database() { fi # Load test data into the database. - ../bin/load-smoke-data -path ../testdata/local -dbName evergreen_local -amboyDBName amboy_local + ../bin/load-smoke-data -path ../testdata/local -dbName "$EVERGREEN_DB_NAME" -amboyDBName "$AMBOY_DB_NAME" cd - # Load credentials for the Image Visibility API. echo "Adding runtime environments credentials..." if [ "$CI" != 'true' ]; then - mongosh $DB_NAME --quiet $RUNTIME_ENVIRONMENTS_SCRIPT_PATH + mongosh $EVERGREEN_DB_NAME --quiet $RUNTIME_ENVIRONMENTS_SCRIPT_PATH else - ../../evergreen/mongosh/mongosh $DB_NAME --quiet $RUNTIME_ENVIRONMENTS_SCRIPT_PATH + ../../evergreen/mongosh/mongosh $EVERGREEN_DB_NAME --quiet $RUNTIME_ENVIRONMENTS_SCRIPT_PATH fi echo "Finished adding runtime environments credentials." cd - || exit } -# Function to create a dump of the database. -dump_database() { - clean_up +# Helper function for dumping DBs. +# Pass the DB name as the first argument and the URI as the second argument. +dump_db() { # Use 'mongodump' to create a database dump. - if ! mongodump --quiet --uri="$URI" -o "$DUMP_ROOT"; then - echo "Error creating dump from $DB_NAME db." + if ! mongodump --quiet --uri="$2" -o "$DUMP_ROOT"; then + echo "Error creating dump from $1 db." exit 1 fi - echo "Dump successfully created in $DUMP_ROOT" + echo "$1 dump successfully created in $DUMP_ROOT" +} + +# Function to create dumps of the databases. +dump_databases() { + clean_up + dump_db "$AMBOY_DB_NAME" "$AMBOY_URI" + dump_db "$EVERGREEN_DB_NAME" "$EVERGREEN_URI" } -# Function to reseed the database and then create a dump. -reseed_and_dump_database() { - reseed_database - dump_database +# Function to reseed the databases and then create dumps. +reseed_and_dump_databases() { + reseed_databases + dump_databases } -# Function to restore the database from a dump. -restore_database() { +# Function to restore database from a dump. +# Pass the dump folder as the first argument and the URI as the second argument. +restore_db() { + MAX_RETRIES=2 + # Check if the specified dump folder exists. - if [ ! -d "$DUMP_FOLDER" ]; then - echo "Error: $DUMP_FOLDER does not exist. Ensure you have a valid dump before restoring." + if [ ! -d "$1" ]; then + echo "Error: $1 does not exist. Ensure you have a valid dump before restoring." exit 1 fi - MAX_RETRIES=2 - # Use 'mongorestore' to restore the database from the dump. for ((retry=0; retry<=MAX_RETRIES; retry++)); do - if mongorestore --quiet --drop --uri="$URI" "$DUMP_FOLDER"; then - echo "Successfully restored the database from $DUMP_FOLDER." - exit 0 + if mongorestore --quiet --drop --uri="$2" "$1"; then + echo "Successfully restored the database from $1." + break else - echo "Error restoring the database from $DUMP_FOLDER. Retry attempt: $retry" + echo "Error restoring the database from $1. Retry attempt: $retry" if [ $retry -eq $MAX_RETRIES ]; then echo "Max retries reached. Exiting." exit 1 @@ -86,22 +97,27 @@ restore_database() { # Check the command-line argument to determine the action to perform. case "$1" in --dump) - dump_database + dump_databases ;; --restore) - restore_database + if [ "$2" = "evergreen" ]; then + restore_db "$EVERGREEN_DUMP_FOLDER" "$EVERGREEN_URI" + fi + if [ "$2" = "amboy" ]; then + restore_db "$AMBOY_DUMP_FOLDER" "$AMBOY_URI" + fi ;; --clean-up) clean_up ;; --reseed) - reseed_database + reseed_databases ;; --reseed-and-dump) - reseed_and_dump_database + reseed_and_dump_databases ;; *) - echo "Usage: $0 {--dump|--restore|--clean-up|--reseed-and-dump}" + echo "Usage: $0 {--dump|--restore |--clean-up|--reseed-and-dump}" exit 1 ;; esac