-
Notifications
You must be signed in to change notification settings - Fork 113
/
Copy pathrun_test
executable file
·212 lines (185 loc) · 7.36 KB
/
run_test
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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
#!/usr/bin/env bash
set -Eeo pipefail
CI=${CI:-false}
HAB_ORIGIN=${HAB_ORIGIN:-chef}
TIMEOUT=$((${BUILDKITE_TIMEOUT:-32} - 2))
source_dir=$(cd "$(dirname "${BASH_SOURCE[0]}" )" && pwd)
source "${source_dir}/helpers/log.sh"
source "${source_dir}/base.sh"
source "$1"
BUILDKITE_JOB_ID="${BUILDKITE_JOB_ID:-"${test_build_slug}"}"
complete_commandline="$0 $*"
test_commandline() {
local env_vars
if [[ -n "$VAULT_UTIL_SECRETS" ]]; then
echo " # When on the Chef VPN"
echo " CHEF_USERNAME=yourusername scripts/get_secrets.sh"
echo " export VAULT_ADDR=https://vault.chef.co:8200"
# jq -r 'to_entries | map(" export " + .key + "=$(vault kv get -field=" + .value.field + " " + .value.path + ")")[]' <<<"$VAULT_UTIL_SECRETS"
fi
# Reproduce a select set of environment variables that influence
# the tests.
env_vars=$(set +o pipefail; env | grep -E '^(FLAKY|IAM)=' | tr '\n' ' ')
echo " $env_vars $complete_commandline"
}
cleanup() {
if [ ${#test_external_services[@]} -ne 0 ]; then
log_info "Cleaning Up External Services"
for external_service in "${test_external_services[@]}"
do
log_info "Dumping logs for $external_service"
${external_service}_dump_logs
log_info "Stopping $external_service"
${external_service}_teardown
done
destroy_services_config_path
fi
echo "Attempting to clean up docker container $test_container_name"
docker stop "$test_container_name"
if [[ "${test_proxy}" = "true" ]]; then
echo "Attempting to clean up docker container $test_proxy_container_name"
docker stop "$test_proxy_container_name"
echo "Attempting to clean up docker network $test_proxy_internal_network_name"
docker network rm "$test_proxy_internal_network_name"
fi
echo "------------------------------------------------------------"
echo "To run this test locally:"
echo ""
test_commandline
echo ""
echo "------------------------------------------------------------"
}
cleanup_ci() {
# We condition this on CI so that in CI we always cleanup, even on failure
if [[ "$CI" = "true" ]]; then
cleanup
else
echo "Not cleaning up because CI=$CI"
fi
}
if [[ "$CI" = "true" ]]; then
echo "===== PRE-TEST HOST HEALTH REPORT ====="
echo "docker ps"
docker ps
echo "free -m"
free -m
echo "uptime"
uptime
echo "======================================="
else
# In CI we expect the images to have already pulled the latest docker
# layers, but we can't rely on that for local machines. Here we'll make sure
# we start containers with the latest images.
docker pull chefes/centos-systemd:latest
docker pull chefes/a2-integration:latest
fi
workdir="/go/src/github.com/chef/automate"
CURR_DIR=$(pwd)
docker_run_args=(
"--detach"
"--env" "CI"
"--env" "HOST_PWD=$CURR_DIR"
"--env" "HAB_ORIGIN=$HAB_ORIGIN"
"--env" "HAB_STUDIO_SUP=false"
"--env" "HAB_NONINTERACTIVE=true"
"--env" "CONTAINER_HOSTNAME=${test_container_name}"
"--env" "test_notifications_endpoint=${test_notifications_endpoint}"
"--env" "IAM"
"--env" "FLAKY"
"--env" "A2_LICENSE"
"--env" "CYPRESS_AUTOMATE_ACCEPTANCE_TARGET_HOST"
"--env" "CYPRESS_AUTOMATE_ACCEPTANCE_TARGET_USER"
"--env" "CYPRESS_AUTOMATE_ACCEPTANCE_TARGET_KEY"
"--env" "CYPRESS_AUTOMATE_INFRA_ADMIN_KEY"
"--env" "AWS_ACCESS_KEY_ID"
"--env" "AWS_SECRET_ACCESS_KEY"
"--env" "AWS_SESSION_TOKEN"
"--env" "GOOGLE_APPLICATION_JSON"
"--env" "test_container_name=${test_container_name}"
"--env" "test_build_slug=${test_build_slug}"
"--env" "test_proxy_container_name=${test_proxy_container_name}"
"--env" "test_proxy_internal_network_name=${test_proxy_internal_network_name}"
"--hostname" "$test_container_name"
"--interactive"
"--name" "$test_container_name"
"--privileged"
"--rm"
"--tmpfs=/tmp:rw,noexec,nosuid"
"--tmpfs=/var/tmp:rw,noexec,nosuid"
"--tmpfs=/dev/shm:rw,noexec,nosuid"
"--tty"
"--volume" "$CURR_DIR:$workdir"
# I have no idea why our container doesn't like it when
# we try to mount docker.sock in the default place. Removing
# the privileged flag makes it work.
"-v" "/var/run/docker.sock:/docker.sock"
"--env" "DOCKER_HOST=unix:///docker.sock"
"--workdir" "$workdir"
)
if [ $CI = "true" ]
then
buildkite_agent=$(command -v buildkite-agent)
docker_run_args+=(
"--env" "BUILDKITE_JOB_ID"
"--env" "BUILDKITE_BUILD_ID"
"--env" "BUILDKITE_AGENT_ACCESS_TOKEN"
"--env" "BUILDKITE"
"--volume" "$buildkite_agent:/usr/bin/buildkite-agent"
"--label" "buildkitejob=$BUILDKITE_JOB_ID"
)
fi
trap cleanup_ci EXIT
if [[ "${test_proxy}" = "true" ]]; then
mkdir -p logs
log_info "Creating proxy network"
# Create a network with no internet access.
# Automate will connect to this network
docker network create "$test_proxy_internal_network_name" \
--internal \
--label "buildkitejob=$BUILDKITE_JOB_ID"
docker_run_args+=(
"--network" "${test_proxy_internal_network_name}"
)
# Run squid and connect it to the network without internet
# access so that automate can access it.
docker run \
--detach --rm \
--name "${test_proxy_container_name}" \
--hostname "${test_proxy_container_name}" \
--label "buildkitejob=$BUILDKITE_JOB_ID" \
--network "${test_proxy_internal_network_name}" \
--volume "$PWD/logs:/var/log/squid" \
--volume "$PWD/integration/squid.conf:/etc/squid/squid.conf" \
$(docker build -q $PWD/integration/docker-squid)
# Connect the proxy container to the default bridge network
# which has access to the internet
docker network connect bridge "${test_proxy_container_name}"
fi
if [ ${#test_external_services[@]} -ne 0 ]; then
log_info "Start External Services"
source integration/services/common.sh
create_services_config_path
docker_run_args+=(
"--volume" "$SERVICES_CONFIG_PATH:/services"
)
for external_service in "${test_external_services[@]}"
do
log_info "Starting $external_service"
source integration/services/$external_service/init.sh
${external_service}_setup
done
fi
if [ ! -z "$EXTERNAL_ONLY" ]; then
echo "Started external services... exiting"
exit 0
fi
echo "${docker_run_args[*]}"
docker run "${docker_run_args[@]}" chefes/a2-integration:latest
if [ $CI != "true" ]; then
secret_origin_key=$(HAB_LICENSE=accept-no-persist hab origin key export --type secret "$HAB_ORIGIN")
docker exec -e SEC_KEY="$secret_origin_key" -e HAB_LICENSE="accept-no-persist" "$test_container_name" bash -c 'echo "$SEC_KEY" | hab origin key import'
public_origin_key=$(HAB_LICENSE=accept-no-persist hab origin key export --type public "$HAB_ORIGIN")
docker exec -e SEC_KEY="$public_origin_key" -e HAB_LICENSE="accept-no-persist" "$test_container_name" bash -c 'echo "$SEC_KEY" | hab origin key import'
fi
echo "Running the docker. Will timeout after $TIMEOUT mins"
docker exec -e TEST_PATH=$1 "$test_container_name" timeout "${TIMEOUT}m" bash -c 'source ./integration/base.sh; __run_test $TEST_PATH'