|
| 1 | +#!/usr/bin/env bash |
| 2 | +# vim:ts=4:sw=4:noet |
| 3 | + |
| 4 | +set -eo pipefail |
| 5 | + |
| 6 | +trap ctrl_c INT |
| 7 | + |
| 8 | +ctrl_c() { |
| 9 | + echo "** Trapped CTRL-C" |
| 10 | + exit 1 |
| 11 | +} |
| 12 | + |
| 13 | +failed=0 |
| 14 | + |
| 15 | +:echo() { |
| 16 | + local color=${2:-"33;1"} |
| 17 | + echo -e "\e[${color}m$1\e[0m" |
| 18 | +} |
| 19 | + |
| 20 | +:note() { |
| 21 | + :echo "$1" "35;1" |
| 22 | +} |
| 23 | + |
| 24 | +:case() { |
| 25 | + if [ $? -ne 0 ] |
| 26 | + then failed=1 |
| 27 | + fi |
| 28 | + |
| 29 | + if [ "$failed" -eq 1 ] |
| 30 | + then :echo "SKIPPED: $1"; return 1 |
| 31 | + else echo; :echo "CASE: $1" |
| 32 | + fi |
| 33 | +} |
| 34 | + |
| 35 | +:check_diff() { |
| 36 | + expected="$1" |
| 37 | + |
| 38 | + set +e |
| 39 | + terraform plan -detailed-exitcode |
| 40 | + status=$? |
| 41 | + set -e |
| 42 | + # ${status} possible values: |
| 43 | + # 0 - Succeeded, diff is empty (no changes) |
| 44 | + # 1 - Errored |
| 45 | + # 2 - Succeeded, there is a diff |
| 46 | + if [ "${status}" -ne "${expected}" ]; then |
| 47 | + case "${expected}" in |
| 48 | + 0) |
| 49 | + :echo "Error: we don't expect any diff here!" |
| 50 | + return 1 |
| 51 | + ;; |
| 52 | + 2) |
| 53 | + echo "Error: we DO expect some diff here!" |
| 54 | + return 1 |
| 55 | + ;; |
| 56 | + esac |
| 57 | + fi |
| 58 | +} |
| 59 | + |
| 60 | +terraform=$(which terraform) |
| 61 | +terraform() { |
| 62 | + $terraform "$@" < <(yes yes) |
| 63 | +} |
| 64 | + |
| 65 | +:note "Preparing ..." |
| 66 | +rm -rf src |
| 67 | +mkdir -p src |
| 68 | +cp -r "../fixtures/python3.10-app1" src |
| 69 | +terraform init |
| 70 | +:echo "Destroy / Remove ZIP files" |
| 71 | +terraform destroy |
| 72 | +rm -rf builds 2>/dev/null || true |
| 73 | + |
| 74 | +############################################################# |
| 75 | +# Part 1: Check that CICD environment won't detect any diff # |
| 76 | +############################################################# |
| 77 | + |
| 78 | +:echo |
| 79 | +:note "Starting Part 1: Check that CICD environment won't detect any diff" |
| 80 | + |
| 81 | +:case "Apply / No diff" && { |
| 82 | + terraform apply |
| 83 | + :check_diff 0 |
| 84 | +} |
| 85 | + |
| 86 | +:case "Remove 'builds' dir / No diff" && { |
| 87 | + rm -rf builds |
| 88 | + :check_diff 0 |
| 89 | +} |
| 90 | + |
| 91 | +############################################################################### |
| 92 | +# Part 2: Check that CICD environment will detect diff if lambda code changes # |
| 93 | +############################################################################### |
| 94 | + |
| 95 | +:echo |
| 96 | +:note "Starting Part 2: Check that CICD environment will detect diff if lambda code changes" |
| 97 | + |
| 98 | +:note "Change the source code / Remove 'builds' dir" |
| 99 | +echo "" >> src/python3.10-app1/index.py |
| 100 | +rm -rf builds |
| 101 | + |
| 102 | +:case "Plan / Expect diff" && { |
| 103 | + terraform plan |
| 104 | + :check_diff 2 |
| 105 | +} |
| 106 | + |
| 107 | +:case "Apply / No diff" && { |
| 108 | + terraform apply |
| 109 | + :check_diff 0 |
| 110 | +} |
| 111 | + |
| 112 | +:note "Remove 'builds' dir" |
| 113 | +rm -rf builds |
| 114 | + |
| 115 | +:case "Plan / No diff" && { |
| 116 | + terraform plan |
| 117 | + :check_diff 0 |
| 118 | +} |
| 119 | + |
| 120 | +#:case "Destroy / Remove ZIP files" && { |
| 121 | +# terraform plan -destroy |
| 122 | +# terraform destroy -auto-approve |
| 123 | +# rm builds/*.zip |
| 124 | +#} |
| 125 | + |
| 126 | +:note "All tests have passed successfully." |
0 commit comments