From e4ce5b43a56947935d5994abc7a27f45d70a8015 Mon Sep 17 00:00:00 2001 From: Gregory Haskins Date: Fri, 4 Nov 2016 15:12:09 -0400 Subject: [PATCH] [BUILD] Run unit-tests within docker We create a new docker container "testenv" and use this to execute unit tests. We will add support for running other aspects of the build (linter, protoc, behave) later in the series. Change-Id: Ic6cf20a5cd04b412e6bd786b6f13809fef413677 Signed-off-by: Greg Haskins --- Makefile | 39 ++++++++++++---------- images/testenv/Dockerfile.in | 3 ++ unit-test/docker-compose.yml | 21 ++++++++++++ scripts/goUnitTests.sh => unit-test/run.sh | 28 +++------------- 4 files changed, 50 insertions(+), 41 deletions(-) create mode 100644 images/testenv/Dockerfile.in create mode 100644 unit-test/docker-compose.yml rename scripts/goUnitTests.sh => unit-test/run.sh (50%) diff --git a/Makefile b/Makefile index 8f279e50639..46ec53141d6 100644 --- a/Makefile +++ b/Makefile @@ -54,6 +54,8 @@ OS=$(shell uname) CHAINTOOL_RELEASE=v0.10.0 BASEIMAGE_RELEASE=$(shell cat ./.baseimage-release) +export GO_LDFLAGS + DOCKER_TAG=$(ARCH)-$(PROJECT_VERSION) BASE_DOCKER_TAG=$(ARCH)-$(BASEIMAGE_RELEASE) @@ -73,7 +75,7 @@ GOSHIM_DEPS = $(shell ./scripts/goListFiles.sh $(PKGNAME)/core/chaincode/shim | JAVASHIM_DEPS = $(shell git ls-files core/chaincode/shim/java) PROTOS = $(shell git ls-files *.proto | grep -v vendor) PROJECT_FILES = $(shell git ls-files) -IMAGES = peer orderer ccenv javaenv +IMAGES = peer orderer ccenv javaenv testenv pkgmap.peer := $(PKGNAME)/peer pkgmap.orderer := $(PKGNAME)/orderer @@ -103,8 +105,10 @@ peer-docker: build/image/peer/.dummy orderer: build/bin/orderer orderer-docker: build/image/orderer/.dummy -unit-test: peer-docker gotools - @./scripts/goUnitTests.sh $(DOCKER_TAG) "$(GO_LDFLAGS)" +testenv: build/image/testenv/.dummy + +unit-test: peer-docker testenv + cd unit-test && docker-compose up --abort-on-container-exit --force-recreate && docker-compose down unit-tests: unit-test @@ -120,19 +124,6 @@ linter: gotools @echo "LINT: Running code checks.." @./scripts/golinter.sh -# We (re)build protoc-gen-go from within docker context so that -# we may later inject the binary into a different docker environment -# This is necessary since we cannot guarantee that binaries built -# on the host natively will be compatible with the docker env. -%/bin/protoc-gen-go: Makefile - @echo "Building $@" - @mkdir -p $(@D) - @$(DRUN) \ - -v $(abspath vendor/github.com/golang/protobuf):/opt/gopath/src/github.com/golang/protobuf \ - -v $(abspath $(@D)):/opt/gopath/bin \ - hyperledger/fabric-baseimage:$(BASE_DOCKER_TAG) \ - go install github.com/golang/protobuf/protoc-gen-go - build/bin/chaintool: Makefile @echo "Installing chaintool" @mkdir -p $(@D) @@ -159,6 +150,16 @@ build/docker/bin/%: $(PROJECT_FILES) build/bin: mkdir -p $@ +build/docker/gotools/bin/protoc-gen-go: build/docker/gotools + +build/docker/gotools: gotools/Makefile + @mkdir -p $@/bin $@/obj + @$(DRUN) \ + -v $(abspath $@):/opt/gotools \ + -w /opt/gopath/src/$(PKGNAME)/gotools \ + hyperledger/fabric-baseimage:$(BASE_DOCKER_TAG) \ + make install BINDIR=/opt/gotools/bin OBJDIR=/opt/gotools/obj + # Both peer and peer-docker depend on ccenv and javaenv (all docker env images it supports) build/bin/peer: build/image/ccenv/.dummy build/image/javaenv/.dummy build/image/peer/.dummy: build/image/ccenv/.dummy build/image/javaenv/.dummy @@ -171,7 +172,7 @@ build/bin/%: $(PROJECT_FILES) @touch $@ # payload definitions' -build/image/ccenv/payload: build/docker/bin/protoc-gen-go \ +build/image/ccenv/payload: build/docker/gotools/bin/protoc-gen-go \ build/bin/chaintool \ build/goshim.tar.bz2 build/image/javaenv/payload: build/javashim.tar.bz2 \ @@ -182,6 +183,7 @@ build/image/peer/payload: build/docker/bin/peer \ msp/peer-config.json build/image/orderer/payload: build/docker/bin/orderer \ orderer/orderer.yaml +build/image/testenv/payload: build/gotools.tar.bz2 build/image/%/payload: mkdir -p $@ @@ -198,6 +200,9 @@ build/image/%/.dummy: Makefile build/image/%/payload docker tag $(PROJECT_NAME)-$(TARGET) $(PROJECT_NAME)-$(TARGET):$(DOCKER_TAG) @touch $@ +build/gotools.tar.bz2: build/docker/gotools + (cd $ $@ + build/goshim.tar.bz2: $(GOSHIM_DEPS) @echo "Creating $@" @tar -jhc -C $(GOPATH)/src $(patsubst $(GOPATH)/src/%,%,$(GOSHIM_DEPS)) > $@ diff --git a/images/testenv/Dockerfile.in b/images/testenv/Dockerfile.in new file mode 100644 index 00000000000..425a946620d --- /dev/null +++ b/images/testenv/Dockerfile.in @@ -0,0 +1,3 @@ +FROM hyperledger/fabric-baseimage:_BASE_TAG_ +ADD payload/gotools.tar.bz2 /usr/local/bin/ +WORKDIR /opt/gopath/src/github.com/hyperledger/fabric diff --git a/unit-test/docker-compose.yml b/unit-test/docker-compose.yml new file mode 100644 index 00000000000..ee44591574d --- /dev/null +++ b/unit-test/docker-compose.yml @@ -0,0 +1,21 @@ +vp: + image: hyperledger/fabric-peer + expose: + - 7051 + environment: + - CORE_PEER_ADDRESSAUTODETECT=true + volumes: + - /var/run/docker.sock:/var/run/docker.sock + +unit-tests: + image: hyperledger/fabric-testenv + links: + - vp + environment: + - UNIT_TEST_PEER_IP=vp + - GO_LDFLAGS + - OUTPUT + volumes: + - /var/run/docker.sock:/var/run/docker.sock + - ${GOPATH}/src/github.com/hyperledger/fabric:/opt/gopath/src/github.com/hyperledger/fabric + command: ./unit-test/run.sh diff --git a/scripts/goUnitTests.sh b/unit-test/run.sh similarity index 50% rename from scripts/goUnitTests.sh rename to unit-test/run.sh index a0b365e4df5..99f15bc23c9 100755 --- a/scripts/goUnitTests.sh +++ b/unit-test/run.sh @@ -2,37 +2,17 @@ set -e -TAG=$1 -GO_LDFLAGS=$2 - -BASEIMAGE="hyperledger/fabric-peer" -IMAGE=$BASEIMAGE - -if [ "$TAG" != "" ] -then - IMAGE="$BASEIMAGE:$TAG" -fi - -echo "Running unit tests using $IMAGE" - echo -n "Obtaining list of tests to run.." # Some examples don't play nice with `go test` -PKGS=`go list github.com/hyperledger/fabric/... | grep -v /vendor/ | \ +PKGS=`go list github.com/hyperledger/fabric/... 2> /dev/null | \ + grep -v /vendor/ | \ grep -v /build/ | \ grep -v /examples/chaincode/chaintool/ | \ grep -v /examples/chaincode/go/asset_management | \ grep -v /examples/chaincode/go/utxo | \ - grep -v /examples/chaincode/go/rbac_tcerts_no_attrs` -echo "DONE!" - -echo -n "Starting peer.." -CID=`docker run -dit -p 7051:7051 $IMAGE peer node start` -cleanup() { - echo "Stopping peer.." - docker kill $CID 2>&1 > /dev/null -} -trap cleanup 0 + grep -v /examples/chaincode/go/rbac_tcerts_no_attrs` echo "DONE!" echo "Running tests..." gocov test -ldflags "$GO_LDFLAGS" $PKGS -p 1 -timeout=20m | gocov-xml > report.xml +