Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Backport PR #15810 to 8.12: [CI] Send Java and ruby tests to sonarqube simultaneously #15819

Open
wants to merge 1 commit into
base: 8.12
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 28 additions & 8 deletions .buildkite/pull_request_pipeline.yml
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ steps:

source .buildkite/scripts/common/container-agent.sh
ci/unit_tests.sh ruby
artifact_paths:
- "coverage/coverage.json"

- label: ":java: Java unit tests"
key: "java-unit-tests"
Expand All @@ -45,19 +47,37 @@ steps:
retry:
automatic:
- limit: 3
env:
ENABLE_SONARQUBE: true
command: |
set -euo pipefail
if [[ $BUILDKITE_PULL_REQUEST == "false" ]]; then
# https://github.com/elastic/logstash/pull/15486 for background
export ENABLE_SONARQUBE="false"
else
source .buildkite/scripts/pull-requests/sonar-env.sh
fi

set -euo pipefail
source .buildkite/scripts/common/container-agent.sh
ci/unit_tests.sh java
artifact_paths:
- "**/build/test-results/javaTests/TEST-*.xml"
- "**/jacocoTestReport.xml"
- "**/build/classes/**/*.*"

- label: ":sonarqube: Continuous Code Inspection"
if: |
build.pull_request.id != null ||
build.branch == "main" ||
build.branch =~ /^[0-9]+\.[0-9]+\$/
env:
VAULT_SONAR_TOKEN_PATH: "kv/ci-shared/platform-ingest/elastic/logstash/sonar-analyze-token"
agents:
image: "docker.elastic.co/cloud-ci/sonarqube/buildkite-scanner:latest"
command:
- "buildkite-agent artifact download --step ruby-unit-tests coverage/coverage.json ."
- "buildkite-agent artifact download --step java-unit-tests **/jacocoTestReport.xml ."
- "buildkite-agent artifact download --step java-unit-tests **/build/classes/**/*.* ."
- "/scan-source-code.sh"
depends_on:
- "ruby-unit-tests"
- "java-unit-tests"
retry:
manual:
allowed: true

- label: ":lab_coat: Integration Tests / part 1"
key: "integration-tests-part-1"
Expand Down
9 changes: 0 additions & 9 deletions .buildkite/scripts/pull-requests/sonar-env.sh

This file was deleted.

4 changes: 3 additions & 1 deletion Gemfile.template
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,12 @@ gem "benchmark-ips", :group => :development
gem "ci_reporter_rspec", "~> 1", :group => :development
gem "flores", "~> 0.0.8", :group => :development
gem "json-schema", "~> 2", :group => :development
gem "logstash-devutils", "~> 2", :group => :development
gem "logstash-devutils", "~> 2.6.0", :group => :development
gem "rack-test", :require => "rack/test", :group => :development
gem "rspec", "~> 3.5", :group => :development
gem "webmock", "~> 3", :group => :development
gem "simplecov", "~> 0.22.0", :group => :development
gem "simplecov-json", require: false, :group => :development
gem "jar-dependencies", "= 0.4.1" # Gem::LoadError with jar-dependencies 0.4.2
gem "murmurhash3", "= 0.1.6" # Pins until version 0.1.7-java is released
gem "thwait"
Expand Down
9 changes: 1 addition & 8 deletions ci/docker_unit_tests.sh
Original file line number Diff line number Diff line change
@@ -1,10 +1,3 @@
#!/bin/bash
# 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 $@
ci/docker_run.sh logstash-unit-tests ci/unit_tests.sh $@
23 changes: 6 additions & 17 deletions ci/unit_tests.sh
Original file line number Diff line number Diff line change
Expand Up @@ -19,24 +19,13 @@ fi

SELECTED_TEST_SUITE=$1

if [[ $SELECTED_TEST_SUITE == $"java" ]]; then
SONAR_ARGS=()

if [[ $(echo $ENABLE_SONARQUBE | tr '[:lower:]' '[:upper:]') == "TRUE" ]]; then
SONAR_ARGS=(
"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"
)
fi
SONAR_ARGS=()
if [[ $(echo $ENABLE_SONARQUBE | tr '[:lower:]' '[:upper:]') == "TRUE" ]]; then
SONAR_ARGS=("jacocoTestReport")
export COVERAGE=true
fi

if [[ $SELECTED_TEST_SUITE == $"java" ]]; then
echo "Running Java Tests"
./gradlew javaTests "${SONAR_ARGS[@]}" --console=plain --warning-mode all
elif [[ $SELECTED_TEST_SUITE == $"ruby" ]]; then
Expand Down
1 change: 0 additions & 1 deletion logstash-core/lib/logstash/modules/logstash_config.rb
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@ def template
def configured_inputs(default = [], aliases = {})
name = "var.inputs"
values = get_setting(LogStash::Setting::SplittableStringArray.new(name, String, default))

aliases.each { |k, v| values << v if values.include?(k) }
aliases.invert.each { |k, v| values << v if values.include?(k) }
values.flatten.uniq
Expand Down
11 changes: 11 additions & 0 deletions sonar-project.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
sonar.projectKey=elastic_logstash_AYm_nEbQaV3I-igkX1q9
sonar.host.url=https://sonar.elastic.dev

sonar.exclusions=vendor/**, gradle/**, rakelib/**, logstash-core-plugin-api/**, licenses/**, qa/**, spec/**

# Ruby
sonar.ruby.coverage.reportPaths=coverage/coverage.json

# Java
sonar.coverage.jacoco.xmlReportPaths=**/jacocoTestReport.xml
sonar.java.binaries=**/build/classes
20 changes: 19 additions & 1 deletion spec/spec_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@
require "flores/random"
require "pathname"
require "stud/task"
require "logstash/devutils/rspec/spec_helper"
require "support/resource_dsl_methods"
require "support/mocks_classes"
require "support/helpers"
Expand All @@ -34,6 +33,19 @@
require "json"
require 'logstash/runner'

# Code coverage setup
if ENV['COVERAGE']
require 'simplecov'
require 'simplecov-json'

SimpleCov.formatter = SimpleCov::Formatter::JSONFormatter

SimpleCov.start do
add_filter 'spec/'
add_filter 'vendor/'
end
end

class JSONIOThingy < IO
def initialize; end
def flush; end
Expand All @@ -52,6 +64,12 @@ def puts(payload)
c.include LogStashHelper
c.extend LogStashHelper

if ENV['COVERAGE']
c.after(:suite) do
SimpleCov.result.format!
end
end

# Some tests mess with LogStash::SETTINGS, and data on the filesystem can leak state
# from one spec to another; run each spec with its own temporary data directory for `path.data`
c.around(:each) do |example|
Expand Down