From 0f67503b9daf0e7e4d9909ad4b60f67ec8da5104 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Horv=C3=A1th=20D=C3=B3ra?= Date: Fri, 10 Mar 2023 20:14:43 +0100 Subject: [PATCH] HBASE-27685 Enable code coverage reporting to SonarQube in HBase (#5076) (cherry picked from commit 72d5a46899ef0866d777f0a9508649beed6d8dd3) --- dev-support/code-coverage/README.md | 49 ++++++++++++++ dev-support/code-coverage/run-coverage.sh | 78 +++++++++++++++++++++++ hbase-it/pom.xml | 2 +- pom.xml | 33 +++++++--- 4 files changed, 153 insertions(+), 9 deletions(-) create mode 100644 dev-support/code-coverage/README.md create mode 100755 dev-support/code-coverage/run-coverage.sh diff --git a/dev-support/code-coverage/README.md b/dev-support/code-coverage/README.md new file mode 100644 index 00000000000..0b3eaf044ac --- /dev/null +++ b/dev-support/code-coverage/README.md @@ -0,0 +1,49 @@ + + +# Code analysis + +The `run-coverage.sh` script runs maven with the **jacoco** profile +which generates the test coverage data for the java classes. +If the required parameters are given it also runs the sonar code analysis +and uploads the results to the given SonarQube Server. + +## Running code analysis + +After running the script the reports generated by the JaCoCo +code coverage library can be found under the `/target/site/jacoco/` folder of +the related modules. + +Here is how you can generate the code coverage report: + +```sh dev/code-coverage/run-coverage.sh``` + +## Publishing coverage results to SonarQube + +The required parameters for publishing the results to SonarQube are: + +- host URL, +- login credentials, +- project key + +The project name is an optional parameter. + +Here is an example command for running and publishing the coverage data: + +`./dev/code-coverage/run-coverage.sh -l ProjectCredentials +-u https://exampleserver.com -k Project_Key -n Project_Name` diff --git a/dev-support/code-coverage/run-coverage.sh b/dev-support/code-coverage/run-coverage.sh new file mode 100755 index 00000000000..2accb313a65 --- /dev/null +++ b/dev-support/code-coverage/run-coverage.sh @@ -0,0 +1,78 @@ +#!/usr/bin/env bash +# +# Licensed to the Apache Software Foundation (ASF) under one or more +# contributor license agreements. See the NOTICE file distributed with +# this work for additional information regarding copyright ownership. +# The ASF licenses this file to You under the Apache License, Version 2.0 +# (the "License"); you may not use this file except in compliance with +# the License. You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# + +usage() { + echo + echo "Options:" + echo " -h Display help" + echo " -u SonarQube Host URL" + echo " -l SonarQube Login Credentials" + echo " -k SonarQube Project Key" + echo " -n SonarQube Project Name" + echo + echo "Important:" + echo " The required parameters for publishing the coverage results to SonarQube are:" + echo " - Host URL" + echo " - Login Credentials" + echo " - Project Key" + echo +} + +execute() { + SCRIPT_DIR="$(cd -- "$(dirname -- "${BASH_SOURCE[0]}")" &>/dev/null && pwd)" + MAIN_POM="${SCRIPT_DIR}/../../pom.xml" + + echo "Running unit and integration tests with runAllTests profile" + + mvn -B -e -f "${MAIN_POM}" clean test -PrunAllTests -Pjacoco -Pbuild-with-jdk11 -fn + + echo "Starting verifying phase" + + mvn -B -e -f "${MAIN_POM}" verify -DskipTests -DskipITs -Pjacoco -Pbuild-with-jdk11 -fn + + echo "Starting sonar scanner analysis" + + # If the required parameters are given, the code coverage results are uploaded to the SonarQube Server + if [ -n "$SONAR_LOGIN" ] && [ -n "$SONAR_PROJECT_KEY" ] && [ -n "$SONAR_URL" ]; then + mvn -B -e -f "${MAIN_POM}" sonar:sonar -Dsonar.host.url="$SONAR_URL" \ + -Dsonar.login="$SONAR_LOGIN" -Dsonar.projectKey="$SONAR_PROJECT_KEY" \ + -Dsonar.projectName="$SONAR_PROJECT_NAME" -Pjacoco + fi + + echo "Build finished" +} + +while getopts ":u:l:k:n:h" option; do + case $option in + u) SONAR_URL=${OPTARG:-} ;; + l) SONAR_LOGIN=${OPTARG:-} ;; + k) SONAR_PROJECT_KEY=${OPTARG:-} ;; + n) SONAR_PROJECT_NAME=${OPTARG:-} ;; + h) # Display usage + usage + exit + ;; + \?) # Invalid option + echo "Error: Invalid option" + exit + ;; + esac +done + +# Start code analysis +execute diff --git a/hbase-it/pom.xml b/hbase-it/pom.xml index 0cbb83823d0..4cfac1f8849 100644 --- a/hbase-it/pom.xml +++ b/hbase-it/pom.xml @@ -308,7 +308,7 @@ 1800 -enableassertions -Xmx${failsafe.Xmx} -Djava.security.egd=file:/dev/./urandom -XX:+CMSClassUnloadingEnabled - -verbose:gc -XX:+PrintCommandLineFlags -XX:+PrintFlagsFinal + -verbose:gc -XX:+PrintCommandLineFlags -XX:+PrintFlagsFinal @{jacocoArgLine} diff --git a/pom.xml b/pom.xml index 65f491f7839..f2e4c83734c 100644 --- a/pom.xml +++ b/pom.xml @@ -648,6 +648,10 @@ 1.9 1.5.0-4 4.1.4 + + 0.8.8 + + 3.9.1.2184 @@ -705,8 +709,7 @@ -Dorg.apache.hbase.thirdparty.io.netty.leakDetection.level=advanced -Dio.opentelemetry.context.enableStrictContext=true - ${hbase-surefire.argLine} - 0.7.5.201505241946 + ${hbase-surefire.argLine} @{jacocoArgLine} 1.5.1 3.0.0 0.14.0 @@ -2905,7 +2908,8 @@ --add-opens java.base/jdk.internal.util.random=ALL-UNNAMED --add-exports java.base/jdk.internal.misc=ALL-UNNAMED --add-exports java.security.jgss/sun.security.krb5=ALL-UNNAMED - ${hbase-surefire.argLine} + ${hbase-surefire.argLine} + @{jacocoArgLine}