run-tests.sh 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. #!/bin/bash
  2. # Test the current package under a different kernel.
  3. # Requires virtme and qemu to be installed.
  4. set -eu
  5. set -o pipefail
  6. if [[ "${1:-}" = "--in-vm" ]]; then
  7. shift
  8. readonly home="$(mktemp --directory)"
  9. mount -t bpf bpf /sys/fs/bpf
  10. export CGO_ENABLED=0
  11. export HOME="$home"
  12. echo Running tests...
  13. /usr/local/bin/go test -mod=vendor -coverprofile="$1/coverage.txt" -covermode=atomic -v ./...
  14. touch "$1/success"
  15. exit 0
  16. fi
  17. # Force Go modules, so that vendoring and building are easier.
  18. export GO111MODULE=on
  19. # Pull all dependencies, so that we can run tests without the
  20. # vm having network access.
  21. go mod vendor
  22. # Use sudo if /dev/kvm isn't accessible by the current user.
  23. sudo=""
  24. if [[ ! -r /dev/kvm || ! -w /dev/kvm ]]; then
  25. sudo="sudo"
  26. fi
  27. readonly sudo
  28. readonly kernel_version="${1:-}"
  29. if [[ -z "${kernel_version}" ]]; then
  30. echo "Expecting kernel version as first argument"
  31. exit 1
  32. fi
  33. readonly kernel="linux-${kernel_version}.bz"
  34. readonly output="$(mktemp -d)"
  35. readonly tmp_dir="$(mktemp -d)"
  36. test -e "${tmp_dir}/${kernel}" || {
  37. echo Fetching ${kernel}
  38. curl --fail -L "https://github.com/newtools/ci-kernels/blob/master/${kernel}?raw=true" -o "${tmp_dir}/${kernel}"
  39. }
  40. echo Testing on ${kernel_version}
  41. $sudo virtme-run --kimg "${tmp_dir}/${kernel}" --memory 256M --pwd --rwdir=/run/output="${output}" --script-sh "$(realpath "$0") --in-vm /run/output"
  42. if [[ ! -e "${output}/success" ]]; then
  43. echo "Test failed on ${kernel_version}"
  44. exit 1
  45. else
  46. echo "Test successful on ${kernel_version}"
  47. if [[ -v CODECOV_TOKEN ]]; then
  48. curl --fail -s https://codecov.io/bash > "${tmp_dir}/codecov.sh"
  49. chmod +x "${tmp_dir}/codecov.sh"
  50. "${tmp_dir}/codecov.sh" -f "${output}/coverage.txt"
  51. fi
  52. fi
  53. $sudo rm -r "${output}"