-
Notifications
You must be signed in to change notification settings - Fork 9
/
run.sh
executable file
·464 lines (390 loc) · 13.5 KB
/
run.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
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
#!/usr/bin/env bash
# Unless explicitly stated otherwise all files in this repository are licensed under the the Apache License Version 2.0.
# This product includes software developed at Datadog (https://www.datadoghq.com/).
# Copyright 2021 Datadog, Inc.
set -e
set -u
set -o pipefail
function hint() {
local program="${BASH_SOURCE[0]##*/}"
echo "see ${program} ++help for documentation"
}
function help() {
local program="${BASH_SOURCE[0]##*/}"
cat <<EOS
NAME
${program} - run system tests test suite
SYNOPSIS
${program} +h
${program} [+d] [+S scenario...] [+G scenario group...] [++] [pytest arguments]
${program} [+d] SCENARIO [pytest arguments]
${program} [+d] GROUPED_SCENARIOS [pytest arguments]
OPTIONS
Using option flags is the recommended way to use ${program}.
+v, ++verbose
Increase verbosity.
+y, ++dry
Do a dry run, i.e pretend to run but do nothing, instead outputting
commands that would be run.
+d, ++docker
Run tests in a Docker container. The runner image must be built beforehand.
+S, ++scenario SCENARIO
Add scenario SCENARIO to the list of scenarios to run. Case-insensitive.
+G, ++scenario-group GROUPED_SCENARIOS
Add all scenarios in GROUPED_SCENARIOS group to the list of scenarios to
run. Case insensitive.
+l, ++library LIBRARY
Inform test suite that test pertains to LIBRARY.
++
Ignore flags after this separator. All subsequent arguments are passed
as-is to pytest.
POSITIONAL ARGUMENTS
Using positional arguments is deprecated in favor of options (see OPTIONS
above). Subsequent flags are ignored and arguments passed as-is to pytest.
SCENARIO
Run scenario SCENARIO. Case sensitive, must be uppercase.
GROUPED_SCENARIOS
Run all scenarios in GROUPED_SCENARIOS group. Case sensitive, must be
uppercase, must end with _SCENARIOS.
HOMEPAGE
<https://github.com/Datadog/system-tests>
Please report bugs and feature requests in the issue tracker. Please do
your best to provide a reproducible test case for bugs.
EOS
}
function error() {
echo "error:" "$@" 1>&2
}
function warn() {
echo "warn:" "$@" 1>&2
}
function die() {
local rc=1
if [[ $1 =~ ^-?[0-9]+$ ]]; then
rc="$1"
shift
fi
error "$@"
exit "${rc}"
}
function lookup_scenario_group() {
local group="$1"
local mode="$2"
local python=()
case "${mode}" in
'docker')
python+=(
docker run
--rm -i
system_tests/runner
venv/bin/python
)
;;
'direct')
python+=(python)
;;
*)
die "unsupported run mode: ${mode}"
;;
esac
python+=(
-c 'import yaml; import sys; group = sys.argv[1]; groups = yaml.safe_load(sys.stdin.read()); [[print(t) for t in s] if isinstance(s, list) else print(s) for s in groups[group]]'
)
echo "${python[*]}" 1>&2
cat < scenario_groups.yml | "${python[@]}" "${group}"
}
function upcase() {
tr '[:lower:]' '[:upper:]'
}
function downcase() {
tr '[:upper:]' '[:lower:]'
}
function is_using_nix() {
[[ -n "${IN_NIX_SHELL:-}" ]]
}
function activate_venv() {
# shellcheck disable=SC1091
source venv/bin/activate
}
function network_name() {
perl -ne '/_NETWORK_NAME = "(.*)"/ and print "$1\n"' utils/_context/containers.py
}
function ensure_network() {
local network_name
network_name="$(network_name)"
if docker network ls | grep -q "${network_name}"; then
: # network exists
else
docker network create "${network_name}"
fi
}
function run_scenario() {
local dry="$1"
shift
local mode="$1"
shift
local scenario="$1"
shift
local pytest_args=("$@")
local cmd=()
if [[ "${dry}" -gt 0 ]]; then
cmd+=(echo)
fi
case "${mode}" in
'docker')
# infer log dir from scenario
local log_dir
# default scenario does not follow the convention
if [[ "${scenario}" == 'DEFAULT' ]]; then
log_dir='logs'
else
# downcase via ${scenario,,} is unsupported on bash 3.x
log_dir="logs_$(echo "${scenario}" | downcase )"
fi
cmd+=(
docker run
--network system-tests_default
--rm -i
)
if [ -t 1 ]; then
cmd+=(-t)
fi
if [[ -n "${DD_API_KEY:-}" ]]; then
cmd+=(
-e DD_API_KEY="${DD_API_KEY}"
)
fi
if [[ -f .env ]]; then
cmd+=(
-v "${PWD}"/.env:/app/.env
)
fi
cmd+=(
-v /var/run/docker.sock:/var/run/docker.sock
-v "${PWD}/${log_dir}":"/app/${log_dir}"
-e SYSTEM_TESTS_PROXY_HOST=proxy
-e SYSTEM_TESTS_WEBLOG_HOST=weblog
-e SYSTEM_TESTS_WEBLOG_PORT=7777
-e SYSTEM_TESTS_WEBLOG_GRPC_PORT=7778
-e SYSTEM_TESTS_HOST_PROJECT_DIR="${PWD}"
--name system-tests-runner
system_tests/runner
venv/bin/pytest
)
;;
'direct')
cmd+=(pytest)
;;
*)
die "unsupported run mode: ${mode}"
;;
esac
cmd+=(
-S "${scenario}"
"${pytest_args[@]}"
)
"${cmd[@]}"
}
function main() {
local docker="${DOCKER_MODE:-0}"
local verbosity=0
local dry=0
local scenario_args=()
local libraries=()
local pytest_args=()
local pytest_numprocesses='auto'
## handle environment variables
# split TEST_LIBRARY on ','
IFS=',' read -r -a libraries <<< "${TEST_LIBRARY:-}"
## parse command arguments
# parse flags
while [[ "$#" -gt 0 ]]; do
case "$1" in
+h|++help)
help
exit
;;
+v|++verbose)
verbosity=$(( verbosity + 1 ))
;;
+y|++dry)
dry=1
;;
+d|++docker)
docker=1
;;
+G|++scenario-group)
if [[ "$#" -eq 1 ]]; then
error "missing argument value for: $1"
help
exit 64
fi
# upcase via ${2^^} is unsupported on bash 3.x
scenario_args+=("$(echo "$2" | upcase)")
shift
;;
+S|++scenario|-S|--scenario)
# this also catches '-S' even though it's a pytest flag because
# there may be special treatment for specific scenarios
if [[ "$#" -eq 1 ]]; then
error "missing argument value for: $1"
hint
exit 64
fi
# upcase via ${2^^} is unsupported on bash 3.x
scenario_args+=("$(echo "$2" | upcase)")
shift
;;
+l|++library)
if [[ "$#" -eq 1 ]]; then
error "missing argument value for: $1"
hint
exit 64
fi
libraries+=("$2")
shift
;;
++)
# ignore and stop flag processing to force remainder to be captured as is
shift
break
;;
+*)
# unknown flag: be helpful
error "unknown flag: $1"
hint
exit 64
;;
*)
# handle positional arguments
if [[ "$1" =~ ^[A-Z0-9_]+$ ]]; then
scenario_args+=("$1")
else
# pass any unmatched arguments to pytest
pytest_args+=("$1")
fi
;;
esac
shift
done
# capture remainder of arguments to pass as-is for pytest
pytest_args+=("$@")
## prepare commands
# set run mode
if [[ "${docker}" == 1 ]]; then
run_mode='docker'
else
run_mode='direct'
fi
# ensure environment
if [[ "${run_mode}" == "docker" ]] || is_using_nix; then
: # no venv needed
else
activate_venv
fi
local python_version
python_version="$(python -V 2>&1 | sed -E 's/Python ([0-9]+)\.([0-9]+).*/\1\2/')"
if [[ "$python_version" -lt "312" ]]; then
echo "⚠️️️️⚠️⚠️️️️⚠️⚠️️️️⚠️⚠️️️️⚠️⚠️️️️⚠️⚠️️️️⚠️⚠️️️️⚠️⚠️️️️⚠️⚠️️️️⚠️⚠️️️️⚠️⚠️️️️⚠️⚠️⚠️⚠️️️️⚠️️️️⚠️⚠️️️️⚠️⚠️️️️⚠️⚠️️️️⚠️⚠️️️️⚠️⚠️️️️⚠️⚠️️️️⚠️⚠️️️️⚠️⚠️️️️⚠️⚠️️️️⚠️⚠️️️️⚠️⚠️⚠️⚠️️️️⚠️⚠️⚠️️️️⚠️⚠️⚠️️️️⚠️⚠️⚠️️️️⚠️⚠️⚠️️️️⚠️"
echo "DEPRECRATION WARNING: you're using python ${python_version} to run system-tests."
echo "This won't be supported soon. Please install python3.12, then run:"
echo "> rm -rf venv && ./build.sh -i runner"
echo "⚠️️️️⚠️⚠️️️️⚠️⚠️️️️⚠️⚠️️️️⚠️⚠️️️️⚠️⚠️️️️⚠️⚠️️️️⚠️⚠️️️️⚠️⚠️️️️⚠️⚠️️️️⚠️⚠️️️️⚠️⚠️⚠️⚠️️️️⚠️⚠️️️️⚠️⚠️️️️⚠️⚠️️️️⚠️⚠️️️️⚠️⚠️️️️⚠️⚠️️️️⚠️⚠️️️️⚠️⚠️️️️⚠️⚠️️️️⚠️⚠️️️️⚠️⚠️️️️⚠️⚠️⚠️⚠️️️️⚠️⚠️️️️⚠️⚠️️️️⚠️⚠️️️️⚠️⚠️️️️⚠️⚠️️️️⚠️⚠️️️️"
fi
# process scenario list
local scenarios=()
# expand scenario groups
# bash 3.x does not support mapfile, dance around with tr and IFS
# bash 3.x considers ${arr[@}} undefined if empty
if [[ ${#scenario_args[@]} -gt 0 ]]; then
for i in "${scenario_args[@]}"; do
if [[ "${i}" =~ [A-Z0-9_]+_SCENARIOS$ ]]; then
# bash 3.x does not support mapfile, dance around with tr and IFS
IFS=',' read -r -a group <<< "$(lookup_scenario_group "${i}" "${run_mode}" | tr '\n' ',')"
scenarios+=("${group[@]}")
else
scenarios+=("${i}")
fi
done
fi
# when no scenario is provided, use a nice default
if [[ "${#scenarios[@]}" -lt 1 ]]; then
scenarios+=('DEFAULT')
fi
# backward compatibility with scenarios that have been removed/renamed
# TODO: remove once all CIs have been updated
for i in "${!scenarios[@]}"; do
case "${scenarios["${i}"]}" in
APPSEC_IP_BLOCKING_MAXED|APPSEC_IP_BLOCKING)
scenarios+=(APPSEC_BLOCKING_FULL_DENYLIST)
unset "scenarios[${i}]"
;;
LIBRARY_CONF_CUSTOM_HEADERS_SHORT|LIBRARY_CONF_CUSTOM_HEADERS_LONG)
scenarios+=(LIBRARY_CONF_CUSTOM_HEADER_TAGS)
unset "scenarios[${i}]"
;;
APPSEC_DISABLED)
scenarios+=(EVERYTHING_DISABLED)
unset "scenarios[${i}]"
;;
esac
done
# TODO: remove duplicates
# TODO: upgrade the dependencies to the latest version of pulumi once the protobuf bug is fixed
# In the meantime remove the warning from the output
pytest_args+=( '-p' 'no:warnings' )
# evaluate max pytest number of process
for scenario in "${scenarios[@]}"; do
if [[ "${scenario}" != "PARAMETRIC" ]]; then
pytest_numprocesses=1
fi
done
if [[ "${#libraries[@]}" -gt 0 ]]; then
for library in "${libraries[@]}"; do
if [ "${library}" = "dotnet" ]; then
pytest_numprocesses=1
fi
done
fi
# evaluate max pytest number of process for K8s_lib_injection
for scenario in "${scenarios[@]}"; do
#TODO DELETE WHEN THE SCENARIO IS REMOVED. REPLACED BY K8S_LIBRARY_INJECTION
if [[ "${scenario}" == K8S_LIB_INJECTION_* ]]; then
pytest_numprocesses=$(nproc)
fi
if [[ "${scenario}" == K8S_LIBRARY_INJECTION_* ]]; then
pytest_numprocesses=$(nproc)
fi
if [[ "${scenario}" == *_AUTO_INJECTION ]]; then
pytest_numprocesses=6
#https://pytest-xdist.readthedocs.io/en/latest/distribution.html
pytest_args+=( '--dist' 'loadgroup' )
fi
done
case "${pytest_numprocesses}" in
0|1)
;;
*)
pytest_args+=( '-n' "${pytest_numprocesses}" )
;;
esac
## run tests
if [[ "${verbosity}" -gt 0 ]]; then
echo "plan:"
echo " mode: ${run_mode}"
echo " dry run: ${dry}"
echo " scenarios:"
for scenario in "${scenarios[@]}"; do
echo " - ${scenario}"
done
fi
if [[ "${run_mode}" == "docker" ]]; then
ensure_network >/dev/null
fi
for scenario in "${scenarios[@]}"; do
run_scenario "${dry}" "${run_mode}" "${scenario}" "${pytest_args[@]}"
done
}
if [[ "$0" == "${BASH_SOURCE[0]}" ]]; then
main "$@"
fi