forked from yugabyte/yugabyte-db
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcommon-test-env.sh
2104 lines (1875 loc) · 74.8 KB
/
common-test-env.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
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
#!/usr/bin/env bash
# Copyright (c) YugaByte, Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
# in compliance with the License. You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software distributed under the License
# is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
# or implied. See the License for the specific language governing permissions and limitations under
# the License.
#
# Common bash code for test scripts.
declare -i global_exit_code=0
if [[ ${BASH_SOURCE[0]} == "$0" ]]; then
echo "${BASH_SOURCE[0]} must be sourced, not executed" >&2
exit 1
fi
# shellcheck source=build-support/common-build-env.sh
. "${BASH_SOURCE[0]%/*}/common-build-env.sh"
NON_GTEST_TESTS=(
merge-test
non_gtest_failures-test
c_test
compact_on_deletion_collector_test
db_sanity_test
merge_test
)
make_regex_from_list NON_GTEST_TESTS "${NON_GTEST_TESTS[@]}"
# There gtest suites have internal dependencies between tests, so those tests can't be run
# separately.
TEST_BINARIES_TO_RUN_AT_ONCE=(
tests-rocksdb/thread_local_test
)
make_regex_from_list TEST_BINARIES_TO_RUN_AT_ONCE "${TEST_BINARIES_TO_RUN_AT_ONCE[@]}"
VALID_TEST_BINARY_DIRS_PREFIX="tests"
VALID_TEST_BINARY_DIRS_RE="^${VALID_TEST_BINARY_DIRS_PREFIX}-[0-9a-zA-Z\-]+"
# gdb command to print a backtrace from a core dump. Taken from:
# http://www.commandlinefu.com/commands/view/6004/print-stack-trace-of-a-core-file-without-needing-to-enter-gdb-interactively
DEFAULT_TEST_TIMEOUT_SEC=${DEFAULT_TEST_TIMEOUT_SEC:-600}
INCREASED_TEST_TIMEOUT_SEC=$(( DEFAULT_TEST_TIMEOUT_SEC * 2 ))
# This timeout will be applied by the process_tree_supervisor.py script.
# shellcheck disable=SC2034
PROCESS_TREE_SUPERVISOR_TEST_TIMEOUT_SEC=$(( 30 * 60 ))
# We grep for these log lines and show them in the main log on test failure. This regular expression
# is used with "grep -E".
RELEVANT_LOG_LINES_RE="^[[:space:]]*(Value of|Actual|Expected):|^Expected|^Failed|^Which is:"
RELEVANT_LOG_LINES_RE+="|: Failure$|ThreadSanitizer: data race|Check failure"
readonly RELEVANT_LOG_LINES_RE
# Some functions use this to output to stdout/stderr along with a file.
append_output_to=/dev/null
readonly TEST_RESTART_PATTERN="Address already in use|\
pthread .*: Device or resource busy|\
FATAL: could not create shared memory segment: No space left on device"
# We use this to submit test jobs for execution on Spark.
readonly INITIAL_SPARK_DRIVER_CORES=8
# This is used to separate relative binary path from gtest_filter for C++ tests in what we call
# a "test descriptor" (a string that identifies a particular test).
#
# This must match the constant with the same name in run_tests_on_spark.py.
readonly TEST_DESCRIPTOR_SEPARATOR=":::"
readonly JENKINS_NFS_BUILD_REPORT_BASE_DIR="/Volumes/n/jenkins/build_stats"
# https://github.com/google/sanitizers/wiki/SanitizerCommonFlags
readonly SANITIZER_COMMON_OPTIONS=""
# shellcheck disable=SC2034
declare -i -r MIN_REPEATED_TEST_PARALLELISM=1
# shellcheck disable=SC2034
declare -i -r MAX_REPEATED_TEST_PARALLELISM=100
# shellcheck disable=SC2034
declare -i -r DEFAULT_REPEATED_TEST_PARALLELISM=4
# shellcheck disable=SC2034
declare -i -r DEFAULT_REPEATED_TEST_PARALLELISM_TSAN=1
# These options are added to all Maven command lines used in testing (collecting the list of Java
# tests, running the tests.)
readonly MVN_COMMON_OPTIONS_IN_TESTS=(
-Dmaven.javadoc.skip
)
# -------------------------------------------------------------------------------------------------
# Functions
# -------------------------------------------------------------------------------------------------
# Sanitize the given string so we can make it a path component.
sanitize_for_path() {
echo "$1" | sed 's/\//__/g; s/[:.]/_/g;'
}
# shellcheck disable=SC2120
set_common_test_paths() {
expect_num_args 0 "$@"
if [[ -z ${YB_TEST_LOG_ROOT_DIR:-} ]]; then
# Allow putting all the test logs into a separate directory by setting YB_TEST_LOG_ROOT_SUFFIX.
YB_TEST_LOG_ROOT_DIR="$BUILD_ROOT/yb-test-logs${YB_TEST_LOG_ROOT_SUFFIX:-}"
fi
mkdir_safe "$YB_TEST_LOG_ROOT_DIR"
# This is needed for tests to find the lsof program.
add_path_entry_last /usr/sbin
}
validate_relative_test_binary_path() {
expect_num_args 1 "$@"
local rel_test_binary=$1
if [[ ! $rel_test_binary =~ ^[a-zA-Z0-9_-]+/[a-zA-Z0-9_.-]+$ ]]; then
fatal "Expected a relative test binary path to consist of two components separated by '/'." \
"Got: '$rel_test_binary'."
fi
local rel_test_binary_dirname=${rel_test_binary%/*}
if [[ ! $rel_test_binary_dirname =~ $VALID_TEST_BINARY_DIRS_RE ]]; then
fatal "Expected the directory name a test binary is contained in to match regexp: " \
"$VALID_TEST_BINARY_DIRS_RE. Relative test binary path: $rel_test_binary" >&2
fi
}
create_abs_test_binary_path() {
expect_num_args 1 "$@"
local rel_test_binary_path=$1
local abs_test_binary_path=$BUILD_ROOT/$rel_test_binary_path
if [[ ! -f $abs_test_binary_path && -f "$abs_test_binary_path.sh" ]]; then
abs_test_binary_path+=".sh"
fi
echo "$abs_test_binary_path"
}
validate_abs_test_binary_path() {
if [[ ! -f $abs_test_binary_path ]]; then
fatal "Test binary '$abs_test_binary_path' does not exist"
fi
if [[ ! -x $abs_test_binary_path ]]; then
fatal "Test binary '$abs_test_binary_path' is not executable"
fi
}
# Validates a "test descriptor" string. This is our own concept to identify tests within our test
# suite. A test descriptor has one of the two forms:
# - <relative_test_binary>$TEST_DESCRIPTOR_SEPARATOR<test_name_within_binary>
# - <relative_test_binary>
# We've chosen triple colon as a separator that would rarely occur otherwise.
# Examples:
# - bin/tablet-test$TEST_DESCRIPTOR_SEPARATORTestTablet/5.TestDeleteWithFlushAndCompact
# The test name within the binary is what can be passed to Google Test as the --gtest_filter=...
# argument. Some test binaries have to be run at once, e.g. non-gtest test binary or
# test binaries with internal dependencies between tests. For those there is on
# "$TEST_DESCRIPTOR_SEPARATOR" separator or the <test_name_within_binary> part.
validate_test_descriptor() {
expect_num_args 1 "$@"
local test_descriptor=$1
if [[ $test_descriptor =~ $TEST_DESCRIPTOR_SEPARATOR ]]; then
if [[ $test_descriptor =~ $TEST_DESCRIPTOR_SEPARATOR.*$TEST_DESCRIPTOR_SEPARATOR ]]; then
fatal "The '$TEST_DESCRIPTOR_SEPARATOR' separator occurs twice within the test descriptor:" \
"$test_descriptor"
fi
# Remove $TEST_DESCRIPTOR_SEPARATOR and all that follows and get a relative binary path.
validate_relative_test_binary_path "${test_descriptor%"$TEST_DESCRIPTOR_SEPARATOR"*}"
else
validate_relative_test_binary_path "$test_descriptor"
fi
}
# Some tests are not based on the gtest framework and don't generate an XML output file.
# Also, RocksDB's thread_list_test is like that on Mac OS X.
is_known_non_gtest_test() {
local test_name=$1
if [[ "$OSTYPE" =~ ^darwin && "$test_name" == thread_list_test || \
"$test_name" =~ $NON_GTEST_TESTS_RE ]]; then
return 0 # "true return value" in bash
else
return 1 # "false return value" in bash
fi
}
# This is used by yb_test.sh.
# Takes relative test name such as "bin/client-test" or "bin/db_test".
is_known_non_gtest_test_by_rel_path() {
if [[ $# -ne 1 ]]; then
fatal "is_known_non_gtest_test_by_rel_path takes exactly one argument" \
"(test binary path relative to the build directory)"
fi
local rel_test_path=$1
validate_relative_test_binary_path "$rel_test_path"
local test_binary_basename=${rel_test_path##*/}
# Remove .sh extensions for bash-based tests.
local test_binary_basename_no_ext=${test_binary_basename%[.]sh}
is_known_non_gtest_test "$test_binary_basename_no_ext"
}
# Collects Google Test tests from the given test binary.
# Input:
# rel_test_binary
# Test binary path relative to the build directory. Copied from the arguemnt.
# YB_GTEST_FILTER
# If this is set, this is passed to the test binary as --gtest_filter=... along with
# --gtest_list_tests, so that we only run a subset of tests. The reason this is an all-uppercase
# variable is that we sometimes set it as an environment variable passed down to run-test.sh.
# Output variables:
# tests
# This is an array that should be initialized by the caller. This adds new "test descriptors" to
# this array.
# num_binaries_to_run_at_once
# The number of binaries to be run in one shot. This variable is incremented if it exists.
# num_test_cases
# Total number of test cases collected. Test cases are Google Test's way of grouping tests
# (test functions) together. Usually they are methods of the same test class.
# This variable is incremented if it exists.
# num_tests
# Total number of tests (test functions) collected. This variable is incremented if it exists.
# Also, this function unsets test_log_path if it was previously set.
collect_gtest_tests() {
expect_num_args 0 "$@"
validate_relative_test_binary_path "$rel_test_binary"
if [[ $rel_test_binary == "bin/db_sanity_test" ]]; then
# db_sanity_check is not a test, but a command-line tool.
return
fi
if is_known_non_gtest_test_by_rel_path "$rel_test_binary" || \
[[ "$rel_test_binary" =~ $TEST_BINARIES_TO_RUN_AT_ONCE_RE ]]; then
tests+=( "$rel_test_binary" )
if [ -n "${num_binaries_to_run_at_once:-}" ]; then
(( num_binaries_to_run_at_once+=1 ))
fi
return
fi
local gtest_list_stderr_path
gtest_list_stderr_path=$( mktemp -t "gtest_list_tests.stderr.XXXXXXXXXX" )
local abs_test_binary_path
abs_test_binary_path=$( create_abs_test_binary_path "$rel_test_binary" )
validate_abs_test_binary_path "$abs_test_binary_path"
local gtest_list_tests_tmp_dir
gtest_list_tests_tmp_dir=$( mktemp -t -d "gtest_list_tests_tmp_dir.XXXXXXXXXX" )
mkdir_safe "$gtest_list_tests_tmp_dir" # on Mac OS X, mktemp does not create the directory
local gtest_list_tests_cmd=( "$abs_test_binary_path" --gtest_list_tests )
if [[ -n ${YB_GTEST_FILTER:-} ]]; then
gtest_list_tests_cmd+=( "--gtest_filter=$YB_GTEST_FILTER" )
fi
# We need to list the tests without any user-specified sanitizer options, because that might
# produce unintended output.
local old_sanitizer_extra_options=${YB_SANITIZER_EXTRA_OPTIONS:-}
local YB_SANITIZER_EXTRA_OPTIONS=""
set_sanitizer_runtime_options
set +e
pushd "$gtest_list_tests_tmp_dir" >/dev/null
local gtest_list_tests_result # this has to be on a separate line to capture the exit code
gtest_list_tests_result=$(
"${gtest_list_tests_cmd[@]}" 2>"$gtest_list_stderr_path"
)
gtest_list_tests_exit_code=$?
popd >/dev/null
set -e
YB_SANITIZER_EXTRA_OPTIONS=$old_sanitizer_extra_options
set_sanitizer_runtime_options
set_expected_core_dir "$gtest_list_tests_tmp_dir"
test_log_path="$gtest_list_stderr_path"
process_core_file
unset test_log_path
rm -rf "$gtest_list_tests_tmp_dir"
if [[ -z $gtest_list_tests_result ]]; then
if [[ -s $gtest_list_stderr_path ]]; then
log "Standard error from ${gtest_list_tests_cmd[*]}:"
cat "$gtest_list_stderr_path"
fi
fatal "Empty output from ${gtest_list_tests_cmd[*]}," \
"exit code: $gtest_list_tests_exit_code."
fi
# https://nixtricks.wordpress.com/2013/01/09/sed-delete-the-lines-lying-in-between-two-patterns/
# Use the following command to delete the lines lying between PATTERN-1 and PATTERN-2, including
# the lines containing these patterns:
# sed '/PATTERN-1/,/PATTERN-2/d' input.txt
sed '/^Suppressions used:$/,/^-\{53\}$/d' \
<"$gtest_list_stderr_path" | sed '/^-\{53\}$/d; /^\s*$/d;' | \
( grep -Ev "^(\
Starting tracking the heap|\
Dumping heap profile to .* \\(Exiting\\))|\
Shared library .* loaded at address 0x[0-9a-f]+$" || true ) \
>"$gtest_list_stderr_path.filtered"
# -s tests if the given file is non-empty
if [[ -s "$gtest_list_stderr_path.filtered" ]]; then
(
echo
echo "'$abs_test_binary_path' produced non-empty stderr output when run with" \
"--gtest_list_tests:"
echo "[ ==== STDERR OUTPUT START ========================================================== ]"
cat "$gtest_list_stderr_path.filtered"
echo "[ ==== STDERR OUTPUT END ============================================================ ]"
echo "Please add the test '$rel_test_binary' to the appropriate list in"
echo "common-test-env.sh or fix the underlying issue in the code."
) >&2
rm -f "$gtest_list_stderr_path" "$gtest_list_stderr_path.filtered"
exit 1
fi
rm -f "$gtest_list_stderr_path"
if [ "$gtest_list_tests_exit_code" -ne 0 ]; then
echo "'$rel_test_binary' does not seem to be a gtest test (--gtest_list_tests failed)" >&2
echo "Please add this test to the appropriate blacklist in common-test-env.sh" >&2
exit 1
fi
local test_list_item
local IFS=$'\n' # so that we can iterate through lines in $gtest_list_tests_result
for test_list_item in $gtest_list_tests_result; do
if [[ "$test_list_item" =~ ^\ \ ]]; then
# This is a "test": an individual piece of test code to run, described by TEST or TEST_F
# in a Google Test program.
test=${test_list_item# } # Remove two leading spaces
test=${test%%#*} # Remove everything after a "#" comment
test=${test% } # Remove two trailing spaces
if [[ $test == *YB_DISABLE_TEST_IN_* ]]; then
fatal "YB_DISABLE_TEST_IN_... is not allowed in C++ test names. This could happen when" \
"trying to use YB_DISABLE_TEST_IN_TSAN or YB_DISABLE_TEST_IN_SANITIZERS in a" \
"parameterized test with TEST_P. For parameterized tests, please use" \
"YB_SKIP_TEST_IN_TSAN() as the first line of the test instead. Test name: $test."
fi
if [[ -n ${total_num_tests:-} ]]; then
(( total_num_tests+=1 ))
fi
tests+=( "${rel_test_binary}$TEST_DESCRIPTOR_SEPARATOR$test_case$test" )
if [[ -n ${num_tests:-} ]]; then
(( num_tests+=1 ))
fi
else
# This is a "test case", a "container" for a number of tests, as in
# TEST(TestCaseName, TestName). There is no executable item here.
test_case=${test_list_item%%#*} # Remove everything after a "#" comment
test_case=${test_case% } # Remove two trailing spaces
if [ -n "${num_test_cases:-}" ]; then
(( num_test_cases+=1 ))
fi
fi
done
}
using_nfs() {
if [[ $YB_SRC_ROOT =~ ^/Volumes/n/ ]]; then
return 0
fi
return 1
}
# Ensure we have a TEST_TMPDIR defined and it exists.
ensure_test_tmp_dir_is_set() {
if [[ ${TEST_TMPDIR:-} == "/tmp" ]]; then
fatal "TEST_TMPDIR cannot be set to /tmp, it has to be a test-specific directory."
fi
if [[ -z ${TEST_TMPDIR:-} ]]; then
export TEST_TMPDIR="${YB_TEST_TMP_BASE_DIR:-/tmp}/yb_test.tmp.$RANDOM.$RANDOM.$RANDOM.pid$$"
fi
mkdir_safe "$TEST_TMPDIR"
}
# Set a number of variables in preparation to running a test.
# Inputs:
# test_descriptor
# Identifies the test. Consists fo the relative test binary, optionally followed by
# "$TEST_DESCRIPTOR_SEPARATOR" and a gtest-compatible test description.
# test_attempt_index
# If this is set to a number, it is appended (followed by an underscore) to test output
# directory, work directory, JUnit xml file name, and other file/directory paths that are
# expected to be unique. This allows to run each test multiple times within the same test suite
# run.
# Outputs:
# rel_test_binary
# Relative path of the test binary
# test_name
# Test name within the test binary, to be passed to --gtest_filter=...
# junit_test_case_id
# Test ID for JUnit report in format: <Test suite name>.<Test case name>, used to generate
# XML in case it is not possible to parse test case info from test log file (non-gtest binary,
# crashed to early).
# run_at_once
# Whether we need to run all tests in this binary at once ("true" or "false")
# what_test_str
# A human-readable string describing what tests we are running within the test binary.
# rel_test_log_path_prefix
# A prefix of various log paths for this test relative to the root directory we're using for
# all test logs and temporary files (YB_TEST_LOG_ROOT_DIR).
# is_gtest_test
# Whether this is a Google Test test ("true" or "false")
# TEST_TMPDIR (exported)
# The temporary directory for the test to use. The test should also run in this directory so
# that the core file would be generated there, if any.
# test_cmd_line
# An array representing the test command line to run, including the JUnit-compatible test result
# XML file location and --gtest_filter=..., if applicable.
# test_log_path
# The final log path (after any applicable filtering and appending additional debug info).
# xml_output_file
# The location at which JUnit-compatible test result XML file should be generated.
# test_failed
# This is being set to "false" initially.
# test_log_url
# Test log URL served by Jenkins. This is only set when running on Jenkins.
#
# This function also removes the old XML output file at the path "$xml_output_file".
prepare_for_running_cxx_test() {
expect_num_args 0 "$@"
expect_vars_to_be_set \
YB_TEST_LOG_ROOT_DIR \
test_descriptor
validate_test_descriptor "$test_descriptor"
local test_attempt_suffix=""
if [[ -n ${test_attempt_index:-} ]]; then
if [[ ! ${test_attempt_index} =~ ^[0-9]+$ ]]; then
fatal "test_attempt_index is expected to be a number, found '$test_attempt_index'"
fi
test_attempt_suffix="__attempt_$test_attempt_index"
fi
if [[ "$test_descriptor" =~ $TEST_DESCRIPTOR_SEPARATOR ]]; then
rel_test_binary=${test_descriptor%"${TEST_DESCRIPTOR_SEPARATOR}"*}
test_name=${test_descriptor#*"${TEST_DESCRIPTOR_SEPARATOR}"}
run_at_once=false
junit_test_case_id=$test_name
else
# Run all test cases in the given test binary
rel_test_binary=$test_descriptor
test_name=""
run_at_once=true
junit_test_case_id="${rel_test_binary##*/}.all"
fi
local test_binary_sanitized
test_binary_sanitized=$( sanitize_for_path "$rel_test_binary" )
local test_name_sanitized
test_name_sanitized=$( sanitize_for_path "$test_name" )
test_dir=$YB_TEST_LOG_ROOT_DIR/$test_binary_sanitized
mkdir_safe "$test_dir"
rel_test_log_path_prefix="$test_binary_sanitized"
if [[ ${run_at_once} == "true" ]]; then
# Make this similar to the case when we run tests separately. Pretend that the test binary name
# is the test name.
rel_test_log_path_prefix+="/${rel_test_binary##*/}"
else
rel_test_log_path_prefix+="/$test_name_sanitized"
fi
# Use a separate directory to store results of every test attempt.
rel_test_log_path_prefix+=$test_attempt_suffix
# At this point $rel_test_log_path contains the common prefix of the log (.log), the temporary
# directory (.tmp), and the XML report (.xml) for this test relative to the yb-test-logs
# directory.
local abs_test_binary_path
abs_test_binary_path=$( create_abs_test_binary_path "$rel_test_binary" )
# Word splitting is intentional here, so YB_EXTRA_GTEST_FLAGS is not quoted.
# shellcheck disable=SC2206
test_cmd_line=( "$abs_test_binary_path" ${YB_EXTRA_GTEST_FLAGS:-} )
test_log_path_prefix="$YB_TEST_LOG_ROOT_DIR/$rel_test_log_path_prefix"
register_test_artifact_files \
"$test_log_path_prefix.*" \
"${test_log_path_prefix}_test_report.json"
xml_output_file="$test_log_path_prefix.xml"
if is_known_non_gtest_test_by_rel_path "$rel_test_binary"; then
is_gtest_test=false
else
test_cmd_line+=( "--gtest_output=xml:$xml_output_file" )
is_gtest_test=true
fi
if ! "$run_at_once" && "$is_gtest_test"; then
test_cmd_line+=( "--gtest_filter=$test_name" )
fi
ensure_test_tmp_dir_is_set
test_log_path="$test_log_path_prefix.log"
# gtest won't overwrite old junit test files, resulting in a build failure
# even when retries are successful.
rm -f "$xml_output_file"
test_failed=false
if [[ -n ${BUILD_URL:-} ]]; then
if [[ -z $test_log_url_prefix ]]; then
echo "Expected test_log_url_prefix to be set when running on Jenkins." >&2
exit 1
fi
test_log_url="$test_log_url_prefix/$rel_test_log_path_prefix.log"
fi
}
# Set the directory to look for core files in to the given path. On Mac OS X, the input argument is
# ignored and the standard location is used instead.
set_expected_core_dir() {
expect_num_args 1 "$@"
local new_core_dir=$1
if is_mac; then
core_dir=/cores
else
core_dir="$new_core_dir"
fi
}
# shellcheck disable=SC2120
set_debugger_input_for_core_stack_trace() {
expect_num_args 0 "$@"
expect_vars_to_be_set \
core_path \
executable_path
if is_mac; then
debugger_cmd=( lldb "$executable_path" -c "$core_path" )
debugger_input="thread backtrace ${thread_for_backtrace:-all}" # backtrace
# TODO: dump the current thread AND all threads, like we already do with gdb.
else
debugger_cmd=(
gdb -q -n
-ex 'bt'
-ex 'thread apply all bt'
-batch "$executable_path" "$core_path"
)
debugger_input=""
fi
}
analyze_existing_core_file() {
expect_num_args 2 "$@"
local core_path=$1
local executable_path=$2
ensure_file_exists "$core_path"
ensure_file_exists "$executable_path"
( echo "Found a core file at '$core_path', backtrace:" | tee -a "$append_output_to" ) >&2
# The core might have been generated by yb-master or yb-tserver launched as part of an
# ExternalMiniCluster.
# TODO(mbautin): don't run gdb/lldb the second time in case we get the binary name right the
# first time around.
local debugger_cmd debugger_input
# shellcheck disable=SC2119
set_debugger_input_for_core_stack_trace
local debugger_output
debugger_output=$( echo "$debugger_input" | "${debugger_cmd[@]}" 2>&1 )
if [[ "$debugger_output" =~ Core\ was\ generated\ by\ .(yb-master|yb-tserver) ]]; then
# The test program may be launching masters and tablet servers through an ExternalMiniCluster,
# and the core file was generated by one of those masters and tablet servers.
executable_path="$BUILD_ROOT/bin/${BASH_REMATCH[1]}"
set_debugger_input_for_core_stack_trace
fi
set +e
(
set -x
echo "$debugger_input" |
"${debugger_cmd[@]}" 2>&1 |
grep -Ev "^\[New LWP [0-9]+\]$" |
"$YB_SRC_ROOT"/build-support/dedup_thread_stacks.py |
tee -a "$append_output_to"
) >&2
set -e
echo >&2
echo >&2
}
# Looks for a core dump file in the specified directory, shows a stack trace using gdb, and removes
# the core dump file to save space.
# Inputs:
# rel_test_binary
# Test binary path relative to $BUILD_ROOT
# core_dir
# The directory to look for a core dump file in.
# test_log_path
# If this is defined and is not empty, any output generated by this function is appended to the
# file at this path.
process_core_file() {
expect_num_args 0 "$@"
expect_vars_to_be_set \
rel_test_binary
local abs_test_binary_path=$BUILD_ROOT/$rel_test_binary
local append_output_to=/dev/null
if [[ -n ${test_log_path:-} ]]; then
append_output_to=$test_log_path
fi
local thread_for_backtrace="all"
if is_mac; then
# Finding core files on Mac OS X.
if [[ -f ${test_log_path:-} ]]; then
set +e
local signal_received_line
signal_received_line=$(
grep -E 'SIG[A-Z]+.*received by PID [0-9]+' "$test_log_path" | head -1
)
set -e
local core_pid=""
# Parsing a line like this:
# *** SIGSEGV (@0x0) received by PID 46158 (TID 0x70000028d000) stack trace: ***
if [[ $signal_received_line =~ received\ by\ PID\ ([0-9]+)\ \(TID\ (0x[0-9a-fA-F]+)\) ]]; then
core_pid=${BASH_REMATCH[1]}
# TODO: find a way to only show the backtrace of the relevant thread. We can't just pass
# the thread id to "thread backtrace", apparently it needs the thread index of some sort
# that is not identical to thread id.
# thread_for_backtrace=${BASH_REMATCH[2]}
fi
if [[ -n $core_pid ]]; then
local core_path=$core_dir/core.$core_pid
log "Determined core_path=$core_path"
if [[ ! -f $core_path ]]; then
log "Core file not found at presumed location '$core_path'."
fi
else
if grep -Eq 'SIG[A-Z]+.*received by PID' "$test_log_path"; then
log "Warning: could not determine test pid from '$test_log_path'" \
"(matched log line: $signal_received_line)"
fi
return
fi
else
log "Warning: test_log_path is not set, or file does not exist ('${test_log_path:-}')," \
"cannot look for core files on Mac OS X"
return
fi
else
# Finding core files on Linux.
if [[ ! -d $core_dir ]]; then
echo "${FUNCNAME[0]}: directory '$core_dir' does not exist" >&2
exit 1
fi
local core_path=$core_dir/core
if [[ ! -f $core_path ]]; then
# If there is just one file named "core.[0-9]+" in the core directory, pick it up and assume
# it belongs to the test. This is necessary on some systems, e.g. CentOS workstations with no
# special core location overrides.
local core_candidates=()
for core_path in "$core_dir"/core.*; do
if [[ -f $core_path && $core_path =~ /core[.][0-9]+$ ]]; then
core_candidates+=( "$core_path" )
fi
done
if [[ ${#core_candidates[@]} -eq 1 ]]; then
core_path=${core_candidates[0]}
elif [[ ${#core_candidates[@]} -gt 1 ]]; then
log "Found too many core files in '$core_dir', not analyzing: ${core_candidates[*]}"
return
fi
fi
fi
if [[ -f $core_path ]]; then
local core_binary_path=$abs_test_binary_path
analyze_existing_core_file "$core_path" "$core_binary_path"
rm -f "$core_path" # to save disk space
fi
}
# Used for both C++ and Java tests.
stop_process_tree_supervisor() {
process_supervisor_success=true
if [[ ${process_tree_supervisor_pid:-0} -eq 0 ]]; then
return
fi
# process_supervisor_log_path should be set in case process_tree_supervisor_pid is set.
expect_vars_to_be_set process_supervisor_log_path
if ! kill -SIGUSR1 "$process_tree_supervisor_pid"; then
log "Warning: could not stop the process tree supervisor (pid $process_tree_supervisor_pid)." \
"This could be because it has already terminated."
fi
set +e
wait "$process_tree_supervisor_pid"
declare -i supervisor_exit_code=$?
set -e
if [[ $supervisor_exit_code -eq $SIGUSR1_EXIT_CODE ]]; then
# This could happen when we killed the supervisor script before it had a chance to set the
# signal handler for SIGUSR1.
supervisor_exit_code=0
fi
declare -i return_code=0
if [[ $supervisor_exit_code -ne 0 ]]; then
log "Process tree supervisor exited with code $supervisor_exit_code"
process_supervisor_success=false
fi
# process_supervisor_log_path should be set in run-test.sh.
# shellcheck disable=SC2154
if [[ -f $process_supervisor_log_path ]]; then
if is_jenkins || [[ $process_supervisor_success != "true" ]] || \
grep -q YB_STRAY_PROCESS "$process_supervisor_log_path"; then
log "Process supervisor log dump ($process_supervisor_log_path):"
cat "$process_supervisor_log_path" >&2
else
log "Omitting process supervisor log, nothing interesting there"
fi
# TODO: Re-enable the logic below for treating stray processes as test failures, when the
# following tests are fixed:
# - org.yb.pgsql.TestPgRegressFeature.testPgRegressFeature
# - org.yb.pgsql.TestPgRegressTypesNumeric.testPgRegressTypes
# See https://github.com/YugaByte/yugabyte-db/issues/946 for details.
if false && grep -q YB_STRAY_PROCESS "$process_supervisor_log_path"; then
log "Stray processes reported in $process_supervisor_log_path, considering the test failed."
log "The JUnit-compatible XML file will be updated to reflect this error."
process_supervisor_success=false
if [[ -f ${process_tree_supervisor_append_log_to_on_error:-} ]]; then
log "Appending process supervisor log to $process_tree_supervisor_append_log_to_on_error"
(
echo "Process supervisor log:";
cat "$process_supervisor_log_path"
) >>"$process_tree_supervisor_append_log_to_on_error"
fi
fi
rm -f "$process_supervisor_log_path"
fi
# To make future calls to this function a no-op.
process_tree_supervisor_pid=0
}
# Checks if the there is no XML file at path "$xml_output_file". This may happen if a gtest test
# suite fails before it has a chance to generate an XML output file. In that case, this function
# generates a substitute XML output file using parse_test_failure.py, but only if the log file
# exists at its expected location.
handle_cxx_test_xml_output() {
expect_vars_to_be_set \
rel_test_binary \
test_log_path \
xml_output_file
if [[ ! -f "$xml_output_file" || ! -s "$xml_output_file" ]]; then
# XML does not exist or empty (most probably due to test crash during XML generation)
if is_known_non_gtest_test_by_rel_path "$rel_test_binary"; then
echo "$rel_test_binary is a known non-gtest binary, OK that it did not produce XML" \
"output" >&2
else
echo "$rel_test_binary failed to produce an XML output file at $xml_output_file" >&2
test_failed=true
fi
if [[ ! -f "$test_log_path" ]]; then
echo "Test log path '$test_log_path' does not exist"
test_failed=true
# parse_test_failure will also generate XML file in this case.
fi
echo "Generating an XML output file using parse_test_failure.py: $xml_output_file" >&2
"$YB_SRC_ROOT"/build-support/parse_test_failure.py -x \
"$junit_test_case_id" "$test_log_path" >"$xml_output_file"
fi
process_tree_supervisor_append_log_to_on_error=$test_log_path
stop_process_tree_supervisor
if ! "$process_supervisor_success"; then
log "Process tree supervisor reported that the test failed"
test_failed=true
fi
if [[ ${test_failed} == "true" ]]; then
log "Test failed, updating $xml_output_file"
else
log "Test succeeded, updating $xml_output_file"
fi
update_test_result_xml_cmd=(
"$YB_SRC_ROOT"/build-support/update_test_result_xml.py
--result-xml "$xml_output_file"
--mark-as-failed "$test_failed"
)
if [[ -n ${test_log_url:-} ]]; then
update_test_result_xml_cmd+=( --log-url "$test_log_url" )
fi
( set -x; "${update_test_result_xml_cmd[@]}" )
# Useful for distributed builds in an NFS environment.
chmod g+w "$xml_output_file"
}
# Sets test log URL prefix if we're running in Jenkins.
set_test_log_url_prefix() {
test_log_url_prefix=""
local build_dir_name=${BUILD_ROOT##*/}
if [[ -n ${BUILD_URL:-} ]]; then
build_url_no_trailing_slash=${BUILD_URL%/}
test_log_url_prefix="${build_url_no_trailing_slash}/artifact/build/$build_dir_name/yb-test-logs"
fi
}
# shellcheck disable=SC2120
determine_test_timeout() {
expect_num_args 0 "$@"
expect_vars_to_be_set rel_test_binary
if [[ -n ${YB_TEST_TIMEOUT:-} ]]; then
timeout_sec=$YB_TEST_TIMEOUT
else
if [[ $rel_test_binary == "tests-pgwrapper/create_initial_sys_catalog_snapshot" || \
$rel_test_binary == "tests-pgwrapper/pg_libpq-test" || \
$rel_test_binary == "tests-pgwrapper/pg_libpq_err-test" || \
$rel_test_binary == "tests-pgwrapper/pg_mini-test" || \
$rel_test_binary == "tests-pgwrapper/pg_wrapper-test" || \
$rel_test_binary == "tests-tools/yb-admin-snapshot-schedule-test" ]]; then
timeout_sec=$INCREASED_TEST_TIMEOUT_SEC
else
timeout_sec=$DEFAULT_TEST_TIMEOUT_SEC
fi
fi
}
try_set_ulimited_ulimit() {
# Setting the ulimit may fail with an error message, and that's what we want. We will still
# run the test. In case we do manage to set the core file size limit, the caller can restore the
# previous limit after exiting a subshell.
if ! ulimit -c unlimited; then
# Print some diagnostics if we fail to set core file size limit.
log "Command 'ulimit -c unlimited' failed. Current 'ulimit -c' output: $( ulimit -c )"
fi
}
# YB_CTEST_VERBOSE makes test output go to stderr, and then we separately make it show up on
# the console by giving ctest the --verbose option. This is intended for development. When we
# run tests on Jenkins or when running all tests using "ctest -j8" from the build root, one
# should leave YB_CTEST_VERBOSE unset.
is_ctest_verbose() {
[[ ${YB_CTEST_VERBOSE:-0} == "1" ]]
}
run_one_cxx_test() {
expect_num_args 0 "$@"
expect_vars_to_be_set \
BUILD_ROOT \
TEST_TMPDIR \
test_cmd_line \
test_failed \
rel_test_binary
# We expect the exact string "false" here for added safety.
if [[ $test_failed != "false" ]]; then
fatal "Expected test_failed to be false before running test, found: $test_failed"
fi
# shellcheck disable=SC2119
determine_test_timeout
local test_wrapper_cmd_line=(
"$BUILD_ROOT"/bin/run-with-timeout $(( timeout_sec + 1 )) "${test_cmd_line[@]}"
)
if [[ $TEST_TMPDIR == "/" || $TEST_TMPDIR == "/tmp" ]]; then
# Let's be paranoid because we'll be deleting everything inside this directory.
fatal "Invalid TEST_TMPDIR: '$TEST_TMPDIR': must be a unique temporary directory."
fi
pushd "$TEST_TMPDIR" >/dev/null
export YB_FATAL_DETAILS_PATH_PREFIX=$test_log_path_prefix.fatal_failure_details
about_to_start_running_test
local attempts_left
for attempts_left in {1..0}; do
# Clean up anything that might have been left from the previous attempt.
rm -rf "${TEST_TMPDIR:-/tmp/yb_never_delete_entire_filesystem}"/*
set +e
(
try_set_ulimited_ulimit
if is_ctest_verbose; then
( set -x; "${test_wrapper_cmd_line[@]}" 2>&1 ) | tee "${test_log_path}"
# Propagate the exit code of the test process, not any of the filters. This will only exit
# this subshell, not the entire script calling this function.
exit "${PIPESTATUS[0]}"
else
"${test_wrapper_cmd_line[@]}" &>"$test_log_path"
fi
)
test_exit_code=$?
set -e
# Useful for distributed builds in an NFS environment.
chmod g+w "${test_log_path}"
# Test did not fail, no need to retry.
if [[ $test_exit_code -eq 0 ]]; then
break
fi
# See if the test failed due to "Address already in use" and log a message if we still have more
# attempts left.
if [[ $attempts_left -gt 0 ]] && \
grep -Eq "$TEST_RESTART_PATTERN" "$test_log_path"; then
log "Found one of the intermittent error patterns in the log, restarting the test (once):"
grep -E "$TEST_RESTART_PATTERN" "$test_log_path" >&2
else
# Avoid retrying any other kinds of failures.
break
fi
done
popd >/dev/null
if [[ $test_exit_code -ne 0 ]]; then
test_failed=true
fi
}
# shellcheck disable=SC2120
handle_cxx_test_failure() {
expect_num_args 0 "$@"
expect_vars_to_be_set \
TEST_TMPDIR \
global_exit_code \
rel_test_log_path_prefix \
test_exit_code \
test_cmd_line \
test_log_path
if ! "$test_failed" & ! did_test_succeed "$test_exit_code" "$test_log_path"; then
test_failed=true
fi
if [[ ${test_failed} == "true" || ${YB_FORCE_REWRITE_TEST_LOGS:-0} == "1" ]]; then
(
rewrite_test_log "${test_log_path}"
echo
echo "TEST FAILURE"
echo "Test command: ${test_cmd_line[*]}"
echo "Test exit status: $test_exit_code"
echo "Log path: $test_log_path"
if [[ -n ${BUILD_URL:-} ]]; then
if [[ -z ${test_log_url_prefix:-} ]]; then
fatal "Expected test_log_url_prefix to be set if BUILD_URL is defined"
fi
# Produce a URL like
# https://jenkins.dev.yugabyte.com/job/yugabyte-with-custom-test-script/47/artifact/build/debug/yb-test-logs/bin__raft_consensus-itest/RaftConsensusITest_TestChurnyElections.log
echo "Log URL: $test_log_url_prefix/$rel_test_log_path_prefix.log"
fi
# Show some context from the test log, but only do so if we are not already showing the entire
# test log when invoking tests directly in yb_build.sh.
if ! is_ctest_verbose && [[ -f $test_log_path ]] &&
grep -Eq "$RELEVANT_LOG_LINES_RE" "$test_log_path"; then
echo "Relevant log lines:"
grep -E -C 3 "$RELEVANT_LOG_LINES_RE" "$test_log_path"
fi
) >&2
set_expected_core_dir "$TEST_TMPDIR"
process_core_file
unset core_dir
global_exit_code=1
fi
}
delete_successful_output_if_needed() {
expect_vars_to_be_set \
TEST_TMPDIR \
test_log_path