From 958a3bbd9900c08e8fa4a39bf416fe791819818c Mon Sep 17 00:00:00 2001 From: Tom Wilkie Date: Fri, 25 Sep 2015 07:37:53 +0000 Subject: [PATCH 1/6] Use test script from tools.git --- .gitignore | 1 + Makefile | 6 +++++- test/units.sh | 59 --------------------------------------------------- 3 files changed, 6 insertions(+), 60 deletions(-) delete mode 100755 test/units.sh diff --git a/.gitignore b/.gitignore index 91961534..fa3c6548 100644 --- a/.gitignore +++ b/.gitignore @@ -62,3 +62,4 @@ Vagrantfile.local test/tls/*.pem test/coverage test/coverage.* +.tools/ diff --git a/Makefile b/Makefile index 3dca5c45..5ac94b3c 100644 --- a/Makefile +++ b/Makefile @@ -44,6 +44,9 @@ NETGO_CHECK=@strings $@ | grep cgo_stub\\\.go >/dev/null || { \ echo " sudo go install -tags netgo std"; \ false; \ } +ENSURE_TOOLS=@if [ ! -d .tools ]; then \ + git clone https://github.com/weaveworks/tools.git .tools/;\ +fi BUILD_FLAGS=-ldflags "-extldflags \"-static\" -X main.version $(WEAVE_VERSION)" -tags netgo all: $(WEAVE_EXPORT) $(COVER_EXE) $(RUNNER_EXE) @@ -109,7 +112,8 @@ $(DOCKER_DISTRIB): curl -o $(DOCKER_DISTRIB) $(DOCKER_DISTRIB_URL) tests: $(COVER_EXE) - @test/units.sh + $(ENSURE_TOOLS) + .tools/test $(PUBLISH): publish_%: $(IMAGES_UPTODATE) $(SUDO) docker tag -f $(DOCKERHUB_USER)/$* $(DOCKERHUB_USER)/$*:$(WEAVE_VERSION) diff --git a/test/units.sh b/test/units.sh deleted file mode 100755 index a630a6ab..00000000 --- a/test/units.sh +++ /dev/null @@ -1,59 +0,0 @@ -#!/bin/bash - -set -e - -DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" -SLOW=${SLOW-} -GO_TEST_ARGS="-tags netgo -cpu 4 -timeout 8m" -if [ -n "$SLOW" -o "$1" = "-slow" ]; then - GO_TEST_ARGS="$GO_TEST_ARGS -race -covermode=atomic" - - if [ -n "$COVERDIR" ] ; then - coverdir="$COVERDIR" - else - coverdir=$(mktemp -d coverage.XXXXXXXXXX) - fi - - mkdir -p $coverdir -fi - -fail=0 - -TESTDIRS=$(find . -type f -name '*_test.go' | xargs -n1 dirname | grep -v prog | sort -u) - -# If running on circle, use the scheduler to work out what tests to run on what shard -if [ -n "$CIRCLECI" -a -z "$NO_SCHEDULER" ]; then - TESTDIRS=$(echo $TESTDIRS | "$DIR/sched" sched units-$CIRCLE_BUILD_NUM $CIRCLE_NODE_TOTAL $CIRCLE_NODE_INDEX) - echo $TESTDIRS -fi - -for dir in $TESTDIRS; do - go get -t -tags netgo $dir - - GO_TEST_ARGS_RUN="$GO_TEST_ARGS" - if [ -n "$SLOW" ]; then - COVERPKGS=$((go list $dir; go list -f '{{join .Deps "\n"}}' $dir | grep "weaveworks") | paste -s -d,) - output=$(mktemp $coverdir/unit.XXXXXXXXXX) - GO_TEST_ARGS_RUN="$GO_TEST_ARGS -coverprofile=$output -coverpkg=$COVERPKGS" - fi - - START=$(date +%s) - if ! go test $GO_TEST_ARGS_RUN $dir ; then - fail=1 - fi - RUNTIME=$(( $(date +%s) - $START )) - - # Report test runtime when running on circle, to help scheduler - if [ -n "$CIRCLECI" -a -z "$NO_SCHEDULER" ]; then - "$DIR/sched" time $dir $RUNTIME - fi -done - -if [ -n "$SLOW" -a -z "$COVERDIR" ] ; then - $DIR/../testing/cover/cover $coverdir/* >profile.cov - rm -rf $coverdir - go tool cover -html=profile.cov -o=coverage.html - go tool cover -func=profile.cov | tail -n1 -fi - -exit $fail From 6f7d4a7a01ec752cea5211fd0b4ec3e29b0371a9 Mon Sep 17 00:00:00 2001 From: Tom Wilkie Date: Mon, 28 Sep 2015 08:04:19 +0000 Subject: [PATCH 2/6] Use lint from tools.git - Do lint first, on circle. --- .gitignore | 2 +- .travis.yml | 1 - Makefile | 4 +++ bin/lint | 86 ----------------------------------------------------- circle.yml | 2 ++ 5 files changed, 7 insertions(+), 88 deletions(-) delete mode 100755 bin/lint diff --git a/.gitignore b/.gitignore index fa3c6548..143b10a6 100644 --- a/.gitignore +++ b/.gitignore @@ -62,4 +62,4 @@ Vagrantfile.local test/tls/*.pem test/coverage test/coverage.* -.tools/ +.tools diff --git a/.travis.yml b/.travis.yml index d987e04c..0439e89b 100644 --- a/.travis.yml +++ b/.travis.yml @@ -21,7 +21,6 @@ install: - go get ./... script: - - bin/lint . - make tests after_success: diff --git a/Makefile b/Makefile index 5ac94b3c..624d29d9 100644 --- a/Makefile +++ b/Makefile @@ -115,6 +115,10 @@ tests: $(COVER_EXE) $(ENSURE_TOOLS) .tools/test +lint: + $(ENSURE_TOOLS) + .tools/lint -nocomment -notestpackage . + $(PUBLISH): publish_%: $(IMAGES_UPTODATE) $(SUDO) docker tag -f $(DOCKERHUB_USER)/$* $(DOCKERHUB_USER)/$*:$(WEAVE_VERSION) $(SUDO) docker push $(DOCKERHUB_USER)/$*:$(WEAVE_VERSION) diff --git a/bin/lint b/bin/lint deleted file mode 100755 index 32e791fb..00000000 --- a/bin/lint +++ /dev/null @@ -1,86 +0,0 @@ -#!/bin/bash -# This scipt lints go files for common errors. -# -# Its runs gofmt and go vet, and optionally golint and -# gocyclo, if they are installed. -# -# With no arguments, it lints the current files staged -# for git commit. Or you can pass it explicit filenames -# (or directories) and it will lint them. -# -# To use this script automatically, run: -# ln -s ../../bin/lint .git/hooks/pre-commit - -set -eu - -function lint_go { - filename="$1" - result=0 - - if [ -n "$(gofmt -l "${filename}")" ]; then - result=1 - echo "${filename}: run gofmt -w ${filename}!" - fi - - go tool vet "${filename}" || result=$? - - # golint is completely optional. If you don't like it - # don't have it installed. - if type golint >/dev/null 2>&1; then - # golint doesn't set an exit code it seems - lintoutput=$(golint "${filename}" | grep -vE 'comment|dot imports') - if [ "$lintoutput" != "" ]; then - result=1 - echo "$lintoutput" - fi - fi - - # gocyclo is completely optional. If you don't like it - # don't have it installed. Also never blocks a commit, - # it just warns. - if type gocyclo >/dev/null 2>&1; then - gocyclo -over 25 "${filename}" | while read line; do - echo "${filename}": higher than 25 cyclomatic complexity - "${line}" - done - fi - - return $result -} - -function lint { - filename="$1" - ext="${filename##*\.}" - - # Don't lint this script - if [ "${filename}" = "bin/lint" ]; then - return - fi - - # Don't lint files that don't exist - if ! [ -a "${filename}" ]; then - return - fi - - case "$ext" in - go) lint_go "${filename}" - ;; - esac -} - -function lint_files { - lint_result=0 - while read filename; do - lint "${filename}" || lint_result=1 - done - exit $lint_result -} - -function list_files { - if [ $# -gt 0 ]; then - find "$@" -type f - else - git diff --cached --name-only - fi -} - -list_files "$@" | lint_files diff --git a/circle.yml b/circle.yml index 655c01dc..f590e330 100644 --- a/circle.yml +++ b/circle.yml @@ -37,6 +37,8 @@ dependencies: test: override: + - cd $SRCDIR; make lint: + parallel: true - docker run -v /home/ubuntu:/home/go -e COVERDIR=test/coverage -e SLOW=true -e CIRCLECI -e CIRCLE_BUILD_NUM -e CIRCLE_NODE_TOTAL -e CIRCLE_NODE_INDEX weaveworks/weave-test: parallel: true - docker run -v /var/run/docker.sock:/run/docker.sock -v /home/ubuntu:/home/go -e COVERAGE=true weaveworks/weave-build: From 5208dcd67ea62f45e64141155b5c2f90e590e39f Mon Sep 17 00:00:00 2001 From: Tom Wilkie Date: Mon, 28 Sep 2015 08:07:51 +0000 Subject: [PATCH 3/6] Review feedback - Use git submodule for tools. - Update Makefile to use submodules. - Put submodule at tools/ - Tidy up some obsolete references to tools in .gitignore. --- .gitignore | 3 --- .gitmodules | 3 +++ Makefile | 18 ++++++++---------- tools | 1 + 4 files changed, 12 insertions(+), 13 deletions(-) create mode 100644 .gitmodules create mode 160000 tools diff --git a/.gitignore b/.gitignore index 143b10a6..a47a726b 100644 --- a/.gitignore +++ b/.gitignore @@ -43,8 +43,6 @@ prog/netcheck/netcheck prog/docker_tls_args/docker_tls_args testing/cover/cover testing/runner/runner -tools/bin -tools/build releases .weaver.uptodate .weavedns.uptodate @@ -62,4 +60,3 @@ Vagrantfile.local test/tls/*.pem test/coverage test/coverage.* -.tools diff --git a/.gitmodules b/.gitmodules new file mode 100644 index 00000000..46256a7e --- /dev/null +++ b/.gitmodules @@ -0,0 +1,3 @@ +[submodule "tools"] + path = tools + url = https://github.com/weaveworks/tools diff --git a/Makefile b/Makefile index 624d29d9..02ab27c5 100644 --- a/Makefile +++ b/Makefile @@ -1,7 +1,7 @@ PUBLISH=publish_weave publish_weaveexec .DEFAULT: all -.PHONY: all update tests publish $(PUBLISH) clean clean-bin prerequisites build travis run-smoketests +.PHONY: all update tests lint publish $(PUBLISH) clean clean-bin prerequisites build travis run-smoketests # If you can use docker without being root, you can do "make SUDO=" SUDO=sudo @@ -44,9 +44,6 @@ NETGO_CHECK=@strings $@ | grep cgo_stub\\\.go >/dev/null || { \ echo " sudo go install -tags netgo std"; \ false; \ } -ENSURE_TOOLS=@if [ ! -d .tools ]; then \ - git clone https://github.com/weaveworks/tools.git .tools/;\ -fi BUILD_FLAGS=-ldflags "-extldflags \"-static\" -X main.version $(WEAVE_VERSION)" -tags netgo all: $(WEAVE_EXPORT) $(COVER_EXE) $(RUNNER_EXE) @@ -111,13 +108,14 @@ $(WEAVE_EXPORT): $(IMAGES_UPTODATE) $(DOCKER_DISTRIB): curl -o $(DOCKER_DISTRIB) $(DOCKER_DISTRIB_URL) -tests: $(COVER_EXE) - $(ENSURE_TOOLS) - .tools/test +tests: $(COVER_EXE) tools/.git + tools/test -lint: - $(ENSURE_TOOLS) - .tools/lint -nocomment -notestpackage . +lint: tools/.git + tools/lint -nocomment -notestpackage . + +tools/.git: + git submodule update --init $(PUBLISH): publish_%: $(IMAGES_UPTODATE) $(SUDO) docker tag -f $(DOCKERHUB_USER)/$* $(DOCKERHUB_USER)/$*:$(WEAVE_VERSION) diff --git a/tools b/tools new file mode 160000 index 00000000..caecc98f --- /dev/null +++ b/tools @@ -0,0 +1 @@ +Subproject commit caecc98fa89c7115dd78b20912842ee592d7439d From f3360e61e108436941e7209219973be8edb11021 Mon Sep 17 00:00:00 2001 From: Tom Wilkie Date: Wed, 30 Sep 2015 08:01:02 +0000 Subject: [PATCH 4/6] Remove cover tool, this lives in tools.git now. --- Makefile | 10 ++-- test/gen_coverage_reports.sh | 2 +- testing/cover/cover.go | 97 ------------------------------------ 3 files changed, 5 insertions(+), 104 deletions(-) delete mode 100644 testing/cover/cover.go diff --git a/Makefile b/Makefile index 02ab27c5..3498f500 100644 --- a/Makefile +++ b/Makefile @@ -16,10 +16,9 @@ WEAVEWAIT_EXE=prog/weavewait/weavewait WEAVEHOSTS_EXE=prog/weavehosts/weavehosts NETCHECK_EXE=prog/netcheck/netcheck DOCKERTLSARGS_EXE=prog/docker_tls_args/docker_tls_args -COVER_EXE=testing/cover/cover RUNNER_EXE=testing/runner/runner -EXES=$(WEAVER_EXE) $(SIGPROXY_EXE) $(WEAVEPROXY_EXE) $(WEAVEWAIT_EXE) $(WEAVEHOSTS_EXE) $(NETCHECK_EXE) $(DOCKERTLSARGS_EXE) $(COVER_EXE) $(RUNNER_EXE) +EXES=$(WEAVER_EXE) $(SIGPROXY_EXE) $(WEAVEPROXY_EXE) $(WEAVEWAIT_EXE) $(WEAVEHOSTS_EXE) $(NETCHECK_EXE) $(DOCKERTLSARGS_EXE) $(RUNNER_EXE) WEAVER_UPTODATE=.weaver.uptodate WEAVEEXEC_UPTODATE=.weaveexec.uptodate @@ -46,7 +45,7 @@ NETGO_CHECK=@strings $@ | grep cgo_stub\\\.go >/dev/null || { \ } BUILD_FLAGS=-ldflags "-extldflags \"-static\" -X main.version $(WEAVE_VERSION)" -tags netgo -all: $(WEAVE_EXPORT) $(COVER_EXE) $(RUNNER_EXE) +all: $(WEAVE_EXPORT) $(RUNNER_EXE) travis: $(EXES) @@ -78,11 +77,10 @@ $(NETCHECK_EXE): prog/netcheck/netcheck.go $(SIGPROXY_EXE): prog/sigproxy/main.go $(WEAVEWAIT_EXE): prog/weavewait/main.go net/*.go $(WEAVEHOSTS_EXE): prog/weavehosts/weavehosts.go -$(COVER_EXE): testing/cover/cover.go $(RUNNER_EXE): testing/runner/runner.go $(DOCKERTLSARGS_EXE): prog/docker_tls_args/*.go -$(WEAVEWAIT_EXE) $(SIGPROXY_EXE) $(WEAVEHOSTS_EXE) $(COVER_EXE) $(RUNNER_EXE) $(DOCKERTLSARGS_EXE): +$(WEAVEWAIT_EXE) $(SIGPROXY_EXE) $(WEAVEHOSTS_EXE) $(RUNNER_EXE) $(DOCKERTLSARGS_EXE): go get -tags netgo ./$(@D) go build $(BUILD_FLAGS) -o $@ ./$(@D) @@ -108,7 +106,7 @@ $(WEAVE_EXPORT): $(IMAGES_UPTODATE) $(DOCKER_DISTRIB): curl -o $(DOCKER_DISTRIB) $(DOCKER_DISTRIB_URL) -tests: $(COVER_EXE) tools/.git +tests: tools/.git tools/test lint: tools/.git diff --git a/test/gen_coverage_reports.sh b/test/gen_coverage_reports.sh index 567cb0f3..8f9f3816 100755 --- a/test/gen_coverage_reports.sh +++ b/test/gen_coverage_reports.sh @@ -9,7 +9,7 @@ if [ -n "$CIRCLECI" ]; then done fi -$DIR/../testing/cover/cover +go get github.com/weaveworks/build-tools/cover cover ./coverage/* >profile.cov go tool cover -html=profile.cov -o coverage.html go tool cover -func=profile.cov -o coverage.txt diff --git a/testing/cover/cover.go b/testing/cover/cover.go deleted file mode 100644 index 4c5fcfd7..00000000 --- a/testing/cover/cover.go +++ /dev/null @@ -1,97 +0,0 @@ -package main - -import ( - "fmt" - "os" - "sort" - - "golang.org/x/tools/cover" -) - -func merge(p1, p2 *cover.Profile) *cover.Profile { - output := cover.Profile{ - FileName: p1.FileName, - Mode: p1.Mode, - } - - i, j := 0, 0 - for i < len(p1.Blocks) && j < len(p2.Blocks) { - bi, bj := p1.Blocks[i], p2.Blocks[j] - if bi.StartLine == bj.StartLine && bi.StartCol == bj.StartCol { - - if bi.EndLine != bj.EndLine || - bi.EndCol != bj.EndCol || - bi.NumStmt != bj.NumStmt { - panic("Not run on same source!") - } - - output.Blocks = append(output.Blocks, cover.ProfileBlock{ - StartLine: bi.StartLine, - StartCol: bi.StartCol, - EndLine: bi.EndLine, - EndCol: bi.EndCol, - NumStmt: bi.NumStmt, - Count: bi.Count + bj.Count, - }) - i++ - j++ - } else if bi.StartLine < bj.StartLine || bi.StartLine == bj.StartLine && bi.StartCol < bj.StartCol { - output.Blocks = append(output.Blocks, bi) - i++ - } else { - output.Blocks = append(output.Blocks, bj) - j++ - } - } - - for ; i < len(p1.Blocks); i++ { - output.Blocks = append(output.Blocks, p1.Blocks[i]) - } - - for ; j < len(p2.Blocks); j++ { - output.Blocks = append(output.Blocks, p2.Blocks[j]) - } - - return &output -} - -func print(profiles []*cover.Profile) { - fmt.Println("mode: atomic") - for _, profile := range profiles { - for _, block := range profile.Blocks { - fmt.Printf("%s:%d.%d,%d.%d %d %d\n", profile.FileName, block.StartLine, block.StartCol, - block.EndLine, block.EndCol, block.NumStmt, block.Count) - } - } -} - -// Copied from https://github.com/golang/tools/blob/master/cover/profile.go -type byFileName []*cover.Profile - -func (p byFileName) Len() int { return len(p) } -func (p byFileName) Less(i, j int) bool { return p[i].FileName < p[j].FileName } -func (p byFileName) Swap(i, j int) { p[i], p[j] = p[j], p[i] } - -func main() { - outputProfiles := map[string]*cover.Profile{} - for _, input := range os.Args[1:] { - inputProfiles, err := cover.ParseProfiles(input) - if err != nil { - panic(fmt.Sprintf("Error parsing %s: %v", input, err)) - } - for _, ip := range inputProfiles { - op := outputProfiles[ip.FileName] - if op == nil { - outputProfiles[ip.FileName] = ip - } else { - outputProfiles[ip.FileName] = merge(op, ip) - } - } - } - profiles := make([]*cover.Profile, 0, len(outputProfiles)) - for _, profile := range outputProfiles { - profiles = append(profiles, profile) - } - sort.Sort(byFileName(profiles)) - print(profiles) -} From 62f62e1fd5f03e6f8c192d3ae260b4c8b98ee6ba Mon Sep 17 00:00:00 2001 From: Tom Wilkie Date: Wed, 30 Sep 2015 08:02:51 +0000 Subject: [PATCH 5/6] tools.git is now called build-tools.git --- .gitmodules | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.gitmodules b/.gitmodules index 46256a7e..a7445252 100644 --- a/.gitmodules +++ b/.gitmodules @@ -1,3 +1,3 @@ [submodule "tools"] path = tools - url = https://github.com/weaveworks/tools + url = https://github.com/weaveworks/build-tools From f3dc681f61beb3ec56b9292e75586a2ebfdbad66 Mon Sep 17 00:00:00 2001 From: Tom Wilkie Date: Wed, 30 Sep 2015 10:15:50 +0000 Subject: [PATCH 6/6] Only clean weave packages. --- Makefile | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Makefile b/Makefile index 3498f500..e65ca67c 100644 --- a/Makefile +++ b/Makefile @@ -126,7 +126,7 @@ publish: $(PUBLISH) clean-bin: -$(SUDO) docker rmi $(IMAGES) - go clean -r ./... + go clean -r $(addprefix ./,$(dir $(EXES))) rm -f $(EXES) $(IMAGES_UPTODATE) $(WEAVE_EXPORT) clean: clean-bin