github.com/chipaca/snappy@v0.0.0-20210104084008-1f06296fe8ad/run-checks (about)

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