gpt4 book ai didi

django - 在 docker 容器中运行覆盖测试

转载 作者:行者123 更新时间:2023-12-02 18:14:20 25 4
gpt4 key购买 nike

我正在尝试使用覆盖率工具来测量我的 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

和我的 dockerfile
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 - 在 docker 容器中运行覆盖测试,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55240542/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com