Skip to content

Commit d37c3e9

Browse files
committed
adds coverage analysis and junit reports to tests
- collect golangci-lint output as junit - conditionally install required tools
1 parent 3eefb51 commit d37c3e9

8 files changed

+139
-176
lines changed

Diff for: .gitignore

+5-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,8 @@
11
**/*.swp
22
.DS_Store
33
_output
4-
.vscode/
4+
.vscode/
5+
junit*.xml
6+
checkstyle*.xml
7+
mco-unit-test-coverage.out
8+
mco-unit-test-coverage.html

Diff for: .golangci.yml

+2
Original file line numberDiff line numberDiff line change
@@ -112,3 +112,5 @@ linters-settings:
112112
confidence: medium
113113
excludes:
114114
- G204
115+
output:
116+
format: "checkstyle:checkstyle-golangci-lint.xml,junit-xml:junit-golangci-lint.xml,colored-line-number"

Diff for: Makefile

+53-16
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,12 @@ export GOLANGCI_LINT_CACHE=$(shell echo $${GOLANGCI_LINT_CACHE:-$$GOPATH/cache})
2222

2323
GOTAGS = "containers_image_openpgp exclude_graphdriver_devicemapper exclude_graphdriver_btrfs containers_image_ostree_stub"
2424

25+
UNITTEST_OPTS = -tags=$(GOTAGS) -count=1 -v ./cmd/... ./pkg/... ./lib/...
26+
2527
all: binaries
2628

2729
.PHONY: clean test test-unit test-e2e verify update install-tools
30+
2831
# Remove build artifaces
2932
# Example:
3033
# make clean
@@ -46,16 +49,27 @@ image:
4649
test: test-unit test-e2e
4750

4851
# Unit tests only (no active cluster required)
49-
test-unit:
50-
CGO_ENABLED=0 go test -tags=$(GOTAGS) -count=1 -v ./cmd/... ./pkg/... ./lib/...
52+
test-unit: install-go-junit-report
53+
ifdef ARTIFACT_DIR
54+
CGO_ENABLED=0 go test -coverprofile=mco-unit-test-coverage.out $(UNITTEST_OPTS) | ./hack/test-with-junit.sh $(@)
55+
go tool cover -html=mco-unit-test-coverage.out -o mco-unit-test-coverage.html
56+
# Move the test coverage report into ARTIFACT_DIR only when it is not the same as our current dir
57+
# This enables test coverage analysis to be collected locally by running:
58+
# $ ARTIFACT_DIR="$PWD" make test-unit
59+
if [[ "${PWD}" != "${ARTIFACT_DIR}" ]]; then \
60+
mv mco-unit-test-coverage.out mco-unit-test-coverage.html "${ARTIFACT_DIR}"; \
61+
fi
62+
else
63+
CGO_ENABLED=0 go test $(UNITTEST_OPTS)
64+
endif
5165

5266
# Run the code generation tasks.
5367
# Example:
5468
# make update
5569
update:
5670
hack/update-codegen.sh
5771
hack/update-templates.sh
58-
72+
5973
go-deps:
6074
go mod tidy
6175
go mod vendor
@@ -64,14 +78,42 @@ go-deps:
6478
chmod +x ./vendor/k8s.io/code-generator/generate-groups.sh
6579
chmod +x ./vendor/k8s.io/code-generator/generate-internal-groups.sh
6680

67-
install-tools:
81+
SETUP_ENVTEST := $(shell command -v setup-envtest 2> /dev/null)
82+
install-setup-envtest:
83+
ifdef SETUP_ENVTEST
84+
@echo "Found setup-envtest"
85+
else
86+
@echo "Installing setup-envtest"
87+
go install -mod= sigs.k8s.io/controller-runtime/tools/setup-envtest@latest
88+
endif
89+
90+
GO_JUNIT_REPORT := $(shell command -v go-junit-report 2> /dev/null)
91+
install-go-junit-report:
92+
ifdef GO_JUNIT_REPORT
93+
@echo "Found go-junit-report"
94+
go-junit-report --version
95+
else
96+
@echo "Installing go-junit-report"
97+
go install -mod= github.com/jstemmer/go-junit-report@latest
98+
endif
99+
100+
GOLANGCI_LINT := $(shell command -v golangci-lint 2> /dev/null)
101+
install-golangci-lint:
102+
ifdef GOLANGCI_LINT
103+
@echo "Found golangci-lint"
104+
golangci-lint --version
105+
else
106+
@echo "Installing golangci-lint"
68107
GO111MODULE=on go build -o $(GOPATH)/bin/golangci-lint ./vendor/github.com/golangci/golangci-lint/cmd/golangci-lint
108+
endif
109+
110+
install-tools: install-golangci-lint install-setup-envtest install-go-junit-report
69111

70112
# Run verification steps
71113
# Example:
72114
# make verify
73115
verify: install-tools
74-
golangci-lint run --build-tags=$(GOTAGS)
116+
./hack/golangci-lint.sh $(GOTAGS)
75117
hack/verify-codegen.sh
76118
hack/verify-templates.sh
77119

@@ -101,16 +143,11 @@ Dockerfile.rhel7: Dockerfile Makefile
101143
sed -e s,org/openshift/release,org/ocp/builder, -e s,/openshift/origin-v4.0:base,/ocp/4.0:base, < $<) > $@.tmp && mv $@.tmp $@
102144

103145
# This was copied from https://github.com/openshift/cluster-image-registry-operator
104-
test-e2e:
105-
go test -tags=$(GOTAGS) -failfast -timeout 110m -v$${WHAT:+ -run="$$WHAT"} ./test/e2e/
106-
107-
test-e2e-single-node:
108-
go test -tags=$(GOTAGS) -failfast -timeout 110m -v$${WHAT:+ -run="$$WHAT"} ./test/e2e-single-node/
146+
test-e2e: install-go-junit-report
147+
go test -tags=$(GOTAGS) -failfast -timeout 110m -v$${WHAT:+ -run="$$WHAT"} ./test/e2e/ | ./hack/test-with-junit.sh $(@)
109148

110-
bootstrap-e2e:
111-
./hack/bootstrap-e2e-test.sh
149+
test-e2e-single-node: install-go-junit-report
150+
go test -tags=$(GOTAGS) -failfast -timeout 110m -v$${WHAT:+ -run="$$WHAT"} ./test/e2e-single-node/ | ./hack/test-with-junit.sh $(@)
112151

113-
bootstrap-e2e-local:
114-
# Use GOTAGS to exclude the default CGO implementation of signatures, which is not used by MCO
115-
# but dragged in by containers/image/signature
116-
CGO_ENABLED=0 go test -tags=$(GOTAGS) -v$${WHAT:+ -run="$$WHAT"} ./test/e2e-bootstrap/
152+
bootstrap-e2e: install-go-junit-report install-setup-envtest
153+
CGO_ENABLED=0 go test -tags=$(GOTAGS) -v$${WHAT:+ -run="$$WHAT"} ./test/e2e-bootstrap/ | ./hack/test-with-junit.sh $(@)

Diff for: hack/bootstrap-e2e-test.sh

-34
This file was deleted.

Diff for: hack/fetch-ext-bins.sh

-124
This file was deleted.

Diff for: hack/golangci-lint.sh

+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
#!/usr/bin/env bash
2+
3+
set -euo pipefail
4+
5+
# This script exists so that we can ensure that the report files produced by
6+
# golangci-lint are still copied to the ARTIFACT_DIR (if it exists) even when
7+
# golangci-lint exits with a non-zero exit code, such as whenever it finds
8+
# issues.
9+
10+
REPO_ROOT=$(dirname "${BASH_SOURCE[0]}")/..
11+
12+
GOTAGS="${1:-""}"
13+
ARTIFACT_DIR="${ARTIFACT_DIR:-""}"
14+
15+
if [ ! -n "$GOTAGS" ]; then
16+
echo "No Go tags provided"
17+
exit 1
18+
fi
19+
20+
cd "$REPO_ROOT"
21+
22+
retval=0
23+
golangci-lint run --build-tags="$GOTAGS" || retval="$?";
24+
25+
if [ -n "$ARTIFACT_DIR" ] && [ -d "$ARTIFACT_DIR" ]; then
26+
if [ "$ARTIFACT_DIR" != "$PWD" ]; then
27+
mv checkstyle-golangci-lint.xml junit-golangci-lint.xml "$ARTIFACT_DIR"
28+
fi
29+
fi
30+
31+
exit "$retval"

Diff for: hack/lib

+5-1
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,13 @@ export MCD_DAEMONSET="daemonset/machine-config-daemon"
77
export MCD_CONTAINER_NAME="machine-config-daemon"
88
export ROOTFS_MCD_PATH="/rootfs/usr/local/bin/machine-config-daemon"
99

10+
exists() {
11+
builtin type -P "$1" &> /dev/null
12+
}
13+
1014
can_run() {
1115
# Check if we have an oc binary in our path
12-
if [ ! "$(which oc)" ]; then
16+
if ! exists 'oc'; then
1317
echo "oc not in path"
1418
exit 1
1519
fi

Diff for: hack/test-with-junit.sh

+43
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
#!/usr/bin/env bash
2+
3+
set -euo pipefail
4+
5+
# This script exists to process Go test output into junit-compatible output
6+
# whenever we are running in CI. Otherwise, it will just output all given input
7+
# to stdout as-is.
8+
9+
REPO_ROOT=$(dirname "${BASH_SOURCE[0]}")/..
10+
11+
MAKEFILE_TARGET="${1:-""}"
12+
OPENSHIFT_CI="${OPENSHIFT_CI:-""}"
13+
ARTIFACT_DIR="${ARTIFACT_DIR:-""}"
14+
15+
if [ ! -n "$MAKEFILE_TARGET" ]; then
16+
echo "No Makefile target provided"
17+
exit 1
18+
fi
19+
20+
if [ ! -p /dev/stdin ]; then
21+
echo "This script expects Go test output to be piped into it"
22+
exit 1
23+
fi
24+
25+
function generate_junit_report() {
26+
echo "CI env detected, run tests with junit report extraction"
27+
if [ -n "$ARTIFACT_DIR" ] && [ -d "$ARTIFACT_DIR" ]; then
28+
JUNIT_LOCATION="$ARTIFACT_DIR/junit-$MAKEFILE_TARGET.xml"
29+
echo "junit report location: $JUNIT_LOCATION"
30+
cat | tee >(go-junit-report > "$JUNIT_LOCATION")
31+
else
32+
echo "\$ARTIFACT_DIR not set or does not exists, no junit will be published"
33+
cat
34+
fi
35+
}
36+
37+
cd "$REPO_ROOT"
38+
39+
if [ "$OPENSHIFT_CI" == "true" ]; then # detect ci environment there
40+
generate_junit_report
41+
else
42+
cat
43+
fi

0 commit comments

Comments
 (0)