Skip to content

Commit 801f02d

Browse files
author
Dora Horvath
committed
HBASE-27685 Enable code coverage reporting to SonarQube in HBase
1 parent d2b0074 commit 801f02d

File tree

4 files changed

+162
-9
lines changed

4 files changed

+162
-9
lines changed
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
<!--
2+
Licensed to the Apache Software Foundation (ASF) under one
3+
or more contributor license agreements. See the NOTICE file
4+
distributed with this work for additional information
5+
regarding copyright ownership. The ASF licenses this file
6+
to you under the Apache License, Version 2.0 (the
7+
"License"); you may not use this file except in compliance
8+
with the License. You may obtain a copy of the License at
9+
10+
http://www.apache.org/licenses/LICENSE-2.0
11+
12+
Unless required by applicable law or agreed to in writing, software
13+
distributed under the License is distributed on an "AS IS" BASIS,
14+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
See the License for the specific language governing permissions and
16+
limitations under the License.
17+
-->
18+
19+
# Code analysis
20+
21+
The `run-coverage.sh` script runs maven with the **jacoco** profile
22+
which generates the test coverage data for the java classes.
23+
If the required parameters are given it also runs the sonar code analysis
24+
and uploads the results to the given SonarQube Server.
25+
26+
## Running code analysis
27+
28+
After running the script the reports generated by the JaCoCo
29+
code coverage library can be found under the `/target/site/jacoco/` folder of
30+
the related modules.
31+
32+
Here is how you can generate the code coverage report:
33+
34+
```sh dev/code-coverage/run-coverage.sh```
35+
36+
## Publishing coverage results to SonarQube
37+
38+
The required parameters for publishing the results to SonarQube are:
39+
40+
- host URL,
41+
- login credentials,
42+
- project key
43+
44+
The project name is an optional parameter.
45+
46+
Here is an example command for running and publishing the coverage data:
47+
48+
`./dev/code-coverage/run-coverage.sh -l ProjectCredentials
49+
-u https://exampleserver.com -k Project_Key -n Project_Name`
Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
#!/usr/bin/env bash
2+
#
3+
# Licensed to the Apache Software Foundation (ASF) under one or more
4+
# contributor license agreements. See the NOTICE file distributed with
5+
# this work for additional information regarding copyright ownership.
6+
# The ASF licenses this file to You under the Apache License, Version 2.0
7+
# (the "License"); you may not use this file except in compliance with
8+
# the License. You may obtain a copy of the License at
9+
#
10+
# http://www.apache.org/licenses/LICENSE-2.0
11+
#
12+
# Unless required by applicable law or agreed to in writing, software
13+
# distributed under the License is distributed on an "AS IS" BASIS,
14+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
# See the License for the specific language governing permissions and
16+
# limitations under the License.
17+
#
18+
19+
usage() {
20+
echo
21+
echo "Options:"
22+
echo " -h Display help"
23+
echo " -u SonarQube Host URL"
24+
echo " -l SonarQube Login Credentials"
25+
echo " -k SonarQube Project Key"
26+
echo " -n SonarQube Project Name"
27+
echo " -t Number of threads (example: 1 or 2C)."
28+
echo
29+
echo "Important:"
30+
echo " The required parameters for publishing the coverage results to SonarQube are:"
31+
echo " - Host URL"
32+
echo " - Login Credentials"
33+
echo " - Project Key"
34+
echo
35+
}
36+
37+
execute() {
38+
SCRIPT_DIR="$(cd -- "$(dirname -- "${BASH_SOURCE[0]}")" &>/dev/null && pwd)"
39+
MAIN_POM="${SCRIPT_DIR}/../../pom.xml"
40+
41+
# Check the syntax for the thread variable
42+
if [[ "$THREAD_COUNT" =~ ^[0-9]+([.][0-9]+)?$ ]] || [[ "$THREAD_COUNT" =~ ^[0-9]+([.][0-9]+)?[C]?$ ]]; then
43+
THREADS="-T ${THREAD_COUNT[0]}"
44+
else
45+
THREADS=""
46+
fi
47+
48+
echo "Running unit and integration tests with runAllTests profile"
49+
50+
mvn -B -e -f "${MAIN_POM}" clean test -PrunAllTests -Pjacoco -Pbuild-with-jdk11 -fn
51+
52+
echo "Starting verifying phase"
53+
54+
mvn -B -e -f "${MAIN_POM}" verify -DskipTests -DskipITs -Pjacoco -Pbuild-with-jdk11 -fn $THREADS
55+
56+
echo "Starting sonar scanner analysis"
57+
58+
# If the required parameters are given, the code coverage results are uploaded to the SonarQube Server
59+
if [ -n "$SONAR_LOGIN" ] && [ -n "$SONAR_PROJECT_KEY" ] && [ -n "$SONAR_URL" ]; then
60+
mvn -B -e -f "${MAIN_POM}" sonar:sonar -Dsonar.host.url="$SONAR_URL" \
61+
-Dsonar.login="$SONAR_LOGIN" -Dsonar.projectKey="$SONAR_PROJECT_KEY" \
62+
-Dsonar.projectName="$SONAR_PROJECT_NAME" -Pjacoco $THREADS
63+
fi
64+
65+
echo "Build finished"
66+
}
67+
68+
while getopts ":u:l:k:n:t:h" option; do
69+
case $option in
70+
u) SONAR_URL=${OPTARG:-} ;;
71+
l) SONAR_LOGIN=${OPTARG:-} ;;
72+
k) SONAR_PROJECT_KEY=${OPTARG:-} ;;
73+
n) SONAR_PROJECT_NAME=${OPTARG:-} ;;
74+
t) THREAD_COUNT=${OPTARG:-} ;;
75+
h) # Display usage
76+
usage
77+
exit
78+
;;
79+
\?) # Invalid option
80+
echo "Error: Invalid option"
81+
exit
82+
;;
83+
esac
84+
done
85+
86+
# Start code analysis
87+
execute

