Skip to content

Commit 7369d06

Browse files
committed
Add tests
1 parent e704540 commit 7369d06

13 files changed

+1155
-308
lines changed

tests/.lib.sh

Lines changed: 149 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,17 +11,47 @@ set -o pipefail
1111
run() {
1212
_cmd="${1}"
1313

14-
_red="\033[0;31m"
15-
_green="\033[0;32m"
16-
_yellow="\033[0;33m"
14+
#_red="\033[0;31m"
15+
_gray="\033[38;5;244m"
16+
#_green="\033[0;32m"
17+
#_yellow="\033[0;33m"
1718
_reset="\033[0m"
18-
_user="$(whoami)"
19+
#_user="$(whoami)"
1920

20-
printf "${_yellow}[%s] ${_red}%s \$ ${_green}${_cmd}${_reset}\n" "$(hostname)" "${_user}"
21+
printf "${_gray}%s${_cmd}${_reset}\n" "\$ "
2122
sh -c "LANG=C LC_ALL=C ${_cmd}"
2223
}
2324

2425

26+
log() {
27+
local type="${1}"
28+
local message="${2}"
29+
30+
local clr_gray="\033[38;5;244m"
31+
local clr_green="\033[0;32m"
32+
local clr_red="\033[0;31m"
33+
local clr_rst="\033[0m"
34+
35+
if [ "${type}" = "fail" ]; then
36+
printf "${clr_red}%s${clr_rst}\n" "--------------------------------------------------------------------------------" 1>&2
37+
printf "${clr_red}[FAIL] %s${clr_rst}\n" "${message}" 1>&2
38+
printf "${clr_red}%s${clr_rst}\n" "--------------------------------------------------------------------------------" 1>&2
39+
elif [ "${type}" = "ok" ]; then
40+
#printf "${clr_green}%s${clr_rst}\n" "--------------------------------------------------------------------------------" 1>&2
41+
printf "${clr_green}[SUCC] %s${clr_rst}\n" "${message}"
42+
#printf "${clr_green}%s${clr_rst}\n" "--------------------------------------------------------------------------------" 1>&2
43+
elif [ "${type}" = "loop" ]; then
44+
printf "${clr_gray}%s${clr_rst}\n" "--------------------------------------------------------------------------------" 1>&2
45+
printf "${clr_gray}[LOOP] %s${clr_rst}\n" "${message}"
46+
printf "${clr_gray}%s${clr_rst}\n" "--------------------------------------------------------------------------------" 1>&2
47+
else
48+
printf "${clr_red}%s${clr_rst}\n" "--------------------------------------------------------------------------------" 1>&2
49+
printf "${clr_red}[SYS] %s${clr_rst}\n" "Unknown log type: ${type}" 1>&1
50+
printf "${clr_red}%s${clr_rst}\n" "--------------------------------------------------------------------------------" 1>&2
51+
return 1
52+
fi
53+
}
54+
2555

