Skip to content

Commit 148ab3f

Browse files
committed
code coverage: implement JaCoCo report generation in CI
Motivation: To improve test quality visibility and enable coverage tracking, this implements JaCoCo coverage report generation for the whole project in the CI pipeline. Modifications: - CI: Added 3 new jobs to .gitlab-ci.yml: "run_ut_with_coverage" for executing tests and collecting coverage data "get_jacoco_cli" for fetching the JaCoCo CLI tool "generate_jacoco_report" for merging reports and generating final output - Scripts: Added 2 new scripts: get-jacoco.sh for caching/downloading JaCoCo CLI generate-jacoco-report.sh for report aggregation - RPM job: Disabled tests to prevent overhead. Results: - Per-module coverage data (.exec files) is generated during test execution - A merged HTML coverage report is created and archived as a CI artifact. Testing: - Validated report generation locally and in CI. Acked-by: Dmitry Litvintsev Target: master Require-book: no Require-notes: no Committed:
1 parent bebaeee commit 148ab3f

File tree

3 files changed

+154
-4
lines changed

3 files changed

+154
-4
lines changed

.gitlab-ci.yml

Lines changed: 55 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,6 @@ default:
9494
name: dtzar/helm-kubectl:latest
9595
entrypoint: ['']
9696

97-
9897
#
9998
# default cache konfiguration for maven build jobs
10099
# Cache downloaded dependencies and plugins between builds.
@@ -116,14 +115,12 @@ default:
116115
rules:
117116
- if: $CI_COMMIT_TAG
118117

119-
120-
121118
rpm:
122119
stage: build
123120
image: dcache/maven-java21-rpm-build
124121
extends: .build_cache
125122
script:
126-
- mvn $MAVEN_CLI_OPTS -Drun.slow.tests -am -pl packages/fhs -P rpm clean package
123+
- mvn $MAVEN_CLI_OPTS -DskipTests -Drun.slow.tests -am -pl packages/fhs -P rpm clean package
127124
artifacts:
128125
reports:
129126
junit:
@@ -167,6 +164,21 @@ tar:
167164
- "packages/tar/target/dcache-*.tar.gz"
168165
expire_in: 2 days
169166

167+
#Job to create and collect JaCoCo reports
168+
run_ut_with_coverage:
169+
stage: build
170+
image: maven:3.9.12-eclipse-temurin-17
171+
extends: .build_cache
172+
script:
173+
# run all tests with the coverage profile
174+
- mvn $MAVEN_CLI_OPTS -P code-coverage clean verify -DskipTests=false
175+
artifacts:
176+
paths:
177+
# pass the .exec files AND the compiled classes to the next stage
178+
#pass .exec files and compiled classes to the next stage
179+
- "**/target/coverage-reports/jacoco-ut.exec"
180+
- "**/target/classes/"
181+
expire_in: 1 day
170182

171183
spotbugs:
172184
stage: build
@@ -690,3 +702,42 @@ Run OIDC test:
690702
- while ! kubectl -n $K8S_NAMESPACE wait --for=condition=Ready pod oidc-tester; do sleep 1; done
691703
- kubectl -n $K8S_NAMESPACE cp .ci/run-oidc-test.sh oidc-tester:/run-oidc-test.sh
692704
- kubectl -n $K8S_NAMESPACE exec oidc-tester -- /bin/sh /run-oidc-test.sh
705+
706+
#Job to find JaCoCo CLI in cache or download it
707+
get_jacoco_cli:
708+
stage: build
709+
image: eclipse-temurin:17-jre
710+
variables:
711+
JACOCO_VERSION: "0.8.14"
712+
script:
713+
- ./get-jacoco.sh
714+
cache:
715+
key: "jacoco-cli-${JACOCO_VERSION}" #cache per version
716+
paths:
717+
- "jacoco-${JACOCO_VERSION}/" #cache the entire JaCoCo directory
718+
policy: pull
719+
artifacts:
720+
paths:
721+
- "jacoco-${JACOCO_VERSION}/lib/jacococli.jar"
722+
expire_in: 1 week
723+
724+
# Job to generate JaCoCo report using script
725+
generate_jacoco_report:
726+
stage: build
727+
image: eclipse-temurin:17-jre
728+
variables:
729+
JACOCO_VERSION: "0.8.14"
730+
needs:
731+
- job: run_ut_with_coverage
732+
artifacts: true
733+
- job: get_jacoco_cli
734+
cache:
735+
key: "jacoco-cli-${JACOCO_VERSION}"
736+
paths:
737+
- "jacoco-${JACOCO_VERSION}/"
738+
script:
739+
- ./generate-jacoco-report.sh
740+
artifacts:
741+
paths:
742+
- target/coverage-reports/site/
743+
expire_in: 1 week