hbase-it/pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -285,7 +285,7 @@
285285
<forkedProcessTimeoutInSeconds>1800</forkedProcessTimeoutInSeconds>
286286
<argLine>-enableassertions -Xmx${failsafe.Xmx}
287287
-Djava.security.egd=file:/dev/./urandom -XX:+CMSClassUnloadingEnabled
288-
-verbose:gc -XX:+PrintCommandLineFlags -XX:+PrintFlagsFinal</argLine>
288+
-verbose:gc -XX:+PrintCommandLineFlags -XX:+PrintFlagsFinal @{jacocoArgLine}</argLine>
289289
</configuration>
290290
</plugin>
291291
<plugin>

pom.xml

Lines changed: 25 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -881,6 +881,10 @@
881881
<xz.version>1.9</xz.version>
882882
<zstd-jni.version>1.5.0-4</zstd-jni.version>
883883
<hbase-thirdparty.version>4.1.4</hbase-thirdparty.version>
884+
<!-- Coverage properties -->
885+
<jacoco.version>0.8.8</jacoco.version>
886+
<jacocoArgLine/>
887+
<sonar-maven-plugin.version>3.9.1.2184</sonar-maven-plugin.version>
884888
<!-- Intraproject jar naming properties -->
885889
<!-- TODO this is pretty ugly, but works for the moment.
886890
Modules are pretty heavy-weight things, so doing this work isn't too bad. -->
@@ -937,8 +941,7 @@
937941
-Dorg.apache.hbase.thirdparty.io.netty.leakDetection.level=advanced
938942
-Dio.opentelemetry.context.enableStrictContext=true</hbase-surefire.cygwin-argLine>
939943
<!-- Surefire argLine defaults to Linux, cygwin argLine is used in the os.windows profile -->
940-
<argLine>${hbase-surefire.argLine}</argLine>
941-
<jacoco.version>0.7.5.201505241946</jacoco.version>
944+
<argLine>${hbase-surefire.argLine} @{jacocoArgLine}</argLine>
942945
<extra.enforcer.version>1.5.1</extra.enforcer.version>
943946
<enforcer.version>3.0.0</enforcer.version>
944947
<restrict-imports.enforcer.version>0.14.0</restrict-imports.enforcer.version>
@@ -3220,7 +3223,8 @@
32203223
--add-opens java.base/jdk.internal.util.random=ALL-UNNAMED
32213224
--add-exports java.base/jdk.internal.misc=ALL-UNNAMED
32223225
--add-exports java.security.jgss/sun.security.krb5=ALL-UNNAMED
3223-
${hbase-surefire.argLine}</argLine>
3226+
${hbase-surefire.argLine}
3227+
@{jacocoArgLine}</argLine>
32243228
<!--
32253229
Value to use for surefire when running jdk11.
32263230
TODO: replicate logic for windows
@@ -3299,18 +3303,21 @@
32993303
<activation>
33003304
<activeByDefault>false</activeByDefault>
33013305
</activation>
3306+
<properties>
3307+
<sonar.exclusions>**/generated/**/*</sonar.exclusions>
3308+
<sonar.coverage.exclusions>**/generated/**/*,hbase-it/**,**/hbase-logging/**/*,**/hbase-testing-util/**/*,
3309+
**/hbase-protocol-shaded/**/*,**/hbase-external-blockcache/**/*,**/hbase-examples/**/*,
3310+
**/hbase-archetypes/**/*</sonar.coverage.exclusions>
3311+
</properties>
33023312
<build>
33033313
<plugins>
33043314
<plugin>
33053315
<groupId>org.jacoco</groupId>
33063316
<artifactId>jacoco-maven-plugin</artifactId>
33073317
<version>${jacoco.version}</version>
33083318
<configuration>
3309-
<systemPropertyVariables>
3310-
<jacoco-agent.destfile>target/jacoco.exec</jacoco-agent.destfile>
3311-
</systemPropertyVariables>
33123319
<excludes>
3313-
<exclude>**/generated/**/*.class</exclude>
3320+
<exclude>**/generated/**/*</exclude>
33143321
</excludes>
33153322
</configuration>
33163323
<executions>
@@ -3319,6 +3326,11 @@
33193326
<goals>
33203327
<goal>prepare-agent</goal>
33213328
</goals>
3329+
<phase>initialize</phase>
3330+
<configuration>
3331+
<propertyName>jacocoArgLine</propertyName>
3332+
<append>true</append>
3333+
</configuration>
33223334
</execution>
33233335
<execution>
33243336
<id>report</id>
@@ -3329,6 +3341,11 @@
33293341
</execution>
33303342
</executions>
33313343
</plugin>
3344+
<plugin>
3345+
<groupId>org.sonarsource.scanner.maven</groupId>
3346+
<artifactId>sonar-maven-plugin</artifactId>
3347+
<version>${sonar-maven-plugin.version}</version>
3348+
</plugin>
33323349
</plugins>
33333350
</build>
33343351
</profile>
@@ -3364,7 +3381,7 @@
33643381
</activation>
33653382
<properties>
33663383
<build.platform>cygwin</build.platform>
3367-
<argLine>${hbase-surefire.cygwin-argLine}</argLine>
3384+
<argLine>${hbase-surefire.cygwin-argLine} @{jacocoArgLine}</argLine>
33683385
</properties>
33693386
</profile>
33703387
<!-- this profile should match the name of the release profile in the root asf pom -->

0 commit comments

Comments
 (0)