1#!/usr/bin/env bash
2# Copyright 2019 The Go Cloud Development Kit Authors
3#
4# Licensed under the Apache License, Version 2.0 (the "License");
5# you may not use this file except in compliance with the License.
6# You may obtain a copy of the License at
7#
8#     https://www.apache.org/licenses/LICENSE-2.0
9#
10# Unless required by applicable law or agreed to in writing, software
11# distributed under the License is distributed on an "AS IS" BASIS,
12# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13# See the License for the specific language governing permissions and
14# limitations under the License.
15
16# This script runs expensive checks that we don't normally run on Travis, but
17# that should run periodically, before each release.
18# For example, tests that can't use record/replay, so must be performed live
19# against the backing service.
20#
21# It should be run from the root directory.
22
23# https://coderwall.com/p/fkfaqq/safer-bash-scripts-with-set-euxo-pipefail
24# Change to -euxo if debugging.
25set -euo pipefail
26
27function usage() {
28  echo
29  echo "Usage: prerelease.sh <init | run | cleanup>" 1>&2
30  echo "  init: creates any needed resources; rerun until it succeeds"
31  echo "  run: runs all needed checks"
32  echo "  cleanup: cleans up resources created in init"
33  exit 64
34}
35
36if [[ $# -ne 1 ]] ; then
37  echo "Need at least one argument."
38  usage
39fi
40
41op="$1"
42case "$op" in
43  init|run|cleanup);;
44  rerecord);;
45  *) echo "Unknown operation '$op'" && usage;;
46esac
47
48# TODO: It would be nice to ensure that none of the tests are skipped. For now,
49#       we assume that if the "init" steps succeeded, the necessary tests will
50#       run.
51
52rootdir="$(pwd)"
53FAILURES=""
54
55TESTDIR="mysql/azuremysql"
56echo "***** $TESTDIR *****"
57pushd "$TESTDIR" &> /dev/null
58case "$op" in
59  init)
60    terraform init && terraform apply -var location="centralus" -var resourcegroup="GoCloud" -auto-approve || FAILURES="$FAILURES $TESTDIR"
61    ;;
62  run)
63    go test -mod=readonly -race -json | go run "$rootdir"/internal/testing/test-summary/test-summary.go -progress || FAILURES="$FAILURES $TESTDIR"
64    ;;
65  cleanup)
66    terraform destroy -var location="centralus" -var resourcegroup="GoCloud" -auto-approve || FAILURES="$FAILURES $TESTDIR"
67    ;;
68esac
69popd &> /dev/null
70
71
72TESTDIR="mysql/gcpmysql"
73echo
74echo "***** $TESTDIR *****"
75pushd "$TESTDIR" &> /dev/null
76case "$op" in
77  init)
78    terraform init && terraform apply -var project="go-cloud-test-216917" -auto-approve || FAILURES="$FAILURES $TESTDIR"
79    ;;
80  run)
81    go test -mod=readonly -race -json | go run "$rootdir"/internal/testing/test-summary/test-summary.go -progress || FAILURES="$FAILURES $TESTDIR"
82    ;;
83  cleanup)
84    terraform destroy -var project="go-cloud-test-216917" -auto-approve || FAILURES="$FAILURES $TESTDIR"
85    ;;
86esac
87popd &> /dev/null
88
89
90TESTDIR="mysql/awsmysql"
91echo
92echo "***** $TESTDIR *****"
93pushd "$TESTDIR" &> /dev/null
94case "$op" in
95  init)
96    terraform init && terraform apply -var region="us-west-1" -auto-approve || FAILURES="$FAILURES $TESTDIR"
97    ;;
98  run)
99    go test -mod=readonly -race -json | go run "$rootdir"/internal/testing/test-summary/test-summary.go -progress || FAILURES="$FAILURES $TESTDIR"
100    ;;
101  cleanup)
102    terraform destroy -var region="us-west-1" -auto-approve || FAILURES="$FAILURES $TESTDIR"
103    ;;
104esac
105popd &> /dev/null
106
107
108TESTDIR="postgres/gcppostgres"
109echo
110echo "***** $TESTDIR *****"
111pushd "$TESTDIR" &> /dev/null
112case "$op" in
113  init)
114    terraform init && terraform apply -var project="go-cloud-test-216917" -auto-approve || FAILURES="$FAILURES $TESTDIR"
115    ;;
116  run)
117    go test -mod=readonly -race -json | go run "$rootdir"/internal/testing/test-summary/test-summary.go -progress || FAILURES="$FAILURES $TESTDIR"
118    ;;
119  cleanup)
120    terraform destroy -var project="go-cloud-test-216917" -auto-approve || FAILURES="$FAILURES $TESTDIR"
121    ;;
122esac
123popd &> /dev/null
124
125
126TESTDIR="postgres/awspostgres"
127echo
128echo "***** $TESTDIR *****"
129pushd "$TESTDIR" &> /dev/null
130case "$op" in
131  init)
132    terraform init && terraform apply -var region="us-west-1" -auto-approve || FAILURES="$FAILURES $TESTDIR"
133    ;;
134  run)
135    go test -mod=readonly -race -json  | go run "$rootdir"/internal/testing/test-summary/test-summary.go -progress || FAILURES="$FAILURES $TESTDIR"
136    ;;
137  cleanup)
138    terraform destroy -var region="us-west-1" -auto-approve || FAILURES="$FAILURES $TESTDIR"
139    ;;
140esac
141popd &> /dev/null
142
143
144# This iterates over all packages that have a "testdata" directory, using that
145# as a signal for record/replay tests, and runs the tests with a "-record" flag.
146# This verifies that we can generate a fresh recording against the live service.
147while read -r TESTDIR; do
148  # Skip some packages that have a testdata/ dir but aren't record/replay.
149  if [ "$TESTDIR" == "./samples/order" ]; then
150    continue;
151  fi
152  echo
153  echo "***** $TESTDIR *****"
154  pushd "$TESTDIR" &> /dev/null
155  case "$op" in
156    init)
157      ;;
158    run|rerecord)
159      go test -mod=readonly -race -record -json | go run "$rootdir"/internal/testing/test-summary/test-summary.go -progress || FAILURES="$FAILURES $TESTDIR"
160      ;;
161    cleanup)
162      ;;
163  esac
164  popd &> /dev/null
165done < <( find . -name testdata -printf "%h\\n" )
166
167echo
168echo
169if [ ! -z "$FAILURES" ]; then
170  echo "FAILED!"
171  echo "Investigate and re-run -record tests for the following packages: $FAILURES"
172  exit 1
173fi
174
175echo "SUCCESS!"
176