How to get modules' coverage via sonar runner in jenkins - jenkins

What we have:
jenkins 1.6 + SonarQube 4.5.6 + sonnar-runner 2.4 + maven 3.0.5
Issue Description:
We use 'Invoke Standalone SonarQube Analysis' in jobs' configuration, and it works fine except that it cannot get test coverage while the analysis just try to parsing the <base_dir>/target/site/cobertura/coverage.xml. But we have coverage.xml in the modules directory instead, just like <base_dir>/module1/target/site/cobertura/coverage.xml
I have tried to set sonar modules, but nothing happened, we still cannot get the coverage data.
Previously, we used sonar-maven-plugin for the analysis, but after we upgrade to SonarQube 4.5.6, we met some issues, like api incompatibility, measure twice, and so we changed to use sonar-runner. sonar-maven-plugin works fine to parsing the modules' coverage.xml.
How can we retrieve the coverage data in the modules' directory.
Or how can we merge the modules coverage.xml into one coverage.xml in the root directory for sonar-runner to parse.
Any comment? Thanks very much!

Related

Testing Jenkins "Code Coverage API Plugin"

I am new to Jenkins, I have installed "Code Coverage API Plugin" in Jenkins, now want to test it.
I am trying to do steps from this page https://plugins.jenkins.io/code-coverage-api/.
But I did not understand where to put XML files from points 1 and 2.
Can anybody help me, please?
The tags present in the images are the plugins that will be added in the pom.xml of the application. By running mvn clean package cobertura will generate a report based on the formats provided, xml or html.
If the code is checked out to the job and after running "mvn clean package" cobertura report will be generated under project workspace target/site/cobertura
The xml path is provided to code coverage api to provide the graphical representation.
Jacaco is preferred for java versions >=8 whereas cobertura doesn't cover for java8 based functionalities.

Sonar Scanner coverage empty and test file analysis

Some context
I am trying to configure the plugin 'Sonar Scanner' in our Jenkins, the trigger and the project validation is "working", I added the " " because it is actually validating everything, even the test files and it is not checking the coverage.
Things you should know before
I have no access to Jenkins machine
I have no access to SonarQube machine
All plugins installed from Jenkins are the most recent versions
My Config
My project folder structure looks like this (Java project):
.git
.scannerwork
bl
commons
datafeed
keyfiles
persistence
post-aggregator
project-setup
webapp
webapp-server
.gitignore
pom.xml
My Sonar Scanner config is:
#required metadata
sonar.projectKey=inventory
sonar.projectName=Inventory1
sonar.language=java
sonar.sourceEncoding=UTF-8
#sonar.tests=**/.*Test.java
sonar.exclusions=**/*Dto*, **/*Entity*
sonar.binaries=build/classes
sonar.java.binaries=/data/application/jenkins/jenkins/workspace/$JOB_NAME
#Adding more info to the log
sonar.verbose=true
#Path to source directory
sonar.sources=/data/application/jenkins/jenkins/workspace/$JOB_NAME
The problems
Test coverage is 0
Checking the report I see that the current Scanner config checks everything, even the tests
So.. the questions are:
My test files are spread all around in the project, how can (or if it is even possible) to add a regex to the "sonar.tests"? i have been trying to do it but no success, all my tests ends with Test.java
How can I avoid properly Sonar to scan the test files.
Do I need jacoco as well in order to make this work or its possible to do it without it? (we are configuring the jacoco-maven in the project)
Thank you for your time!

Where does sonar store its code coverage results in Jenkins workspace?

We have a Jenkins master hosted by Cloudbees and we are using Sonar-as-a-Service.
We have a regular maven project, and we are using the Sonar "Post Build Action" to successfully collect code coverage. I see the code coverage % in Sonar. Everything works fine.
My question is this: If I examine the Jenkins workspace of this project, I cannot find the code coverage results. I thought it would be saved in an xml file somewhere in the target directory, but i do not see it. Does sonar save the results in the Jenkins workspace somewhere?
Thanks!
Sonar uses Jacoco by default for test coverage, this one uses a binary file afaik

Sonar does not shows up Code Coverage after build successful with Jenkins Sonar plugin

