Files
snapd/run-checks
Sergio Cazzolato 5d6b7ab6a5 tests: include new testing tools and utils (#11399)
* Squashed 'tests/lib/external/snapd-testing-tools/' changes from dc6be12e76..575f5d2016

575f5d2016 Merge pull request #13 from snapcore/new-utils-section
4701612434 avoid installing and removing snapd in ubuntu focal
4d0317633d shellcheck in xenial is not needed
d95cb2e627 Moving some tools to the new utils section
839a0ce808 Merge pull request #10 from snapcore/new-spread-shellcheck-tool
5d5efd6fbe Merge pull request #12 from snapcore/run-shellchek-in-old-version
497b7d691f Force testing shellchek with versions 0.8 and 0.7
b3cb5a5a0a Update the copyright
872b64e05b Merge pull request #11 from Meulengracht/main
aa8799d0f8 tools/repack-kernel: fix SC2061
a44a987381 Merge pull request #9 from snapcore/check-spread-yaml-files
5cfa7cb84d New shellcheck for spread tasks
a9f6e09e0d Merge pull request #8 from Meulengracht/main
cd7926a85f New check-test-format tool
210b232d9c tools/repack-kernel: fix additional shellchecks
ac67796b5c Merge pull request #7 from Meulengracht/main
13fd7270b7 tools/repack-kernel: PR feedback comments
3844702a17 Merge pull request #6 from Meulengracht/main
4afdeefe42 tests/repack-kernel: move restore above execute to pass ./run-checks
e755eca0f8 Merge pull request #5 from Meulengracht/main
6199fc48f9 tools: add core22 targets in os.query
057919c07a Merge pull request #4 from mvo5/remove-ubuntu-21.04-eol
26b35af9b3 Merge pull request #1 from Meulengracht/main
b0fa82117a many: remove Ubuntu 21.04 (hirsute), it's EOL
878fd76976 repack-kernel: remove some cds and add a newline
4b84abd26b repack-kernel: fix issue in test related to wrong help message pattern
2e7fd675a7 repack-kernel: additional fixes to error messages. additional failure tests
545d72ef20 repack-kernel: additional fixes to tool based on feedback, additional tests
da1e43b4bb repack-kernel: change the install apt command for initramfs
131bcd11b2 repack-kernel: fix prepare command and error message having to much copy-pasta
9b2aef208f repack-kernel: add more tests, fix setup command
b98c024146 review feedback
5b76f3fe99 remove nested.sh, restructure repack-kernel and add test
0e5b5052e3 nested.sh: add missing command for waiting for snap command
9591a6e700 nested.sh: fix issues with OVMF
4db61f2b13 remove scripts that were hard to reuse, handle missing variables
9b956243e1 use env for path instead of hardcoding
b8e9d73349 core: add shared shell scripts for building image and controlling the L2 VM

git-subtree-dir: tests/lib/external/snapd-testing-tools
git-subtree-split: 575f5d201619fc18f83c404222fc98e2ff696b04

* Usinr new utils and tools from the snapd-testing-tools project

* Squashed 'tests/lib/external/snapd-testing-tools/' changes from 575f5d2016..61a4c3142d

61a4c3142d Merge pull request #14 from snapcore/include-support-s390x
3a1a65f6e1 Update the comments about shellchek
5ddfb7e1db New support for s390x arch in os.query tool

git-subtree-dir: tests/lib/external/snapd-testing-tools
git-subtree-split: 61a4c3142d43d3e72330fd570ee16106d861ed24
2022-02-25 09:07:55 -03:00

340 lines
10 KiB
Bash
Executable File

#!/bin/sh -eu
export LANG=C.UTF-8
export LANGUAGE=en
if command -v goctest >/dev/null; then
goctest="goctest"
else
goctest="go test"
fi
COVERMODE=${COVERMODE:-atomic}
COVERAGE_SUFFIX=${GO_BUILD_TAGS:-notags}
COVERAGE_OUT=${COVERAGE_OUT:-.coverage/coverage-$COVERAGE_SUFFIX.cov}
if [ -z "${GITHUB_WORKFLOW:-}" ]; then
# when *not* running inside github, ensure we use go-1.13 by default
export PATH=/usr/lib/go-1.13/bin:"${PATH}"
fi
# add workaround for https://github.com/golang/go/issues/24449
if [ "$(uname -m)" = "s390x" ]; then
if go version | grep -q go1.10; then
echo "covermode 'atomic' crashes on s390x with go1.10, reseting "
echo "to 'set'. see https://github.com/golang/go/issues/24449"
COVERMODE="set"
fi
fi
export GOPATH="${GOPATH:-$(realpath "$(dirname "$0")"/../../../../)}"
export PATH="$PATH:${GOPATH%%:*}/bin"
short=
STATIC=
UNIT=
case "${1:-all}" in
all)
STATIC=1
UNIT=1
;;
--static)
STATIC=1
;;
--unit)
UNIT=1
;;
--short-unit)
UNIT=1
short=1
;;
*)
echo "Wrong flag ${1}. To run a single suite use --static, --unit."
exit 1
esac
CURRENTTRAP="true"
EXIT_CODE=99
store_exit_code() {
EXIT_CODE=$?
}
exit_with_exit_code() {
exit $EXIT_CODE
}
addtrap() {
CURRENTTRAP="$CURRENTTRAP ; $1"
# shellcheck disable=SC2064
trap "store_exit_code; $CURRENTTRAP ; exit_with_exit_code" EXIT
}
endmsg() {
if [ $EXIT_CODE -eq 0 ]; then
p="success.txt"
m="All good, what could possibly go wrong."
else
p="failure.txt"
m="Crushing failure and despair."
fi
echo
if [ -t 1 ] && [ -z "$STATIC" ]; then
cat "data/$p"
else
echo "$m"
fi
}
addtrap endmsg
missing_interface_spread_test() {
snap_yaml="tests/lib/snaps/test-snapd-policy-app-consumer/meta/snap.yaml"
core_snap_yaml="tests/main/interfaces-many-snap-provided/test-snapd-policy-app-provider-core/meta/snap.yaml"
classic_snap_yaml="tests/main/interfaces-many-snap-provided/test-snapd-policy-app-provider-classic/meta/snap.yaml"
for iface in $(go run ./tests/lib/list-interfaces.go) ; do
search="plugs: \\[ $iface \\]"
case "$iface" in
bool-file|gpio|pwm|dsp|netlink-driver|hidraw|i2c|iio|serial-port|spi)
# skip gadget provided interfaces for now
continue
;;
dbus|content)
search="interface: $iface"
;;
autopilot)
search='plugs: \[ autopilot-introspection \]'
;;
esac
if ! grep -q "$search" "$snap_yaml" ; then
echo "Missing high-level test for interface '$iface'. Please add to:"
echo "* $snap_yaml"
echo "* $core_snap_yaml (if needed)"
echo "* $classic_snap_yaml (if needed)"
exit 1
fi
done
}
if [ "$STATIC" = 1 ]; then
./get-deps.sh
# Run static tests.
echo Checking docs
./mdlint.py ./*.md docs/*.md
# XXX: remove once we can use an action, see workflows/test.yaml for
# details why we still use this script
if [ -n "${GITHUB_PULL_REQUEST:-}" ] && [ "${GITHUB_PULL_REQUEST:-}" != "false" ]; then
echo Checking pull request summary
./check-pr-title.py "$GITHUB_PULL_REQUEST"
fi
# check commit author/committer name for unicode
./check-commit-email.py
if [ -z "${SKIP_GOFMT:-}" ]; then
echo Checking formatting
fmt=""
for dir in $(go list -f '{{.Dir}}' ./... | grep -v '/\(c-\)\?vendor/' ); do
# skip vendor packages
s="$(${GOFMT:-gofmt} -s -d "$dir" || true)"
if [ -n "$s" ]; then
fmt="$s\\n$fmt"
fi
done
if [ -n "$fmt" ]; then
echo "Formatting wrong in following files:"
# shellcheck disable=SC2001
echo "$fmt" | sed -e 's/\\n/\n/g'
exit 1
fi
fi
# go vet
echo Running vet
go list ./... | grep -v '/vendor/' | xargs go vet
echo 'Checking for usages of http.Status*'
got=""
for dir in $(go list -f '{{.Dir}}' ./... | grep -v '/vendor/' ); do
s="$(grep -nP 'http\.Status(?!Text)' "$dir"/*.go || true)"
if [ -n "$s" ]; then
got="$s\\n$got"
fi
done
if [ -n "$got" ]; then
echo 'Usages of http.Status*, we prefer the numeric values directly:'
echo "$got"
exit 1
fi
echo "Checking for direct usages of math/rand"
got=""
for dir in $(go list -f '{{.Dir}}' ./... | grep -v '/vendor/' ); do
# shellcheck disable=SC2063
s="$(grep -nP --exclude '*_test.go' --exclude 'randutil/*.go' math/rand "$dir"/*.go || true)"
if [ -n "$s" ]; then
got="$s\\n$got"
fi
done
if [ -n "$got" ]; then
echo 'Direct usages of math/rand, we prefer randutil:'
echo "$got"
exit 1
fi
if command -v shellcheck >/dev/null; then
exclude_tools_path=tests/lib/external/snapd-testing-tools
echo "Checking shell scripts..."
( git ls-files -z 2>/dev/null ||
find . \( -name .git -o -name vendor -o -name c-vendor \) -prune -o -print0 ) |
xargs -0 file -N |
awk -F": " '$2~/shell.script/{print $1}' |
grep -v "$exclude_tools_path" |
xargs shellcheck -x
regexp='GOPATH(?!%%:\*)(?!:)[^= ]*/'
if grep -qPr --exclude HACKING.md --exclude 'Makefile.*' --exclude-dir .git --exclude-dir vendor "$regexp"; then
echo "Using GOPATH as if it were a single entry and not a list:"
grep -PHrn -C1 --color=auto --exclude HACKING.md --exclude 'Makefile.*' --exclude-dir .git --exclude-dir vendor "$regexp"
echo "Use GOHOME, or {GOPATH%%:*}, instead."
exit 1
fi
unset regexp
# also run spread-shellcheck
./tests/lib/external/snapd-testing-tools/utils/spread-shellcheck spread.yaml tests --exclude "$exclude_tools_path"
fi
echo "Checking spelling errors"
if ! command -v misspell >/dev/null; then
go get -u github.com/client9/misspell/cmd/misspell
fi
# FIXME: auter is only misspelled in the changelog so we should fix there
# PROCES is used in the seccomp tests (PRIO_PROCES{,S,SS})
# exportfs is used in the nfs-support test
MISSPELL_IGNORE="auther,PROCES,PROCESSS,proces,processs,exportfs"
git ls-files -z -- . ':!:./po' ':!:./vendor' ':!:./c-vendor' ':!:./cmd/libsnap-confine-private/bpf/vendor' |
xargs -0 misspell -error -i "$MISSPELL_IGNORE"
if dpkg --compare-versions "$(go version | awk '$3 ~ /^go[0-9]/ {print substr($3, 3)}')" ge 1.12; then
echo "Checking for ineffective assignments"
if ! command -v ineffassign >/dev/null; then
go get -u github.com/gordonklaus/ineffassign
fi
# ineffassign knows about ignoring vendor/ \o/
ineffassign ./...
fi
echo "Checking for naked returns"
if ! command -v nakedret >/dev/null; then
go get -u github.com/alexkohler/nakedret
fi
got=$(go list ./... | grep -v '/osutil/udev/' | grep -v '/vendor/' | xargs nakedret 2>&1)
if [ -n "$got" ]; then
echo "$got"
if [ -z "${SKIP_NAKEDRET:-}" ]; then
exit 1
else
echo "Ignoring nakedret errors as requested"
fi
fi
echo "Checking all interfaces have minimal spread test"
missing_interface_spread_test
echo "Checking for incorrect multiline strings in spread tests"
badmultiline=$(find tests -name 'task.yaml' -print0 -o -name 'spread.yaml' -print0 | \
xargs -0 grep -R -n -E '(restore*|prepare*|execute|debug):\s*$' || true)
if [ -n "$badmultiline" ]; then
echo "Incorrect multiline strings at the following locations:"
echo "$badmultiline"
exit 1
fi
echo "Checking for potentially incorrect use of MATCH -v"
badMATCH=$(find tests -name 'task.yaml' -print0 -o -name 'spread.yaml' -print0 | \
xargs -0 grep -R -n -E 'MATCH +-v' || true)
if [ -n "$badMATCH" ]; then
echo "Potentially incorrect use of MATCH -v at the following locations:"
echo "$badMATCH"
exit 1
fi
# FIXME: re-add staticcheck with a matching version for the used go-version
if [ -z "${SKIP_TESTS_FORMAT_CHECK-}" ] || [ "$SKIP_TESTS_FORMAT_CHECK" = 0 ]; then
echo "Checking tests formatting"
./tests/lib/external/snapd-testing-tools/utils/check-test-format ./tests
fi
fi
if [ "$UNIT" = 1 ]; then
./get-deps.sh
echo "Show go version"
command -v go
go version
tags=
if [ -n "${GO_BUILD_TAGS-}" ]; then
echo "Using build tags: $GO_BUILD_TAGS"
tags="-tags $GO_BUILD_TAGS"
fi
echo Building
# shellcheck disable=SC2086
go build -v $tags github.com/snapcore/snapd/...
# tests
echo Running tests from "$PWD"
if [ "$short" = 1 ]; then
# shellcheck disable=SC2046,SC2086
GOTRACEBACK=1 $goctest $tags -short -timeout 5m $(go list ./... | grep -v '/vendor/' )
else
coverage=""
if [ -z "${SKIP_COVERAGE-}" ]; then
coverage="-coverprofile=$COVERAGE_OUT -covermode=$COVERMODE"
# Prepare the coverage output profile.
mkdir -p "$(dirname "$COVERAGE_OUT")"
echo "mode: $COVERMODE" > "$COVERAGE_OUT"
else
echo "Skipping test coverage"
fi
# shellcheck disable=SC2046,SC2086
GOTRACEBACK=1 $goctest $tags -timeout 5m $coverage $(go list ./... | grep -v '/vendor/' )
fi
# python unit test for mountinfo.query and version-compare
command -v python2 && python2 ./tests/lib/tools/mountinfo.query --run-unit-tests
command -v python3 && python3 ./tests/lib/tools/mountinfo.query --run-unit-tests
command -v python2 && python2 ./tests/lib/tools/version-compare --run-unit-tests
command -v python3 && python3 ./tests/lib/tools/version-compare --run-unit-tests
fi
UNCLEAN="$(git status -s|grep '^??')" || true
SKIP_UNCLEAN=${SKIP_UNCLEAN=}
if [ -n "$UNCLEAN" ] && [ -z "$SKIP_UNCLEAN" ]; then
cat <<EOF
There are files left in the git tree after the tests:
$UNCLEAN
EOF
exit 1
fi
if [ -n "${SKIP_DIRTY_CHECK-}" ]; then
exit 0
fi
# XXX: re-enable after vendor/vendor.json is removed
# if git describe --always --dirty | grep -q dirty; then
# echo "Build tree is dirty"
# git diff
# exit 1
# fi