-
Notifications
You must be signed in to change notification settings - Fork 7
/
teardown
executable file
·154 lines (129 loc) · 3.33 KB
/
teardown
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
#!/usr/bin/env bash
set -euo pipefail
# Init lib.
# shellcheck source=SCRIPTDIR/../lib/vector-test-harness/init.sh
source "$(cd "$(dirname "${BASH_SOURCE[0]}")" >/dev/null 2>&1 && pwd)/../lib/vector-test-harness/init.sh"
#
# Usage
#
register_usage <<END
USAGE
teardown -t test [-c configuration] [-u user-id]
DESCRIPTION
Runs and profiles the provided case, uploading the results to S3.
OPTIONS
-c, --configuration The test configuration (default: default, env: VECTOR_TEST_CONFIGURATION)
-t, --test The test to run (ex: tcp_to_blackhole, env: VECTOR_TEST_NAME)
-u, --user-id Your user ID (ex: ben, env: VECTOR_TEST_USER_ID)
-y, --yes Answer yes to any prompts
-h, --help Print this message
EXAMPLE
teardown -t tcp_to_blackhole
teardown -t tcp_to_blackhole -c high_concurrency
END
#
# Dependencies
#
ensure_commad_available "terraform" "brew install terraform"
ensure_echanced_getopt_available
#
# Variables
#
# Configurable via cli flags
TEST_CONFIGURATION="${VECTOR_TEST_CONFIGURATION:-"default"}"
TEST_NAME="${VECTOR_TEST_NAME:-""}"
TEST_RESULTS_S3_BUCKET_NAME="${VECTOR_TEST_RESULTS_S3_BUCKET_NAME:-""}"
TEST_SSH_PUBLIC_KEY="${VECTOR_TEST_SSH_PUBLIC_KEY:-""}"
TEST_USER_ID="${VECTOR_TEST_USER_ID:-""}"
TEST_YES_PROMPTS="${VECTOR_TEST_YES_PROMPTS:-""}"
#
# Flags
#
OPTIONS="c:t:u:yh"
LONGOPTS="configuration:,test:,user-id:,yes,help"
PARSED="$(parse_arguments "$OPTIONS" "$LONGOPTS" "$0" "$@")"
eval set -- "$PARSED"
while true; do
case "$1" in
-c | --configuration)
TEST_CONFIGURATION="$2"
shift 2
;;
-t | --test)
TEST_NAME="$2"
shift 2
;;
-u | --user-id)
TEST_USER_ID="$2"
shift 2
;;
-u | --user-id)
TEST_USER_ID="$2"
shift 2
;;
-y | --yes)
TEST_YES_PROMPTS=true
shift 1
;;
-h | --help)
usage
exit 0
;;
--)
shift
break
;;
*)
fail_arg_invalid "Invalid option detected: $1"
;;
esac
done
#
# Requirements
#
if [ -z "$TEST_CONFIGURATION" ]; then
fail_arg_invalid "Error: you must supply a test configuration via the -c flag:"
fi
if [ -z "$TEST_NAME" ]; then
fail_arg_invalid "Error: you must supply a test name via the -t flag:"
fi
if [ -z "$TEST_USER_ID" ]; then
fail_arg_invalid "Error: you must supply a user ID via the -u flag or VECTOR_TEST_USER_ID env var:"
fi
TEST_CASE_DIR="cases/$TEST_NAME"
if [[ ! -d "$TEST_CASE_DIR" ]]; then
fail_arg_invalid "Error: test $TEST_NAME does not exist"
fi
#
# Header
#
print_vector_logo
print_divider
cat <<END
User ID: $TEST_USER_ID
Test name: $TEST_NAME
Test configuration: $TEST_CONFIGURATION
END
print_divider
print_spacer
prepare_terraform_params true
prepare_ansible_config true
prepare_ansible_extra_args_array true
#
# Execute
#
print_section_header "Tearing down test resources"
cd "${TEST_CASE_DIR}/terraform"
export TF_IN_AUTOMATION=true
export TF_DATA_DIR=".terraform/$TEST_CONFIGURATION/$TEST_USER_ID"
terraform destroy \
"${TERRAFORM_COMMON_EXTRA_ARGS[@]}" \
"$(if [[ $TEST_YES_PROMPTS ]]; then echo "--auto-approve"; fi)" \
-var pub_key="$TEST_SSH_PUBLIC_KEY" \
-var test_name="$TEST_NAME" \
-var test_configuration="$TEST_CONFIGURATION" \
-var user_id="$TEST_USER_ID" \
-var results_s3_bucket_name="$TEST_RESULTS_S3_BUCKET_NAME" \
-var-file="../configurations/$TEST_CONFIGURATION/terraform.tfvars"
print_divider
echo 'Done!'