运行 docker 容器中的覆盖率测试
Run coverage test in a docker container
我正在尝试使用覆盖率工具来测量我的 Django 应用程序的代码覆盖率,当我测试它时它工作正常,但是当我推送到 github 时,我在 travis-ci:
Traceback (most recent call last):
File "/usr/local/bin/coverage", line 10, in <module>
sys.exit(main())
File "/usr/local/lib/python3.6/site-packages/coverage/cmdline.py", line 756, in main
status = CoverageScript().command_line(argv)
File "/usr/local/lib/python3.6/site-packages/coverage/cmdline.py", line 491, in command_line
return self.do_run(options, args)
File "/usr/local/lib/python3.6/site-packages/coverage/cmdline.py", line 641, in do_run
self.coverage.save()
File "/usr/local/lib/python3.6/site-packages/coverage/control.py", line 782, in save
self.data_files.write(self.data, suffix=self.data_suffix)
File "/usr/local/lib/python3.6/site-packages/coverage/data.py", line 680, in write
data.write_file(filename)
File "/usr/local/lib/python3.6/site-packages/coverage/data.py", line 467, in write_file
with open(filename, 'w') as fdata:
PermissionError: [Errno 13] Permission denied: '/backend/.coverage'
The command "docker-compose run backend sh -c "coverage run manage.py test"" exited with 1.
我的travis.yml:
language: python
python:
- "3.6"
services:
- docker
before_script: pip install docker-compose
script:
- docker-compose run backend sh -c "python manage.py test && flake8"
- docker-compose run backend sh -c "coverage run manage.py test"
after_success:
- coveralls
和我的docker文件
FROM python:3.6-alpine
ENV PYTHONUNBUFFERED 1
# Install dependencies
COPY ./requirements.txt /requirements.txt
RUN apk add --update --no-cache postgresql-client jpeg-dev
RUN apk add --update --no-cache --virtual .tmp-build-deps \
gcc libc-dev linux-headers postgresql-dev musl-dev zlib zlib-dev
RUN pip install -r /requirements.txt
RUN apk del .tmp-build-deps
# Setup directory structure
RUN mkdir /backend
WORKDIR /backend
COPY scripts/start_dev.sh /
RUN dos2unix /start_dev.sh
COPY . /backend
RUN mkdir -p /vol/web/media
RUN mkdir -p /vol/web/static
RUN adduser -D user
RUN chown -R user:user /vol/
RUN chmod -R 755 /vol/web
USER user
docker-撰写:
backend:
container_name: backend_dev_blog
build: ./backend
command: "sh -c /start_dev.sh"
volumes:
- ./backend:/backend
ports:
- "8000:8000"
networks:
- main
environment:
- DB_HOST=db
- DB_NAME=blog
- DB_USER=postgres
- DB_PASS=supersecretpassword
depends_on:
- db
所以在看到 /.coverage 缺少权限后,我简单地添加了 chmod +x .coverage,但是,这没有任何区别,我仍然收到完全相同的错误。
您的权限问题很可能是由于您有一个卷 (./backend:/backend
) 并且您在容器中使用的用户没有在此卷上写入的正确权限 (USER user
)
由于您可能无法更改 Travis CI 目录 ./backend
的权限,您可以尝试更改用于将文件写入该位置的用户。使用 docker-compose:
很容易做到这一点
backend:
container_name: backend_dev_blog
build: ./backend
command: "sh -c /start_dev.sh"
user: root
volumes:
- ./backend:/backend
ports:
- "8000:8000"
networks:
- main
environment:
- DB_HOST=db
- DB_NAME=blog
- DB_USER=postgres
- DB_PASS=supersecretpassword
depends_on:
- db
我正在尝试使用覆盖率工具来测量我的 Django 应用程序的代码覆盖率,当我测试它时它工作正常,但是当我推送到 github 时,我在 travis-ci:
Traceback (most recent call last):
File "/usr/local/bin/coverage", line 10, in <module>
sys.exit(main())
File "/usr/local/lib/python3.6/site-packages/coverage/cmdline.py", line 756, in main
status = CoverageScript().command_line(argv)
File "/usr/local/lib/python3.6/site-packages/coverage/cmdline.py", line 491, in command_line
return self.do_run(options, args)
File "/usr/local/lib/python3.6/site-packages/coverage/cmdline.py", line 641, in do_run
self.coverage.save()
File "/usr/local/lib/python3.6/site-packages/coverage/control.py", line 782, in save
self.data_files.write(self.data, suffix=self.data_suffix)
File "/usr/local/lib/python3.6/site-packages/coverage/data.py", line 680, in write
data.write_file(filename)
File "/usr/local/lib/python3.6/site-packages/coverage/data.py", line 467, in write_file
with open(filename, 'w') as fdata:
PermissionError: [Errno 13] Permission denied: '/backend/.coverage'
The command "docker-compose run backend sh -c "coverage run manage.py test"" exited with 1.
我的travis.yml:
language: python
python:
- "3.6"
services:
- docker
before_script: pip install docker-compose
script:
- docker-compose run backend sh -c "python manage.py test && flake8"
- docker-compose run backend sh -c "coverage run manage.py test"
after_success:
- coveralls
和我的docker文件
FROM python:3.6-alpine
ENV PYTHONUNBUFFERED 1
# Install dependencies
COPY ./requirements.txt /requirements.txt
RUN apk add --update --no-cache postgresql-client jpeg-dev
RUN apk add --update --no-cache --virtual .tmp-build-deps \
gcc libc-dev linux-headers postgresql-dev musl-dev zlib zlib-dev
RUN pip install -r /requirements.txt
RUN apk del .tmp-build-deps
# Setup directory structure
RUN mkdir /backend
WORKDIR /backend
COPY scripts/start_dev.sh /
RUN dos2unix /start_dev.sh
COPY . /backend
RUN mkdir -p /vol/web/media
RUN mkdir -p /vol/web/static
RUN adduser -D user
RUN chown -R user:user /vol/
RUN chmod -R 755 /vol/web
USER user
docker-撰写:
backend:
container_name: backend_dev_blog
build: ./backend
command: "sh -c /start_dev.sh"
volumes:
- ./backend:/backend
ports:
- "8000:8000"
networks:
- main
environment:
- DB_HOST=db
- DB_NAME=blog
- DB_USER=postgres
- DB_PASS=supersecretpassword
depends_on:
- db
所以在看到 /.coverage 缺少权限后,我简单地添加了 chmod +x .coverage,但是,这没有任何区别,我仍然收到完全相同的错误。
您的权限问题很可能是由于您有一个卷 (./backend:/backend
) 并且您在容器中使用的用户没有在此卷上写入的正确权限 (USER user
)
由于您可能无法更改 Travis CI 目录 ./backend
的权限,您可以尝试更改用于将文件写入该位置的用户。使用 docker-compose:
backend:
container_name: backend_dev_blog
build: ./backend
command: "sh -c /start_dev.sh"
user: root
volumes:
- ./backend:/backend
ports:
- "8000:8000"
networks:
- main
environment:
- DB_HOST=db
- DB_NAME=blog
- DB_USER=postgres
- DB_PASS=supersecretpassword
depends_on:
- db