#HELP : 0% Coverage in sonarqube server

I’m working on a project where code coverage needs to display in Sonarqube.
We are framing dockerfile.test where at ENTRYPOINT nosetests is provided. setup.cfg file is created putting all required nosetests plugins. Then sonar-project.properties file has been created for sonarqube reference. And a stage named “Test/Sonar” is created in jenkins ,

below has the script of all above mentioned files.

FROM python:3.7-slim
RUN apt-get update && apt-get install -y --no-install-recommends gcc gfortran perl libtest-utf8-perl \
libtest-file-perl libtest-files-perl cpanminus build-essential curl libcurl4-openssl-dev libssl-dev \
gfortran liblzma-dev libblas-dev liblapack-dev libpq-dev  procps libldap2-dev libsasl2-dev ldap-utils  git

RUN cpanm install File::Copy::Recursive

RUN pip install numpy && pip install scipy pandas scikit-learn
COPY requirements.txt /requirements.txt
RUN pip install --no-cache-dir -r /requirements.txt 
COPY test_requirements.txt /test_requirements.txt
RUN pip install --no-cache-dir -r /test_requirements.txt 

COPY . /app
WORKDIR /app

#Install Mim
RUN python mim-python-client/setup.py install
RUN mkdir -p /local/tmp/mim-cache
RUN chmod -R 777 /local/tmp/mim-cache

#Install Extract
WORKDIR /app/extract-rules
RUN bash --posix ./dev/build.sh
RUN cp ./bin/python_parser.py /opt/extract/bin/


WORKDIR /app/backend/worker

ENV PYTHONUNBUFFERED=1

ENTRYPOINT nosetests
[nosetests]
verbose=3 
with-xunit=1
xunit-file=nosetests.xml
with-coverage=1
cover-xml=1
cover-package=.
exclude-dir=test
sonar.projectKey=IPA_NIP
sonar.projectName=IPA_NIP
sonar.projectVersion=1.0.0
sonar.language=py
sonar.sourceEncoding=UTF-8
sonar.python.coverage.reportPaths=coverage.xml
sonar.python.xunit.reportPaths=nosetests.xml
sonar.python.xunit.skipDetails=false
sonar.sources=backend/orch, backend/worker, webserver/pinap 
sonar.typescript.lcov.reportPaths=coverage/IPA_NIP/lcov.info
sonar.python.coveragePlugin=cobertura
                stage ('Test/Sonar') {
                    
					steps {
						// This specific examples expects that you have a Dockerfile.test that you build, then run to generate test results.
						// Since different projects can vary however, you should make sure you use the solution that works best for you.
						sh "docker build . -f Dockerfile.test -t python-test:${BUILD_ID}"

						sh '''docker run -u 50001255:25 -v "$(pwd)":/app python-test:${BUILD_ID}'''
						sh '''sed -i 's|filename="|filename="'"$(pwd)"'/|' coverage.xml'''
                        
						sonarScan('Sonar')
					}

					// Make test results visible in Jenkins UI if the install step completed successfully
					post {
						success {
							junit testResults: 'nosetests.xml', allowEmptyResults: true
						}
					}
                }

I couldn’t able to find why I’m getting 0% coverage at sonarqube.

dockerfile.test is executing without any error. I’m getting status about tests (Unit test) as OK. But I’m getting a warning

WARN: The report '/apps/dftjenkins/jenkins_node/workspace/v_AutomationAuthenticationScript/coverage.xml' seems to be empty, ignoring. '{}'
org.sonar.plugins.python.EmptyReportException: null

Could anyone help me out with my queries?

Hi,

Welcome to the community!

Could you provide the full analysis log? Without more context, it’s difficult to know what sensor emitted that warning. From your analysis parameters, I guess it correlates to the sonar.python.coverage.reportPaths value, which gives a relative path to coverage.xml in project root. Is that actually where the file exists? In the directory analysis starts from?

