test-unit 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. #!/bin/bash
  2. set -e
  3. DEST=$1
  4. : ${PARALLEL_JOBS:=$(nproc 2>/dev/null || echo 1)} # if nproc fails (usually because we don't have it), let's not parallelize by default
  5. RED=$'\033[31m'
  6. GREEN=$'\033[32m'
  7. TEXTRESET=$'\033[0m' # reset the foreground colour
  8. # Run Docker's test suite, including sub-packages, and store their output as a bundle
  9. # If $TESTFLAGS is set in the environment, it is passed as extra arguments to 'go test'.
  10. # You can use this to select certain tests to run, eg.
  11. #
  12. # TESTFLAGS='-run ^TestBuild$' ./hack/make.sh test-unit
  13. #
  14. bundle_test_unit() {
  15. {
  16. date
  17. # Run all the tests if no TESTDIRS were specified.
  18. if [ -z "$TESTDIRS" ]; then
  19. TESTDIRS=$(find_dirs '*_test.go')
  20. fi
  21. (
  22. export LDFLAGS="$LDFLAGS $LDFLAGS_STATIC_DOCKER"
  23. export TESTFLAGS
  24. export HAVE_GO_TEST_COVER
  25. export DEST
  26. if command -v parallel &> /dev/null; then
  27. # accomodate parallel to be able to access variables
  28. export SHELL="$BASH"
  29. export HOME="$(mktemp -d)"
  30. mkdir -p "$HOME/.parallel"
  31. touch "$HOME/.parallel/ignored_vars"
  32. # some hack to export array variables
  33. export BUILDFLAGS_FILE="$HOME/buildflags_file"
  34. ( IFS=$'\n'; echo "${BUILDFLAGS[*]}" ) > "$BUILDFLAGS_FILE"
  35. echo "$TESTDIRS" | parallel --jobs "$PARALLEL_JOBS" --env _ "$(dirname "$BASH_SOURCE")/.go-compile-test-dir"
  36. rm -rf "$HOME"
  37. else
  38. # aww, no "parallel" available - fall back to boring
  39. for test_dir in $TESTDIRS; do
  40. "$(dirname "$BASH_SOURCE")/.go-compile-test-dir" "$test_dir" || true
  41. # don't let one directory that fails to build tank _all_ our tests!
  42. done
  43. fi
  44. )
  45. echo "$TESTDIRS" | go_run_test_dir
  46. }
  47. }
  48. go_run_test_dir() {
  49. TESTS_FAILED=()
  50. while read dir; do
  51. echo
  52. echo '+ go test' $TESTFLAGS "${DOCKER_PKG}${dir#.}"
  53. precompiled="$DEST/precompiled/$dir.test$(binary_extension)"
  54. if ! ( cd "$dir" && "$precompiled" $TESTFLAGS ); then
  55. TESTS_FAILED+=("$dir")
  56. echo
  57. echo "${RED}Tests failed: $dir${TEXTRESET}"
  58. sleep 1 # give it a second, so observers watching can take note
  59. fi
  60. done
  61. echo
  62. echo
  63. echo
  64. # if some tests fail, we want the bundlescript to fail, but we want to
  65. # try running ALL the tests first, hence TESTS_FAILED
  66. if [ "${#TESTS_FAILED[@]}" -gt 0 ]; then
  67. echo "${RED}Test failures in: ${TESTS_FAILED[@]}${TEXTRESET}"
  68. echo
  69. false
  70. else
  71. echo "${GREEN}Test success${TEXTRESET}"
  72. echo
  73. true
  74. fi
  75. }
  76. bundle_test_unit 2>&1 | tee -a $DEST/test.log