Docker, Code Coverage and Jenkins

taqkarim

Taq Karim

Posted on March 11, 2020

Docker, Code Coverage and Jenkins

These posts are super quick and to the point - mainly, they are solutions that worked for me but were not anything I could easily find googling.

Problem

Trying to write to .coverage inside of docker fails. Here's the dump:

INTERNALERROR> Traceback (most recent call last):
INTERNALERROR>   File "/usr/local/lib/python2.7/site-packages/_pytest/main.py", line 206, in wrap_session
INTERNALERROR>     session.exitstatus = doit(config, session) or 0
INTERNALERROR>   File "/usr/local/lib/python2.7/site-packages/_pytest/main.py", line 250, in _main
INTERNALERROR>     config.hook.pytest_runtestloop(session=session)
INTERNALERROR>   File "/usr/local/lib/python2.7/site-packages/pluggy/hooks.py", line 286, in __call__
INTERNALERROR>     return self._hookexec(self, self.get_hookimpls(), kwargs)
INTERNALERROR>   File "/usr/local/lib/python2.7/site-packages/pluggy/manager.py", line 93, in _hookexec
INTERNALERROR>     return self._inner_hookexec(hook, methods, kwargs)
INTERNALERROR>   File "/usr/local/lib/python2.7/site-packages/pluggy/manager.py", line 87, in <lambda>
INTERNALERROR>     firstresult=hook.spec.opts.get("firstresult") if hook.spec else False,
INTERNALERROR>   File "/usr/local/lib/python2.7/site-packages/pluggy/callers.py", line 203, in _multicall
INTERNALERROR>     gen.send(outcome)
INTERNALERROR>   File "/usr/local/lib/python2.7/site-packages/pytest_cov/plugin.py", line 254, in pytest_runtestloop
INTERNALERROR>     self.cov_controller.finish()
INTERNALERROR>   File "/usr/local/lib/python2.7/site-packages/pytest_cov/engine.py", line 197, in finish
INTERNALERROR>     self.cov.stop()
INTERNALERROR>   File "/usr/local/lib/python2.7/site-packages/coverage/control.py", line 782, in save
INTERNALERROR>     self.data_files.write(self.data, suffix=self.data_suffix)
INTERNALERROR>   File "/usr/local/lib/python2.7/site-packages/coverage/data.py", line 680, in write
INTERNALERROR>     data.write_file(filename)
INTERNALERROR>   File "/usr/local/lib/python2.7/site-packages/coverage/data.py", line 467, in write_file
INTERNALERROR>     with open(filename, 'w') as fdata:
INTERNALERROR> IOError: [Errno 13] Permission denied: '/app/.coverage'

Woof. The issue is definitely due to permissions issues. Here's my docker command:

docker run -v $(PWD):/app some_image pytest --cov=some_dir

The Fix

Adding --user=root solved my problem

docker run --user=root -v $(PWD):/app some_image pytest --cov=some_dir

In this case, as I am simply running some unit tests I don't think running as root user makes much of a difference. (Also note that if you are using docker-compose, there is also a supported method for specifying user either via cmd line or in docker-compose file)

I hope this helps!

💖 💪 🙅 🚩
taqkarim
Taq Karim

Posted on March 11, 2020

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related