forked from doitintl/iris3
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathintegration_test.sh
executable file
·166 lines (135 loc) · 5.84 KB
/
integration_test.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
#!/bin/bash
# This is an integration test with a deployed app and deployed cloud resources.
# * Deploys to the cloud with a unique run-id
# * Creates resources of all supported types except Cloud SQL
# * Checks that a label was added to each resource,
# * Deletes the resources.
#
# See usage text for parameters.
set -u
set -e
set -x
START_TEST=$(date "+%s")
if [[ $# -lt 2 ]]; then
cat >&2 <<EOF
Usage: integration_test.sh deployment-project project-under-test [execution-id]
- The project to which Iris is deployed
- The project where resources will be labeled (can be the same project)
- An optional lower-case alphanumerical string to identify this run,
used as a prefix on Iris labels and as part of the name of launched resources.
If omitted, one will be generated.
Returns exit code 0 on test-success, non-zero on test-failure
EOF
exit
fi
export RUN_ID
if [[ $# -eq 3 ]]; then
RUN_ID=$3
else
# Random value to distinguish this test runs from others
RUN_ID="iris$(base64 </dev/urandom | tr -d '+/' | head -c 6 | awk '{print tolower($0)}')"
fi
export PUBSUB_TEST_TOKEN
PUBSUB_TEST_TOKEN=$(hexdump -n 16 -e '4/4 "%08X" 1 "\n"' /dev/urandom| tr '[:upper:]' '[:lower:]')
export DEPLOYMENT_PROJECT=$1
export TEST_PROJECT=$2
declare -a projects
projects=("$DEPLOYMENT_PROJECT" "$TEST_PROJECT")
for p in "${projects[@]}"; do
gcloud projects describe "$p" || {
echo >&2 "Project $p not found"
exit 1
}
done
gcloud config set project "$TEST_PROJECT"
if [ -n "$(echo "$RUN_ID" | grep '[_-]')" ]; then
cat >&2 <<EOF
Illegal run id $RUN_ID. No dashes or underlines permitted because
underlines are illegal in snapshot (and other) names
and dashes are illegal in BigQuery names.
EOF
exit 1
fi
# Move aside the config file while we use a temporary one
mv config.yaml config.yaml.before_test
# Prepare to revert config on exit
function revert_config() {
# Cleanup should not stop on error
set +e
echo >&2 "Reverting config"
mv config.yaml.before_test config.yaml
}
trap "revert_config" EXIT
envsubst <config.yaml.test.template >config.yaml
export GAEVERSION=$RUN_ID
./deploy.sh $DEPLOYMENT_PROJECT
ERROR=0
# Cleanup on exit
function clean_resources() {
echo >&2 "Cleaning up resources. Exit code is now $ERROR"
# Cleanup should not stop on error
set +e
# Include the earlier on-exit code inside this one.
revert_config
gcloud compute instances delete "instance${RUN_ID}" -q --project "$TEST_PROJECT"
gcloud compute snapshots delete "snapshot${RUN_ID}" -q --project "$TEST_PROJECT"
gcloud compute disks delete "disk${RUN_ID}" -q --project "$TEST_PROJECT"
gcloud pubsub topics delete "topic${RUN_ID}" -q --project "$TEST_PROJECT"
gcloud pubsub subscriptions delete "subscription${RUN_ID}" -q --project "$TEST_PROJECT"
gcloud bigtable instances delete "bigtable${RUN_ID}" -q --project "$TEST_PROJECT"
bq rm -f --table "${TEST_PROJECT}:dataset${RUN_ID}.table${RUN_ID}"
bq rm -f --dataset "${TEST_PROJECT}:dataset${RUN_ID}"
gsutil rm -r "gs://bucket${RUN_ID}"
gcloud app services delete iris3 -q --project $DEPLOYMENT_PROJECT
FINISH_TEST=$(date "+%s")
ELAPSED_SEC_TEST=$((FINISH_TEST - START_TEST))
echo >&2 "Elapsed time for $(basename "$0") ${ELAPSED_SEC_TEST} s; exiting with $ERROR"
exit $ERROR
}
trap "clean_resources" EXIT
sleep 20 # Need time for traffic to be migrated to the new version
gcloud compute instances create "instance${RUN_ID}" --project "$TEST_PROJECT"
gcloud compute disks create "disk${RUN_ID}" --project "$TEST_PROJECT"
gcloud compute disks snapshot "instance${RUN_ID}" --snapshot-names "snapshot${RUN_ID}" --project $TEST_PROJECT
gcloud pubsub topics create "topic${RUN_ID}" --project "$TEST_PROJECT"
gcloud pubsub subscriptions create "subscription${RUN_ID}" --topic "topic${RUN_ID}" --project "$TEST_PROJECT"
gcloud bigtable instances create "bigtable${RUN_ID}" --display-name="bigtable${RUN_ID}" --cluster="bigtable${RUN_ID}" --cluster-zone=us-east1-c --project "$TEST_PROJECT"
bq mk --dataset "${TEST_PROJECT}:dataset${RUN_ID}"
bq mk --table "${TEST_PROJECT}:dataset${RUN_ID}.table${RUN_ID}"
gsutil mb -p $TEST_PROJECT "gs://bucket${RUN_ID}"
# It takes time before labels are available to be read by "describe".
#
# jq -e generates exit code 1 on failure. Since we set -e, the script will fail appropriately if the value is not found
sleep 30
DESCRIBE_FLAGS=(--project "$TEST_PROJECT" --format json)
JQ=(jq -e ".labels.${RUN_ID}_name")
#From now on , don't exit on test failure
set +e
gcloud pubsub topics describe "topic${RUN_ID}" "${DESCRIBE_FLAGS[@]}" | "${JQ[@]}"
if [[ $? -ne 0 ]]; then ERROR=1 ; fi
gcloud pubsub subscriptions describe "subscription${RUN_ID}" "${DESCRIBE_FLAGS[@]}" | "${JQ[@]}"
if [[ $? -ne 0 ]]; then ERROR=1 ; fi
gcloud bigtable instances describe "bigtable${RUN_ID}" "${DESCRIBE_FLAGS[@]}" | "${JQ[@]}"
if [[ $? -ne 0 ]]; then ERROR=1 ; fi
bq show --format=json "${TEST_PROJECT}:dataset${RUN_ID}" | "${JQ[@]}"
if [[ $? -ne 0 ]]; then ERROR=1 ; fi
bq show --format=json "${TEST_PROJECT}:dataset${RUN_ID}.table${RUN_ID}" | "${JQ[@]}"
if [[ $? -ne 0 ]]; then ERROR=1 ; fi
# 1. For buckets, JSON shows labels without the label:{} wrapper seen in the others
# 2. For this test, we specified a label for buckets that is different from other resource types
gsutil label get "gs://bucket${RUN_ID}" | jq -e ".gcs${RUN_ID}_name"
if [[ $? -ne 0 ]]; then ERROR=1 ; fi
gcloud compute instances describe "instance${RUN_ID}" "${DESCRIBE_FLAGS[@]}" | "${JQ[@]}"
if [[ $? -ne 0 ]]; then ERROR=1 ; fi
gcloud compute disks describe "disk${RUN_ID}" "${DESCRIBE_FLAGS[@]}" | "${JQ[@]}"
if [[ $? -ne 0 ]]; then ERROR=1 ; fi
gcloud compute snapshots describe "snapshot${RUN_ID}" "${DESCRIBE_FLAGS[@]}" | "${JQ[@]}"
if [[ $? -ne 0 ]]; then ERROR=1 ; fi
if [ $ERROR -ne 0 ];
then
# On Error leave resources; do not delete them
trap - EXIT
# But still, revert the config
revert_config
exit $ERROR
fi