And is your file-level coverage at 0% or just for Python? Because it seems that you’re also providing a TypeScript coverage report, but that parameter key should be sonar.javascript.lcov.reportPaths (sonar.javascript, not sonar.typescript). Yes, I know it’s counterintuitive.

BTW, according to the docs, the XUnit parameter should be sonar.python.xunit.reportPath - no ‘s’ - although I won’t guarantee there’s no backward compatibility magic taking place.

Also, these analysis parameters aren’t doing anything and can be dropped:

sonar.langauge=py
...
sonar.python.coveragePlugin=cobertura

 
HTH,
Ann

Thanks ganncamp,
Here is the logs generated w.r.t commands given in jenkins stage

sh "docker build . -f Dockerfile.test -t python-test:${BUILD_ID}"

Step 17/20 : RUN cp ./bin/python_parser.py /opt/extract/bin/
 ---> Running in e8a4516b3bfd
Removing intermediate container e8a4516b3bfd
 ---> 059885d75fee
Step 18/20 : WORKDIR /app/backend/worker
 ---> Running in 43a312f896ec
Removing intermediate container 43a312f896ec
 ---> 13f885fa36b2
Step 19/20 : ENV PYTHONUNBUFFERED=1
 ---> Running in 27347170ef94
Removing intermediate container 27347170ef94
 ---> 7ae055b454c8
Step 20/20 : ENTRYPOINT nosetests
 ---> Running in 6b5ff8addff5
Removing intermediate container 6b5ff8addff5
 ---> a6a87e197ec6
Successfully built a6a87e197ec6
Successfully tagged python-test:3

sh ‘’‘docker run -u 50001255:25 -v “$(pwd)”:/app python-test:${BUILD_ID}’’'

+ docker run -u 50001255:25 -v /apps/dftjenkins/jenkins_node/workspace/v_AutomationAuthenticationScript:/app python-test:34
..............................
----------------------------------------------------------------------
Ran 30 tests in 5.196s

OK

sh ‘’‘sed -i ‘s|filename="|filename="’"$(pwd)"’/|’ coverage.xml’’'

+ sed -i 's|filename="|filename="/apps/dftjenkins/jenkins_node/workspace/v_AutomationAuthenticationScript/|' coverage.xml

Shell Script Logs (Adding Warning Portion)

