-
Notifications
You must be signed in to change notification settings - Fork 7
/
test.sh
executable file
·228 lines (183 loc) · 5.8 KB
/
test.sh
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
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
#!/usr/bin/env bash
set -eu
set -o pipefail
readonly PROG_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
readonly STACK_DIR="$(cd "${PROG_DIR}/.." && pwd)"
readonly STACK_IMAGES_JSON_PATH="${STACK_DIR}/images.json"
readonly INTEGRATION_JSON="${STACK_DIR}/integration.json"
declare STACK_IMAGES
# shellcheck source=SCRIPTDIR/.util/tools.sh
source "${PROG_DIR}/.util/tools.sh"
# shellcheck source=SCRIPTDIR/.util/print.sh
source "${PROG_DIR}/.util/print.sh"
function main() {
local clean token test_only_stacks registryPort registryPid localRegistry setupLocalRegistry
help=""
clean="false"
token=""
test_only_stacks=""
registryPid=""
setupLocalRegistry=""
while [[ "${#}" != 0 ]]; do
case "${1}" in
--help|-h)
shift 1
help="true"
;;
--clean|-c)
shift 1
clean="true"
;;
--token|-t)
token="${2}"
shift 2
;;
--test-only-stacks)
test_only_stacks="${2}"
shift 2
;;
"")
# skip if the argument is empty
shift 1
;;
*)
util::print::error "unknown argument \"${1}\""
esac
done
if [ -f "${STACK_IMAGES_JSON_PATH}" ]; then
all_stack_images=$(jq -c '.' "${STACK_IMAGES_JSON_PATH}")
else
# If there is no images.json file, fallback to the default image configuration
all_stack_images=$(jq -nc '{
"images": [
{
"config_dir": "stack",
"output_dir": "build",
"build_image": "build",
"run_image": "run",
"create_build_image": true
}
]
}' | jq -c '.')
fi
if [[ -n "${test_only_stacks}" ]]; then
filter_stacks=$(echo $test_only_stacks | jq -R 'split(" ")')
STACK_IMAGES=$(echo "${all_stack_images}" | \
jq --argjson names "$filter_stacks" -c \
'.images[] | select(.name | IN($names[]))')
export TEST_ONLY_STACKS="${test_only_stacks}"
else
STACK_IMAGES=$(echo "${all_stack_images}" | jq -c '.images[]')
export TEST_ONLY_STACKS=""
fi
## The help is after the image parsing and filtering in order to output
## proper usage information based on the images that are available
if [[ "${help}" == "true" ]]; then
usage
exit 0
fi
tools::install "${token}"
if [[ "${clean}" == "true" ]]; then
util::print::title "Cleaning up preexisting stack archives..."
clean::stacks
fi
stack_output_builds_exist=$(stack_builds_exist)
if [[ "${stack_output_builds_exist}" == "false" ]]; then
util::print::title "Creating stack..."
while read -r image; do
config_dir=$(echo "${image}" | jq -r '.config_dir')
output_dir=$(echo "${image}" | jq -r '.output_dir')
"${STACK_DIR}/scripts/create.sh" \
--stack-dir "${config_dir}" \
--build-dir "${output_dir}"
done <<<"$STACK_IMAGES"
fi
if [[ -f $INTEGRATION_JSON ]]; then
setupLocalRegistry=$(jq '.setup_local_registy' $INTEGRATION_JSON)
fi
if [[ "${setupLocalRegistry}" == "true" ]]; then
registryPort=$(get::random::port)
registryPid=$(local::registry::start $registryPort)
localRegistry="127.0.0.1:$registryPort"
export REGISTRY_URL="${localRegistry}"
fi
tests::run
if [[ "${setupLocalRegistry}" == "true" ]]; then
kill $registryPid
fi
}
function join_by {
local d=${1-} f=${2-}
if shift 2; then
printf %s "$f" "${@/#/$d}"
fi
}
function usage() {
oci_images_arr=()
while read -r image; do
output_dir=$(echo "${image}" | jq -r '.output_dir')
build_image=$(echo "${image}" | jq -r '.build_image')
create_build_image=$(echo "${image}" | jq -r '.create_build_image')
run_image=$(echo "${image}" | jq -r '.run_image')
if [ $create_build_image == 'true' ]; then
oci_images_arr =("${STACK_DIR}/${output_dir}/${build_image}.oci")
fi
oci_images_arr =("${STACK_DIR}/${output_dir}/${run_image}.oci")
done <<<"$STACK_IMAGES"
joined_oci_images=$(join_by $'\nand\n' ${oci_images_arr[*]})
cat <<-USAGE
test.sh [OPTIONS]
Runs acceptance tests against the stack. Uses the OCI images
${joined_oci_images}
if they exist. Otherwise, first runs create.sh to create them.
OPTIONS
--clean -c clears contents of stack output directory before running tests
--token <token> -t Token used to download assets from GitHub (e.g. jam, pack, etc) (optional)
--test-only-stacks Runs the tests of the stacks passed to this argument (e.g. java-8 nodejs-16) (optional)
--help -h prints the command usage
USAGE
}
function tools::install() {
local token
token="${1}"
util::tools::jam::install \
--directory "${STACK_DIR}/.bin" \
--token "${token}"
util::tools::pack::install \
--directory "${STACK_DIR}/.bin" \
--token "${token}"
util::tools::skopeo::check
util::tools::crane::install \
--directory "${STACK_DIR}/.bin" \
--token "${token}"
}
function tests::run() {
util::print::title "Run Stack Acceptance Tests"
export CGO_ENABLED=0
testout=$(mktemp)
pushd "${STACK_DIR}" > /dev/null
if GOMAXPROCS="${GOMAXPROCS:-4}" go test -count=1 -timeout 0 ./... -v -run Acceptance | tee "${testout}"; then
util::tools::tests::checkfocus "${testout}"
util::print::success "** GO Test Succeeded **"
else
util::print::error "** GO Test Failed **"
fi
popd > /dev/null
}
function stack_builds_exist() {
local stack_output_builds_exist="true"
while IFS= read -r image; do
stack_output_dir=$(echo "${image}" | jq -r '.output_dir')
if ! [[ -f "${STACK_DIR}/${stack_output_dir}/build.oci" ]] || ! [[ -f "${STACK_DIR}/${stack_output_dir}/run.oci" ]]; then
stack_output_builds_exist="false"
fi
done <<<"$STACK_IMAGES"
echo "$stack_output_builds_exist"
}
function clean::stacks(){
while read -r image; do
output_dir=$(echo "${image}" | jq -r '.output_dir')
rm -rf "${STACK_DIR}/${output_dir}"
done <<<"$STACK_IMAGES"
}
main "${@:-}"