Repository navigation
Expand file tree
/
Copy pathrun_tests.sh
More file actions
executable file
·2000 lines (1912 loc) · 83.8 KB
/
Copy pathrun_tests.sh
File metadata and controls
executable file
·2000 lines (1912 loc) · 83.8 KB
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
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/bin/sh
# Copyright (C) 2026 Qore Technologies, s.r.o.
print_usage () {
echo "Usage: run_tests.sh [OPTIONS] [TEST...]"
echo "Run qore tests: the given test files, or else the tests in the test directories."
echo
echo " -d <dir> Run only specified tests (as found in $BASE_TEST_PATH)."
echo " -j Use --format=junit option for the tests, making them print JUnit output."
echo " -l List the tests that would run (after the selection and the shard), one per line, and exit."
echo " -E Exclude performance/stress tests (matched by *Perf* or PipelineMemory pattern)."
echo " -P Run only performance/stress tests."
echo " -t Measure execution time of the tests."
echo " -v Use --format=plain option for the tests, making them print one statement per each test case."
echo
echo "Environment variables:"
echo " QORE_TEST_OPTS Additional options to pass to qore (e.g., '-penable-debug')."
echo " QORE_TEST_DIRS Space-separated test directories (as found in $BASE_TEST_PATH) to run when no"
echo " -d option is given (e.g., 'qore/misc qore/vars'); default: all tests."
echo " QORE_TEST_EXTRA_DIRS Space-separated directories outside $BASE_TEST_PATH (relative to the current"
echo " directory or absolute) whose tests are run as well (e.g., 'modules/ml/test');"
echo " the tests of the binary modules in ./modules are run only when named here."
echo " TEST_TIMEOUT Seconds a single test may run before it is killed (default: 300); a test"
echo " declares its own budget with a '# test-timeout: <seconds>' comment. A test"
echo " that exceeds it is first sent SIGUSR1 if it handles it (QUnit tests print the"
echo " call stacks of their threads), then aborted (SIGABRT), so that it dumps core;"
echo " a test that does not handle SIGUSR1, or one run with signal handling disabled"
echo " (-b in QORE_TEST_OPTS), is aborted at once."
echo " QORE_TEST_STACK_DUMP_WAIT"
echo " Seconds to wait for those stacks before the test is ended (default: 30)."
echo " QORE_TEST_INTERRUPT_WAIT Seconds a test may take to end after a signal ends the run (default: 10); it is"
echo " killed then."
echo " CI_NODE_INDEX Shard index (1-based) for parallel test execution."
echo " CI_NODE_TOTAL Total number of shards for parallel test execution; the shards are balanced by"
echo " the durations of the tests in examples/test/docker_test/test-durations.tsv."
echo " QORE_TEST_DURATION_PROFILE"
echo " The column of test-durations.tsv used (default: CI_JOB_NAME without its shard)."
echo " QORE_TEST_DURATIONS_FILE The durations file (default: examples/test/docker_test/test-durations.tsv)."
echo " QORE_EXCLUDE_PERF_TESTS Set to '1' to exclude performance tests (same as -E)."
echo " QORE_PERF_TESTS_ONLY Set to '1' to run only performance tests (same as -P)."
echo " QORE_TEST_EXCLUDE_EXEC_MODE_CHILDREN"
echo " Set to '1' to leave out the tests that declare '# test-exec-mode: children'"
echo " (needs --exec-mode in QORE_TEST_OPTS): their child processes set their own"
echo " execution mode, so a run in the default mode makes the same checks."
echo " QORE_TEST_PLATFORM_SUBSET"
echo " Set to 'macos' to run only the tests that declare '# test-platform: macos - <why>'"
echo " (the macOS CI job sets it): the tests whose checks depend on the platform."
echo " QORE_TEST_QMOD_DIR Directory with AOT qmods to prefer before ./qlib."
echo " QORE_TEST_SOURCE_MODULES Set to '1' to ignore the default build qmod directory."
echo " QORE_TEST_BUILD_JOBS Jobs a test that builds a CMake project runs (default: the number of CPUs, at"
echo " least 2), e.g. examples/test/ir/CMakeBuildHelpers*.qtest."
echo " QORE_TEST_PRESERVE_PROVIDER_ENV"
echo " Set to '1' to preserve external provider discovery env vars."
}
err_multiple_format_opts() {
echo "Multiple formatting options can't be used at the same time." >&2
print_usage
exit 1
}
BASE_TEST_PATH="./examples/test"
MEASURE_TIME=0
PRINT_TEXT=1
TEST_DIRS=""
TEST_OUTPUT_FORMAT=""
PERF_EXCLUDE=0
PERF_ONLY=0
LIST_TESTS=0
# Support environment variables for CI integration
if [ "${QORE_EXCLUDE_PERF_TESTS}" = "1" ]; then
PERF_EXCLUDE=1
fi
if [ "${QORE_PERF_TESTS_ONLY}" = "1" ]; then
PERF_ONLY=1
fi
while getopts ":d:jlvtEP" opt; do
case $opt in
d)
TEST_DIRS="$TEST_DIRS $BASE_TEST_PATH/$OPTARG"
;;
j)
if [ -n "$TEST_OUTPUT_FORMAT" ]; then
err_multiple_format_opts
else
TEST_OUTPUT_FORMAT="--format=junit"
PRINT_TEXT=0
fi
;;
v)
if [ -n "$TEST_OUTPUT_FORMAT" ]; then
err_multiple_format_opts
else
TEST_OUTPUT_FORMAT="-v"
fi
;;
l)
LIST_TESTS=1
;;
t)
MEASURE_TIME=1
;;
E)
PERF_EXCLUDE=1
;;
P)
PERF_ONLY=1
;;
\?)
echo "Unknown option: -$OPTARG" >&2
print_usage
exit 1
;;
:)
echo "Option -$OPTARG requires an argument." >&2
print_usage
exit 1
;;
esac
done
shift `expr $OPTIND - 1`
# the test files given as arguments, instead of the tests in the test directories
TEST_FILES=""
for test in "$@"; do
case "$test" in
*" "*|*" "*)
echo "ERROR: a test path cannot contain spaces: '$test'" >&2
exit 1
;;
esac
if [ ! -f "$test" ]; then
echo "ERROR: test '$test' is not a file" >&2
exit 1
fi
TEST_FILES="$TEST_FILES $test"
done
if [ $PERF_EXCLUDE -eq 1 ] && [ $PERF_ONLY -eq 1 ]; then
echo "Cannot use -E and -P together." >&2
exit 1
fi
# Test dirs can also be given in the environment, for CI jobs that run a subset of the tests
if [ -z "$TEST_DIRS" ] && [ -n "$QORE_TEST_DIRS" ]; then
for dir in $QORE_TEST_DIRS; do
if [ ! -d "$BASE_TEST_PATH/$dir" ]; then
echo "ERROR: QORE_TEST_DIRS entry '$dir' is not a directory in $BASE_TEST_PATH" >&2
exit 1
fi
TEST_DIRS="$TEST_DIRS $BASE_TEST_PATH/$dir"
done
fi
# If no test dirs were specified, run all the tests
if [ -z "$TEST_DIRS" ]; then
TEST_DIRS="$BASE_TEST_PATH"
fi
# Tests outside the test directory, such as those of the binary modules (modules/*/test), are run when named, so
# that the default set of tests stays the same
for dir in $QORE_TEST_EXTRA_DIRS; do
if [ ! -d "$dir" ]; then
echo "ERROR: QORE_TEST_EXTRA_DIRS entry '$dir' is not a directory" >&2
exit 1
fi
TEST_DIRS="$TEST_DIRS $dir"
done
# Search for tests in the test directory.
if [ -n "$TEST_FILES" ]; then
TESTS=$TEST_FILES
else
TESTS=`find $TEST_DIRS -name "*.qtest" | sort`
fi
FAILED_TESTS=""
# Helper: check if a test matches the perf/stress test pattern
is_perf_test() {
# the file name, without a subshell for each of the 2000 tests
case "${1##*/}" in
*Perf*|PipelineMemory.qtest) return 0 ;;
*) return 1 ;;
esac
}
# Filter perf tests if requested
if [ $PERF_EXCLUDE -eq 1 ]; then
FILTERED=""
for test in $TESTS; do
if ! is_perf_test "$test"; then
FILTERED="$FILTERED $test"
fi
done
TESTS="$FILTERED"
elif [ $PERF_ONLY -eq 1 ]; then
FILTERED=""
for test in $TESTS; do
if is_perf_test "$test"; then
FILTERED="$FILTERED $test"
fi
done
TESTS="$FILTERED"
fi
# The tests that declare "# test-exec-mode: children" make all their checks in child processes that set their own
# execution mode (--exec-mode) with ExecModeHarness.qm, so the execution mode of the test itself changes nothing they
# check: a run whose tests all get an execution mode (an --exec-mode option in QORE_TEST_OPTS) leaves them out when
# QORE_TEST_EXCLUDE_EXEC_MODE_CHILDREN=1, as a run in the default mode runs the same child processes. A harness test
# with checks of its own declares "# test-exec-mode: parent" and always runs.
# examples/test/runner/test_exec_mode_markers.py checks the declarations.
if [ "${QORE_TEST_EXCLUDE_EXEC_MODE_CHILDREN}" = "1" ]; then
case " $QORE_TEST_OPTS " in
*" --exec-mode="*|*" --exec-mode "*) ;;
*)
echo "ERROR: QORE_TEST_EXCLUDE_EXEC_MODE_CHILDREN=1 needs an --exec-mode option in QORE_TEST_OPTS: the" \
"tests it leaves out run only in the default execution mode" >&2
exit 1
;;
esac
CHILD_MODE_TESTS=""
if [ -n "$TESTS" ]; then
CHILD_MODE_TESTS=`grep -l '^# test-exec-mode: children' $TESTS`
fi
FILTERED=""
EXCLUDED_COUNT=0
for test in $TESTS; do
case "
$CHILD_MODE_TESTS
" in
*"
$test
"*) EXCLUDED_COUNT=$(( EXCLUDED_COUNT + 1 )) ;;
*) FILTERED="$FILTERED $test" ;;
esac
done
TESTS="$FILTERED"
if [ $LIST_TESTS -eq 0 ]; then
echo "Left out $EXCLUDED_COUNT tests that declare 'test-exec-mode: children': their child processes set" \
"their own execution mode and run in the default-mode jobs"
fi
fi
# A test whose checks depend on the platform (the OS, its C library, file systems, sockets, processes and signals,
# threads, time zones, locales, dynamic loading, the toolchain and linker) declares each platform beside Linux that it
# must also run on, on a line of its own:
# # test-platform: macos - <why>
# A run with QORE_TEST_PLATFORM_SUBSET=<platform> runs only the tests that declare that platform; the macOS CI job sets
# it to "macos" (test-macos.sh), as the Linux jobs run every test and so cover the checks that do not depend on the
# platform. Without QORE_TEST_PLATFORM_SUBSET every test runs and the declarations change nothing.
# examples/test/runner/test_platform_markers.py checks the declarations.
case "${QORE_TEST_PLATFORM_SUBSET}" in
"") ;;
macos)
PLATFORM_TESTS=""
if [ -n "$TESTS" ]; then
PLATFORM_TESTS=`grep -l "^# test-platform: ${QORE_TEST_PLATFORM_SUBSET} - " $TESTS`
fi
FILTERED=""
SELECTED_COUNT=0
ALL_COUNT=0
for test in $TESTS; do
ALL_COUNT=$(( ALL_COUNT + 1 ))
case "
$PLATFORM_TESTS
" in
*"
$test
"*) SELECTED_COUNT=$(( SELECTED_COUNT + 1 )); FILTERED="$FILTERED $test" ;;
esac
done
TESTS="$FILTERED"
if [ $LIST_TESTS -eq 0 ]; then
echo "Selected $SELECTED_COUNT of $ALL_COUNT tests that declare" \
"'test-platform: ${QORE_TEST_PLATFORM_SUBSET}' (QORE_TEST_PLATFORM_SUBSET): the Linux jobs run the others"
fi
;;
*)
echo "ERROR: QORE_TEST_PLATFORM_SUBSET is empty or a platform that tests declare with '# test-platform:'" \
"(macos): '${QORE_TEST_PLATFORM_SUBSET}'" >&2
exit 1
;;
esac
# The durations of the tests: each CI test job is a column of test-durations.tsv (see update-test-durations.py),
# named by the job without its shard (CI_JOB_NAME "test-alpine-amd64 2/3" reads column test-alpine-amd64), or by
# QORE_TEST_DURATION_PROFILE; another name reads column test-ubuntu-amd64. A test with no duration counts as the
# median of the others.
TEST_DURATIONS_FILE=${QORE_TEST_DURATIONS_FILE:-$BASE_TEST_PATH/docker_test/test-durations.tsv}
TEST_DURATION_PROFILE=${QORE_TEST_DURATION_PROFILE:-${CI_JOB_NAME% [0-9]*/[0-9]*}}
# the column read: the profile's, else test-ubuntu-amd64; none without a durations file
TEST_DURATION_COLUMN=""
if [ -r "$TEST_DURATIONS_FILE" ]; then
TEST_DURATION_COLUMN=`awk -F '\t' -v profile="$TEST_DURATION_PROFILE" '
/^#/ { next }
$1 == "test" {
for (i = 2; i <= NF; ++i) {
if ($i == profile) {
found = profile
}
}
print (found != "" ? found : "test-ubuntu-amd64")
exit
}' "$TEST_DURATIONS_FILE"`
fi
# Prints "<shard> <seconds> <number> <test>" for each test in TESTS (its number is its place in TESTS), in their
# order (longest first with a second argument), with the shards (1 to the given number)
# that longest-processing-time-first packing gives: the longest test goes to the shard with the least work so far
# (the first such shard on a tie), and tests of the same duration in the order of their names, so that every shard of
# a job computes the same split. A test counts as at least 0.1 s.
plan_tests() {
printf '%s\n' $TESTS | awk -v shards="$1" -v order="$2" -v durations="$TEST_DURATIONS_FILE" \
-v profile="$TEST_DURATION_COLUMN" '
# sorts idx[lo..hi] by descending duration, then by name
function before(a, b) {
return dur[a] > dur[b] || (dur[a] == dur[b] && key[a] < key[b])
}
function sort_tests(lo, hi, i, last, t) {
if (lo >= hi) {
return
}
t = idx[lo]; idx[lo] = idx[int((lo + hi) / 2)]; idx[int((lo + hi) / 2)] = t
last = lo
for (i = lo + 1; i <= hi; ++i) {
if (before(idx[i], idx[lo])) {
++last
t = idx[last]; idx[last] = idx[i]; idx[i] = t
}
}
t = idx[lo]; idx[lo] = idx[last]; idx[last] = t
sort_tests(lo, last - 1)
sort_tests(last + 1, hi)
}
function sort_values(lo, hi, i, last, t) {
if (lo >= hi) {
return
}
t = val[lo]; val[lo] = val[int((lo + hi) / 2)]; val[int((lo + hi) / 2)] = t
last = lo
for (i = lo + 1; i <= hi; ++i) {
if (val[i] < val[lo]) {
++last
t = val[last]; val[last] = val[i]; val[i] = t
}
}
t = val[lo]; val[lo] = val[last]; val[last] = t
sort_values(lo, last - 1)
sort_values(last + 1, hi)
}
BEGIN {
# the number of tests, a number from the start: an unassigned variable is "" as a subscript
n = 0
col = 0
while ((getline line < durations) > 0) {
if (line ~ /^#/ || line == "") {
continue
}
nf = split(line, f, "\t")
if (f[1] == "test") {
for (i = 2; i <= nf; ++i) {
if (f[i] == profile) {
col = i
}
}
continue
}
if (col && f[col] != "" && f[col] != "-") {
known[f[1]] = f[col] + 0
}
}
}
{
test[n] = $0
key[n] = $0
sub(/^\.\//, "", key[n])
++n
}
END {
nv = 0
for (i = 0; i < n; ++i) {
if (key[i] in known) {
val[nv++] = known[key[i]]
}
}
sort_values(0, nv - 1)
median = nv ? (nv % 2 ? val[int(nv / 2)] : (val[nv / 2 - 1] + val[nv / 2]) / 2) : 1
# a test takes at least 0.1 s (the durations have that precision), so that the shortest tests are
# spread over the shards too
for (i = 0; i < n; ++i) {
dur[i] = (key[i] in known) ? known[key[i]] : median
if (dur[i] < 0.1) {
dur[i] = 0.1
}
idx[i] = i
}
sort_tests(0, n - 1)
for (s = 1; s <= shards; ++s) {
load[s] = 0
}
for (j = 0; j < n; ++j) {
i = idx[j]
best = 1
for (s = 2; s <= shards; ++s) {
if (load[s] < load[best]) {
best = s
}
}
shard[i] = best
load[best] += dur[i]
}
for (j = 0; j < n; ++j) {
i = order ? idx[j] : j
printf "%d\t%s\t%d\t%s\n", shard[i], dur[i], i + 1, test[i]
}
}'
}
# Shard tests for parallel CI execution: GitLab sets CI_NODE_INDEX (1-based) and CI_NODE_TOTAL with parallel: N; the
# shards are balanced by the durations of the tests (see plan_tests())
TEST_PLAN=""
SHARD_SUMMARY_FD=1
if [ $LIST_TESTS -eq 1 ]; then
SHARD_SUMMARY_FD=2
fi
if [ -n "$CI_NODE_INDEX" ] && [ -n "$CI_NODE_TOTAL" ] && [ "$CI_NODE_TOTAL" -gt 1 ] 2>/dev/null; then
if [ "$CI_NODE_INDEX" -lt 1 ] 2>/dev/null || [ "$CI_NODE_INDEX" -gt "$CI_NODE_TOTAL" ] 2>/dev/null; then
echo "ERROR: CI_NODE_INDEX $CI_NODE_INDEX is not a shard of $CI_NODE_TOTAL" >&2
exit 1
fi
TEST_PLAN=`plan_tests "$CI_NODE_TOTAL"`
TESTS=`printf '%s\n' "$TEST_PLAN" | awk -F '\t' -v shard="$CI_NODE_INDEX" '$1 == shard { print $4 }'`
# on stderr with -l, which lists only the tests on stdout
if [ $PRINT_TEXT -eq 1 ]; then
printf '%s\n' "$TEST_PLAN" | awk -F '\t' -v shard="$CI_NODE_INDEX" -v total="$CI_NODE_TOTAL" \
-v column="${TEST_DURATION_COLUMN:-no durations file}" '
{ all += $2; if ($1 == shard) { mine += $2; ++n } }
END { printf "Shard %d/%d selected: %d tests, %.0fs of %.0fs by the durations of %s\n", shard, total, n,
mine, all, column }' >&$SHARD_SUMMARY_FD
fi
fi
TEST_COUNT=`echo $TESTS | wc -w`
TEST_COUNT=`expr $TEST_COUNT + 0`
# -l: the tests that would run, one per line, and nothing else
if [ $LIST_TESTS -eq 1 ]; then
if [ $TEST_COUNT -eq 0 ]; then
echo "ERROR: no tests found to run" >&2
exit 1
fi
for test in $TESTS; do
echo "$test"
done
exit 0
fi
QORE=""
QR=""
LIBQORE=""
QORE_IS_BUILD_TREE=0
QORE_LIB_PATH="./lib/.libs:./qlib:$LD_LIBRARY_PATH"
set_qore_build_dir () {
d="$1"
if [ ! -f "$d/CMakeCache.txt" ] || [ ! -f "$d/qore" ]; then
return 1
fi
if [ -f "$d/libqore.so" ]; then
LIBQORE="$d/libqore.so"
elif [ -f "$d/libqore.dylib" ]; then
LIBQORE="$d/libqore.dylib"
else
return 1
fi
QORE="$d/qore"
QR=""
QORE_IS_BUILD_TREE=1
if [ -f "$d/qr" ]; then
QR="$d/qr"
fi
return 0
}
# Allow callers to override binaries (ex: debug build output).
if [ -n "$QORE_BINARY" ]; then
QORE="$QORE_BINARY"
QORE_DIR=`dirname "$QORE"`
if [ -f "$QORE_DIR/libqore.so" ]; then
LIBQORE="$QORE_DIR/libqore.so"
elif [ -f "$QORE_DIR/libqore.dylib" ]; then
LIBQORE="$QORE_DIR/libqore.dylib"
fi
if [ -f "$QORE_DIR/CMakeCache.txt" ]; then
QORE_IS_BUILD_TREE=1
fi
if [ -f "$QORE_DIR/qr" ]; then
QR="$QORE_DIR/qr"
fi
fi
# Allow explicit override for libqore if needed.
if [ -n "$LIBQORE_BINARY" ]; then
LIBQORE="$LIBQORE_BINARY"
fi
# Test that qore is built (CMake only, autotools no longer supported).
if [ -z "$QORE" ]; then
if [ -d "build" ]; then
set_qore_build_dir "build"
fi
fi
if [ -z "$QORE" ]; then
for D in `ls -d */`; do
d=`echo ${D%%/}`
if [ "$d" = "build" ]; then
continue
fi
if set_qore_build_dir "$d"; then
break
fi
done
fi
if [ -z "$QORE" ] || [ -z "$LIBQORE" ]; then
echo "Qore is not built. Exiting."
exit 1
fi
export QORE_BINARY="$QORE"
export LIBQORE_BINARY="$LIBQORE"
QORE_DIR=`dirname "$QORE"`
QORE_BUILD_DIR="$QORE_DIR"
if [ -n "$QORE_LIBDIR" ] && [ -d "$QORE_LIBDIR" ]; then
QORE_BUILD_DIR="$QORE_LIBDIR"
elif [ -n "$LIBQORE" ]; then
_libqore_dir=`dirname "$LIBQORE"`
if [ -d "$_libqore_dir/qlib-qmod" ] || [ -d "$_libqore_dir/modules" ]; then
QORE_BUILD_DIR="$_libqore_dir"
elif [ -d "$_libqore_dir/../qlib-qmod" ] || [ -d "$_libqore_dir/../modules" ]; then
QORE_BUILD_DIR="$_libqore_dir/.."
fi
fi
QORE_SOURCE_DIR=`pwd -P`
QORE_BUILD_DIR=`cd "$QORE_BUILD_DIR" && pwd -P`
BUILD_MODULE_DIRS=""
BUILD_QMOD_DIR=""
if [ -d "$QORE_BUILD_DIR/modules" ]; then
for moddir in "$QORE_BUILD_DIR/modules"/*; do
if [ -d "$moddir" ]; then
if [ -z "$BUILD_MODULE_DIRS" ]; then
BUILD_MODULE_DIRS="$moddir"
else
BUILD_MODULE_DIRS="$BUILD_MODULE_DIRS:$moddir"
fi
fi
done
fi
if [ -n "$QORE_TEST_QMOD_DIR" ]; then
if [ ! -d "$QORE_TEST_QMOD_DIR" ]; then
echo "QORE_TEST_QMOD_DIR does not exist or is not a directory: $QORE_TEST_QMOD_DIR"
exit 1
fi
BUILD_QMOD_DIR="$QORE_TEST_QMOD_DIR"
elif [ "$QORE_TEST_SOURCE_MODULES" != "1" ] && [ -d "$QORE_DIR/qlib-qmod" ]; then
BUILD_QMOD_DIR="$QORE_DIR/qlib-qmod"
elif [ "$QORE_TEST_SOURCE_MODULES" != "1" ] && [ -d "$QORE_BUILD_DIR/qlib-qmod" ]; then
BUILD_QMOD_DIR="$QORE_BUILD_DIR/qlib-qmod"
fi
if [ -n "$BUILD_QMOD_DIR" ]; then
BUILD_QMOD_DIR=`cd "$BUILD_QMOD_DIR" && pwd -P`
fi
export LD_LIBRARY_PATH=$QORE_LIB_PATH
TEST_MODULE_DIRS=""
if [ -n "$BUILD_QMOD_DIR" ]; then
TEST_MODULE_DIRS="$BUILD_QMOD_DIR"
fi
if [ -n "$BUILD_MODULE_DIRS" ]; then
if [ -n "$TEST_MODULE_DIRS" ]; then
TEST_MODULE_DIRS="$TEST_MODULE_DIRS:$BUILD_MODULE_DIRS"
else
TEST_MODULE_DIRS="$BUILD_MODULE_DIRS"
fi
fi
if [ -n "$TEST_MODULE_DIRS" ]; then
export QORE_MODULE_DIR=$TEST_MODULE_DIRS:$QORE_SOURCE_DIR/qlib:$QORE_MODULE_DIR
else
export QORE_MODULE_DIR=$QORE_SOURCE_DIR/qlib:$QORE_MODULE_DIR
fi
# Export build qore binary path so that tests spawning sub-processes (via backquote, etc.)
# use the same build binary rather than the system-installed qore.
export QORE_EXECUTABLE="$QORE"
# Keep qore tests hermetic by default. Developer shells often have Qorus or
# application provider discovery configured, which can make negative provider
# lookup tests print unrelated module-load diagnostics.
if [ "${QORE_TEST_PRESERVE_PROVIDER_ENV}" != "1" ]; then
unset QORE_DATA_PROVIDERS
unset QORE_PROVIDER_INDEX_DIR
fi
if [ $MEASURE_TIME -eq 1 ]; then
# Test time commands.
TIME_OK=0
TIME_BIN=""
TIME_CMD=""
TIME_FORMAT="-------------------------------------\nUserTime: %U\nSystemTime: %S\nWallClockTime: %e\nMinorPageFaults: %R\nMajorPageFaults: %F\nAverageSharedTextSize: %X\nAverageUnsharedDataSize: %D\nAverageStackSize: %p\nAverageTotalSize: %K\nMaximumResidentSetSize: %M\nAverageResidentSetSize: %t\nFilesystemInputs: %I\nFilesystemOutputs: %O\nSocketMessagesSent: %s\nSocketMessagesReceived: %r\nExitStatus: %x"
test_time() {
TIME_CMD="$TIME_BIN -f \"$TIME_FORMAT\""
eval $TIME_CMD ls / >/dev/null 2>&1
TIME_OK=$?
}
TIME_BINS="time /usr/bin/time /bin/time `which time`"
for tm in $TIME_BINS; do
TIME_BIN=$tm
test_time
if [ $TIME_OK -eq 0 ]; then
break
fi
TIME_CMD=""
done
if [ "$TIME_CMD" = "" ]; then
TIME_CMD="time -p"
fi
fi
# Put the libqore directory at the front of LD_LIBRARY_PATH only for build-tree
# binaries so they pick up the matching build libqore.so before any installed
# copy. For installed/system binaries, do not prepend system library dirs:
# doing so can override the loader's normal dependency order for libraries such
# as ngtcp2/nghttp3/OpenSSL and mix incompatible stacks.
LIBQORE_DIR=$(dirname "$LIBQORE")
if [ "$QORE_IS_BUILD_TREE" = "1" ]; then
export LD_LIBRARY_PATH="${LIBQORE_DIR}${LD_LIBRARY_PATH:+:${LD_LIBRARY_PATH}}"
else
case "$LIBQORE_DIR" in
/lib|/lib/*|/usr/lib|/usr/lib/*) ;;
*)
case ":$LD_LIBRARY_PATH:" in
*":$LIBQORE_DIR:"*) ;;
*) export LD_LIBRARY_PATH="${LD_LIBRARY_PATH}${LD_LIBRARY_PATH:+:}${LIBQORE_DIR}" ;;
esac
;;
esac
fi
# Enable core dumps for crash diagnostics
ulimit -c unlimited 2>/dev/null
CORE_ULIMIT=$(ulimit -c 2>/dev/null)
echo "Core dump ulimit: ${CORE_ULIMIT:-unknown}"
# Create a directory for core dumps (used as CI artifact)
# Use a path relative to the project root so GitLab can collect it as an artifact
CORE_DIR="${CORE_DIR:-./crash-dumps}"
mkdir -p "$CORE_DIR" 2>/dev/null
CORE_DIR_ABS=$(cd "$CORE_DIR" && pwd)
# Try to set core pattern to write cores to our directory (may fail in containers). Not in a CI job: Linux has one
# core pattern for the whole machine, which a CI job shares with the other jobs on its node, and a path in this tree
# does not exist in their containers, so their cores would be lost; the CI scripts set the pattern every job on the
# node sets (examples/test/docker_test/ci-core-capture.sh), and the cores are found below where it puts them.
CORE_PATTERN_SET=0
# The write itself tells whether the pattern was set: "-w" is true for root also where /proc/sys is read-only (a
# container), and the write then fails
if [ -z "$CI" ] && [ -w /proc/sys/kernel/core_pattern ] \
&& { echo "$CORE_DIR_ABS/core.%e.%p" > /proc/sys/kernel/core_pattern; } 2>/dev/null; then
CORE_PATTERN_SET=1
fi
# Also try sysctl (works on some systems where /proc write fails)
if [ -z "$CI" ] && [ $CORE_PATTERN_SET -eq 0 ]; then
sysctl -w "kernel.core_pattern=$CORE_DIR_ABS/core.%e.%p" 2>/dev/null && CORE_PATTERN_SET=1
fi
# macOS writes cores to kern.corefile (/cores/core.%P), which only root can write, so a crashing test left no
# core and no backtrace. Only on disposable CI machines, where the system-wide setting does not outlive the job.
if [ $CORE_PATTERN_SET -eq 0 ] && [ "$(uname -s)" = "Darwin" ] && [ -n "$CI" ] && sudo -n true 2>/dev/null; then
sudo -n sysctl -w "kern.corefile=$CORE_DIR_ABS/core.%P" > /dev/null 2>&1 && CORE_PATTERN_SET=1
echo "kern.corefile: $(sysctl -n kern.corefile 2>/dev/null)"
fi
# Show the actual core pattern so we know where cores will land
if [ -f /proc/sys/kernel/core_pattern ]; then
KERN_CORE_PATTERN=$(cat /proc/sys/kernel/core_pattern 2>/dev/null)
echo "kernel.core_pattern: $KERN_CORE_PATTERN"
if echo "$KERN_CORE_PATTERN" | grep -q '^|'; then
echo "WARNING: core_pattern pipes to a program; cores may not be saved to disk"
fi
fi
# OBS keeps core files under an absolute kernel pattern such as /.build/cores/%p.
# Derive a find(1) name pattern without eval, and only inspect files newer than
# the current test. Never mistake an earlier test's core for the current failure.
SYSTEM_CORE_DIR=""
SYSTEM_CORE_NAME=""
case "$KERN_CORE_PATTERN" in
/*)
SYSTEM_CORE_DIR=$(dirname "$KERN_CORE_PATTERN")
case "$SYSTEM_CORE_DIR" in
*%*) SYSTEM_CORE_DIR="" ;; # Dynamic directory components cannot be resolved here.
*)
SYSTEM_CORE_NAME=$(basename "$KERN_CORE_PATTERN" | awk '
{
for (i = 1; i <= length($0); ++i) {
c = substr($0, i, 1)
if (c == "%" && i < length($0)) {
n = substr($0, i + 1, 1)
if (n == "%") { printf "%%"; ++i; continue }
if (n ~ /[[:alpha:]]/) { printf "*"; ++i; continue }
}
if (c == "*" || c == "?" || c == "[" || c == "]" || c == "\\") {
printf "\\"
}
printf "%s", c
}
}')
;;
esac
;;
esac
# the directories searched for the core of a test, in the order they are searched
CORE_SEARCH_DIRS='"$CORE_DIR_ABS" "$SYSTEM_CORE_DIR" . /tmp /var/lib/apport/coredump /var/crash'
# prints the find(1) name pattern of the core files in the given search directory
core_search_name() {
if [ "$1" = "$SYSTEM_CORE_DIR" ]; then
printf '%s\n' "$SYSTEM_CORE_NAME"
elif [ "$1" = /var/crash ]; then
printf '%s\n' '*.crash'
else
printf '%s\n' 'core*'
fi
}
# prints the core files in the search directories, in the order they are searched
list_test_cores() {
eval "set -- $CORE_SEARCH_DIRS"
for CORE_SEARCH_DIR in "$@"; do
if [ -n "$CORE_SEARCH_DIR" ] && [ -d "$CORE_SEARCH_DIR" ]; then
find "$CORE_SEARCH_DIR" -maxdepth 1 -type f -name "$(core_search_name "$CORE_SEARCH_DIR")" -print \
2>/dev/null
fi
done
}
# Prints the cores in the given search directory (the arguments: the directory and the find(1) name pattern of its
# cores) that the current test may have left: the files written after the test started, then those that did not exist
# before the test (TEST_CORES_BEFORE), as BusyBox find (Alpine) compares whole seconds and so does not find a core
# written in the second the test started
list_new_test_cores() {
{
find "$1" -maxdepth 1 -type f -name "$2" -newer "$TEST_START_MARKER" -print 2>/dev/null
find "$1" -maxdepth 1 -type f -name "$2" -print 2>/dev/null | awk -v before="$TEST_CORES_BEFORE" \
'BEGIN { while ((getline f < before) > 0) seen[f] = 1 } !($0 in seen)'
} | awk '!($0 in listed) { listed[$0] = 1; print }'
}
# Returns success if the given core is of a process of the current test: the test, and every process it starts and
# those start, have QORE_TEST_PROCESS_MARK=<the test's mark> in their environment (see run_test()), which a core holds
# as a string of its own (an environment entry ends with a NUL byte)
core_has_test_mark() {
LC_ALL=C tr -c 'A-Za-z0-9_=.' '\n' < "$1" 2>/dev/null | grep -qxF "QORE_TEST_PROCESS_MARK=$TEST_PROCESS_MARK"
}
# Sets CORE_FILE to the core the current test left (see list_new_test_cores())
find_test_core() {
CORE_FILE=""
eval "set -- $CORE_SEARCH_DIRS"
for CORE_SEARCH_DIR in "$@"; do
if [ -z "$CORE_SEARCH_DIR" ] || [ ! -d "$CORE_SEARCH_DIR" ]; then
continue
fi
NEW_CORES=$(list_new_test_cores "$CORE_SEARCH_DIR" "$(core_search_name "$CORE_SEARCH_DIR")")
if [ -z "$NEW_CORES" ]; then
continue
fi
if [ "$TEST_JOBS" -le 1 ]; then
CORE_FILE=$(printf '%s\n' "$NEW_CORES" | head -1)
break
fi
# Tests that run at the same time (TEST_JOBS > 1) write their cores to the same places, so a new core is the
# test's only when its name has the process ID of the test as a part of its own (core.%e.%p.%t on Linux CI,
# core.%P on macOS CI), or when it holds the test's mark: the core of a process that the test started
if [ -n "$TEST_PID" ]; then
CORE_FILE=$(printf '%s\n' "$NEW_CORES" | awk -v pid="$TEST_PID" \
'{ n = split($0, path, "/"); m = split(path[n], part, ".") }
{ for (j = 2; j <= m; ++j) { if (part[j] == pid) { print; exit } } }')
fi
if [ -z "$CORE_FILE" ]; then
while IFS= read -r core; do
if core_has_test_mark "$core"; then
CORE_FILE=$core
break
fi
done <<EOF
$NEW_CORES
EOF
fi
if [ -n "$CORE_FILE" ]; then
break
fi
done
}
# Prints the name of the given test in the names of its files in CORE_DIR: its path in the test tree with '_' for '/',
# as tests in different directories can have the same file name
test_artifact_name() {
_name=${1#./}
_name=${_name#examples/test/}
_name=${_name%.qtest}
printf '%s\n' "$_name" | tr '/' '_'
}
# The pipes the output of the tests goes to (Linux only): a CI job reads its output through a pipe and ends only when
# no process holds it any more. run_tests.sh and the processes that started it hold them as they should.
TEST_OUTPUT_PIPES=""
RUN_TESTS_ANCESTORS=""
if [ -r /proc/self/environ ]; then
for fd in 1 2; do
target=`readlink /proc/$$/fd/$fd 2>/dev/null`
case "$target" in
pipe:*) TEST_OUTPUT_PIPES="$TEST_OUTPUT_PIPES $target" ;;
esac
done
RUN_TESTS_ANCESTORS=$$
ancestor=$$
while [ "$ancestor" -gt 1 ]; do
ancestor=`awk '/^PPid:/ { print $2 }' /proc/$ancestor/status 2>/dev/null`
if [ -z "$ancestor" ]; then
break
fi
RUN_TESTS_ANCESTORS="$RUN_TESTS_ANCESTORS $ancestor"
done
fi
# qore_ci_running_processes(): tells the processes a test left running from a BusyBox timeout watcher and from one
# that has ended meanwhile
. "$BASE_TEST_PATH/docker_test/ci-job-processes.sh"
# Prints the processes that descend from the given process (this runner by default; Linux only): its children,
# theirs, and so on. Each status file is read with getline, which skips one that is gone (a process that ended
# during the scan): awk given them as input files would end at a missing one, and print nothing. /proc is
# QORE_TEST_PROC if set, for the tests of run_tests.sh.
runner_descendants() {
awk -v root=${1:-$$} -v proc="${QORE_TEST_PROC:-/proc}" 'BEGIN {
for (i = 1; i < ARGC; ++i) {
f = ARGV[i]
pid = substr(f, length(proc) + 2)
sub(/\/status$/, "", pid)
while ((getline line < f) > 0) {
if (line ~ /^PPid:/) {
split(line, field)
parent[pid] = field[2]
break
}
}
close(f)
}
found[root] = 1
# each pass adds the children of the processes found so far, until a pass adds none
do {
added = 0
for (p in parent) {
if (!(p in found) && (parent[p] in found)) {
found[p] = 1
added = 1
}
}
} while (added)
for (p in found) {
if (p != root) {
printf "%s ", p
}
}
exit
}' "${QORE_TEST_PROC:-/proc}"/[0-9]*/status 2>/dev/null
}
# Sets TEST_SURVIVORS to the processes the current test started that are still running (Linux only). Every process a
# test starts, and every process those start, inherits QORE_TEST_PROCESS_MARK in its environment, also when it
# leaves the test's session or is adopted by another parent, so a process that still has it when the test has ended
# was not ended by the test. Any other process that descends from this runner and holds the output of the tests as
# its standard output or error, such as one a test started with an environment of its own, is one too: only one that
# descends from it, as other runners (the shards of one job) can share the output and are no survivors of this one's
# tests. The trade-off, chosen to protect sibling shards: a process without the mark that has left this runner's
# process tree (adopted by init after its parent ended) holding the output is not found. The output of a test of a
# parallel run is its log, in the run's own directory, which no other runner holds: every process that holds it is
# one, wherever it is in the process tree.
find_test_survivors() {
TEST_SURVIVORS=""
if [ ! -r /proc/self/environ ]; then
return
fi
# the output of a test of a parallel run is its log (TEST_LOG, an absolute path without spaces)
test_outputs="$TEST_OUTPUT_PIPES"
outputs_shared=1
if [ -n "$TEST_LOG" ]; then
test_outputs="$TEST_LOG"
outputs_shared=0
fi
if [ -n "$test_outputs" ]; then
# the commands that look for them must not hold the output themselves
TEST_SURVIVORS=`{ ls -l /proc/[0-9]*/fd/1 /proc/[0-9]*/fd/2 | awk -v pipes="$test_outputs" \
-v own="$RUN_TESTS_ANCESTORS" -v shared=$outputs_shared -v descendants="$(runner_descendants)" 'BEGIN {
n = split(pipes, list, " ")
for (i = 1; i <= n; ++i) {
pipe[list[i]] = 1
}
n = split(own, list, " ")
for (i = 1; i <= n; ++i) {
skip[list[i]] = 1
}
n = split(descendants, list, " ")
for (i = 1; i <= n; ++i) {
mine[list[i]] = 1
}
}
$(NF - 1) == "->" && ($NF in pipe) {
split($(NF - 2), path, "/")
if ((!shared || (path[3] in mine)) && !(path[3] in skip) && !(path[3] in seen)) {
seen[path[3]] = 1
printf "%s ", path[3]
}
}'; } 2>/dev/null`
fi
TEST_SURVIVORS="$TEST_SURVIVORS"`awk -v mark="QORE_TEST_PROCESS_MARK=$1" 'BEGIN {
RS = "\0"
for (i = 1; i < ARGC; ++i) {
f = ARGV[i]
while ((getline e < f) > 0) {
if (e == mark) {
sub(/^\/proc\//, "", f)
sub(/\/environ$/, "", f)
printf "%s ", f
break
}
}
close(f)
}
exit
}' /proc/[0-9]*/environ`
# a process found both ways is listed once
if [ -n "$TEST_SURVIVORS" ]; then
TEST_SURVIVORS=`printf '%s\n' $TEST_SURVIVORS | sort -un | tr '\n' ' '`
fi
}
# Print info about used variables etc.
echo "Using qore: $QORE"
echo "Using libqore: $LIBQORE"
echo "QORE_INCLUDE_DIR=$QORE_INCLUDE_DIR"
echo "QORE_MODULE_DIR=$QORE_MODULE_DIR"
echo "LD_PRELOAD=$LD_PRELOAD"
echo "LD_LIBRARY_PATH=$LD_LIBRARY_PATH"
# Prints a datasource string with its password replaced by "****", for logs that others can read (CI job logs)
# The string is split as parseDatasource() splits it: an option string ("{...}") at the end is kept as is, the
# driver name ends at the first ':', the user name at the first '/' and the password at the last '@'
mask_connstr() {
_cs=$1
_opts=""
case "$_cs" in
*\{*\})
_opts="{${_cs##*\{}"
_cs=${_cs%\{*}
;;
esac
printf '%s%s\n' "$(printf '%s\n' "$_cs" | sed -e 's|^\(\([^:/@%]*:\)\{0,1\}[^/]*\)/.*@|\1/****@|')" "$_opts"
}
echo "QORE_DB_CONNSTR: $(mask_connstr "${QORE_DB_CONNSTR}")"
echo "QORE_DB_CONNSTR_FREETDS: $(mask_connstr "${QORE_DB_CONNSTR_FREETDS}")"
echo "QORE_DB_CONNSTR_MYSQL: $(mask_connstr "${QORE_DB_CONNSTR_MYSQL}")"
echo "QORE_DB_CONNSTR_PGSQL: $(mask_connstr "${QORE_DB_CONNSTR_PGSQL}")"
echo "QORE_DB_CONNSTR_ORACLE: $(mask_connstr "${QORE_DB_CONNSTR_ORACLE}")"
# Mask credentials in REDIS_URL (replace user:pass@ with ***@)
echo "REDIS_URL: $(echo "${REDIS_URL}" | sed 's|://[^@]*@|://***@|')"
if [ $MEASURE_TIME -eq 1 ]; then
printf "TIME_CMD: %s\n" "$TIME_CMD"
fi
echo
# a run without tests ends here, once it has printed its settings, which run-tests-credentials.qtest reads (-l ends
# above, before the settings are known)
if [ $TEST_COUNT -eq 0 ]; then
echo "ERROR: no tests found to run" >&2
exit 1
fi
PASSED_TEST_COUNT=0
FAILED_TEST_COUNT=0
# the tests whose watchdog failed (see run_with_watchdog()), and how many of them passed
WATCHDOG_ERROR_TESTS=""
WATCHDOG_ERROR_COUNT=0
WATCHDOG_ERROR_PASSED_COUNT=0
# The number of tests run at the same time: QORE_TEST_JOBS, a number or "auto" (the number of CPUs); 1 by default.
# A parallel run prints the output of each test when the test has ended, as a whole. A test that cannot run beside
# others declares a lane on its own line: