Skip to content

Commit 30d38e8

Browse files
authored
Merge pull request microservices-demo#172 from weaveworks/testing/skeleton
Testing skeleton. Added scripts to run java and go unit tests in dock…
2 parents 3e2009c + 66a5776 commit 30d38e8

File tree

13 files changed

+493
-0
lines changed

13 files changed

+493
-0
lines changed

testing/README.md

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
# Testing
2+
3+
These directories hold scripts that run tests for each testing scope. For example, the unit folder contains scripts that run unit tests for each service (i.e. just calls mvn test or equivalent). Other scopes may be more complex.
4+
5+
## Layout
6+
7+
Each folder contains shell scripts that (roughly, e.g. java unit tests all run at once) test each service. To create a new test, simply add a `sh` script to the folder.
8+
9+
## Usage
10+
11+
To run a test scope, simply call the `test.sh` file with the scope that you want to test. For example: `./test.sh --verbose unit component` will run the unit and component tests in verbose mode, meaning that all the intermediate testing steps will be printed.

testing/application/README.md

Whitespace-only changes.

testing/assert.sh

Lines changed: 190 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,190 @@
1+
#!/bin/bash
2+
# assert.sh 1.1 - bash unit testing framework
3+
# Copyright (C) 2009-2015 Robert Lehmann
4+
#
5+
# http://github.com/lehmannro/assert.sh
6+
#
7+
# This program is free software: you can redistribute it and/or modify
8+
# it under the terms of the GNU Lesser General Public License as published
9+
# by the Free Software Foundation, either version 3 of the License, or
10+
# (at your option) any later version.
11+
#
12+
# This program is distributed in the hope that it will be useful,
13+
# but WITHOUT ANY WARRANTY; without even the implied warranty of
14+
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15+
# GNU Lesser General Public License for more details.
16+
#
17+
# You should have received a copy of the GNU Lesser General Public License
18+
# along with this program. If not, see <http://www.gnu.org/licenses/>.
19+
20+
export DISCOVERONLY=${DISCOVERONLY:-}
21+
export DEBUG=${DEBUG:-}
22+
export STOP=${STOP:-}
23+
export INVARIANT=${INVARIANT:-}
24+
export CONTINUE=${CONTINUE:-}
25+
26+
args="$(getopt -n "$0" -l \
27+
verbose,help,stop,discover,invariant,continue vhxdic $*)" \
28+
|| exit -1
29+
for arg in $args; do
30+
case "$arg" in
31+
-h)
32+
echo "$0 [-vxidc]" \
33+
"[--verbose] [--stop] [--invariant] [--discover] [--continue]"
34+
echo "`sed 's/./ /g' <<< "$0"` [-h] [--help]"
35+
exit 0;;
36+
--help)
37+
cat <<EOF
38+
Usage: $0 [options]
39+
Language-agnostic unit tests for subprocesses.
40+
41+
Options:
42+
-v, --verbose generate output for every individual test case
43+
-x, --stop stop running tests after the first failure
44+
-i, --invariant do not measure timings to remain invariant between runs
45+
-d, --discover collect test suites only, do not run any tests
46+
-c, --continue do not modify exit code to test suite status
47+
-h show brief usage information and exit
48+
--help show this help message and exit
49+
EOF
50+
exit 0;;
51+
-v|--verbose)
52+
DEBUG=1;;
53+
-x|--stop)
54+
STOP=1;;
55+
-i|--invariant)
56+
INVARIANT=1;;
57+
-d|--discover)
58+
DISCOVERONLY=1;;
59+
-c|--continue)
60+
CONTINUE=1;;
61+
esac
62+
done
63+
64+
_indent=$'\n\t' # local format helper
65+
66+
_assert_reset() {
67+
tests_ran=0
68+
tests_failed=0
69+
tests_errors=()
70+
tests_starttime="$(date +%s%N)" # nanoseconds_since_epoch
71+
}
72+
73+
assert_end() {
74+
# assert_end [suite ..]
75+
tests_endtime="$(date +%s%N)"
76+
# required visible decimal place for seconds (leading zeros if needed)
77+
local tests_time="$( \
78+
printf "%010d" "$(( ${tests_endtime/%N/000000000}
79+
- ${tests_starttime/%N/000000000} ))")" # in ns
80+
tests="$tests_ran ${*:+$* }tests"
81+
[[ -n "$DISCOVERONLY" ]] && echo "collected $tests." && _assert_reset && return
82+
[[ -n "$DEBUG" ]] && echo
83+
# to get report_time split tests_time on 2 substrings:
84+
# ${tests_time:0:${#tests_time}-9} - seconds
85+
# ${tests_time:${#tests_time}-9:3} - milliseconds
86+
[[ -z "$INVARIANT" ]] \
87+
&& report_time=" in ${tests_time:0:${#tests_time}-9}.${tests_time:${#tests_time}-9:3}s" \
88+
|| report_time=
89+
90+
if [[ "$tests_failed" -eq 0 ]]; then
91+
echo "all $tests passed$report_time."
92+
else
93+
for error in "${tests_errors[@]}"; do echo "$error"; done
94+
echo "$tests_failed of $tests failed$report_time."
95+
fi
96+
tests_failed_previous=$tests_failed
97+
[[ $tests_failed -gt 0 ]] && tests_suite_status=1
98+
_assert_reset
99+
}
100+
101+
assert() {
102+
# assert <command> <expected stdout> [stdin]
103+
(( tests_ran++ )) || :
104+
[[ -z "$DISCOVERONLY" ]] || return
105+
expected=$(echo -ne "${2:-}")
106+
result="$(eval 2>/dev/null $1 <<< ${3:-})" || true
107+
if [[ "$result" == "$expected" ]]; then
108+
[[ -z "$DEBUG" ]] || echo -n .
109+
return
110+
fi
111+
result="$(sed -e :a -e '$!N;s/\n/\\n/;ta' <<< "$result")"
112+
[[ -z "$result" ]] && result="nothing" || result="\"$result\""
113+
[[ -z "$2" ]] && expected="nothing" || expected="\"$2\""
114+
_assert_fail "expected $expected${_indent}got $result" "$1" "$3"
115+
}
116+
117+
assert_raises() {
118+
# assert_raises <command> <expected code> [stdin]
119+
(( tests_ran++ )) || :
120+
[[ -z "$DISCOVERONLY" ]] || return
121+
status=0
122+
if [[ -z "$DEBUG" ]] ; then
123+
(eval $1 <<< ${3:-}) > /dev/null 2>&1 || status=$?
124+
else
125+
(eval $1 <<< ${3:-}) || status=$?
126+
fi
127+
expected=${2:-0}
128+
if [[ "$status" -eq "$expected" ]]; then
129+
[[ -z "$DEBUG" ]] || echo -n .
130+
return
131+
fi
132+
_assert_fail "program terminated with code $status instead of $expected" "$1" "$3"
133+
}
134+
135+
_assert_fail() {
136+
# _assert_fail <failure> <command> <stdin>
137+
[[ -n "$DEBUG" ]] && echo -n X
138+
report="test #$tests_ran \"$2${3:+ <<< $3}\" failed:${_indent}$1"
139+
if [[ -n "$STOP" ]]; then
140+
[[ -n "$DEBUG" ]] && echo
141+
echo "$report"
142+
exit 1
143+
fi
144+
tests_errors[$tests_failed]="$report"
145+
(( tests_failed++ )) || :
146+
}
147+
148+
skip_if() {
149+
# skip_if <command ..>
150+
(eval $@) > /dev/null 2>&1 && status=0 || status=$?
151+
[[ "$status" -eq 0 ]] || return
152+
skip
153+
}
154+
155+
skip() {
156+
# skip (no arguments)
157+
shopt -q extdebug && tests_extdebug=0 || tests_extdebug=1
158+
shopt -q -o errexit && tests_errexit=0 || tests_errexit=1
159+
# enable extdebug so returning 1 in a DEBUG trap handler skips next command
160+
shopt -s extdebug
161+
# disable errexit (set -e) so we can safely return 1 without causing exit
162+
set +o errexit
163+
tests_trapped=0
164+
trap _skip DEBUG
165+
}
166+
_skip() {
167+
if [[ $tests_trapped -eq 0 ]]; then
168+
# DEBUG trap for command we want to skip. Do not remove the handler
169+
# yet because *after* the command we need to reset extdebug/errexit (in
170+
# another DEBUG trap.)
171+
tests_trapped=1
172+
[[ -z "$DEBUG" ]] || echo -n s
173+
return 1
174+
else
175+
trap - DEBUG
176+
[[ $tests_extdebug -eq 0 ]] || shopt -u extdebug
177+
[[ $tests_errexit -eq 1 ]] || set -o errexit
178+
return 0
179+
fi
180+
}
181+
182+
183+
_assert_reset
184+
: ${tests_suite_status:=0} # remember if any of the tests failed so far
185+
_assert_cleanup() {
186+
local status=$?
187+
# modify exit code if it's not already non-zero
188+
[[ $status -eq 0 && -z $CONTINUE ]] && exit $tests_suite_status
189+
}
190+
trap _assert_cleanup EXIT

testing/component/README.md

Whitespace-only changes.

testing/container/README.md

Whitespace-only changes.

0 commit comments

Comments
 (0)