|
| 1 | +#!/usr/bin/env bash |
| 2 | + |
| 3 | +set -eof pipefail |
| 4 | + |
| 5 | +# kill containers when this script exits or errors out |
| 6 | +trap exit-script INT TERM |
| 7 | + |
| 8 | +puts-step() { |
| 9 | + echo "-----> $@" |
| 10 | +} |
| 11 | + |
| 12 | +puts-error() { |
| 13 | + echo "!!! $@" |
| 14 | +} |
| 15 | + |
| 16 | +exit-script() { |
| 17 | + kill-containers |
| 18 | + kill-swift |
| 19 | +} |
| 20 | + |
| 21 | +kill-containers() { |
| 22 | + # docker containers |
| 23 | + puts-step "destroying postgres container" |
| 24 | + docker rm -f deis-postgres-swift |
| 25 | +} |
| 26 | + |
| 27 | +kill-swift() { |
| 28 | + # swift containers |
| 29 | + puts-step "cleaning swift" |
| 30 | + swift delete deis-swift-test > /dev/null |
| 31 | +} |
| 32 | + |
| 33 | +# make sure we are in this dir |
| 34 | +CURRENT_DIR=$(cd $(dirname $0); pwd) |
| 35 | + |
| 36 | +puts-step "creating fake postgres credentials" |
| 37 | + |
| 38 | +# create fake postgres credentials |
| 39 | +mkdir -p $CURRENT_DIR/tmp/creds |
| 40 | +echo "testuser" > $CURRENT_DIR/tmp/creds/user |
| 41 | +echo "icanttellyou" > $CURRENT_DIR/tmp/creds/password |
| 42 | + |
| 43 | +puts-step "fetching openstack credentials" |
| 44 | + |
| 45 | +# check if openstack creds are not already in environment |
| 46 | +if [[ -z $OS_USERNAME ]]; then |
| 47 | + echo "it appears that you have not loaded your openstack credentials into your environment" |
| 48 | + exit 1 |
| 49 | +fi |
| 50 | + |
| 51 | +# turn creds into something that we can use. |
| 52 | +mkdir -p $CURRENT_DIR/tmp/swift |
| 53 | + |
| 54 | +# guess which value to use for tenant: |
| 55 | +TENANT=${OS_TENANT_NAME:-$OS_PROJECT_NAME} |
| 56 | +TENANT=${TENANT:-$OS_USERNAME} |
| 57 | + |
| 58 | +echo ${OS_USERNAME} > $CURRENT_DIR/tmp/swift/username |
| 59 | +echo ${OS_PASSWORD} > $CURRENT_DIR/tmp/swift/password |
| 60 | +echo ${TENANT} > $CURRENT_DIR/tmp/swift/tenant |
| 61 | +echo ${OS_AUTH_URL} > $CURRENT_DIR/tmp/swift/authurl |
| 62 | +echo "deis-swift-test" > $CURRENT_DIR/tmp/swift/database-container |
| 63 | + |
| 64 | +# postgres container command |
| 65 | +PG_CMD="docker run -d -e BACKUP_FREQUENCY=3s \ |
| 66 | + -e DATABASE_STORAGE=swift \ |
| 67 | + -v $CURRENT_DIR/tmp/creds:/var/run/secrets/deis/database/creds \ |
| 68 | + -v $CURRENT_DIR/tmp/swift:/var/run/secrets/deis/objectstore/creds \ |
| 69 | + $1" |
| 70 | + |
| 71 | +# boot postgres |
| 72 | +PG_JOB=$(${PG_CMD}) |
| 73 | + |
| 74 | +# wait for postgres to boot |
| 75 | +puts-step "sleeping for 90s while postgres is booting..." |
| 76 | +sleep 90s |
| 77 | + |
| 78 | +# display logs for debugging purposes |
| 79 | +puts-step "displaying postgres logs" |
| 80 | +docker logs $PG_JOB |
| 81 | + |
| 82 | +# check if postgres is running |
| 83 | +puts-step "checking if postgres is running" |
| 84 | +docker exec $PG_JOB is_running |
| 85 | + |
| 86 | +# check if swift has some backups ... 3 ? |
| 87 | +puts-step "checking if swift has at least 3 backups" |
| 88 | + |
| 89 | +BACKUPS="$(swift list deis-swift-test | grep basebackups_005 | grep json)" |
| 90 | +NUM_BACKUPS="$(swift list deis-swift-test | grep basebackups_005 | grep -c json)" |
| 91 | +if [[ ! "$NUM_BACKUPS" -gt "3" ]]; then |
| 92 | + puts-error "did not find at least 3 base backups, which is the default (found $NUM_BACKUPS)" |
| 93 | + puts-error "$BACKUPS" |
| 94 | + exit-script |
| 95 | + exit 1 |
| 96 | +fi |
| 97 | + |
| 98 | +puts-step "found $NUM_BACKUPS" |
| 99 | + |
| 100 | +# kill off postgres, then reboot and see if it's running after recovering from backups |
| 101 | +puts-step "shutting off postgres, then rebooting to test data recovery" |
| 102 | +docker rm -f $PG_JOB |
| 103 | +PG_JOB=$(${PG_CMD}) |
| 104 | + |
| 105 | +# wait for postgres to boot |
| 106 | +puts-step "sleeping for 90s while postgres is recovering from backup..." |
| 107 | +sleep 90s |
| 108 | + |
| 109 | +puts-step "displaying postgres logs" |
| 110 | +docker logs $PG_JOB |
| 111 | + |
| 112 | +# check if postgres is running |
| 113 | +puts-step "checking if postgres is running" |
| 114 | +docker exec $PG_JOB is_running |
| 115 | + |
| 116 | +puts-step "tests PASSED!" |
| 117 | +exit 0 |
0 commit comments