generate-jacoco-report.sh

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
#!/bin/bash
2+
3+
# Define paths
4+
PROJECT_ROOT="${PROJECT_ROOT:-$(pwd)}"
5+
JACOCO_VERSION=0.8.14
6+
JACOCO_DIR="${PROJECT_ROOT}/jacoco-$JACOCO_VERSION"
7+
JACOCO_CLI_JAR="$JACOCO_DIR/lib/jacococli.jar"
8+
MERGED_EXEC="$PROJECT_ROOT/target/coverage-reports/merged.exec"
9+
REPORT_DIR="$PROJECT_ROOT/target/coverage-reports/site"
10+
11+
# Check if JaCoCo CLI JAR exists get-jacoco.sh
12+
if [ ! -f "$JACOCO_CLI_JAR" ]; then
13+
echo "Error: JaCoCo CLI JAR not found at $JACOCO_CLI_JAR"
14+
exit 1
15+
fi
16+
17+
# Ensure the report directory exists
18+
mkdir -p "$REPORT_DIR"
19+
20+
# Issue 1: Simplified find command (avoiding array initialization)
21+
EXEC_FILES=$(find "$PROJECT_ROOT" -name "jacoco-ut.exec" -type f)
22+
23+
# Issue 2: Simplified check for empty results using -z
24+
if [ -z "$EXEC_FILES" ]; then
25+
echo "Error: No jacoco-ut.exec files found in $PROJECT_ROOT"
26+
exit 1
27+
fi
28+
29+
# Merge execution data files
30+
echo "Merging execution data files..."
31+
# Issue 4: Passing the string directly
32+
java -jar "$JACOCO_CLI_JAR" merge ${EXEC_FILES} --destfile "$MERGED_EXEC"
33+
34+
if [ $? -ne 0 ]; then
35+
echo "Error: Failed to merge execution data files"
36+
exit 1
37+
fi
38+
39+
# Generate the coverage report
40+
echo "Generating coverage report..."
41+
42+
# Build classfiles and sourcefiles arguments dynamically
43+
CLASSFILES_ARGS=()
44+
SOURCEFILES_ARGS=()
45+
46+
# Issue 5: Iterating over the string list instead of an array
47+
for exec_file in ${EXEC_FILES}; do
48+
# Extract module path (e.g., core, dlm, rquota) from the exec file path
49+
module_path=$(dirname "$(dirname "$(dirname "$exec_file")")")
50+
51+
# Add classfiles and sourcefiles arguments
52+
CLASSFILES_ARGS+=("--classfiles" "$module_path/target/classes")
53+
SOURCEFILES_ARGS+=("--sourcefiles" "$module_path/src/main/java")
54+
done
55+
56+
# Generate the report with dynamic arguments
57+
java -jar "$JACOCO_CLI_JAR" report "$MERGED_EXEC" \
58+
"${CLASSFILES_ARGS[@]}" \
59+
"${SOURCEFILES_ARGS[@]}" \
60+
--html "$REPORT_DIR"
61+
62+
if [ $? -ne 0 ]; then
63+
echo "Error: Failed to generate coverage report"
64+
exit 1
65+
fi
66+
67+
echo "Coverage report generated successfully at ${REPORT_DIR}/index.html"

get-jacoco.sh

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
#!/bin/bash
2+
3+
apt-get update && apt-get install -y wget unzip
4+
# Define paths
5+
PROJECT_ROOT="${PROJECT_ROOT:-$(pwd)}"
6+
JACOCO_VERSION=0.8.14
7+
JACOCO_DIR="${PROJECT_ROOT}/jacoco-$JACOCO_VERSION"
8+
JACOCO_CLI_JAR="$JACOCO_DIR/lib/jacococli.jar"
9+
10+
echo "DEBUG: Checking if $JACOCO_CLI_JAR exists..."
11+
###
12+
#
13+
# Check if JaCoCo CLI JAR exists in the cache directory
14+
if [ ! -f "$JACOCO_CLI_JAR" ]; then
15+
echo "JaCoCo CLI JAR not found in cache directory. Downloading..."
16+
mkdir -p "$JACOCO_DIR"
17+
wget -q "https://github.com/jacoco/jacoco/releases/download/v$JACOCO_VERSION/jacoco-$JACOCO_VERSION.zip" -O "/tmp/jacoco-$JACOCO_VERSION.zip"
18+
unzip -q "/tmp/jacoco-$JACOCO_VERSION.zip" -d "$JACOCO_DIR"
19+
rm -f "/tmp/jacoco-$JACOCO_VERSION.zip"
20+
echo "DEBUG: Downloaded JaCoCo CLI to $JACOCO_DIR"
21+
ls -la "$JACOCO_DIR"
22+
else
23+
echo "DEBUG: JaCoCo CLI JAR found at $JACOCO_CLI_JAR"
24+
fi
25+
26+
# Check if JaCoCo CLI JAR exists after download
27+
if [ ! -f "$JACOCO_CLI_JAR" ]; then
28+
echo "Error: JaCoCo CLI JAR not found at $JACOCO_CLI_JAR"
29+
exit 1
30+
fi
31+
32+
export JACOCO_CLI_JAR

0 commit comments

Comments
 (0)