I am trying to get code coverage with Sonar and Jenkins. I see Jenkins' Sonar plugin successfully executes JUnit test cases and completes build successfully. But Sonar does not show Code Coverage results (always shows 0.0% as the code coverage) on the project. But Sonar does show "Unit test success".
I am using Maven with Jenkins and Sonar.
I get the below message in Jenkins logs while executing the Sonar plugin:
Project coverage is set to 0% as no JaCoCo execution data has been dumped: .../sonar/target/jacoco.exec
Can any one help me how to get correct code coverage on any Sonar project.
Just because Sonar invoked Surefire correctly (and you received the "Unit test success" message) doesn't mean that JaCoCo instrumented your code.
Try executing JaCoCo directly. You might find out why JaCoCo is failing directly:
mvn jacoco:prepare-agent test jacoco:report
JaCoCo will place jacoco.exec as well as its XML/HTML reports within target/jacoco. Or it will fail, and hopefully you'll have a better idea as to why.
One very common problem is that the JaCoCo javaagent will not run if you've changed the Surefire argLine at all, because jacoco:prepare-agent just sets the argLine property which in this scenario, is conveniently ignored. You can set prepare-agent's propertyName property to something else (like jacocoArgLine) and include that in your argLine config:
<argLine>-Xmx1024m ${jacocoArgLine}</argLine>
I read https://docs.sonarqube.org/display/PLUG/Code+Coverage+by+Unit+Tests+for+Java+Project and used cobertura as my code coverage plugin then I see code coverage displays for small projects. When I check for a big project in sonar I just see code coverage as - that means its empty. In logs I could find that Cobertura report was not found at /.../coverage.xml path.
coverage.xml was not generated due to OutOfMemeryError:heapspace. Since my project is such a big project when I set heap memory to 2GB and cobertura plugin memory to 1.5GB sonar gets code coverage displayed.
According to this blog post, you probably forget to set the sonar.binaries property in your project properties
Don’t forget sonar.binaries, otherwise, you might get something like « Project coverage is set to 0% since there is no directories with classes. » in your logs.
[...]
sonar.surefire.reportsPath=target/surefire-reports
sonar.jacoco.reportPath=target/jacoco.exec
sonar.binaries=target/classes
[...]
Your Sonar plug-in cannot find the report file generated by Jacoco.
In your pom(parent or child) look for the <destFile> tag under jacoco/prepare-agent execution. Change the name and the location to target/jacoco.exec and you are good to go!
Instead of using the Jenkins sonar plugin for running sonar, try to do it with maven, by executing the sonar goal. Usually this is easier then setting up an additional build step and tweaking all the paths till sonar finds all the relevant files.
In your case I guess it can read the junit/testng report, but can't find the jacoco results (jacoco.exec). Is the path sonar outputs(../sonar/target/jacoco.exec) correct?

Sonar displaying 0% Emma coverage report

I agree that this question is asked many a times by many people and many solutions are already been provided. However, after referring to umpteen links, not being able to display the Emma report in Sonar is driving me crazy. Below is a description of what I am trying to do.
My java project is a multi module maven project. We are using EMMA as the code coverage tool and Jenkins to build the project, Sonar to analyze the code and configured Sonar for our build job using the Jenkins sonar plugin. Up to this point, the story is great. The problem comes when I try to reuse the reports generated by Emma during Sonar analysis. There is no apparent error in the Sonar analysis logs during the build, however, in Sonar project dashboard the coverage report is 0%. Below is configuration data that I provide in the Sonar configuration section of my Jenkins build job.
-Dsonar.core.codeCoveragePlugin=emma
-Dsonar.emma.reportPath=/target
-Dsonar.dynamicAnalysis=reuseReports
-Dsonar.surefire.reportsPath=/target/surefire-reports
Sonar Version : 3.0
Emma plugin versions:
emma-maven-plugin : 1.0-alpha-2
Sonar Emma Plugin :1.0.1
emma : 2.0.5312
The Maven build generates the coverage.em files in the individual modules' target folder. No coverage.ec file is generated. Is it mandatory to have both coverage.ec and coverage.em files for Sonar?
The coverage.es,html,txt,xml files are generated in the project build directory/emma folder
The Sonar analysis logs for a module looks like below.
[INFO] [15:06:02.829] Sensor org.sonar.plugins.emma.EmmaSensor#bfc33...
[WARN] [15:06:02.844] Resource will be ignored in next Sonar versions, index is locked: com.mycompany.mypackage.tests.util.Activator
[INFO] [15:06:02.876] Sensor org.sonar.plugins.emma.EmmaSensor#bfc33 done: 47 ms
The above logs make me think that Sonar is indeed doing something with the generated Emma reports, but not displaying anything on the Sonar project dashboard.
Please let me know where I may be going wrong or what additional thing needs to be done to get this working.
I think this thread might me useful. http://sonar.15.n6.nabble.com/Receiving-quot-index-is-locked-quot-warning-when-Emma-sensor-runs-td4676490.html
It describes the same problem and gives you some workarounds

Resources