|
| 1 | +#! /bin/bash |
| 2 | +# Copyright 2018 Google LLC |
| 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 | +# http://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 | +# Entry point for CI Integration Tests. This script is expected to be run |
| 17 | +# inside the same docker image specified in the CI Pipeline definition. |
| 18 | + |
| 19 | +# Always clean up. |
| 20 | +DELETE_AT_EXIT="$(mktemp -d)" |
| 21 | + |
| 22 | +finish() { |
| 23 | + echo 'BEGIN: finish() trap handler' >&2 |
| 24 | + set +e |
| 25 | + bundle exec kitchen destroy |
| 26 | + [[ -d "${DELETE_AT_EXIT}" ]] && rm -rf "${DELETE_AT_EXIT}" |
| 27 | + echo 'END: finish() trap handler' >&2 |
| 28 | +} |
| 29 | + |
| 30 | +# Map the input parameters provided by Concourse CI, or whatever mechanism is |
| 31 | +# running the tests to Terraform input variables. Also setup credentials for |
| 32 | +# use with kitchen-terraform, inspec, and gcloud. |
| 33 | +setup_environment() { |
| 34 | + local tmpfile |
| 35 | + tmpfile="$(mktemp)" |
| 36 | + echo "${SERVICE_ACCOUNT_JSON}" > "${tmpfile}" |
| 37 | + |
| 38 | + # Terraform and most other tools respect GOOGLE_CREDENTIALS |
| 39 | + export GOOGLE_CREDENTIALS="${SERVICE_ACCOUNT_JSON}" |
| 40 | + # gcloud variables |
| 41 | + export CLOUDSDK_AUTH_CREDENTIAL_FILE_OVERRIDE="${tmpfile}" |
| 42 | + export CLOUDSDK_CORE_PROJECT="${PROJECT_ID}" |
| 43 | + export GOOGLE_APPLICATION_CREDENTIALS="${tmpfile}" |
| 44 | + |
| 45 | + # Terraform input variables |
| 46 | + export TF_VAR_project_id="${PROJECT_ID}" |
| 47 | +} |
| 48 | + |
| 49 | +main() { |
| 50 | + set -eu |
| 51 | + # Setup trap handler to auto-cleanup |
| 52 | + export TMPDIR="${DELETE_AT_EXIT}" |
| 53 | + trap finish EXIT |
| 54 | + |
| 55 | + # Setup environment |
| 56 | + setup_environment |
| 57 | + set -x |
| 58 | + # Execute the test lifecycle |
| 59 | + bundle install --path vendor |
| 60 | + bundle exec kitchen create |
| 61 | + bundle exec kitchen converge -c 4 |
| 62 | + bundle exec kitchen verify |
| 63 | +} |
| 64 | + |
| 65 | +# if script is being executed and not sourced. |
| 66 | +if [[ "${BASH_SOURCE[0]}" == "${0}" ]]; then |
| 67 | + main "$@" |
| 68 | +fi |
0 commit comments