Skip to content
This repository has been archived by the owner on Aug 7, 2023. It is now read-only.

Commit

Permalink
Run testing and linting in a Dockerfile.
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
jcvernaleo committed May 30, 2017
1 parent e7b128a commit 9987b02
Show file tree
Hide file tree
Showing 7 changed files with 193 additions and 49 deletions.
2 changes: 2 additions & 0 deletions .dockerignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
*~
vendor
24 changes: 11 additions & 13 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -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
51 changes: 51 additions & 0 deletions Dockerfile-1.7
Original file line number Diff line number Diff line change
@@ -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 "[email protected]"

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
51 changes: 51 additions & 0 deletions Dockerfile-1.8
Original file line number Diff line number Diff line change
@@ -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 "[email protected]"

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
29 changes: 28 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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:

Expand Down
35 changes: 0 additions & 35 deletions goclean.sh

This file was deleted.

50 changes: 50 additions & 0 deletions run_tests.sh
Original file line number Diff line number Diff line change
@@ -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."

0 comments on commit 9987b02

Please sign in to comment.