Skip to content

Commit

Permalink
Go code checks can be run locally in container
Browse files Browse the repository at this point in the history
Before the only option was to spin up a VM to run code checks, which is
expensive in terms of time.

Add Dockerfile-check to provide a container build for a runtime
environment that supports go tool vet, gofmt, and golint

Add script 'code_checks.sh' intended to be run inside the
Dockerfile-check container that runs 'go tool vet', 'gofmt',
misspell, and 'golint' against packages directories passed in.  This
script runs the checks in a different order than the Makefile, to deal
with the more severe issues first, and deal with pretty print
formatting last.

Makefile updated to build the docker container and run it against the
go pakages for netplugin.

Drive-by:
* Fix comment in Dockerfile
* http_proxy and https_proxy are not useful as args when not
referenced
* add GOPATH environment var to Dockerfile for general use

Signed-off-by: Chris Plock <chrisplo@cisco.com>
  • Loading branch information
chrisplo committed Oct 16, 2017
1 parent 766b218 commit 01da373
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 3 deletions.
3 changes: 1 addition & 2 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,8 @@ FROM golang:1.7.6
# a proxy.
#ENV http_proxy ""
#ENV https_proxy ""
ARG http_proxy
ARG https_proxy

ENV GOPATH=/go
WORKDIR /go/src/github.com/contiv/netplugin/

ENTRYPOINT ["netplugin"]
Expand Down
15 changes: 15 additions & 0 deletions Dockerfile-check
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
ARG TAG=latest

FROM golang:1.7.6

ENV GOPATH=/go

WORKDIR /go/src/github.com/contiv/netplugin/
ENTRYPOINT ["/code_checks.sh"]

RUN go get github.com/tools/godep github.com/aktau/github-release \
github.com/contiv/modelgen \
&& go get -u github.com/golang/lint/golint \
github.com/client9/misspell/cmd/misspell

COPY scripts/code_checks.sh /
10 changes: 9 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ NAME := netplugin
VERSION_FILE := $(NAME)-version
VERSION := `cat $(VERSION_FILE)`
TAR_EXT := tar.bz2
# BUILD_VERSION=1.2 make --> 1.2-1097d2a7, otherwise devbuild-1097d2a7
export NETPLUGIN_CONTAINER_TAG := $(or $(BUILD_VERSION),devbuild)-$(shell ./scripts/getGitCommit.sh)
TAR_FILENAME := $(NAME)-$(VERSION).$(TAR_EXT)
TAR_LOC := .
TAR_FILE := $(TAR_LOC)/$(TAR_FILENAME)
Expand Down Expand Up @@ -87,6 +89,12 @@ endif

checks: go-version gofmt-src golint-src govet-src misspell-src

checks-with-docker: compile-with-docker
docker build -f Dockerfile-check -t contiv/netplugin-checks .
docker run --rm --name netplugin-checks \
-v ${PWD}:/go/src/github.com/contiv/netplugin/ \
contiv/netplugin-checks $(PKG_DIRS)

compile:
cd $(GOPATH)/src/github.com/contiv/netplugin && \
NIGHTLY_RELEASE=${NIGHTLY_RELEASE} BUILD_VERSION=${BUILD_VERSION} \
Expand All @@ -100,7 +108,7 @@ compile-with-docker:
docker build \
--build-arg NIGHTLY_RELEASE=${NIGHTLY_RELEASE} \
--build-arg BUILD_VERSION=${BUILD_VERSION} \
-t netplugin:$${BUILD_VERSION:-devbuild}-$$(./scripts/getGitCommit.sh) .
-t netplugin-build:$${BUILD_VERSION:-devbuild}-$$(./scripts/getGitCommit.sh) .

build-docker-image: start
vagrant ssh netplugin-node1 -c 'bash -lc "source /etc/profile.d/envvar.sh && cd /opt/gopath/src/github.com/contiv/netplugin && make host-build-docker-image"'
Expand Down
19 changes: 19 additions & 0 deletions scripts/code_checks.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
#!/bin/bash

set -euo pipefail

PKG_DIRS=$*

############# CORRECTNESS CHECKING #########
echo -e "\nChecking correctness for $PKG_DIRS ...\n"
go tool vet $PKG_DIRS

############# LINT CHECKING ################
echo -e "\nChecking lint for $PKG_DIRS ...\n"
golint -set_exit_status $PKG_DIRS

############# FORMAT CHECKING #############
echo -e "\nChecking format for $PKG_DIRS ...\n"
gofmt -s -d $PKG_DIRS

misspell -locale US -error $PKG_DIRS

0 comments on commit 01da373

Please sign in to comment.