Skip to content

Commit

Permalink
Sonar integration with Java Tests code coverage (#15279)
Browse files Browse the repository at this point in the history
* Sonar integration

* Removed sonar from test target
  • Loading branch information
pazone authored Sep 14, 2023
1 parent cd78558 commit f90647d
Show file tree
Hide file tree
Showing 5 changed files with 71 additions and 23 deletions.
8 changes: 4 additions & 4 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,10 @@ buildscript {
plugins {
id "de.undercouch.download" version "4.0.4"
id "com.dorongold.task-tree" version "2.1.0"
// id "jacoco"
// id "org.sonarqube" version "4.3.0.3225"
}


apply plugin: 'de.undercouch.download'
apply from: "rubyUtils.gradle"

Expand All @@ -57,7 +58,6 @@ allprojects {
apply plugin: 'java'
apply plugin: 'idea'
apply plugin: 'java-library'

project.sourceCompatibility = JavaVersion.VERSION_11
project.targetCompatibility = JavaVersion.VERSION_11

Expand Down Expand Up @@ -860,7 +860,7 @@ if (System.getenv('OSS') != 'true') {
dependsOn copyPluginTestAlias
dependsOn ":logstash-xpack:rubyTests"
}
tasks.register("runXPackIntegrationTests"){
tasks.register("runXPackIntegrationTests") {
dependsOn copyPluginTestAlias
dependsOn ":logstash-xpack:rubyIntegrationTests"
}
}
5 changes: 1 addition & 4 deletions ci/docker_run.sh
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
#!/bin/bash
# Syntax is docker_run.sh IMAGE_NAME SCRIPT_NAME *EXTRA_DOCKER_OPTS

set -x # We want verbosity here, this mostly runs on CI and we want to easily debug stuff

#Note - ensure that the -e flag is NOT set, and explicitly check the $? status to allow for clean up

REMOVE_IMAGE=false
DOCKER_EXTERNAL_JDK=""
if [ -z "$branch_specifier" ]; then
Expand All @@ -16,7 +13,7 @@ else
fi

if [ "$OSS" == "true" ]; then
DOCKER_ENV_OPTS="${DOCKER_ENV_OPTS} --env OSS=true"
DOCKER_ENV_OPTS="${DOCKER_ENV_OPTS} -e OSS=true"
fi

echo "Running Docker CI build for '$IMAGE_NAME' "
Expand Down
10 changes: 9 additions & 1 deletion ci/docker_unit_tests.sh
Original file line number Diff line number Diff line change
@@ -1,2 +1,10 @@
#!/bin/bash
ci/docker_run.sh logstash-unit-tests ci/unit_tests.sh $@
# Init vault
VAULT_TOKEN=$(vault write -field=token auth/approle/login role_id="$VAULT_ROLE_ID" secret_id="$VAULT_SECRET_ID")
export VAULT_TOKEN
unset VAULT_ROLE_ID VAULT_SECRET_ID

SONAR_TOKEN=$(vault read -field=token secret/logstash-ci/sonar-creds)
unset VAULT_TOKEN
DOCKER_ENV_OPTS="-e SONAR_TOKEN=${SONAR_TOKEN} -e SOURCE_BRANCH=$ghprbSourceBranch -e TARGET_BRANCH=$ghprbTargetBranch -e PULL_ID=$ghprbPullId -e COMMIT_SHA=$branch_specifier" \
ci/docker_run.sh logstash-unit-tests ci/unit_tests.sh $@
10 changes: 9 additions & 1 deletion ci/unit_tests.sh
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,15 @@ SELECTED_TEST_SUITE=$1

if [[ $SELECTED_TEST_SUITE == $"java" ]]; then
echo "Running Java Tests"
./gradlew javaTests --console=plain --warning-mode all
./gradlew javaTests jacocoTestReport sonar -Dsonar.token="${SONAR_TOKEN}" \
-Dsonar.host.url=https://sonar.elastic.dev \
-Dsonar.projectKey=elastic_logstash_AYm_nEbQaV3I-igkX1q9 \
-Dsonar.projectName=logstash \
-Dsonar.pullrequest.key=$PULL_ID \
-Dsonar.pullrequest.branch=$SOURCE_BRANCH \
-Dsonar.pullrequest.base=$TARGET_BRANCH \
-Dsonar.scm.revision=$COMMIT_SHA \
--console=plain --warning-mode all
elif [[ $SELECTED_TEST_SUITE == $"ruby" ]]; then
echo "Running Ruby unit tests"
./gradlew rubyTests --console=plain --warning-mode all
Expand Down
61 changes: 48 additions & 13 deletions logstash-core/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,39 @@
* under the License.
*/


buildscript {
repositories {
mavenCentral()
}
dependencies {
classpath "org.yaml:snakeyaml:${snakeYamlVersion}"
}
}

plugins {
id "jacoco"
id "org.sonarqube" version "4.3.0.3225"
}

apply plugin: 'jacoco'
apply plugin: "org.sonarqube"

repositories {
mavenCentral()
}

sonarqube {
properties {
property 'sonar.coverage.jacoco.xmlReportPaths', "${buildDir}/reports/jacoco/test/jacocoTestReport.xml"
}
}

jacoco {
toolVersion = "0.8.9"
}


import org.yaml.snakeyaml.Yaml

// fetch version from Logstash's main versions.yml file
Expand All @@ -30,19 +63,6 @@ String jrubyVersion = versionMap['jruby']['version']

String log4jVersion = '2.17.1'

repositories {
mavenCentral()
}

buildscript {
repositories {
mavenCentral()
}
dependencies {
classpath "org.yaml:snakeyaml:${snakeYamlVersion}"
}
}

tasks.register("sourcesJar", Jar) {
dependsOn classes
from sourceSets.main.allSource
Expand Down Expand Up @@ -102,8 +122,23 @@ tasks.register("javaTests", Test) {
exclude '/org/logstash/plugins/CounterMetricImplTest.class'
exclude '/org/logstash/plugins/factory/PluginFactoryExtTest.class'
exclude '/org/logstash/execution/ObservedExecutionTest.class'

jacoco {
enabled = true
destinationFile = layout.buildDirectory.file('jacoco/test.exec').get().asFile
classDumpDir = layout.buildDirectory.dir('jacoco/classpathdumps').get().asFile
}
}

jacocoTestReport {
reports {
xml.enabled true
html.enabled true
}
}

javaTests.finalizedBy(jacocoTestReport)

tasks.register("rubyTests", Test) {
inputs.files fileTree("${projectDir}/lib")
inputs.files fileTree("${projectDir}/spec")
Expand Down

0 comments on commit f90647d

Please sign in to comment.