Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 6 additions & 9 deletions scripts/test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -125,16 +125,13 @@ function build_pass {

################# REGULAR TESTS ################################################

# run_unit_tests [pkgs] runs unit tests for a current module and givesn set of [pkgs]
function run_unit_tests {
local pkgs="${1:-./...}"
shift 1
# shellcheck disable=SC2068 #For context see - https://github.com/etcd-io/etcd/pull/16433#issuecomment-1684312755
go_test "${pkgs}" "parallel" : -short -timeout="${TIMEOUT:-3m}" ${COMMON_TEST_FLAGS[@]:-} ${RUN_ARG[@]:-} "$@"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Eventually you will remove the function go_test separately?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, similar comment to #20872 (comment)

}

function unit_pass {
run_for_modules run_unit_tests "$@"
run_for_all_workspace_modules \
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I see there is also a function run_for_workspace_modules, which seems confusing (please at least ensure adding sufficient comment to clarify the difference, which can be addressed separately). What's the difference between the them? Why use this one, not the other one?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Once we finish migrating all the Makefile targets, we can clean up the old functions. Right now, as I'm doing a staged migration, I can't do a cleanup yet.

run_go_tests -short \
-timeout="${TIMEOUT:-3m}" \
"${COMMON_TEST_FLAGS[@]}" \
"${RUN_ARG[@]}" \
"$@"
}

function integration_extra {
Expand Down
101 changes: 101 additions & 0 deletions scripts/test_lib.sh
Original file line number Diff line number Diff line change
Expand Up @@ -206,6 +206,17 @@ function run_for_modules {
fi
}

function get_junit_filename_prefix {
local junit_report_dir="$1"
if [[ -z "${junit_report_dir}" ]]; then
echo ""
return
fi

mkdir -p "${junit_report_dir}"
mktemp --dry-run "${junit_report_dir}/junit_XXXXXXXXXX"
}

junitFilenamePrefix() {
if [[ -z "${JUNIT_REPORT_DIR:-}" ]]; then
echo ""
Expand Down Expand Up @@ -335,6 +346,96 @@ function go_test {
fi
}

# run_go_tests_expanding_packages [arguments to pass to go test]
# Expands the packages in the list of arguments, i.e. ./... into a list of
# packages for that given module. Then, it calls run_go_tests with the expanded
# packages.
function run_go_tests_expanding_packages {
local packages=()
local args=()
for arg in "$@"; do
if [[ "${arg}" =~ ./ ]]; then
packages+=("${arg}")
else
args+=("${arg}")
fi
done

# Expanding patterns (like ./...) into list of packages
local unpacked_packages=()
while IFS='' read -r line; do unpacked_packages+=("$line"); done < <(
go list "${packages[@]}"
)

run_go_tests "${unpacked_packages[@]}" "${args[@]}"
}
Comment on lines +349 to +371
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This function isn't used at all. Why add it?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's added for compatibility with the old go_tests function. It's going to be used by integration tests.


# run_go_test [arguments to pass to go test]
# The following environment variables affect how the tests run:
# - KEEP_GOING_TESTS: If set to true it will keep executing tests even after
# a failure. It collects all failures and reports them at
# the end, if there are failures the return code is 2.
# Defaults to false.
# - JUNIT_REPORT_DIR/ARTIFACTS: Enables collecting JUnit XML reports.
# - VERBOSE: Sets a verbose output.
#
# Example:
# KEEP_GOING_TESTS=true run_go_tests "./..." --short
#
# The function returns != 0 code in case of test failure.
function run_go_tests {
local go_test_flags=()

local packages=()
local args=()
for arg in "$@"; do
if [[ "${arg}" =~ ^\./ || "${arg}" =~ ^go\.etcd\.io/etcd ]]; then
packages+=("${arg}")
else
args+=("${arg}")
fi
done

local keep_going=${KEEP_GOING_TESTS:-false}

# If JUNIT_REPORT_DIR is unset, and ARTIFACTS is set, then have them match.
local junit_report_dir=${JUNIT_REPORT_DIR:-${ARTIFACTS:-}}

local go_test_grep_pattern=".*"
if [[ -n "${junit_report_dir}" ]]; then
# Show only summary lines by matching lines like "status package/test"
go_test_grep_pattern="^[^[:space:]]\+[[:space:]]\+[^[:space:]]\+/[^[[:space:]]\+"
fi

if [[ -n "${junit_report_dir}" || "${VERBOSE:-}" == "1" ]]; then
go_test_flags+=("-v" "-json")
fi

local failures=()
# execution of tests against packages:
for pkg in "${packages[@]}"; do
local cmd=(go test "${go_test_flags[@]}" "${pkg}" "${args[@]}")

local junit_filename_prefix
junit_filename_prefix=$(get_junit_filename_prefix "${junit_report_dir}")

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
if [ "${keep_going}" = "true" ]; then
failures+=("${pkg}")
else
produce_junit_xmlreport "${junit_filename_prefix}"
return 2
fi
fi
produce_junit_xmlreport "${junit_filename_prefix}"
done

if [ -n "${failures[*]}" ]; then
log_error -e "FAIL: Tests for following packages failed:\\n ${failures[*]}"
return 2
fi
}

#### Other ####

# tool_exists [tool] [instruction]
Expand Down