From 30d2fd69b1ae4d8e95119484d739344dc45404cf Mon Sep 17 00:00:00 2001 From: Bo-Yi Wu Date: Sat, 30 Jul 2016 20:55:29 +0800 Subject: [PATCH] add docker testing. Signed-off-by: Bo-Yi Wu --- Makefile | 32 ++------ docker/docker-compose.testing.yml | 7 +- docker/golang/Dockerfile | 13 +--- docker/testing.sh | 4 +- script/coverage.sh | 123 ------------------------------ 5 files changed, 16 insertions(+), 163 deletions(-) delete mode 100755 script/coverage.sh diff --git a/Makefile b/Makefile index 0364582..f3bc99b 100644 --- a/Makefile +++ b/Makefile @@ -7,6 +7,7 @@ PRODUCTION_IMAGE := "gorush" DEPLOY_ACCOUNT := "appleboy" VERSION := $(shell git describe --tags) TARGETS_NOVENDOR := $(shell glide novendor) +export PROJECT_PATH = /go/src/github.com/appleboy/gorush all: build @@ -51,8 +52,8 @@ docker_build: clean docker build --rm -t $(BUILD_IMAGE) -f docker/Dockerfile.tmp . docker run --rm $(BUILD_IMAGE) > gorush.tar.gz -docker_test: init - docker-compose -p ${PRODUCTION_IMAGE} -f docker/docker-compose.testing.yml run --rm gorush +docker_test: init clean + docker-compose -p ${PRODUCTION_IMAGE} -f docker/docker-compose.testing.yml run gorush docker-compose -p ${PRODUCTION_IMAGE} -f docker/docker-compose.testing.yml down docker_production: docker_build @@ -66,36 +67,15 @@ endif docker tag $(PRODUCTION_IMAGE):latest $(DEPLOY_ACCOUNT)/$(PRODUCTION_IMAGE):$(tag) docker push $(DEPLOY_ACCOUNT)/$(PRODUCTION_IMAGE):$(tag) -install: tool - @which glide || (curl https://glide.sh/get | sh) - @glide install +install: + glide install update: - @glide up + glide update fmt: @echo $(TARGETS_NOVENDOR) | xargs go fmt -tool: - sh ./script/coverage.sh tool - -junit_report: - sh ./script/coverage.sh junit - -coverage_report: - sh ./script/coverage.sh coverage - -lint_report: - sh ./script/coverage.sh lint - -vet_report: - sh ./script/coverage.sh vet - -cloc_report: - sh ./script/coverage.sh cloc - -report: junit_report coverage_report lint_report vet_report cloc_report - clean: -rm -rf build.tar.gz \ gorush.tar.gz bin/* \ diff --git a/docker/docker-compose.testing.yml b/docker/docker-compose.testing.yml index 54e6e3d..7ce222a 100644 --- a/docker/docker-compose.testing.yml +++ b/docker/docker-compose.testing.yml @@ -2,7 +2,10 @@ version: "2" services: gorush: - build: ./golang + build: + context: ./golang + args: + PROJECT_PATH: $PROJECT_PATH links: - redis privileged: true @@ -10,7 +13,7 @@ services: - ANDROID_API_KEY - ANDROID_TEST_TOKEN volumes: - - ../:/app + - ../:$PROJECT_PATH command: /bin/sh -c './docker/testing.sh' redis: diff --git a/docker/golang/Dockerfile b/docker/golang/Dockerfile index a3cdfe0..71fe994 100644 --- a/docker/golang/Dockerfile +++ b/docker/golang/Dockerfile @@ -1,14 +1,9 @@ -FROM golang:1.6.3-alpine +FROM appleboy/golang-testing MAINTAINER Bo-Yi Wu -RUN apk update \ - && apk add git make curl perl && rm -rf /var/cache/apk/* +ARG PROJECT_PATH -# install glide package management. -RUN curl https://glide.sh/get | sh +ENV PROJECT_PATH=$PROJECT_PATH -RUN mkdir /app && mkdir -p $GOPATH/src/github.com/appleboy -RUN ln -sf /app $GOPATH/src/github.com/appleboy/gorush - -WORKDIR $GOPATH/src/github.com/appleboy/gorush +WORKDIR $PROJECT_PATH diff --git a/docker/testing.sh b/docker/testing.sh index a71acd7..6710593 100755 --- a/docker/testing.sh +++ b/docker/testing.sh @@ -9,6 +9,4 @@ sed -i"" -e "s/localhost/redis/g" config/config.go sed -i"" -e "s/localhost/redis/g" config/config_test.go sed -i"" -e "s/localhost/redis/g" gorush/status_test.go sed -i"" -e "s/localhost/redis/g" storage/redis/redis_test.go -make install -make coverage -make report +make install && coverage all diff --git a/script/coverage.sh b/script/coverage.sh deleted file mode 100755 index 3fd6306..0000000 --- a/script/coverage.sh +++ /dev/null @@ -1,123 +0,0 @@ -#!/bin/sh -# -# Generate test coverage statistics for Go packages. -# - -set -e - -output() { - printf "\033[32m" - echo $1 - printf "\033[0m" - - if [ "$2" = 1 ]; then - exit 1 - fi -} - -workdir=".cover" -coverage_report="$workdir/coverage.txt" -coverage_xml_report="$workdir/coverage.xml" -junit_report="$workdir/junit.txt" -junit_xml_report="$workdir/report.xml" -lint_report="$workdir/lint.txt" -vet_report="$workdir/vet.txt" -cloc_report="$workdir/cloc.xml" -packages=$(go list ./... | grep -v vendor) - -test -d $workdir || mkdir -p $workdir - -show_help() { -cat << EOF -Generate test coverage statistics for Go packages. - - tool Install go dependency tools like gocov or golint. - testing [set|count|atomic] Run go testing for all packages - coverage Generate coverage report for all packages - junit Generate coverage xml report for junit plugin - lint Generate Lint report for all packages - vet Generate Vet report for all packages - cloc Generate Count Lines of Code report for all files -EOF -} - -install_dependency_tool() { - which go-junit-report || go get -u github.com/jstemmer/go-junit-report - which gocov || go get -u github.com/axw/gocov/gocov - which gocov-xml || go get -u github.com/AlekSi/gocov-xml - which golint || go get -u github.com/golang/lint/golint -} - -testing() { - test -f ${junit_report} && rm -f ${junit_report} - coverage_mode=$@ - output "Running ${coverage_mode} mode for coverage." - for pkg in $packages; do - f="$workdir/$(echo $pkg | tr / -).cover" - output "Testing coverage report for ${pkg}" - go test -v -cover -coverprofile=${f} -covermode=${coverage_mode} $pkg | tee -a ${junit_report} - done - - output "Convert all packages coverage report to $coverage_report" - echo "mode: $coverage_mode" > "$coverage_report" - grep -h -v "^mode:" "$workdir"/*.cover >> "$coverage_report" -} - -generate_cover_report() { - gocov convert ${coverage_report} | gocov-xml > ${coverage_xml_report} -} - -generate_junit_report() { - cat $junit_report | go-junit-report > ${junit_xml_report} -} - -generate_lint_report() { - for pkg in $packages; do - output "Go Lint report for ${pkg}" - golint ${pkg} | tee -a ${lint_report} - done -} - -generate_vet_report() { - for pkg in $packages; do - output "Go Vet report for ${pkg}" - go vet -n -x ${pkg} | tee -a ${vet_report} - done -} - -generate_cloc_report() { - case "$OSTYPE" in - darwin*) - which cloc || brew install cloc ;; - linux*) - which cloc || apt-get install cloc ;; - *) - curl https://raw.githubusercontent.com/AlDanial/cloc/master/cloc -o /usr/bin/cloc - chmod 755 /usr/bin/cloc - esac - - cloc --by-file --xml --out=${cloc_report} --exclude-dir=vendor,Godeps,.cover . -} - -case "$1" in - "") - show_help ;; - tool) - install_dependency_tool ;; - testing) - mode="set" - test -z $2 || mode=$2 - testing $mode ;; - coverage) - generate_cover_report ;; - junit) - generate_junit_report ;; - lint) - generate_lint_report ;; - vet) - generate_vet_report ;; - cloc) - generate_cloc_report ;; - *) - show_help ;; -esac