SonarGo code coverage 0%

We are running SonarQube v7.9.2 and SonarScanner v4.2.0.1873.

As far as I can tell I have modeled our build process after the documentation, but I continue to see 0% code coverage for all of my Go projects. At build time we run the following command to run tests and output the coverage file:

go test "./..." -coverprofile="coverage.out"

I’ve verified the coverage.out file is created in the root of the build workspace. Next we run the sonar scanner CLI. Here are the contents of the sonar.properties file in the root of the project:

sonar.host.url=https://sonar.xxxxxx.com
sonar.projectKey=fileIngestionService
sonar.sources=/var/jenkins_home/jobs/fileIngestionService/workspace
sonar.tests=/var/jenkins_home/jobs/fileIngestionService/workspace
sonar.exclusions=**/vendor/**
sonar.test.inclusions=**/*_test.go,**/testdata/**
sonar.go.tests.reportPaths=coverage.out
sonar.projectVersion=v2.8.3

And we simply run

/opt/sonar-scanner/bin/sonar-scanner

When the sonar scan runs, it appears to run successfully. I’ve tested changing the sonar.go.tests.reportPaths in the properties file to a different file name to ensure I get an error, which I do. When I change it back to the coverage.out file I do not get an error, so it appears sonar scanner is finding the coverage file.

Similarly I’ve also run the native go tool to verify that we do indeed see proper code coverage from the coverage.out. I’ve run:

go tool cover -func="coverage.out"

This outputs a bunch of info, but ultimately reports something like 53% overall code coverage, so I know the coverage info in the file seems correct.

On my SonarQube project; however, I see overall coverage at 0% with 927 lines to cover and 927 uncovered lines.

What am I doing wrong?

Here is a log of my sonar scanner output:

INFO: Scanner configuration file: /opt/sonar-scanner/conf/sonar-scanner.properties
INFO: Project root configuration file: /var/jenkins_home/jobs/fileIngestionService/workspace/sonar-project.properties
INFO: SonarQube Scanner 4.2.0.1873
INFO: Java 11.0.3 AdoptOpenJDK (64-bit)
INFO: Linux 3.13.0-170-generic amd64
INFO: User cache: /root/.sonar/cache
INFO: SonarQube server 7.9.2
INFO: Default locale: "en", source code encoding: "UTF-8" (analysis is platform dependent)
INFO: Load global settings
INFO: Load global settings (done) | time=195ms
INFO: Server id: DACD8EC7-AWYhBymxG4coyY0aj8Nh
INFO: User cache: /root/.sonar/cache
INFO: Load/download plugins
INFO: Load plugins index
INFO: Load plugins index (done) | time=59ms
INFO: Load/download plugins (done) | time=106ms
INFO: Loaded core extensions: developer-scanner
INFO: Process project properties
INFO: Execute project builders
INFO: Execute project builders (done) | time=3ms
INFO: Project key: fileIngestionService
INFO: Base dir: /var/jenkins_home/jobs/fileIngestionService/workspace
INFO: Working dir: /var/jenkins_home/jobs/fileIngestionService/workspace/.scannerwork
INFO: Load project settings for component key: 'fileIngestionService'
INFO: Load project settings for component key: 'fileIngestionService' (done) | time=22ms
INFO: Load project branches
INFO: Load project branches (done) | time=38ms
INFO: Load project pull requests
INFO: Load project pull requests (done) | time=20ms
INFO: Load branch configuration
INFO: Load branch configuration (done) | time=2ms
INFO: Load quality profiles
INFO: Load quality profiles (done) | time=40ms
INFO: Load active rules
INFO: Load active rules (done) | time=575ms
INFO: Indexing files...
INFO: Project configuration:
INFO:   Excluded sources: **/vendor/**, **/*_test.go, **/testdata/**
INFO:   Included tests: **/*_test.go, **/testdata/**
INFO: 73 files indexed
INFO: 0 files ignored because of inclusion/exclusion patterns
INFO: 0 files ignored because of scm ignore settings
INFO: Quality profile for go: Sonar way
INFO: ------------- Run sensors on module fileIngestionService
INFO: Load metrics repository
INFO: Load metrics repository (done) | time=30ms
WARNING: An illegal reflective access operation has occurred
WARNING: Illegal reflective access by net.sf.cglib.core.ReflectUtils$1 (file:/root/.sonar/cache/866bb1adbf016ea515620f1aaa15ec53/sonar-javascript-plugin.jar) to method java.lang.ClassLoader.defineClass(java.lang.String,byte[],int,int,java.security.ProtectionDomain)
WARNING: Please consider reporting this to the maintainers of net.sf.cglib.core.ReflectUtils$1
WARNING: Use --illegal-access=warn to enable warnings of further illegal reflective access operations
WARNING: All illegal access operations will be denied in a future release
INFO: Sensor SonarGo [go]
INFO: Load project repositories
INFO: Load project repositories (done) | time=22ms
INFO: Sensor SonarGo [go] (done) | time=1171ms
INFO: Sensor Go Unit Test Report [go]
INFO: Sensor Go Unit Test Report [go] (done) | time=9ms
INFO: Sensor JavaXmlSensor [java]
INFO: Sensor JavaXmlSensor [java] (done) | time=3ms
INFO: ------------- Run sensors on project
INFO: Sensor Zero Coverage Sensor
INFO: Sensor Zero Coverage Sensor (done) | time=51ms
INFO: Calculating CPD for 24 files
INFO: CPD calculation finished
INFO: Analysis report generated in 215ms, dir size=241 KB
INFO: Analysis report compressed in 97ms, zip size=98 KB
INFO: Analysis report uploaded in 147ms
INFO: ANALYSIS SUCCESSFUL, you can browse https://sonar.xxxxxxxx.com/dashboard?id=fileIngestionService
INFO: Note that you will be able to access the updated dashboard once the server has processed the submitted analysis report
INFO: More about the report processing at https://sonar.xxxxxxxx.com/api/ce/task?id=AW_4IhScs7vvXAmqlOZq
INFO: Analysis total time: 4.757 s
INFO: ------------------------------------------------------------------------
INFO: EXECUTION SUCCESS
INFO: ------------------------------------------------------------------------
INFO: Total time: 6.179s
INFO: Final Memory: 11M/44M
INFO: ------------------------------------------------------------------------

Any chance someone from Sonar might see this?

Hello @CameronG,

You should use sonar.go.coverage.reportPaths for test coverage, and not sonar.go.tests.reportPaths, used for test execution (see documentation).

Hope this helps.

Quentin

1 Like

Wow - I’ve been through those docs so many times and never noticed the 2 different parameters. So what is the purpose of the sonar.go.tests.reportPaths or what impact does it have? I don’t see any metrics in SonarQube that appear to relate to either test pass/fail or test execution time, which is the only thing I can see coming out of the json used for that.

My properties file excerpt now looks like this:

sonar.projectKey=fileDeliveryService
sonar.sources=/var/jenkins_home/jobs/TestCodeScan/workspace
sonar.tests=/var/jenkins_home/jobs/TestCodeScan/workspace
sonar.exclusions=**/vendor/**
sonar.test.inclusions=**/*_test.go,**/testdata/**
sonar.go.coverage.reportPaths=coverage.out
sonar.go.test.reportPaths=testreport.json
sonar.projectVersion=v2.2.0

So then my execution log looks like this, which references the coverage.out file, but not the testreport.json file:

INFO: Scanner configuration file: /opt/sonar-scanner/conf/sonar-scanner.properties
INFO: Project root configuration file: /var/jenkins_home/jobs/TestCodeScan/workspace/sonar-project.properties
INFO: SonarQube Scanner 4.2.0.1873
INFO: Java 11.0.3 AdoptOpenJDK (64-bit)
INFO: Linux 3.13.0-170-generic amd64
INFO: User cache: /root/.sonar/cache
INFO: SonarQube server 7.9.2
INFO: Default locale: "en", source code encoding: "UTF-8" (analysis is platform dependent)
INFO: Load global settings
INFO: Load global settings (done) | time=203ms
INFO: Server id: DACD8EC7-AWYhBymxG4coyY0aj8Nh
INFO: User cache: /root/.sonar/cache
INFO: Load/download plugins
INFO: Load plugins index
INFO: Load plugins index (done) | time=73ms
INFO: Load/download plugins (done) | time=131ms
INFO: Loaded core extensions: developer-scanner
INFO: Process project properties
INFO: Execute project builders
INFO: Execute project builders (done) | time=5ms
INFO: Project key: fileDeliveryService
INFO: Base dir: /var/jenkins_home/jobs/TestCodeScan/workspace
INFO: Working dir: /var/jenkins_home/jobs/TestCodeScan/workspace/.scannerwork
INFO: Load project settings for component key: 'fileDeliveryService'
INFO: Load project settings for component key: 'fileDeliveryService' (done) | time=22ms
INFO: Load project branches
INFO: Load project branches (done) | time=39ms
INFO: Load project pull requests
INFO: Load project pull requests (done) | time=18ms
INFO: Load branch configuration
INFO: Load branch configuration (done) | time=6ms
INFO: Load quality profiles
INFO: Load quality profiles (done) | time=51ms
INFO: Load active rules
INFO: Load active rules (done) | time=517ms
INFO: Indexing files...
INFO: Project configuration:
INFO:   Excluded sources: **/vendor/**, **/*_test.go, **/testdata/**
INFO:   Included tests: **/*_test.go, **/testdata/**
INFO: 23 files indexed
INFO: 0 files ignored because of inclusion/exclusion patterns
INFO: 0 files ignored because of scm ignore settings
INFO: Quality profile for go: Sonar way
INFO: ------------- Run sensors on module fileDeliveryService
INFO: Load metrics repository
INFO: Load metrics repository (done) | time=21ms
WARNING: An illegal reflective access operation has occurred
WARNING: Illegal reflective access by net.sf.cglib.core.ReflectUtils$1 (file:/root/.sonar/cache/54f6535c111cefad0fb6a09ba3e61922/sonar-javascript-plugin.jar) to method java.lang.ClassLoader.defineClass(java.lang.String,byte[],int,int,java.security.ProtectionDomain)
WARNING: Please consider reporting this to the maintainers of net.sf.cglib.core.ReflectUtils$1
WARNING: Use --illegal-access=warn to enable warnings of further illegal reflective access operations
WARNING: All illegal access operations will be denied in a future release
INFO: Sensor SonarCSS Rules [cssfamily]
INFO: No CSS, PHP, HTML or VueJS files are found in the project. CSS analysis is skipped.
INFO: Sensor SonarCSS Rules [cssfamily] (done) | time=2ms
INFO: Sensor SonarGo [go]
INFO: 10 source files to be analyzed
INFO: Load project repositories
INFO: Load project repositories (done) | time=21ms
INFO: Sensor SonarGo [go] (done) | time=755ms
INFO: 10/10 source files have been analyzed
INFO: Sensor Go Cover sensor for Go coverage [go]
INFO: Load coverage report from '/var/jenkins_home/jobs/TestCodeScan/workspace/coverage.out'
INFO: Sensor Go Cover sensor for Go coverage [go] (done) | time=18ms
INFO: Sensor JavaXmlSensor [java]
INFO: Sensor JavaXmlSensor [java] (done) | time=3ms
INFO: ------------- Run sensors on project
INFO: Sensor Zero Coverage Sensor
INFO: Sensor Zero Coverage Sensor (done) | time=28ms
INFO: Calculating CPD for 10 files
INFO: CPD calculation finished
INFO: Analysis report generated in 167ms, dir size=130 KB
INFO: Analysis report compressed in 51ms, zip size=41 KB
INFO: Analysis report uploaded in 62ms
INFO: ANALYSIS SUCCESSFUL, you can browse https://sonar.*****.com/dashboard?id=fileDeliveryService
INFO: Note that you will be able to access the updated dashboard once the server has processed the submitted analysis report
INFO: More about the report processing at https://sonar.*****.com/api/ce/task?id=AXAXOOXcIDmGW2gTKc2B
INFO: Analysis total time: 4.329 s
INFO: ------------------------------------------------------------------------
INFO: EXECUTION SUCCESS
INFO: ------------------------------------------------------------------------
INFO: Total time: 5.810s
INFO: Final Memory: 12M/47M
INFO: ------------------------------------------------------------------------

You understood the purpose correctly, you will be able to see the things you listed under the Measures tab of a project → Coverages → Tests.
That being said, if no tests results are provided/imported, the tab will not be shown.

In your case, the problem that I spotted is that you have a typo in your properties file: sonar.go.test.reportPaths should be sonar.go.tests.reportPaths. This could explain why you don’t see anything in the logs.

If you still have trouble, you can find here an example of test report file, and here the test for the import of the test execution.

1 Like

Ah OK - I didn’t realize there were metrics that would only be displayed if the information was supplied. I’ve corrected the typo and now see Tests #, duration, and failures being reported. I’ve run this same process through on a few of our projects now; however, and I only see the Go Unit Test Report data on the master branch. If I am on a short lived branch I see the Go Cover data, but no Unit Test Report data.

Is that expected behavior for some reason?

Also BTW - thank you so much for your help so far!

I saw that you continued the discussion there, and got an answer to your question.

I think the initial problem is solved, if you have others questions, feel free to open an other topic.

Best,
Quentin

You are right that the initial problem is solved. I didn’t get an answer to that new question exactly, so I posted something here for the new question: Why don't Unit Test failures show up in short term branches?

For anyone else who may find it helpful, now that my build process is working, here is a simplified run down of my settings and build process:

My first step is to run my tests and generate my a coverage report and test report:

go test "./..." -coverprofile="coverage.out" -covermode=count -json > report.json;

I also added an optional step to generate a go linter report using this 3rd party cli

golangci-lint run ./... --verbose --no-config --out-format checkstyle > golangci-lint.out

My sonar.properties file (which I generate at build time) has these settings in it:

sonar.host.url=https://sonar.xxxxx.com
sonar.projectKey=${PROJECT_KEY}
sonar.sources=${WORKSPACE}
sonar.tests=${WORKSPACE}
sonar.test.inclusions=**/*_test.go,**/testdata/**

Additional settings that I chose to configure from the SonarQube server under the Go settings:

sonar.go.exclusions=**/vendor/**,**/*_mock.go
sonar.go.tests.reportPaths=report.json
sonar.go.coverage.reportPaths=coverage.out
sonar.go.golangci-lint.reportPaths=golangci-lint.out

