From 9987b029831ff37ae110bbcfc6ce21fd37fbb788 Mon Sep 17 00:00:00 2001 From: "John C. Vernaleo" Date: Tue, 23 May 2017 15:55:16 -0400 Subject: [PATCH] Run testing and linting in a Dockerfile. This simplifies the use of travis significantly and should speed things up as parts of the setup are now done once when the docker image is created. Tests may be run locally (no docker) by using the 'local' arg to run_tests.sh. --- .dockerignore | 2 ++ .travis.yml | 24 +++++++++++------------- Dockerfile-1.7 | 51 ++++++++++++++++++++++++++++++++++++++++++++++++++ Dockerfile-1.8 | 51 ++++++++++++++++++++++++++++++++++++++++++++++++++ README.md | 29 +++++++++++++++++++++++++++- goclean.sh | 35 ---------------------------------- run_tests.sh | 50 +++++++++++++++++++++++++++++++++++++++++++++++++ 7 files changed, 193 insertions(+), 49 deletions(-) create mode 100644 .dockerignore create mode 100644 Dockerfile-1.7 create mode 100644 Dockerfile-1.8 delete mode 100755 goclean.sh create mode 100755 run_tests.sh diff --git a/.dockerignore b/.dockerignore new file mode 100644 index 00000000..5b6f95a1 --- /dev/null +++ b/.dockerignore @@ -0,0 +1,2 @@ +*~ +vendor diff --git a/.travis.yml b/.travis.yml index 24786f9a..a696375f 100644 --- a/.travis.yml +++ b/.travis.yml @@ -1,15 +1,13 @@ -dist: trusty +sudo: required + language: go -go: - - 1.7.x - - 1.8.x -sudo: false -install: - - go get -v github.com/Masterminds/glide - - glide install - - go install -v . ./cmd/... - - go get -v github.com/alecthomas/gometalinter - - gometalinter --install + +services: + - docker + +env: + - GOVERSION = 1.7 + - GOVERSION = 1.8 + script: - - export PATH=$PATH:$HOME/gopath/bin - - ./goclean.sh + - ./run_tests.sh $GOVERSION diff --git a/Dockerfile-1.7 b/Dockerfile-1.7 new file mode 100644 index 00000000..9f8db72e --- /dev/null +++ b/Dockerfile-1.7 @@ -0,0 +1,51 @@ +#decred-golang-builder-1.7 +FROM golang:1.7.6 + +LABEL description="Decred golang builder image" +LABEL version="1.0" +LABEL maintainer "john@netpurgatory.com" + +ENV TERM linux +ENV USER build + +# create user +RUN adduser --disabled-password --gecos '' build + +# update base distro & install build tooling +ENV DEBIAN_FRONTEND noninteractive + +RUN apt-get update && \ + apt-get install -qy rsync + +# create directory for build artifacts, adjust user permissions +RUN mkdir /release && \ + chown $USER /release + +# create directory to get source from +RUN mkdir /src && \ + chown $USER /src && \ + mkdir -p /go/src/github.com/decred/dcrd && \ + mkdir -p /go/src/github.com/decred/dcrwallet && \ + mkdir -p /go/src/github.com/decred/dcrctl && \ + mkdir -p /go/src/github.com/decred/dcrrpcclient && \ + chown -R $USER /go/src/github.com/decred + +# switch user +USER $USER +ENV HOME /home/$USER + +#Get deps +ENV GLIDE_TAG v0.12.3 +ENV GOMETALINTER_TAG v1.2.1 + +WORKDIR /go/src +RUN go get -v github.com/Masterminds/glide && \ + cd /go/src/github.com/Masterminds/glide && \ + git checkout $GLIDE_TAG && \ + make build && \ + mv glide `which glide` && \ + go get -v github.com/alecthomas/gometalinter && \ + cd /go/src/github.com/alecthomas/gometalinter && \ + git checkout $GOMETALINTER_TAG && \ + go install && \ + gometalinter --install diff --git a/Dockerfile-1.8 b/Dockerfile-1.8 new file mode 100644 index 00000000..9902b580 --- /dev/null +++ b/Dockerfile-1.8 @@ -0,0 +1,51 @@ +#decred-golang-builder-1.8 +FROM golang:1.8.3 + +LABEL description="Decred golang builder image" +LABEL version="1.0" +LABEL maintainer "john@netpurgatory.com" + +ENV TERM linux +ENV USER build + +# create user +RUN adduser --disabled-password --gecos '' build + +# update base distro & install build tooling +ENV DEBIAN_FRONTEND noninteractive + +RUN apt-get update && \ + apt-get install -qy rsync + +# create directory for build artifacts, adjust user permissions +RUN mkdir /release && \ + chown $USER /release + +# create directory to get source from +RUN mkdir /src && \ + chown $USER /src && \ + mkdir -p /go/src/github.com/decred/dcrd && \ + mkdir -p /go/src/github.com/decred/dcrwallet && \ + mkdir -p /go/src/github.com/decred/dcrctl && \ + mkdir -p /go/src/github.com/decred/dcrrpcclient && \ + chown -R $USER /go/src/github.com/decred + +# switch user +USER $USER +ENV HOME /home/$USER + +#Get deps +ENV GLIDE_TAG v0.12.3 +ENV GOMETALINTER_TAG v1.2.1 + +WORKDIR /go/src +RUN go get -v github.com/Masterminds/glide && \ + cd /go/src/github.com/Masterminds/glide && \ + git checkout $GLIDE_TAG && \ + make build && \ + mv glide `which glide` && \ + go get -v github.com/alecthomas/gometalinter && \ + cd /go/src/github.com/alecthomas/gometalinter && \ + git checkout $GOMETALINTER_TAG && \ + go install && \ + gometalinter --install diff --git a/README.md b/README.md index a9900b0e..c0459131 100644 --- a/README.md +++ b/README.md @@ -82,7 +82,34 @@ go install $(glide nv) For more information about decred and how to set up your software please go to our docs page at [docs.decred.org](https://docs.decred.org/getting-started/beginner-guide/). -## Contact +## Docker + +All tests and linters may be run in a docker container using the script `run_tests.sh`. This script defaults to using the current supported version of go. You can run it with the major version of go you would like to use as the only arguement to test a previous on a previous version of go (generally decred supports the current version of go and the previous one). + +``` +./run_tests.sh 1.7 +``` + +To run the tests locally without docker: + +``` +./run_tests.sh local +``` + +### Updating docker go versions + +When a new minor version of go is released, the docker images must be updated and rebuilt to support it. For example, when go1.8.3 was released, the file `Dockerfile-1.8` was edited by changing `FROM golang:1.8.2` to `FROM golang:1.8.3`. Then the image was rebuilt and pushed to docker hub: +``` +GOVERSION=1.8 +DOCKER_IMAGE_TAG=decred-golang-builder-$GOVERSION +docker build -t $DOCKER_IMAGE_TAG -f ./Dockerfile-$GOVERSION . +docker tag $DOCKER_IMAGE_TAG decred/$DOCKER_IMAGE_TAG +docker push decred/$DOCKER_IMAGE_TAG +``` + +For a new major version, a new file `Dockerfile-VERSION` must be created and then the previous steps can be followed. That new version must be added to the travis build matrix to run the tests in travis. + +## Contact If you have any further questions you can find us at: diff --git a/goclean.sh b/goclean.sh deleted file mode 100755 index 82dc18ee..00000000 --- a/goclean.sh +++ /dev/null @@ -1,35 +0,0 @@ -#!/bin/bash -# The script does automatic checking on a Go package and its sub-packages, including: -# 1. gofmt (http://golang.org/cmd/gofmt/) -# 2. go vet (http://golang.org/cmd/vet) -# 3. unconvert (https://github.com/mdempsky/unconvert) -# 4. race detector (http://blog.golang.org/race-detector) -# 5. test coverage (http://blog.golang.org/cover) - -# gometalinter (github.com/alecthomas/gometalinter) is used to run each each -# static checker. -set -ex - -# Make sure glide is installed and $GOPATH/bin is in your path. -# $ go get -u github.com/Masterminds/glide -# $ glide install -if [ ! -x "$(type -p glide)" ]; then - exit 1 -fi - -# Make sure gometalinter is installed and $GOPATH/bin is in your path. -# $ go get -v github.com/alecthomas/gometalinter" -# $ gometalinter --install" -if [ ! -x "$(type -p gometalinter)" ]; then - exit 1 -fi - -# Automatic checks -test -z "$(gometalinter --disable-all \ ---enable=gofmt \ ---enable=vet \ ---enable=unconvert \ ---vendor \ ---deadline=10m . 2>&1 | tee /dev/stderr)" - -env GORACE="halt_on_error=1" go test -short -race $(glide novendor) diff --git a/run_tests.sh b/run_tests.sh new file mode 100755 index 00000000..25f5b16e --- /dev/null +++ b/run_tests.sh @@ -0,0 +1,50 @@ +#!/usr/bin/env bash +set -ex + +# The script does automatic checking on a Go package and its sub-packages, +# including: +# 1. gofmt (http://golang.org/cmd/gofmt/) +# 2. go vet (http://golang.org/cmd/vet) +# 3. unconvert (https://github.com/mdempsky/unconvert) +# 4. race detector (http://blog.golang.org/race-detector) + +# gometalinter (github.com/alecthomas/gometalinter) is used to run each each +# static checker. + +# To run on docker on windows, symlink /mnt/c to /c and then execute the script +# from the repo path under /c. See: +# https://github.com/Microsoft/BashOnWindows/issues/1854 +# for more details. + +#Default GOVERSION +GOVERSION=${1:-1.8} +REPO=dcrd + +TESTCMD="test -z \"\$(gometalinter --disable-all \ + --enable=gofmt \ + --enable=vet \ + --enable=unconvert \ + --vendor \ + --deadline=10m . | tee /dev/stderr)\"&& \ + env GORACE='halt_on_error=1' go test -short -race \$(glide novendor)" + +if [ $GOVERSION == "local" ]; then + eval $TESTCMD + exit +fi + +DOCKER_IMAGE_TAG=decred-golang-builder-$GOVERSION + +docker pull decred/$DOCKER_IMAGE_TAG + +docker run --rm -it -v $(pwd):/src decred/$DOCKER_IMAGE_TAG /bin/bash -c "\ + rsync -ra --filter=':- .gitignore' \ + /src/ /go/src/github.com/decred/$REPO/ && \ + cd github.com/decred/$REPO/ && \ + glide install && \ + go install \$(glide novendor) && \ + $TESTCMD +" + +echo "------------------------------------------" +echo "Tests complete."