forked from paketo-community/ubi-base-stack
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test.sh
executable file
·199 lines (160 loc) · 4.99 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
#!/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 OUTPUT_DIR="${STACK_DIR}/build"
readonly OUTPUT_DIR_NODEJS16="${STACK_DIR}/build-nodejs-16"
readonly OUTPUT_DIR_NODEJS18="${STACK_DIR}/build-nodejs-18"
readonly OUTPUT_DIR_NODEJS20="${STACK_DIR}/build-nodejs-20"
readonly OUTPUT_DIR_JAVA8="${STACK_DIR}/build-java-8"
readonly OUTPUT_DIR_JAVA11="${STACK_DIR}/build-java-11"
readonly OUTPUT_DIR_JAVA17="${STACK_DIR}/build-java-17"
readonly OUTPUT_DIR_JAVA21="${STACK_DIR}/build-java-21"
# 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
clean="false"
token=""
local regport create_registry
regport=""
create_registry="true"
while [[ "${#}" != 0 ]]; do
case "${1}" in
--help|-h)
shift 1
usage
exit 0
;;
--clean|-c)
shift 1
clean="true"
;;
--token|-t)
token="${2}"
shift 2
;;
--regport | -p)
regport="${2}"
shift 2
;;
"")
# skip if the argument is empty
shift 1
;;
*)
util::print::error "unknown argument \"${1}\""
esac
done
tools::install "${token}"
if [[ "${clean}" == "true" ]]; then
util::print::title "Cleaning up preexisting stack archives..."
rm -rf "${OUTPUT_DIR}"
rm -rf "${OUTPUT_DIR_NODEJS16}"
rm -rf "${OUTPUT_DIR_NODEJS18}"
rm -rf "${OUTPUT_DIR_NODEJS20}"
rm -rf "${OUTPUT_DIR_JAVA8}"
rm -rf "${OUTPUT_DIR_JAVA11}"
rm -rf "${OUTPUT_DIR_JAVA17}"
rm -rf "${OUTPUT_DIR_JAVA21}"
fi
if ! [[ -f "${OUTPUT_DIR}/build.oci" ]] || \
! [[ -f "${OUTPUT_DIR}/run.oci" ]] || \
! [[ -f "${OUTPUT_DIR_NODEJS16}/run.oci" ]] || \
! [[ -f "${OUTPUT_DIR_NODEJS18}/run.oci" ]] || \
! [[ -f "${OUTPUT_DIR_NODEJS20}/run.oci" ]] || \
! [[ -f "${OUTPUT_DIR_JAVA8}/run.oci" ]] || \
! [[ -f "${OUTPUT_DIR_JAVA11}/run.oci" ]] || \
! [[ -f "${OUTPUT_DIR_JAVA17}/run.oci" ]] || \
! [[ -f "${OUTPUT_DIR_JAVA21}/run.oci" ]]; then
util::print::title "Creating stack..."
"${STACK_DIR}/scripts/create.sh"
fi
if [[ -z "${regport:-}" ]]; then
regport="5000"
fi
util::print::title "Setting up local registry"
registry_container_id=$(util::tools::setup_local_registry "$regport")
export REGISTRY_URL="localhost:${regport}"
tests::run "${registry_container_id}"
util::tools::cleanup_local_registry "${registry_container_id}"
}
function usage() {
cat <<-USAGE
test.sh [OPTIONS]
Runs acceptance tests against the stack. Uses the OCI images
${STACK_DIR}/build/build.oci
and
${STACK_DIR}/build/run.oci
and
${STACK_DIR}/build-nodejs-16/run.oci
and
${STACK_DIR}/build-nodejs-18/run.oci
and
${STACK_DIR}/build-nodejs-20/run.oci
and
${STACK_DIR}/build-java-8/run.oci
and
${STACK_DIR}/build-java-11/run.oci
and
${STACK_DIR}/build-java-17/run.oci
and
${STACK_DIR}/build-java-21/run.oci
if they exist. Otherwise, first runs create.sh to create them.
OPTIONS
--clean -c clears contents of stack output directory before running tests
--regport <port> -p Local port to use for local registry during tests, defaults to 5000
--token <token> -t Token used to download assets from GitHub (e.g. jam, pack, etc) (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
}
function tests::run() {
local registry_container_id
registry_container_id="${1}"
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::tools::cleanup_local_registry "${registry_container_id}"
util::print::success "** GO Test Succeeded **"
else
util::tools::cleanup_local_registry "${registry_container_id}"
util::print::error "** GO Test Failed **"
fi
popd > /dev/null
}
function util::tools::setup_local_registry() {
registry_port="${1}"
local registry_container_id
if [[ "$(curl -s -o /dev/null -w "%{http_code}" localhost:$registry_port/v2/)" == "200" ]]; then
registry_container_id=""
else
registry_container_id=$(docker run -d -p "${registry_port}:5000" --restart=always registry:2)
fi
echo $registry_container_id
}
function util::tools::cleanup_local_registry() {
local registry_container_id
registry_container_id="${1}"
if [[ -n "${registry_container_id}" ]]; then
docker stop $registry_container_id
docker rm $registry_container_id
fi
}
main "${@:-}"