Skip to content

Commit 58b4e34

Browse files
committed
Migrate unit tests to use Go workspace
Introduce the new functions: run_go_tests and run_go_test_expanding_packages, which implements (without Shellcheck exceptions) the old behavior from go_test, simplifying the arguments received. Replaced the three possible values mode with: 1. keep_going: By using the KEEP_GOING_TESTS environment variable. This mode expanded the relative modules; it can be run by using run_go_tests_expanding_packages. 2. parallel: It worked without expanding the relative modules with the absolute modules. Therefore, this mode can be run directly using run_go_tests. 3. fail_fast: By adding an argument "-failfast", and not setting the KEEP_GOING_TESTS environment variable. The argument flags_for_package_func can be replaced by regular arguments passed to the function. These two changes simplify the complexity of the go test wrapper function and allow further customization in the callers. The JUnit XML generation works the same way, by reading the JUNIT_REPORT_DIR or ARTIFACTS environment variable. It generates the same reports. Temporarily introduce the get_junit_filename_prefix function, as a replacement for junitFilenamePrefix. The latter will be removed once all test targets are using the new functions. Finally, update the unit tests function to make use of the new functions, and allow running the tests by using the workspace (i.e., there's no need to change directories anymore). Signed-off-by: Ivan Valdes <ivan@vald.es>
1 parent d661826 commit 58b4e34

File tree

2 files changed

+108
-8
lines changed

2 files changed

+108
-8
lines changed

‎scripts/test.sh‎

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -126,15 +126,15 @@ function build_pass {
126126
################# REGULAR TESTS ################################################
127127

128128
# run_unit_tests [pkgs] runs unit tests for a current module and givesn set of [pkgs]
129-
function run_unit_tests {
130-
local pkgs="${1:-./...}"
131-
shift 1
132-
# shellcheck disable=SC2068 #For context see - https://github.com/etcd-io/etcd/pull/16433#issuecomment-1684312755
133-
GOLANG_TEST_SHORT=true go_test "${pkgs}" "parallel" : -short -timeout="${TIMEOUT:-3m}" ${COMMON_TEST_FLAGS[@]:-} ${RUN_ARG[@]:-} "$@"
134-
}
135-
136129
function unit_pass {
137-
run_for_modules run_unit_tests "$@"
130+
KEEP_GOING_TESTS=true \
131+
run_for_all_workspace_modules \
132+
run_go_tests \
133+
-short \
134+
-timeout="${TIMEOUT:-3m}" \
135+
"${COMMON_TEST_FLAGS[@]}" \
136+
"${RUN_ARG[@]}" \
137+
"$@"
138138
}
139139

140140
function integration_extra {

‎scripts/test_lib.sh‎

Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -206,6 +206,16 @@ function run_for_modules {
206206
fi
207207
}
208208

209+
function get_junit_filename_prefix {
210+
local junit_report_dir="$1"
211+
if [[ -z "${junit_report_dir}" ]]; then
212+
echo ""
213+
return
214+
fi
215+
216+
mktemp "${junit_report_dir}/junit_XXXXXXXXXX"
217+
}
218+
209219
junitFilenamePrefix() {
210220
if [[ -z "${JUNIT_REPORT_DIR:-}" ]]; then
211221
echo ""
@@ -335,6 +345,96 @@ function go_test {
335345
fi
336346
}
337347

348+
# run_go_tests_expanding_packages [arguments to pass to go test]
349+
# Expands the packages in the list of arguments, i.e. ./... into a list of
350+
# packages for that given module. Then, it calls run_go_tests with the expanded
351+
# packages.
352+
function run_go_tests_expanding_packages {
353+
local packages=()
354+
local args=()
355+
for arg in "$@"; do
356+
if [[ "${arg}" =~ ./ ]]; then
357+
packages+=("${arg}")
358+
else
359+
args+=("${arg}")
360+
fi
361+
done
362+
363+
# Expanding patterns (like ./...) into list of packages
364+
local unpacked_packages=()
365+
while IFS='' read -r line; do unpacked_packages+=("$line"); done < <(
366+
go list "${packages[@]}"
367+
)
368+
369+
run_go_tests "${unpacked_packages[@]}" "${args[@]}"
370+
}
371+
372+
# run_go_test [arguments to pass to go test]
373+
# The following environment variables affect how the tests run:
374+
# - KEEP_GOING_TESTS: If set to true it will keep executing tests even after
375+
# a failure. It collects all failures and reports them at
376+
# the end, if there are failures the return code is 2.
377+
# Defaults to false.
378+
# - JUNIT_REPORT_DIR/ARTIFACTS: Enables collecting JUnit XML reports.
379+
# - VERBOSE: Sets a verbose output.
380+
#
381+
# Example:
382+
# KEEP_GOING_TESTS=true run_go_tests "./..." --short
383+
#
384+
# The function returns != 0 code in case of test failure.
385+
function run_go_tests {
386+
local go_test_flags=()
387+
388+
local packages=()
389+
local args=()
390+
for arg in "$@"; do
391+
if [[ "${arg}" =~ ^\./ || "${arg}" =~ ^go\.etcd\.io/etcd ]]; then
392+
packages+=("${arg}")
393+
else
394+
args+=("${arg}")
395+
fi
396+
done
397+
398+
local keep_going=${KEEP_GOING_TESTS:-false}
399+
400+
# If JUNIT_REPORT_DIR is unset, and ARTIFACTS is set, then have them match.
401+
local junit_report_dir=${JUNIT_REPORT_DIR:-${ARTIFACTS:-}}
402+
403+
local go_test_grep_pattern=".*"
404+
if [[ -n "${junit_report_dir}" ]]; then
405+
# Show only summary lines by matching lines like "status package/test"
406+
go_test_grep_pattern="^[^[:space:]]\+[[:space:]]\+[^[:space:]]\+/[^[[:space:]]\+"
407+
fi
408+
409+
if [[ -n "${junit_report_dir}" || "${VERBOSE:-}" == "1" ]]; then
410+
go_test_flags+=("-v" "-json")
411+
fi
412+
413+
local failures=()
414+
# execution of tests against packages:
415+
for pkg in "${packages[@]}"; do
416+
local cmd=(go test "${go_test_flags[@]}" "${pkg}" "${args[@]}")
417+
418+
local junit_filename_prefix
419+
junit_filename_prefix=$(get_junit_filename_prefix "${junit_report_dir}")
420+
421+
if ! run env ETCD_VERIFY="${ETCD_VERIFY}" "${cmd[@]}" | tee ${junit_filename_prefix:+"${junit_filename_prefix}.stdout"} | grep --binary-files=text "${go_test_grep_pattern}" ; then
422+
if [ "${keep_going}" = "false" ]; then
423+
produce_junit_xmlreport "${junit_filename_prefix}"
424+
return 2
425+
else
426+
failures+=("${pkg}")
427+
fi
428+
fi
429+
produce_junit_xmlreport "${junit_filename_prefix}"
430+
done
431+
432+
if [ -n "${failures[*]}" ]; then
433+
log_error -e "FAIL: Tests for following packages failed:\\n ${failures[*]}"
434+
return 2
435+
fi
436+
}
437+
338438
#### Other ####
339439

340440
# tool_exists [tool] [instruction]

0 commit comments

Comments
 (0)