test 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. #!/usr/bin/env bash
  2. #
  3. # Run all CNI tests
  4. # ./test
  5. # ./test -v
  6. #
  7. # Run tests for one package
  8. # PKG=./plugins/ipam/dhcp ./test
  9. #
  10. set -e
  11. source ./build
  12. TESTABLE="plugins/ipam/dhcp plugins/ipam/host-local plugins/main/loopback pkg/invoke pkg/ns pkg/skel pkg/types pkg/utils plugins/main/ipvlan plugins/main/macvlan plugins/main/bridge"
  13. FORMATTABLE="$TESTABLE libcni pkg/ip pkg/ipam pkg/testutils plugins/ipam/host-local plugins/main/bridge plugins/meta/flannel plugins/meta/tuning"
  14. # user has not provided PKG override
  15. if [ -z "$PKG" ]; then
  16. TEST=$TESTABLE
  17. FMT=$FORMATTABLE
  18. # user has provided PKG override
  19. else
  20. # strip out slashes and dots from PKG=./foo/
  21. TEST=${PKG//\//}
  22. TEST=${TEST//./}
  23. # only run gofmt on packages provided by user
  24. FMT="$TEST"
  25. fi
  26. # split TEST into an array and prepend REPO_PATH to each local package
  27. split=(${TEST// / })
  28. TEST=${split[@]/#/${REPO_PATH}/}
  29. echo -n "Running tests "
  30. function testrun {
  31. sudo -E bash -c "umask 0; PATH=$GOROOT/bin:$GOBIN:$PATH go test -covermode set $@"
  32. }
  33. if [ ! -z "${COVERALLS}" ]; then
  34. echo "with coverage profile generation..."
  35. i=0
  36. for t in ${TEST}; do
  37. testrun "-coverprofile ${i}.coverprofile ${t}"
  38. i=$((i+1))
  39. done
  40. gover
  41. goveralls -service=travis-ci -coverprofile=gover.coverprofile -repotoken=$COVERALLS_TOKEN
  42. else
  43. echo "without coverage profile generation..."
  44. testrun "${TEST}"
  45. fi
  46. echo "Checking gofmt..."
  47. fmtRes=$(gofmt -l $FMT)
  48. if [ -n "${fmtRes}" ]; then
  49. echo -e "gofmt checking failed:\n${fmtRes}"
  50. exit 255
  51. fi
  52. echo "Checking govet..."
  53. vetRes=$(go vet $TEST)
  54. if [ -n "${vetRes}" ]; then
  55. echo -e "govet checking failed:\n${vetRes}"
  56. exit 255
  57. fi
  58. echo "Checking license header..."
  59. licRes=$(
  60. for file in $(find . -type f -iname '*.go' ! -path './vendor/*'); do
  61. head -n1 "${file}" | grep -Eq "(Copyright|generated)" || echo -e " ${file}"
  62. done
  63. )
  64. if [ -n "${licRes}" ]; then
  65. echo -e "license header checking failed:\n${licRes}"
  66. exit 255
  67. fi
  68. echo "Success"