gopkg.in/ubuntu-core/snappy.v0@v0.0.0-20210902073436-25a8614f10a6/run-checks (about) 1 #!/bin/sh -eu 2 3 export LANG=C.UTF-8 4 export LANGUAGE=en 5 6 if command -v goctest >/dev/null; then 7 goctest="goctest" 8 else 9 goctest="go test" 10 fi 11 COVERMODE=${COVERMODE:-atomic} 12 13 if [ -z "${GITHUB_WORKFLOW:-}" ]; then 14 # when *not* running inside github, ensure we use go-1.13 by default 15 export PATH=/usr/lib/go-1.13/bin:"${PATH}" 16 fi 17 18 # add workaround for https://github.com/golang/go/issues/24449 19 if [ "$(uname -m)" = "s390x" ]; then 20 if go version | grep -q go1.10; then 21 echo "covermode 'atomic' crashes on s390x with go1.10, reseting " 22 echo "to 'set'. see https://github.com/golang/go/issues/24449" 23 COVERMODE="set" 24 fi 25 fi 26 27 export GOPATH="${GOPATH:-$(realpath "$(dirname "$0")"/../../../../)}" 28 export PATH="$PATH:${GOPATH%%:*}/bin" 29 30 short= 31 32 STATIC= 33 UNIT= 34 35 case "${1:-all}" in 36 all) 37 STATIC=1 38 UNIT=1 39 ;; 40 --static) 41 STATIC=1 42 ;; 43 --unit) 44 UNIT=1 45 ;; 46 --short-unit) 47 UNIT=1 48 short=1 49 ;; 50 *) 51 echo "Wrong flag ${1}. To run a single suite use --static, --unit, --spread." 52 exit 1 53 esac 54 55 CURRENTTRAP="true" 56 EXIT_CODE=99 57 58 store_exit_code() { 59 EXIT_CODE=$? 60 } 61 62 exit_with_exit_code() { 63 exit $EXIT_CODE 64 } 65 66 addtrap() { 67 CURRENTTRAP="$CURRENTTRAP ; $1" 68 # shellcheck disable=SC2064 69 trap "store_exit_code; $CURRENTTRAP ; exit_with_exit_code" EXIT 70 } 71 72 endmsg() { 73 if [ $EXIT_CODE -eq 0 ]; then 74 p="success.txt" 75 m="All good, what could possibly go wrong." 76 else 77 p="failure.txt" 78 m="Crushing failure and despair." 79 fi 80 echo 81 if [ -t 1 ] && [ -z "$STATIC" ]; then 82 cat "data/$p" 83 else 84 echo "$m" 85 fi 86 } 87 addtrap endmsg 88 89 # Append the coverage profile of a package to the project coverage. 90 append_coverage() ( 91 profile="$1" 92 if [ -f "$profile" ]; then 93 grep -v "^mode:" -- "$profile" >> .coverage/coverage.out || true 94 rm "$profile" 95 fi 96 ) 97 98 missing_interface_spread_test() { 99 snap_yaml="tests/lib/snaps/test-snapd-policy-app-consumer/meta/snap.yaml" 100 core_snap_yaml="tests/lib/snaps/test-snapd-policy-app-provider-core/meta/snap.yaml" 101 classic_snap_yaml="tests/lib/snaps/test-snapd-policy-app-provider-classic/meta/snap.yaml" 102 for iface in $(go run ./tests/lib/list-interfaces.go) ; do 103 search="plugs: \\[ $iface \\]" 104 case "$iface" in 105 bool-file|gpio|pwm|dsp|netlink-driver|hidraw|i2c|iio|serial-port|spi) 106 # skip gadget provided interfaces for now 107 continue 108 ;; 109 dbus|content) 110 search="interface: $iface" 111 ;; 112 autopilot) 113 search='plugs: \[ autopilot-introspection \]' 114 ;; 115 esac 116 if ! grep -q "$search" "$snap_yaml" ; then 117 echo "Missing high-level test for interface '$iface'. Please add to:" 118 echo "* $snap_yaml" 119 echo "* $core_snap_yaml (if needed)" 120 echo "* $classic_snap_yaml (if needed)" 121 exit 1 122 fi 123 done 124 } 125 126 127 if [ "$STATIC" = 1 ]; then 128 ./get-deps.sh 129 130 # Run static tests. 131 echo Checking docs 132 ./mdlint.py ./*.md docs/*.md 133 134 # XXX: remove once we can use an action, see workflows/test.yaml for 135 # details why we still use this script 136 if [ -n "${TRAVIS_PULL_REQUEST:-}" ] && [ "${TRAVIS_PULL_REQUEST:-}" != "false" ]; then 137 echo Checking pull request summary 138 ./check-pr-title.py "$TRAVIS_PULL_REQUEST" 139 fi 140 141 if [ -z "${SKIP_GOFMT:-}" ]; then 142 echo Checking formatting 143 fmt="" 144 for dir in $(go list -f '{{.Dir}}' ./... | grep -v '/\(c-\)\?vendor/' ); do 145 # skip vendor packages 146 s="$(${GOFMT:-gofmt} -s -d "$dir" || true)" 147 if [ -n "$s" ]; then 148 fmt="$s\\n$fmt" 149 fi 150 done 151 if [ -n "$fmt" ]; then 152 echo "Formatting wrong in following files:" 153 # shellcheck disable=SC2001 154 echo "$fmt" | sed -e 's/\\n/\n/g' 155 exit 1 156 fi 157 fi 158 159 # go vet 160 echo Running vet 161 go list ./... | grep -v '/vendor/' | xargs go vet 162 163 echo 'Checking for usages of http.Status*' 164 got="" 165 for dir in $(go list -f '{{.Dir}}' ./... | grep -v '/vendor/' ); do 166 s="$(grep -nP 'http\.Status(?!Text)' "$dir"/*.go || true)" 167 if [ -n "$s" ]; then 168 got="$s\\n$got" 169 fi 170 done 171 172 if [ -n "$got" ]; then 173 echo 'Usages of http.Status*, we prefer the numeric values directly:' 174 echo "$got" 175 exit 1 176 fi 177 178 echo "Checking for direct usages of math/rand" 179 got="" 180 for dir in $(go list -f '{{.Dir}}' ./... | grep -v '/vendor/' ); do 181 # shellcheck disable=SC2063 182 s="$(grep -nP --exclude '*_test.go' --exclude 'randutil/*.go' math/rand "$dir"/*.go || true)" 183 if [ -n "$s" ]; then 184 got="$s\\n$got" 185 fi 186 done 187 188 if [ -n "$got" ]; then 189 echo 'Direct usages of math/rand, we prefer randutil:' 190 echo "$got" 191 exit 1 192 fi 193 194 if command -v shellcheck >/dev/null; then 195 echo Checking shell scripts... 196 ( git ls-files -z 2>/dev/null || 197 find . \( -name .git -o -name vendor -o -name c-vendor \) -prune -o -print0 ) | 198 xargs -0 file -N | 199 awk -F": " '$2~/shell.script/{print $1}' | 200 xargs shellcheck -x 201 regexp='GOPATH(?!%%:\*)(?!:)[^= ]*/' 202 if grep -qPr --exclude HACKING.md --exclude 'Makefile.*' --exclude-dir .git --exclude-dir vendor "$regexp"; then 203 echo "Using GOPATH as if it were a single entry and not a list:" 204 grep -PHrn -C1 --color=auto --exclude HACKING.md --exclude 'Makefile.*' --exclude-dir .git --exclude-dir vendor "$regexp" 205 echo "Use GOHOME, or {GOPATH%%:*}, instead." 206 exit 1 207 fi 208 unset regexp 209 # also run spread-shellcheck 210 ./spread-shellcheck spread.yaml tests 211 fi 212 213 echo "Checking spelling errors" 214 if ! command -v misspell >/dev/null; then 215 go get -u github.com/client9/misspell/cmd/misspell 216 fi 217 # FIXME: auter is only misspelled in the changelog so we should fix there 218 # PROCES is used in the seccomp tests (PRIO_PROCES{,S,SS}) 219 # exportfs is used in the nfs-support test 220 MISSPELL_IGNORE="auther,PROCES,PROCESSS,proces,processs,exportfs" 221 git ls-files -z -- . ':!:./po' ':!:./vendor' ':!:./c-vendor' ':!:./cmd/libsnap-confine-private/bpf/vendor' | 222 xargs -0 misspell -error -i "$MISSPELL_IGNORE" 223 224 if dpkg --compare-versions "$(go version | awk '$3 ~ /^go[0-9]/ {print substr($3, 3)}')" ge 1.12; then 225 echo "Checking for ineffective assignments" 226 if ! command -v ineffassign >/dev/null; then 227 go get -u github.com/gordonklaus/ineffassign 228 fi 229 # ineffassign knows about ignoring vendor/ \o/ 230 ineffassign ./... 231 fi 232 233 echo "Checking for naked returns" 234 if ! command -v nakedret >/dev/null; then 235 go get -u github.com/alexkohler/nakedret 236 fi 237 got=$(go list ./... | grep -v '/osutil/udev/' | grep -v '/vendor/' | xargs nakedret 2>&1) 238 if [ -n "$got" ]; then 239 echo "$got" 240 if [ -z "${SKIP_NAKEDRET:-}" ]; then 241 exit 1 242 else 243 echo "Ignoring nakedret errors as requested" 244 fi 245 fi 246 247 echo "Checking all interfaces have minimal spread test" 248 missing_interface_spread_test 249 250 echo "Checking for incorrect multiline strings in spread tests" 251 badmultiline=$(find tests -name 'task.yaml' -print0 -o -name 'spread.yaml' -print0 | \ 252 xargs -0 grep -R -n -E '(restore*|prepare*|execute|debug):\s*$' || true) 253 if [ -n "$badmultiline" ]; then 254 echo "Incorrect multiline strings at the following locations:" 255 echo "$badmultiline" 256 exit 1 257 fi 258 259 echo "Checking for potentially incorrect use of MATCH -v" 260 badMATCH=$(find tests -name 'task.yaml' -print0 -o -name 'spread.yaml' -print0 | \ 261 xargs -0 grep -R -n -E 'MATCH +-v' || true) 262 if [ -n "$badMATCH" ]; then 263 echo "Potentially incorrect use of MATCH -v at the following locations:" 264 echo "$badMATCH" 265 exit 1 266 fi 267 268 # FIXME: re-add staticcheck with a matching version for the used go-version 269 270 if [ -z "${SKIP_TESTS_FORMAT_CHECK-}" ] || [ "$SKIP_TESTS_FORMAT_CHECK" = 0 ]; then 271 echo "Checking tests formatting" 272 ./tests/utils/check-test-format ./tests 273 fi 274 fi 275 276 if [ "$UNIT" = 1 ]; then 277 ./get-deps.sh 278 279 echo "Show go version" 280 command -v go 281 go version 282 283 tags= 284 if [ -n "${GO_BUILD_TAGS-}" ]; then 285 echo "Using build tags: $GO_BUILD_TAGS" 286 tags="-tags $GO_BUILD_TAGS" 287 fi 288 289 echo Building 290 # shellcheck disable=SC2086 291 go build -v $tags github.com/snapcore/snapd/... 292 293 # tests 294 echo Running tests from "$PWD" 295 if [ "$short" = 1 ]; then 296 # shellcheck disable=SC2046,SC2086 297 GOTRACEBACK=1 $goctest $tags -short -timeout 5m $(go list ./... | grep -v '/vendor/' ) 298 else 299 coverage="" 300 if [ -z "${SKIP_COVERAGE-}" ]; then 301 coverage="-coverprofile=.coverage/coverage.out -covermode=$COVERMODE" 302 # Prepare the coverage output profile. 303 rm -rf .coverage 304 mkdir .coverage 305 echo "mode: $COVERMODE" > .coverage/coverage.out 306 else 307 echo "Skipping test coverage" 308 fi 309 310 if command -v dpkg >/dev/null && dpkg --compare-versions "$(go version | awk '$3 ~ /^go[0-9]/ {print substr($3, 3)}')" ge 1.10; then 311 # shellcheck disable=SC2046,SC2086 312 GOTRACEBACK=1 $goctest $tags -timeout 5m $coverage $(go list ./... | grep -v '/vendor/' ) 313 else 314 for pkg in $(go list ./... | grep -v '/vendor/' ); do 315 # shellcheck disable=SC2086 316 GOTRACEBACK=1 go test $tags -timeout 5m -i "$pkg" 317 if [ -z "${SKIP_COVERAGE-}" ]; then 318 # shellcheck disable=SC2086 319 GOTRACEBACK=1 $goctest $tags -timeout 5m $coverage "$pkg" 320 append_coverage .coverage/profile.out 321 fi 322 done 323 fi 324 fi 325 326 # python unit test for mountinfo.query and version-compare 327 command -v python2 && python2 ./tests/lib/tools/mountinfo.query --run-unit-tests 328 command -v python3 && python3 ./tests/lib/tools/mountinfo.query --run-unit-tests 329 command -v python2 && python2 ./tests/lib/tools/version-compare --run-unit-tests 330 command -v python3 && python3 ./tests/lib/tools/version-compare --run-unit-tests 331 fi 332 333 UNCLEAN="$(git status -s|grep '^??')" || true 334 SKIP_UNCLEAN=${SKIP_UNCLEAN=} 335 if [ -n "$UNCLEAN" ] && [ -z "$SKIP_UNCLEAN" ]; then 336 cat <<EOF 337 338 There are files left in the git tree after the tests: 339 340 $UNCLEAN 341 EOF 342 exit 1 343 fi 344 345 if [ -n "${SKIP_DIRTY_CHECK-}" ]; then 346 exit 0 347 fi 348 349 if git describe --always --dirty | grep -q dirty; then 350 echo "Build tree is dirty" 351 git diff 352 exit 1 353 fi