I also have some branching logic in my build script that depending on GIT_BRANCH and GIT_TAG_NAME values will set one or both of these values in my sonar.properties file:

sonar.projectVersion=${GIT_TAG_NAME}
sonar.branch.name=${GIT_BRANCH}

I then run the sonar scanner:

/opt/sonar-scanner/bin/sonar-scanner

Which then generates output from the scanner that looks like this:

INFO: Scanner configuration file: /opt/sonar-scanner/conf/sonar-scanner.properties
INFO: Project root configuration file: /var/jenkins_home/jobs/phcommon/workspace/sonar-project.properties
INFO: SonarQube Scanner 4.2.0.1873
INFO: Java 11.0.3 AdoptOpenJDK (64-bit)
INFO: Linux 3.13.0-170-generic amd64
INFO: User cache: /var/jenkins_home/.sonar/cache
INFO: SonarQube server 7.9.2
INFO: Default locale: "en", source code encoding: "UTF-8" (analysis is platform dependent)
INFO: Load global settings
INFO: Load global settings (done) | time=206ms
INFO: Server id: DACD8EC7-AWYhBymxG4coyY0aj8Nh
INFO: User cache: /var/jenkins_home/.sonar/cache
INFO: Load/download plugins
INFO: Load plugins index
INFO: Load plugins index (done) | time=75ms
INFO: Load/download plugins (done) | time=313ms
INFO: Loaded core extensions: developer-scanner
INFO: Process project properties
INFO: Execute project builders
INFO: Execute project builders (done) | time=2ms
INFO: Project key: phcommon
INFO: Base dir: /var/jenkins_home/jobs/phcommon/workspace
INFO: Working dir: /var/jenkins_home/jobs/phcommon/workspace/.scannerwork
INFO: Load project settings for component key: 'phcommon'
INFO: Load project settings for component key: 'phcommon' (done) | time=26ms
INFO: Load project branches
INFO: Load project branches (done) | time=37ms
INFO: Load project pull requests
INFO: Load project pull requests (done) | time=19ms
INFO: Load branch configuration
INFO: Load branch configuration (done) | time=1ms
INFO: Load quality profiles
INFO: Load quality profiles (done) | time=50ms
INFO: Detected Jenkins
INFO: Load active rules
INFO: Load active rules (done) | time=581ms
INFO: Indexing files...
INFO: Project configuration:
INFO:   Excluded sources: **/*_test.go, **/testdata/**
INFO:   Included tests: **/*_test.go, **/testdata/**
INFO: 34 files indexed
INFO: 0 files ignored because of inclusion/exclusion patterns
INFO: 0 files ignored because of scm ignore settings
INFO: Quality profile for go: Sonar way
INFO: ------------- Run sensors on module phcommon
INFO: Load metrics repository
INFO: Load metrics repository (done) | time=23ms
INFO: Sensor SonarCSS Rules [cssfamily]
INFO: No CSS, PHP, HTML or VueJS files are found in the project. CSS analysis is skipped.
INFO: Sensor SonarCSS Rules [cssfamily] (done) | time=1ms
INFO: Sensor SonarGo [go]
INFO: 17 source files to be analyzed
INFO: Load project repositories
INFO: Load project repositories (done) | time=31ms
INFO: Sensor SonarGo [go] (done) | time=1089ms
INFO: 17/17 source files have been analyzed
INFO: Sensor Go Unit Test Report [go]
INFO: Sensor Go Unit Test Report [go] (done) | time=63ms
INFO: Sensor Go Cover sensor for Go coverage [go]
INFO: Load coverage report from '/var/jenkins_home/jobs/phcommon/workspace/coverage.out'
WARN: File 'git.pursuanthealth.net/go/phcommon/net/apiauth_mock.go' is not included in the project, ignoring coverage
WARN: File 'git.pursuanthealth.net/go/phcommon/net/oauth2_mock.go' is not included in the project, ignoring coverage
WARN: File 'git.pursuanthealth.net/go/phcommon/net/apiclient_mock.go' is not included in the project, ignoring coverage
WARN: File 'git.pursuanthealth.net/go/phcommon/configuration/configuration_mock.go' is not included in the project, ignoring coverage
WARN: File 'git.pursuanthealth.net/go/phcommon/net/httpclient_mock.go' is not included in the project, ignoring coverage
INFO: Sensor Go Cover sensor for Go coverage [go] (done) | time=38ms
INFO: Sensor Import of GolangCI-Lint issues [go]
INFO: Importing /var/jenkins_home/jobs/phcommon/workspace/golangci-lint.out
INFO: Sensor Import of GolangCI-Lint issues [go] (done) | time=15ms
INFO: Sensor JavaXmlSensor [java]
INFO: Sensor JavaXmlSensor [java] (done) | time=1ms
INFO: ------------- Run sensors on project
INFO: Sensor Zero Coverage Sensor
INFO: Sensor Zero Coverage Sensor (done) | time=15ms
INFO: 2 files had no CPD blocks
INFO: Calculating CPD for 15 files
INFO: CPD calculation finished
INFO: Analysis report generated in 153ms, dir size=159 KB
INFO: Analysis report compressed in 66ms, zip size=59 KB
INFO: Analysis report uploaded in 82ms

Hopefully this will be helpful to someone else!

4 Likes

I tried all steps you guys mentioned with no luck, I generate the two files, coverage.out and test-result.json, load them and has the message WARN: Failed to find test file for package

I’m using github actions

  - name: SonarCloud Scan
    uses: sonarsource/sonarcloud-github-action@master
    env:
      GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
      SONAR_TOKEN: ${{ secrets.SONAR_TOKEN }}
    with:
      args: >
        -Dsonar.organization=aaaa
        -Dsonar.projectKey=test
        -Dsonar.sources=.
        -Dsonar.go.coverage.reportPaths=coverage.out
        -Dsonar.go.tests.reportPaths=test-report.json
        -Dsonar.tests=.
        -Dsonar.test.inclusions=**/*_test.go
        -Dsonar.cpd.exclusions=**/app.go,**/*.App.vue,**/vue.config.js
        -Dsonar.cpd.exclusions=**/app.go,**/App.vue
        -Dsonar.exclusions=**/*.py,bootstrap.min.css,**/registerServiceWorker.js,**/*_graphql*.go,**/dist/**,**/*.xsd,**/*.bat,**/*.xml,**/*.mod;**/*.sum,**/*.bat,**/*.sh,**/*.json
        -Dsonar.sourceEncoding=UTF-8

WARN: Failed to find test file for package comps/apps/pedido/tests and test TestImportaPedidoZip

coverage metric is not working in sonarcloud, I need it, I don’t know what to do anymore, does someone can help-me?