diff --git a/.ci/go-test.sh b/.ci/go-test.sh index 6fae379d8..47db5b8c1 100755 --- a/.ci/go-test.sh +++ b/.ci/go-test.sh @@ -5,92 +5,7 @@ set -e -script_dir=$(cd `dirname $0`; pwd) -root_dir=`dirname $script_dir` +cidir=$(dirname "$0") +source "${cidir}/lib.sh" -test_packages="." - -# Set default test run timeout value. -# -# CC_GO_TEST_TIMEOUT can be set to any value accepted by -# "go test -timeout X" -timeout_value=${CC_GO_TEST_TIMEOUT:-10s} - -go_test_flags="-v -race -timeout $timeout_value" -cov_file="profile.cov" -tmp_cov_file="profile_tmp.cov" - -# Run a command as either root or the current user (which might still be root). -# -# If the first argument is "root", run using sudo, else run as normal. -# All arguments after the first will be treated as the command to run. -function run_as_user -{ - user="$1" - shift - cmd=$* - - if [ "$user" = root ] - then - # use a shell to ensure PATH is correct. - sudo -E PATH="$PATH" sh -c "$cmd" - else - $cmd - fi -} - -function test_html_coverage -{ - html_report="coverage.html" - - test_coverage - - go tool cover -html="${cov_file}" -o "${html_report}" - rm -f "${cov_file}" - - run_as_user "current" chmod 644 "${html_report}" -} - -function test_coverage -{ - echo "mode: atomic" > "$cov_file" - - if [ $(id -u) -eq 0 ] - then - echo >&2 "WARNING: Already running as root so will not re-run tests as non-root user." - echo >&2 "WARNING: As a result, only a subset of tests will be run" - echo >&2 "WARNING: (run this script as a non-privileged to ensure all tests are run)." - users="current" - else - # Run the unit-tests *twice* (since some must run as root and - # others must run as non-root), combining the resulting test - # coverage files. - users="current root" - fi - - for pkg in $test_packages; do - for user in $users; do - printf "INFO: Running 'go test' as %s user on packages '%s' with flags '%s'\n" "$user" "$test_packages" "$go_test_flags" - - run_as_user "$user" go test $go_test_flags -covermode=atomic -coverprofile="$tmp_cov_file" $pkg - if [ -f "${tmp_cov_file}" ]; then - run_as_user "$user" chmod 644 "$tmp_cov_file" - tail -n +2 "$tmp_cov_file" >> "$cov_file" - run_as_user "$user" rm -f "$tmp_cov_file" - fi - done - done -} - -function test_local -{ - go test $go_test_flags $test_packages -} - -if [ "$1" = "html-coverage" ]; then - test_html_coverage -elif [ "$CI" = "true" ]; then - test_coverage -else - test_local -fi +run_go_test diff --git a/.ci/lib.sh b/.ci/lib.sh index c5c8582f4..8292508c1 100644 --- a/.ci/lib.sh +++ b/.ci/lib.sh @@ -23,3 +23,9 @@ run_static_checks() clone_tests_repo bash "$tests_repo_dir/.ci/static-checks.sh" } + +run_go_test() +{ + clone_tests_repo + bash "$tests_repo_dir/.ci/go-test.sh" +}