Skip to content

Commit e4ce5b4

Browse files
committed
[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 <[email protected]>
1 parent eb90b88 commit e4ce5b4

File tree

4 files changed

+50
-41
lines changed

4 files changed

+50
-41
lines changed

Makefile

+22-17
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,8 @@ OS=$(shell uname)
5454
CHAINTOOL_RELEASE=v0.10.0
5555
BASEIMAGE_RELEASE=$(shell cat ./.baseimage-release)
5656

57+
export GO_LDFLAGS
58+
5759
DOCKER_TAG=$(ARCH)-$(PROJECT_VERSION)
5860
BASE_DOCKER_TAG=$(ARCH)-$(BASEIMAGE_RELEASE)
5961

@@ -73,7 +75,7 @@ GOSHIM_DEPS = $(shell ./scripts/goListFiles.sh $(PKGNAME)/core/chaincode/shim |
7375
JAVASHIM_DEPS = $(shell git ls-files core/chaincode/shim/java)
7476
PROTOS = $(shell git ls-files *.proto | grep -v vendor)
7577
PROJECT_FILES = $(shell git ls-files)
76-
IMAGES = peer orderer ccenv javaenv
78+
IMAGES = peer orderer ccenv javaenv testenv
7779

7880
pkgmap.peer := $(PKGNAME)/peer
7981
pkgmap.orderer := $(PKGNAME)/orderer
@@ -103,8 +105,10 @@ peer-docker: build/image/peer/.dummy
103105
orderer: build/bin/orderer
104106
orderer-docker: build/image/orderer/.dummy
105107

106-
unit-test: peer-docker gotools
107-
@./scripts/goUnitTests.sh $(DOCKER_TAG) "$(GO_LDFLAGS)"
108+
testenv: build/image/testenv/.dummy
109+
110+
unit-test: peer-docker testenv
111+
cd unit-test && docker-compose up --abort-on-container-exit --force-recreate && docker-compose down
108112

109113
unit-tests: unit-test
110114

@@ -120,19 +124,6 @@ linter: gotools
120124
@echo "LINT: Running code checks.."
121125
@./scripts/golinter.sh
122126

123-
# We (re)build protoc-gen-go from within docker context so that
124-
# we may later inject the binary into a different docker environment
125-
# This is necessary since we cannot guarantee that binaries built
126-
# on the host natively will be compatible with the docker env.
127-
%/bin/protoc-gen-go: Makefile
128-
@echo "Building $@"
129-
@mkdir -p $(@D)
130-
@$(DRUN) \
131-
-v $(abspath vendor/github.com/golang/protobuf):/opt/gopath/src/github.com/golang/protobuf \
132-
-v $(abspath $(@D)):/opt/gopath/bin \
133-
hyperledger/fabric-baseimage:$(BASE_DOCKER_TAG) \
134-
go install github.com/golang/protobuf/protoc-gen-go
135-
136127
build/bin/chaintool: Makefile
137128
@echo "Installing chaintool"
138129
@mkdir -p $(@D)
@@ -159,6 +150,16 @@ build/docker/bin/%: $(PROJECT_FILES)
159150
build/bin:
160151
mkdir -p $@
161152

153+
build/docker/gotools/bin/protoc-gen-go: build/docker/gotools
154+
155+
build/docker/gotools: gotools/Makefile
156+
@mkdir -p $@/bin $@/obj
157+
@$(DRUN) \
158+
-v $(abspath $@):/opt/gotools \
159+
-w /opt/gopath/src/$(PKGNAME)/gotools \
160+
hyperledger/fabric-baseimage:$(BASE_DOCKER_TAG) \
161+
make install BINDIR=/opt/gotools/bin OBJDIR=/opt/gotools/obj
162+
162163
# Both peer and peer-docker depend on ccenv and javaenv (all docker env images it supports)
163164
build/bin/peer: build/image/ccenv/.dummy build/image/javaenv/.dummy
164165
build/image/peer/.dummy: build/image/ccenv/.dummy build/image/javaenv/.dummy
@@ -171,7 +172,7 @@ build/bin/%: $(PROJECT_FILES)
171172
@touch $@
172173

173174
# payload definitions'
174-
build/image/ccenv/payload: build/docker/bin/protoc-gen-go \
175+
build/image/ccenv/payload: build/docker/gotools/bin/protoc-gen-go \
175176
build/bin/chaintool \
176177
build/goshim.tar.bz2
177178
build/image/javaenv/payload: build/javashim.tar.bz2 \
@@ -182,6 +183,7 @@ build/image/peer/payload: build/docker/bin/peer \
182183
msp/peer-config.json
183184
build/image/orderer/payload: build/docker/bin/orderer \
184185
orderer/orderer.yaml
186+
build/image/testenv/payload: build/gotools.tar.bz2
185187

186188
build/image/%/payload:
187189
mkdir -p $@
@@ -198,6 +200,9 @@ build/image/%/.dummy: Makefile build/image/%/payload
198200
docker tag $(PROJECT_NAME)-$(TARGET) $(PROJECT_NAME)-$(TARGET):$(DOCKER_TAG)
199201
@touch $@
200202

203+
build/gotools.tar.bz2: build/docker/gotools
204+
(cd $</bin && tar -jc *) > $@
205+
201206
build/goshim.tar.bz2: $(GOSHIM_DEPS)
202207
@echo "Creating $@"
203208
@tar -jhc -C $(GOPATH)/src $(patsubst $(GOPATH)/src/%,%,$(GOSHIM_DEPS)) > $@

images/testenv/Dockerfile.in

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
FROM hyperledger/fabric-baseimage:_BASE_TAG_
2+
ADD payload/gotools.tar.bz2 /usr/local/bin/
3+
WORKDIR /opt/gopath/src/github.com/hyperledger/fabric

unit-test/docker-compose.yml

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
vp:
2+
image: hyperledger/fabric-peer
3+
expose:
4+
- 7051
5+
environment:
6+
- CORE_PEER_ADDRESSAUTODETECT=true
7+
volumes:
8+
- /var/run/docker.sock:/var/run/docker.sock
9+
10+
unit-tests:
11+
image: hyperledger/fabric-testenv
12+
links:
13+
- vp
14+
environment:
15+
- UNIT_TEST_PEER_IP=vp
16+
- GO_LDFLAGS
17+
- OUTPUT
18+
volumes:
19+
- /var/run/docker.sock:/var/run/docker.sock
20+
- ${GOPATH}/src/github.com/hyperledger/fabric:/opt/gopath/src/github.com/hyperledger/fabric
21+
command: ./unit-test/run.sh

scripts/goUnitTests.sh unit-test/run.sh

+4-24
Original file line numberDiff line numberDiff line change
@@ -2,37 +2,17 @@
22

33
set -e
44

5-
TAG=$1
6-
GO_LDFLAGS=$2
7-
8-
BASEIMAGE="hyperledger/fabric-peer"
9-
IMAGE=$BASEIMAGE
10-
11-
if [ "$TAG" != "" ]
12-
then
13-
IMAGE="$BASEIMAGE:$TAG"
14-
fi
15-
16-
echo "Running unit tests using $IMAGE"
17-
185
echo -n "Obtaining list of tests to run.."
196
# Some examples don't play nice with `go test`
20-
PKGS=`go list github.com/hyperledger/fabric/... | grep -v /vendor/ | \
7+
PKGS=`go list github.com/hyperledger/fabric/... 2> /dev/null | \
8+
grep -v /vendor/ | \
219
grep -v /build/ | \
2210
grep -v /examples/chaincode/chaintool/ | \
2311
grep -v /examples/chaincode/go/asset_management | \
2412
grep -v /examples/chaincode/go/utxo | \
25-
grep -v /examples/chaincode/go/rbac_tcerts_no_attrs`
26-
echo "DONE!"
27-
28-
echo -n "Starting peer.."
29-
CID=`docker run -dit -p 7051:7051 $IMAGE peer node start`
30-
cleanup() {
31-
echo "Stopping peer.."
32-
docker kill $CID 2>&1 > /dev/null
33-
}
34-
trap cleanup 0
13+
grep -v /examples/chaincode/go/rbac_tcerts_no_attrs`
3514
echo "DONE!"
3615

3716
echo "Running tests..."
3817
gocov test -ldflags "$GO_LDFLAGS" $PKGS -p 1 -timeout=20m | gocov-xml > report.xml
18+

0 commit comments

Comments
 (0)