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

Coverage api not saving files inside of docker #1581

Closed
NLion74 opened this issue Mar 10, 2023 · 4 comments
Closed

Coverage api not saving files inside of docker #1581

NLion74 opened this issue Mar 10, 2023 · 4 comments
Labels
bug Something isn't working

Comments

@NLion74
Copy link

NLion74 commented Mar 10, 2023

Describe the bug
When using the coverage api inside of docker its not saving the files. It works perfectly fine outside of docker though.

To Reproduce
The issue occurs on python 3.11 and coverage 7.2.1, didnt test older versions.
I have written a small reproduction of the problem that I hope anyone can follow. In the following steps I will be showing how to run that.

  1. First you'll have to install python3, python3-pip, docker.io and docker-compose.
  2. After that install coverage via pip3 install coverage.
  3. Third step would be to create a directory. The name doesnt matter. I'll just call mine reproduction
  4. After that copy the code below this step into a file called main.py.
from coverage import Coverage
from time import time


# Change to ./data when running outside of docker
datapath = "/data"


# To show that it can infact write
def some_code():
    with open(f"{datapath}/t.txt", "w") as f:
        f.write("T")


def main():
    # Start coverage
    coveragedatafile = ".coverage-" + str(int(time()))
    cov = Coverage(data_file=f"{datapath}/coverage/{coveragedatafile}")
    cov.start()

    # Run your code
    some_code()

    # Stop Coverage
    cov.stop()
    cov.save()
    print("Successfully saved coverage data, or not?")


if __name__ == "__main__":
    main()
  1. Copy the code below this into a file called Dockerfile.
FROM debian

RUN apt-get update && apt-get install -y python3 python3-pip

WORKDIR /app

COPY ./ ./

RUN pip3 install --user --no-cache-dir coverage

ENV docker yes

CMD [ "python3", "main.py" ]
  1. After that you should have a file structure like this.
reproduction
|
├── main.py
└── Dockerfile
  1. Now build the Dockerfile like this: docker build . and run it with this command docker run -v /root/tests/data:/data [image id]. Replace the [image id] with the actual image id.
  2. And now if everything would be working normally there should be a folder called coverage in the data directory and in which the coverage files should be. Except they wont be.

Expected behavior
Normally it should just stop and save the files when running inside of docker. Like it does when running outside of docker.

Edit
Added docker.io and docker-compose to requirements for reproduction. Second edit now writing to a file called t.txt to show that I do indeed have write permissions.

@NLion74 NLion74 added bug Something isn't working needs triage labels Mar 10, 2023
@nedbat
Copy link
Owner

nedbat commented Mar 10, 2023

Hmm, you say:

# Change to ./data when running outside of docker
datapath = "/data"

Are you using ./data outside, but /data inside? Perhaps you don't have rights to write into the root?

@NLion74
Copy link
Author

NLion74 commented Mar 11, 2023

Im pretty sure I do because while testing I also wrote to a file and the coverage folder also gets created. Which means I do have write permissions right?

@LewisGaul
Copy link
Contributor

I had a go at reproducing this (after thinking it was related to another issue, which it turned out not to be), and I cannot reproduce with the exact setup described above.

root@ubuntu:~/reproduction# docker build . -t cov-1581
...
root@ubuntu:~/reproduction# docker run -v /root/data:/data cov-1581
Successfully saved coverage data, or not?
root@ubuntu:~/reproduction# ls -Al /root/data/coverage/
total 52
-rw-r--r-- 1 root root 53248 Apr  2 22:57 .coverage-1680476276

I wonder if it could be something to do with the filesystem type that you're mounting in from the host... What does stat -f /root/tests/data/ on the host show for you? Is docker running natively on the host, or is it connecting to another host using the DOCKER_HOST env var?

@NLion74
Copy link
Author

NLion74 commented Apr 6, 2023

Hey, sorry it me so long to respond, I was on vacation for a few days. Anyways, after a little bit of investigating it seems that I may have been a little stupid.

First of all for some reason the files actually don't appear when running this on windows, but when using debian as described in the reproduction they are actually there. I just didn't know about the -A option. That's my bad and im honestly a little embarrassed about it. I don't know why exactly it's not showing on windows, im guessing it's because of docker using wsl or something. I don't know and honestly I also don't really care since it doesn't matter in my use case.

Im really sorry for bothering everyone, but also thankful to you for pointing that out to me. I wish you all a great day.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

3 participants