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

Usagov 1777 db integrity testing #1855

Open
wants to merge 5 commits into
base: dev
Choose a base branch
from
Open
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
71 changes: 71 additions & 0 deletions bin/cloudgov/audit/db-integrity
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
#!/bin/bash

#
# Requires sqlvalidator, run pip install sqlvalidator if this doesn't work.
#

# we might be running in circleci
if [ -f /home/circleci/project/env.local ]; then
. /home/circleci/project/env.local
fi
# we might be running from a local dev machine
SCRIPT_DIR="$(dirname "$0")"
if [ -f $SCRIPT_DIR/env.local ]; then
. $SCRIPT_DIR/env.local
fi
if [ -f ./env.local ]; then
. ./env.local
fi
if [ -f $SCRIPT_DIR/../../deploy/includes ]; then
. $SCRIPT_DIR/../../deploy/includes
else
echo Cannot find $SCRIPT_DIR/../../deploy/includes
exit 1
fi

# just testing?
if [ x$1 = x"--dryrun" ]; then
export echo=echo
shift
fi

SPACE=${1:-please-provide-space-name-as-first-argument}
SPACE=$(echo "$SPACE" | tr '[:upper:]' '[:lower:]')
assertCurSpace $SPACE
shift

echo "Testing static site integrity."

echo "Getting s3 access."
source bin/cloudgov/get-s3-access storage >/dev/null 2>&1

echo "Getting three random backup tags from s3."
DEPLOY_TAGS=$(aws s3 ls s3://"$S3_BUCKET"/db-backup/ | awk '{ print $4 }' | sort -R | tail -n 3 )

echo "-----------------------------------"
echo "$DEPLOY_TAGS" | while read -r DEPLOY_TAG ; do
echo "Downloading db: $DEPLOY_TAG"
aws s3 cp --only-show-errors s3://"$S3_BUCKET"/db-backup/"$DEPLOY_TAG" "$DEPLOY_TAG"
gunzip "$DEPLOY_TAG"
sleep 5
SQL_FILE=$(echo "$DEPLOY_TAG" | sed 's/.gz$//')

echo "Testing: $SQL_FILE"

size=$(du -hs "$SQL_FILE")
echo "SQL file size: $(echo "$size" | awk '{print $1}')"

echo "Validating SQL. This will take a while."
validate=$(sqlvalidator --verbose-validate "$SQL_FILE")
if [ -z "$validate" ]; then
echo "SQL file is valid."
else
echo -e "\033[0;31mSQL file is invalid. Error(s):"
echo -e "$validate\033[0m"
fi

rm "$SQL_FILE"
echo "-----------------------------------"

done
echo "Automated testing complete."
Loading