Get code coverage working

This commit is contained in:
Arthur Barr
2017-11-08 16:46:14 +00:00
parent 7c91187ce3
commit 4874483f9c
5 changed files with 58 additions and 14 deletions

View File

@@ -12,11 +12,19 @@
# See the License for the specific language governing permissions and
# limitations under the License.
FROM mqadvanced:latest-x86_64
# Build stage to build Go code
FROM golang:1.9 as builder
WORKDIR /go/src/github.com/ibm-messaging/mq-container/
COPY cmd/ ./cmd
COPY pkg/ ./pkg
COPY vendor/ ./vendor
RUN go test -c -covermode=count ./cmd/runmqserver
FROM mq-advancedserver:latest-x86_64
# Copy in the version of the code instrumented for code coverage
COPY build/runmqserver.test /usr/local/bin/runmqserver.test
COPY --from=builder /go/src/github.com/ibm-messaging/mq-container/runmqserver.test /usr/local/bin/
RUN chmod +x /usr/local/bin/runmqserver.test \
&& mkdir -p /var/coverage/
ENTRYPOINT ["runmqserver.test", "-test", "-test.coverprofile", "/var/coverage/coverage.cov"]
ENTRYPOINT ["runmqserver.test", "-test", "-test.coverprofile", "/var/coverage/container.cov"]

View File

@@ -28,6 +28,7 @@ DOCKER_FULL_ADVANCEDSERVER = $(DOCKER_REPO_ADVANCEDSERVER):$(DOCKER_TAG)
TEST_OPTS_DOCKER ?=
# Options to `go test` for the Kubernetes tests
TEST_OPTS_KUBERNETES ?=
TEST_IMAGE ?= $(DOCKER_FULL_ADVANCEDSERVER)
.PHONY: default
default: build-devserver test
@@ -79,6 +80,16 @@ test-devserver:
cd pkg/name && go test
cd test/docker && TEST_IMAGE=$(DOCKER_FULL_DEVSERVER) go test
.PHONY: test-advancedserver-cover
test-advancedserver-cover:
cd pkg/name && go test
rm -f ./test/docker/coverage/*.cov
rm -f ./coverage/docker.*
cd test/docker && TEST_IMAGE=$(DOCKER_REPO_ADVANCEDSERVER):cover go test $(TEST_OPTS_DOCKER)
echo 'mode: count' > ./coverage/docker.cov
tail -q -n +2 ./test/docker/coverage/*.cov >> ./coverage/docker.cov
go tool cover -html=./coverage/docker.cov -o ./coverage/docker.html
.PHONY: test-kubernetes-devserver
test-kubernetes-devserver:
$(call test-kubernetes,$(DOCKER_REPO_DEVSERVER),$(DOCKER_TAG),"../../charts/ibm-mqadvanced-server-dev")
@@ -148,8 +159,8 @@ build-devserver: downloads/mqadv_dev903_ubuntu_x86-64.tar.gz
# $(DOCKER) tag mq-server:latest-$(DOCKER_TAG_ARCH) mq-server:9.0.3-$(DOCKER_TAG_ARCH)
.PHONY: build-advancedserver-cover
build-advancedserver-cover: build-advanced-server build-cov
$(DOCKER) build -t mq-advancedserver:cover -f Dockerfile-server.cover .
build-advancedserver-cover:
$(DOCKER) build -t $(DOCKER_REPO_ADVANCEDSERVER):cover -f Dockerfile-server.cover .
# .PHONY: build-web
# build-web: build downloads/CNJR7ML.tar.gz

View File

@@ -4,7 +4,7 @@
You need to ensure you have the following tools installed:
* [Docker](https://www.docker.com/)
* [Go](https://golang.org/)
* [Go](https://golang.org/) - only needed for running the tests
* [Glide](https://glide.sh/)
* [dep](https://github.com/golang/dep) (official Go dependency management tool)
* make
@@ -21,10 +21,27 @@ There are three main sets of tests:
### Running the tests
The unit and Docker tests can be run locally. For example:
```bash
```
make test-devserver
```
or:
```
make test-advancedserver
```
### Running the tests with code coverage
You can produce code coverage results from the Docker tests by running the following:
```
make build-advancedserver-cover
make test-advancedserver-cover
```
In order to generate code coverage metrics from the Docker tests, the build step creates a new Docker image with an instrumented version of the code. Each test is then run individually, producing a coverage report each under `test/docker/coverage/`. These individual reports are then combined. The combined report is written to the `coverage` directory.
### Running the Kubernetes tests
For the Kubernetes tests, you need to have built the Docker image, and pushed it to the registry used by your Kubernetes cluster. Most of the configuration used by the tests is picked up from your `kubectl` configuration, but you will typically need to specify the image details. For example:

View File

@@ -123,7 +123,6 @@ func TestWithVolume(t *testing.T) {
hostConfig := container.HostConfig{
Binds: []string{
coverageBind(t),
//"coverage:/var/coverage",
vol.Name + ":/mnt/mqm",
},
}

View File

@@ -41,12 +41,18 @@ func imageName() string {
return image
}
func coverageBind(t *testing.T) string {
// coverageDir returns the host directory to use for code coverage data
func coverageDir(t *testing.T) string {
dir, err := os.Getwd()
if err != nil {
t.Fatal(err)
}
return filepath.Join(dir, "coverage") + ":/var/coverage"
return filepath.Join(dir, "coverage")
}
// coverageBind returns a string to use to add a bind-mounted directory for code coverage data
func coverageBind(t *testing.T) string {
return coverageDir(t) + ":/var/coverage"
}
func cleanContainer(t *testing.T, cli *client.Client, ID string) {
@@ -62,10 +68,8 @@ func cleanContainer(t *testing.T, cli *client.Client, ID string) {
// Just log the error and continue
t.Log(err)
}
//waitForContainer(t, cli, ID, 20, container.WaitConditionNotRunning)
// TODO: This is probably no longer necessary
time.Sleep(20 * time.Second)
// If a code coverage file has been generated, then rename it to match the test name
os.Rename(filepath.Join(coverageDir(t), "container.cov"), filepath.Join(coverageDir(t), t.Name()+".cov"))
// Log the container output for any container we're about to delete
t.Logf("Console log from container %v:\n%v", ID, inspectLogs(t, cli, ID))
@@ -87,6 +91,8 @@ func runContainer(t *testing.T, cli *client.Client, containerConfig *container.C
if containerConfig.Image == "" {
containerConfig.Image = imageName()
}
// if coverage
containerConfig.Env = append(containerConfig.Env, "COVERAGE_FILE="+t.Name()+".cov")
hostConfig := container.HostConfig{
PortBindings: nat.PortMap{
"1414/tcp": []nat.PortBinding{
@@ -96,6 +102,9 @@ func runContainer(t *testing.T, cli *client.Client, containerConfig *container.C
},
},
},
Binds: []string{
coverageBind(t),
},
}
networkingConfig := network.NetworkingConfig{}
t.Logf("Running container (%s)", containerConfig.Image)