Skip to content

Commit 794a129

Browse files
Merge pull request #974 from jhadvig/junit
CONSOLE-4481: Add junit generation for unit and e2e testing
2 parents cf1abd6 + 23b02a0 commit 794a129

File tree

3 files changed

+116
-6
lines changed

3 files changed

+116
-6
lines changed

Makefile

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ all: build
33

44
# Include the library makefile
55
include $(addprefix ./vendor/github.com/openshift/build-machinery-go/make/, \
6-
golang.mk \
76
targets/openshift/deps-gomod.mk \
87
targets/openshift/images.mk \
98
targets/openshift/bindata.mk \
@@ -36,18 +35,29 @@ $(call build-image,ocp-console-operator,$(IMAGE_REGISTRY)/ocp/4.5:console-operat
3635
# $3 - manifests directory
3736
$(call add-profile-manifests,manifests,./profile-patches,./manifests)
3837

39-
GO_TEST_PACKAGES :=./pkg/... ./cmd/...
38+
GO_UNIT_TEST_PACKAGES :=./pkg/... ./cmd/...
4039

40+
# Run tests
4141
test: test-unit test-e2e
42-
.PHONY: test
4342

44-
test-unit:
43+
test-unit: install-go-junit-report
44+
./test-unit.sh PKG=$(GO_UNIT_TEST_PACKAGES)
4545
.PHONY: test-unit
4646

47-
test-e2e:
48-
KUBERNETES_CONFIG=${KUBECONFIG} go test -timeout 30m -v ./test/e2e/
47+
test-e2e: install-go-junit-report
48+
./test-e2e.sh
4949
.PHONY: test-e2e
5050

51+
# Automatically install go-junit-report if not found
52+
GO_JUNIT_REPORT := $(shell command -v go-junit-report 2> /dev/null)
53+
install-go-junit-report:
54+
ifndef GO_JUNIT_REPORT
55+
@echo "Installing go-junit-report..."
56+
go install -mod= github.com/jstemmer/go-junit-report@latest
57+
else
58+
@echo "go-junit-report already installed."
59+
go-junit-report --version
60+
endif
5161

5262
# Remove all build artifacts.
5363
#

test-e2e.sh

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
#!/usr/bin/env bash
2+
3+
set -e
4+
5+
ARTIFACT_DIR=${ARTIFACT_DIR:=/tmp/artifacts}
6+
7+
# https://ci-operator-configresolver-ui-ci.apps.ci.l2s4.p1.openshiftapps.com/help#env
8+
OPENSHIFT_CI=${OPENSHIFT_CI:=false}
9+
10+
echo "Running tests..."
11+
if [ "$OPENSHIFT_CI" = true ]; then
12+
KUBERNETES_CONFIG=${KUBECONFIG} go test -timeout 30m -v ./test/e2e/ 2>&1 | tee "$ARTIFACT_DIR/test.out"
13+
RESULT="${PIPESTATUS[0]}"
14+
go-junit-report < "$ARTIFACT_DIR/test.out" > "$ARTIFACT_DIR/junit.xml"
15+
if [ "$RESULT" -ne 0 ]; then
16+
exit 255
17+
fi
18+
else
19+
echo 'KUBERNETES_CONFIG=${KUBECONFIG} go test -timeout 30m -v ./test/e2e/'
20+
KUBERNETES_CONFIG=${KUBECONFIG} go test -timeout 30m -v ./test/e2e/
21+
fi
22+
23+
echo "Success"

test-unit.sh

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
#!/usr/bin/env bash
2+
3+
set -e
4+
5+
#
6+
# Run all tests (not including functional)
7+
# ./test-.sh
8+
# ./test-unit.sh -v
9+
#
10+
# Run tests for one package
11+
# PKG=./unit ./test-unit.sh
12+
# PKG=ssh ./test-unit.sh
13+
#
14+
15+
ARTIFACT_DIR=${ARTIFACT_DIR:=/tmp/artifacts}
16+
17+
export GOBIN=${PWD}/bin:${GOBIN}
18+
19+
# Use deps from vendor dir.
20+
export GOFLAGS="-mod=vendor"
21+
22+
# Invoke ./cover for HTML output
23+
COVER=${COVER:-"-cover"}
24+
25+
# https://ci-operator-configresolver-ui-ci.apps.ci.l2s4.p1.openshiftapps.com/help#env
26+
OPENSHIFT_CI=${OPENSHIFT_CI:=false}
27+
28+
TESTABLE="./pkg/... ./cmd/..."
29+
FORMATTABLE=(cmd pkg)
30+
31+
# user has not provided PKG override
32+
if [ -z "${PKG}" ]; then
33+
TEST=${TESTABLE}
34+
FMT=("${FORMATTABLE[@]}")
35+
36+
# user has provided PKG override
37+
else
38+
# strip out slashes and dots from PKG=./foo/
39+
TEST=${PKG//\//}
40+
TEST=${TEST//./}
41+
42+
# only run gofmt on packages provided by user
43+
FMT=("${TEST[@]}")
44+
fi
45+
46+
# split TEST into an array and prepend repo path to each local package
47+
read -ra split <<<"$TEST"
48+
TEST=("${split[@]/#/github.com/openshift/console-operator/}")
49+
50+
echo "Running tests..."
51+
if [ "$OPENSHIFT_CI" = true ]; then
52+
go test -v "${COVER}" "$@" "${TEST[@]}" 2>&1 | tee "$ARTIFACT_DIR/test.out"
53+
RESULT="${PIPESTATUS[0]}"
54+
go-junit-report < "$ARTIFACT_DIR/test.out" > "$ARTIFACT_DIR/junit.xml"
55+
if [ "$RESULT" -ne 0 ]; then
56+
exit 255
57+
fi
58+
else
59+
echo "go test "${COVER}" "$@" "${TEST[@]}""
60+
go test "${COVER}" "${TEST[@]}"
61+
fi
62+
63+
echo "Checking gofmt..."
64+
fmtRes=$(gofmt -l "${FMT[@]}")
65+
if [ -n "${fmtRes}" ]; then
66+
echo -e "gofmt checking failed:\n${fmtRes}"
67+
exit 255
68+
fi
69+
70+
echo "Checking govet..."
71+
vetRes=$(go vet "${TEST[@]}")
72+
if [ -n "${vetRes}" ]; then
73+
echo -e "govet checking failed:\n${vetRes}"
74+
exit 255
75+
fi
76+
77+
echo "Success"

0 commit comments

Comments
 (0)