2656
###
2757
### Get 15 character random word
@@ -39,3 +69,117 @@ function get_random_name() {
3969
done
4070
echo "${name}"
4171
}
72+
73+
tmp_dir() {
74+
local tmp_dir=
75+
tmp_dir="$( mktemp -d )"
76+
chmod 755 "${tmp_dir}"
77+
echo "${tmp_dir}"
78+
}
79+
80+
81+
docker_logs() {
82+
local container_name="${1}"
83+
docker logs "${container_name}" || true
84+
}
85+
86+
docker_stop() {
87+
local container_name="${1}"
88+
docker stop "${container_name}" >/dev/null 2>&1 || true
89+
docker rm -f "${container_name}" >/dev/null 2>&1 || true
90+
}
91+
92+
93+
# --------------------------------------------------------------------------------------------------
94+
# TESTS
95+
# --------------------------------------------------------------------------------------------------
96+
97+
###
98+
### Application 1
99+
###
100+
create_app() {
101+
local path="${1}"
102+
local docr="${2}"
103+
local name="${3}"
104+
local file="${4}"
105+
local cont="${5}"
106+
107+
run "mkdir -p ${path}/${name}/${docr}"
108+
run "echo \"${cont}\" > ${path}/${name}/${docr}/${file}"
109+
}
110+
111+
112+
113+
###
114+
### Find expected string in URL
115+
###
116+
test_vhost_response() {
117+
local expect="${1}"
118+
local url="${2}"
119+
local header="${3:-}"
120+
121+
#local clr_gray="\033[38;5;244m"
122+
local clr_rst="\033[0m"
123+
local clr_test="\033[0;34m" # blue
124+
125+
126+
printf "${clr_test}%s${clr_rst}\n" "--------------------------------------------------------------------------------" 1>&2
127+
printf "${clr_test}[TEST] %s${clr_rst}\n" "Exec: curl -sS -k -L '${url}' -H '${header}'"
128+
printf "${clr_test}[TEST] %s${clr_rst}" "Find: '${expect}' "
129+
130+
count=0
131+
retry=30
132+
output=""
133+
while ! output="$( sh -c "LANG=C LC_ALL=C curl --fail -sS -k -L '${url}' -H '${header}' 2>/dev/null | grep '^${expect}$'" )"; do
134+
printf "."
135+
if [ "${count}" = "${retry}" ]; then
136+
printf "\\n"
137+
sh -c "LANG=C LC_ALL=C curl -v --fail -sS -k -L '${url}' -H '${header}'" || true
138+
return 1
139+
fi
140+
count=$(( count + 1 ))
141+
sleep 1
142+
done
143+
144+
# Print success
145+
printf "\\n"
146+
log "ok" "Resp: '${output}'"
147+
echo
148+
}
149+
150+
151+
###
152+
### Check docker logs for Errors
153+
###
154+
test_docker_logs_err() {
155+
local container_name="${1}"
156+
157+
local re_internal='(\[FAILURE|FAILED|FAIL|FATAL|ERROR|ERR|WARNING|WARN\])'
158+
local re_upper='(FAULT|FAIL|FATAL|ERR|WARN)'
159+
local re_lower='(segfault|fail|fatal|warn)'
160+
local re_mixed='([Ss]egfault|[Ff]ail|[Ff]atal|[Ww]arn)'
161+
local regex="${re_internal}|${re_upper}|${re_lower}|${re_mixed}"
162+
163+
# Ignore this pattern
164+
local ignore1='creating Certificate Authority'
165+
local ignore2='error_log' # nginx error log directive
166+
local ignore3='stackoverflow' # contains a link comment with 'error' in url
167+
local ignore="${ignore1}|${ignore2}|${ignore3}"
168+
169+
#local clr_gray="\033[38;5;244m"
170+
local clr_test="\033[0;34m" # blue
171+
local clr_rst="\033[0m"
172+
173+
printf "${clr_test}%s${clr_rst}\n" "--------------------------------------------------------------------------------" 1>&2
174+
printf "${clr_test}[TEST] %s${clr_rst}\n" "Exec: docker logs ${container_name}"
175+
printf "${clr_test}[TEST] %s${clr_rst}\n" "Find: '${regex}'"
176+
177+
if docker logs "${container_name}" 2>&1 | grep -Ev "${ignore}" | grep -E "${regex}" >/dev/null; then
178+
log "fail" "Found: $( docker logs "${container_name}" 2>&1 | grep -Ev "${ignore}" | grep -E "${regex}" )"
179+
return 1
180+
fi
181+
182+
# Print success
183+
log "ok" "Resp: Nothing found in docker logs"
184+
echo
185+
}

tests/00-test-html.sh

Lines changed: 0 additions & 70 deletions
This file was deleted.

tests/001-test-xargs.sh

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
#!/usr/bin/env bash
2+
3+
set -e
4+
set -u
5+
set -o pipefail
6+
7+
CWD="$(cd -P -- "$(dirname -- "$0")" && pwd -P)"
8+
9+
IMAGE="${1}"
10+
TAG="${2}"
11+
ARCH="${3}"
12+
13+
14+
###
15+
### Load Library
16+
###
17+
# shellcheck disable=SC1090,SC1091
18+
. "${CWD}/.lib.sh"
19+
20+
21+
22+
#---------------------------------------------------------------------------------------------------
23+
# MAIN ENTRYPOINT
24+
#---------------------------------------------------------------------------------------------------
25+
26+
###
27+
### Globals
28+
###
29+
NAME_HTTPD="$( get_random_name )"
30+
31+
32+
###
33+
### Start HTTPD Container
34+
###
35+
if ! run "docker run --platform ${ARCH} --name ${NAME_HTTPD} \
36+
-e DEBUG_ENTRYPOINT=4 \
37+
-e DEBUG_RUNTIME=1 \
38+
--entrypoint=bash \
39+
${IMAGE}:${TAG} -c 'command -v xargs >/dev/null'"; then
40+
docker_logs "${NAME_HTTPD}"
41+
docker_stop "${NAME_HTTPD}"
42+
log "fail" "command 'xargs' not found inside image, but required"
43+
exit 1
44+
fi
45+
echo "command 'xargs' found"
46+
47+
48+
###
49+
### Cleanup
50+
###
51+
docker_stop "${NAME_HTTPD}"
52+
log "ok" "Test succeeded"

tests/01-test-php.sh

Lines changed: 0 additions & 85 deletions
This file was deleted.

0 commit comments

Comments
 (0)