INFO: Base dir: /apps/dftjenkins/jenkins_node/workspace/v_AutomationAuthenticationScript
INFO: Working dir: /apps/dftjenkins/jenkins_node/workspace/v_AutomationAuthenticationScript/.scannerwork
INFO: Load project settings for component key: 'PIN_API'
INFO: Load project settings for component key: 'PIN_API' (done) | time=130ms
INFO: Load project branches
INFO: Load project branches (done) | time=66ms
INFO: Load project pull requests
INFO: Load project pull requests (done) | time=48ms
INFO: Load branch configuration
INFO: Load branch configuration (done) | time=1ms
INFO: Load quality profiles
INFO: Load quality profiles (done) | time=574ms
INFO: Detected Jenkins
INFO: Load active rules
INFO: Load active rules (done) | time=4345ms
INFO: Indexing files...
INFO: Project configuration:
INFO:   Excluded sources: mimir-python-client, backend/worker/tests/test_device.py, backend/worker/tests/test_utils.py, backend/worker/tests/test_s3.py, backend/worker/tests/test_api.py, backend/worker/tests/test_webserver.py, backend/worker/Relations.py
WARN: Exception caught during execution of command '[git, config, --system, --edit]' in '/apps/dftjenkins/bin', return code '128', error message 'fatal: Could not switch to '/apps/dftjenkins/local/git/git-2.9.2/etc/': No such file or directory
'
INFO: 51 files indexed
INFO: 0 files ignored because of inclusion/exclusion patterns
INFO: 0 files ignored because of scm ignore settings
INFO: Quality profile for py: Cisco IT Standard
INFO: ------------- Run sensors on module PIN_API
INFO: Load metrics repository
INFO: Load metrics repository (done) | time=105ms
WARNING: An illegal reflective access operation has occurred
WARNING: Illegal reflective access by net.sf.cglib.core.ReflectUtils$1 (file:/apps/dftjenkins/.sonar/cache/a89f1943fc75b65becd9fb4ecab8d913/sonar-tsql-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 JavaXmlSensor [java]
INFO: Sensor JavaXmlSensor [java] (done) | time=2ms
INFO: Sensor HTML [web]
INFO: Sensor HTML [web] (done) | time=3ms
INFO: Sensor Python Sensor [python]
INFO: Starting global symbols computation
INFO: 8 source files to be analyzed
INFO: Load project repositories
INFO: Load project repositories (done) | time=59ms
INFO: Starting rules execution
INFO: 8 source files to be analyzed
INFO: 8/8 source files have been analyzed
INFO: Sensor Python Sensor [python] (done) | time=1968ms
INFO: Sensor Cobertura Sensor for Python coverage [python]
INFO: 8/8 source files have been analyzed
INFO: Python test coverage
INFO: Parsing report '/apps/dftjenkins/jenkins_node/workspace/v_AutomationAuthenticationScript/coverage.xml'
WARN: The report '/apps/dftjenkins/jenkins_node/workspace/v_AutomationAuthenticationScript/coverage.xml' seems to be empty, ignoring. '{}'


I am getting coverage in jenkins but not in sonarqube
There is an issue to access coverage.xml file from jenkins to sonar

Hi,

Thanks for the logs and screenshots.

So we’re back to your original report: a warning that the coverage report was empty. Have you verified that it’s not? Also, from your screenshot you’re not running a supported version of SonarQube. You should upgrade at your earliest convenience.

 
Ann

Yes, as per the nosetest plugins , there should be coverage.xml formation with the coverage report.

I couldn’t able to verify, as the entire build/execution is taking place inside docker container.
I passed all possible path to get coverage.xml file.

I don’t know what to change. Please help me out!!
I’ve said the operational team to upgrade SonarQube, They will do it soon.
If possible, can we connect through google meet to have live discussion on problem statement?

Hi,

Could you try running the build & analysis locally so you could debug what’s going on with that empty coverage file?

 
Ann

I’ve one question as of now, that will sonarQube able to pic the coverage.xml file generated inside the docker container to display the coverage in its UI? Because everything is getting build as an image and running inside a container.

Hi @UjjwalGupta ,

will sonarQube able to pic the coverage.xml file generated inside the docker container to display the coverage in its UI?

It depends how your CI is configured. From the logs it looks like SonarQube is able to find coverage.xml:

Parsing report '/apps/dftjenkins/jenkins_node/workspace/v_AutomationAuthenticationScript/coverage.xml'

As already said by @ganncamp , I’d suggest to try running the docker image generating the coverage.xml locally and the sed command and see how coverage.xml looks like.

Can you please look at these files again, and let me know is it good to go with!!

setup.cfg file content

[nosetests]
verbose=3
with-xunit=1
xunit-file=nosetests.xml
with-xcoverage=1
cover-package=.
xcoverage-file=coverage.xml
exclude-dir=test

sonar-project.properties

sonar.projectKey=IPA_NIP
sonar.projectName=IPA_NIP
sonar.projectVersion=1.0.0
sonar.language=py
sonar.sourceEncoding=UTF-8
sonar.python.coverage.reportPaths=coverage.xml
sonar.python.xunit.reportPath=nosetests.xml
sonar.python.xunit.skipDetails=false
sonar.sources=backend/orch, backend/worker, webserver/pinap 
sonar.exclusions=mim-py-client, backend/worker/tests/test_dev.py, backend/worker/tests/test_utils.py
sonar.typescript.lcov.reportPaths=coverage/IPA_NIP/lcov.info
sonar.python.coveragePlugin=cobertura