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

DEVPROD-13428: Reset Amboy database in e2e tests when mutations enqueue jobs #616

Merged
merged 3 commits into from
Feb 11, 2025
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
4 changes: 2 additions & 2 deletions apps/parsley/cypress/support/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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");
}
});
})();
27 changes: 17 additions & 10 deletions apps/spruce/cypress/support/e2e.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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')

Expand Down Expand Up @@ -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");
Expand All @@ -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");
}
});
})();
Expand Down
86 changes: 51 additions & 35 deletions scripts/evg-db-ops.sh
Original file line number Diff line number Diff line change
@@ -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"

Expand All @@ -15,65 +18,73 @@ 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"
exit 1
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 database 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
Expand All @@ -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 <evergreen|amboy>|--clean-up|--reseed-and-dump}"
exit 1
;;
esac