forked from bentoml/BentoML
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrun_tests.sh
executable file
·232 lines (180 loc) · 4.75 KB
/
run_tests.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
229
230
231
#!/usr/bin/env bash
# Prerequisite:
# This scripts assumes BentoML and all its test dependencies are already installed:
#
# pip install -e .
# pip install requirements/tests-requirements.txt
fname=$(basename "$0")
dname=$(dirname "$0")
source "$dname/helpers.sh"
set_on_failed_callback "ERR=1"
GIT_ROOT=$(git rev-parse --show-toplevel)
ERR=0
declare -a PYTESTARGS
CONFIG_FILE="$dname/config.yml"
REQ_FILE="/tmp/additional-requirements.txt"
SKIP_DEPS=0
cd "$GIT_ROOT" || exit
run_yq() {
need_cmd yq
yq "$@";
}
getval(){
run_yq eval "$@" "$CONFIG_FILE";
}
run_python(){
python -m "$@"
}
validate_yaml() {
# validate YAML file
if ! [ -f "$CONFIG_FILE" ]; then
FAIL "$CONFIG_FILE does not exists"
exit 1
fi
if ! (run_yq e --exit-status 'tag == "!!map" or tag== "!!seq"' "$CONFIG_FILE"> /dev/null); then
FAIL "Invalid YAML file"
exit 1
fi
}
usage() {
need_cmd cat
cat <<HEREDOC
Running unit/integration tests with pytest and generate coverage reports. Make sure that given testcases is defined under $CONFIG_FILE.
Usage:
$dname/$fname [-h|--help] [-v|--verbose] [-s|--skip_deps] <target> <pytest_additional_arguments>
Flags:
-h, --help show this message
-v, --verbose set verbose scripts
-s, --skip_deps skip install dependencies
If pytest_additional_arguments is given, this will be appended to given tests run.
Example:
$ $dname/$fname pytorch --gpus
HEREDOC
exit 2
}
parse_args() {
if [ "${#@}" -eq 0 ]; then
FAIL "$0 doesn't run without any arguments";
exit 1;
fi
for arg in "$@"; do
case "$arg" in
-h | --help)
usage;;
-v | --verbose)
set -x;
shift;;
-s | --skip_deps)
SKIP_DEPS=1;
shift;;
*)
;;
esac
done
PYTESTARGS=( "${*:2}" )
shift $(( OPTIND - 1 ))
}
parse_config() {
target=$@
test_dir=
is_dir=
override_name_or_path=
external_scripts=
type_tests=
test_dir=$(getval ".$target.root_test_dir")
is_dir=$(getval ".$target.is_dir")
override_name_or_path=$(getval ".$target.override_name_or_path")
external_scripts=$(getval ".$target.external_scripts")
type_tests=$(getval ".$target.type_tests")
# processing file name
if [[ "$override_name_or_path" != "" ]]; then
fname="$override_name_or_path"
elif [[ "$is_dir" == "false" ]]; then
fname="test_""$target""_impl.py"
elif [[ "$is_dir" == "true" ]]; then
fname=""
shift
else
fname="$target"
fi
# processing dependencies
run_yq eval '.'"$target"'.dependencies[]' "$CONFIG_FILE" >"$REQ_FILE" || exit
}
install_yq() {
set -ex
target_dir="$HOME/.local/bin"
mkdir -p "$target_dir"
export PATH=$target_dir:$PATH
YQ_VERSION=4.16.1
echo "Trying to install yq..."
shell=$(uname | tr '[:upper:]' '[:lower:]')
extensions=".tar.gz"
if [[ "$shell" =~ "mingw64" ]]; then
shell="windows"
extensions=".zip"
fi
YQ_BINARY=yq_"$shell"_amd64
YQ_EXTRACT="./$YQ_BINARY"
if [[ "$shell" == "windows" ]]; then
YQ_EXTRACT="$YQ_BINARY.exe"
fi
curl -fsSLO https://github.com/mikefarah/yq/releases/download/v"$YQ_VERSION"/"$YQ_BINARY""$extensions"
echo "tar $YQ_BINARY$extensions and move to /usr/bin/yq..."
if [[ $(uname | tr '[:upper:]' '[:lower:]') =~ "mingw64" ]]; then
unzip -qq "$YQ_BINARY$extensions" -d yq_dir && cd yq_dir
mv "$YQ_EXTRACT" "$target_dir"/yq && cd ..
rm -rf yq_dir
else
tar -zvxf "$YQ_BINARY$extensions" "$YQ_EXTRACT" && mv "$YQ_EXTRACT" "$target_dir"/yq
fi
rm -f ./"$YQ_BINARY""$extensions"
}
main() {
parse_args "$@"
need_cmd make
need_cmd curl
need_cmd tr
(need_cmd yq && echo "Using yq via $(which yq)...";) || install_yq
run_python pip install -U pip setuptools
for args in "$@"; do
if [[ "$args" != "-"* ]]; then
argv="$args"
break;
else
shift;
fi
done
# validate_yaml
parse_config "$argv"
OPTS=(--cov=bentoml --cov-config="$GIT_ROOT"/setup.cfg --cov-report=xml:"$target.xml" --cov-report=term-missing)
if [ -n "${PYTESTARGS[*]}" ]; then
# shellcheck disable=SC2206
OPTS=( ${OPTS[@]} ${PYTESTARGS[@]} )
fi
if [ "$SKIP_DEPS" -eq 0 ]; then
# setup tests environment
if [ -f "$REQ_FILE" ]; then
run_python pip install -r "$REQ_FILE" || exit 1
fi
fi
if [ -n "$external_scripts" ]; then
eval "$external_scripts" || exit 1
fi
if [ "$type_tests" == 'e2e' ]; then
cd "$GIT_ROOT"/"$test_dir"/"$fname" || exit 1
path="."
else
path="$GIT_ROOT"/"$test_dir"/"$fname"
fi
if ! (run_python pytest "$path" "${OPTS[@]}"); then
ERR=1
fi
# Return non-zero if pytest failed
if ! test $ERR = 0; then
FAIL "$args $type_tests tests failed!"
exit 1
fi
PASS "$args $type_tests tests passed!"
}
main "$@" || exit 1
# vim: set ft=sh ts=2 sw=2 tw=0 et :