Skip to content

Commit

Permalink
[BUILD] Run unit-tests within docker
Browse files Browse the repository at this point in the history
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 <gregory.haskins@gmail.com>
  • Loading branch information
ghaskins committed Nov 21, 2016
1 parent eb90b88 commit e4ce5b4
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 41 deletions.
39 changes: 22 additions & 17 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand All @@ -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
Expand Down Expand Up @@ -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

Expand All @@ -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)
Expand All @@ -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
Expand All @@ -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 \
Expand All @@ -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 $@
Expand All @@ -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 $</bin && tar -jc *) > $@

build/goshim.tar.bz2: $(GOSHIM_DEPS)
@echo "Creating $@"
@tar -jhc -C $(GOPATH)/src $(patsubst $(GOPATH)/src/%,%,$(GOSHIM_DEPS)) > $@
Expand Down
3 changes: 3 additions & 0 deletions images/testenv/Dockerfile.in
Original file line number Diff line number Diff line change
@@ -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
21 changes: 21 additions & 0 deletions unit-test/docker-compose.yml
Original file line number Diff line number Diff line change
@@ -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
28 changes: 4 additions & 24 deletions scripts/goUnitTests.sh → unit-test/run.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit e4ce5b4

Please sign in to comment.