| 1 | #!/usr/bin/env bash |
| 2 | # |
| 3 | # Run CNI plugin tests. |
| 4 | # |
| 5 | # This needs sudo, as we'll be creating net interfaces. |
| 6 | # |
| 7 | set -e |
| 8 | |
| 9 | # switch into the repo root directory |
| 10 | cd "$(dirname "$0")" |
| 11 | |
| 12 | # Build all plugins before testing |
| 13 | . ./build_linux.sh |
| 14 | |
| 15 | echo "Running tests" |
| 16 | |
| 17 | testrun() { |
| 18 | sudo -E sh -c "umask 0; PATH=${GOPATH}/bin:$(pwd)/bin:${PATH} go test -race $*" |
| 19 | } |
| 20 | |
| 21 | ensure_sysctl() { |
| 22 | local key |
| 23 | local val |
| 24 | local existing |
| 25 | |
| 26 | key="$1" |
| 27 | val="$2" |
| 28 | existing="$(sysctl -ben "$key")" |
| 29 | |
| 30 | sysctl -r |
| 31 | |
| 32 | if [ "$val" -ne "$existing" ]; then |
| 33 | echo "sudo sysctl -we '$key'='$val'" |
| 34 | sudo sysctl -we "$key"="$val" |
| 35 | fi |
| 36 | } |
| 37 | |
| 38 | COVERALLS=${COVERALLS:-""} |
| 39 | |
| 40 | if [ -n "${COVERALLS}" ]; then |
| 41 | echo "with coverage profile generation..." |
| 42 | else |
| 43 | echo "without coverage profile generation..." |
| 44 | fi |
| 45 | |
| 46 | PKG=${PKG:-$(go list ./... | xargs echo)} |
| 47 | |
| 48 | i=0 |
| 49 | for t in ${PKG}; do |
| 50 | if [ -n "${COVERALLS}" ]; then |
| 51 | COVERFLAGS="-covermode atomic -coverprofile ${i}.coverprofile" |
| 52 | fi |
| 53 | echo "${t}" |
| 54 | testrun "${COVERFLAGS:-""} ${t}" |
| 55 | i=$((i+1)) |
| 56 | done |
| 57 | |
| 58 | # Run the pkg/ns tests as non root user |
| 59 | mkdir -p /tmp/cni-rootless |
| 60 | ensure_sysctl kernel.unprivileged_userns_clone 1 |
| 61 | ensure_sysctl kernel.apparmor_restrict_unprivileged_userns 0 |
| 62 | |
| 63 | (export XDG_RUNTIME_DIR=/tmp/cni-rootless; cd pkg/ns/; unshare -rmn go test) |