工作服:错误-使用Docker容器的应用程序中无源 [英] Coveralls: Error- No source for in my application using Docker container

查看:125
本文介绍了工作服:错误-使用Docker容器的应用程序中无源的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在尝试将代码覆盖率集成到我的Django应用程序中。.构建成功,并且所有测试都成功,但是当我检查Coveralls.io或codecov.io时,没有数据..我已经搜索了所有内容,添加了.coveragerc,但仍然无济于事。



Dockerfile

 从python:3.7-alpine 
MAINTAINER abhie-lp

ENV PYTHONUNBUFFERED 1
COPY ./requirements.txt /requirements.txt

运行apk添加-更新-无缓存jpeg-dev
运行apk添加-更新-无缓存-虚拟.tmp-build-deps \
gcc libc -dev musl-dev zlib zlib-dev
RUN pip install -r /requirements.txt
RUN apk del .tmp-build-deps

RUN mkdir / app
WORKDIR / app
COPY ./app / app

RUN mkdir -p / vol / web / media
RUN mkdir -p / vol / web / static
RUN adduser -D ABHIE
RUN chown -R ABHIE:ABHIE / vol /
RUN chmod -R 755 / vol / web
USER ABHIE

docker-compose.yml

 版本: 3 

服务:
应用程序:
构建:
上下文:。
端口:
- 8000:8000
数量:
-./app:/app
命令:>
sh -c python manage.py wait_for_db&&
python manage.py migration&&
python manage.py runserver 0.0.0.0:8000

.travis.yml

 语言:python 
python:
- 3.6

服务:
-码头工人

before_script:
-pip安装docker-compose
-pip安装工作服
-pip install codecov
-docker-compose run --user ='root'app chmod -R 777。

脚本:
-docker-compose run app sh -c coverage run --source =。manage.py test
-docker-compose run app sh -c flake8

after_success:
-工作服
-codecov

.coveragerc

  [运行] 
来源= / home / travis / build / abhie-lp / recipe-app-api / app
parallel =真
data_file = / home / travis / build / abhie-lp / recipe-app-api / app /。 coverage

[路径]
来源=
/ home / travis / build / abhie-lp / recipe-app-api
/ app /


解决方案

您显示的测试设置中存在三个主要问题:


  1. docker-compose中的量:声明。 yml 文件在图像中隐藏 / app 树的内容,这意味着您的测试设置未在测试其生成的图像。 / p>


  2. 您的 pip安装命令安装主机Python环境中的其他软件包,但在Docker容器内将看不到这些软件包。


  3. 每个 docker-compose run 命令使用新的临时文件系统启动一个新容器,因此在您 docker-compose运行coverage 之后,具有覆盖率报告的临时容器文件系统将丢失。


对于基本测试覆盖率指标,希望您的单元测试设置对于在Docker中运行不是特别敏感,或者部署在不同的路径上,或从不同的开发人员的工作站部署。我成功使用的设置是在Docker外部运行单元测试和诸如代码覆盖率之类的事情,并且仅构建和发布Docker映像作为最后一步。虽然值得对已构建的映像运行一些集成测试,但您应该能够从Docker外部进行驱动,而无需更改映像本身。



要从Docker内部运行这些测试很重要,您需要将这些仅用于开发的工具添加到生产映像中,或从单个<$ c中执行所有操作(安装额外的工具,运行测试并提取结果) $ c> docker-compose run 命令。单线可能看起来像

  docker-compose run \ 
-v $ PWD:/ coverage \
sh -c'pip安装范围&& COVERAGE_FILE = / coverage / .coverage coverage运行--source =。 manage.py test'

您也可以将其分解为COPYed或bind-装入容器中

  docker-compose run -v $ PWD:/ coverage / coverage / cov-pytest 

这可能更易于维护,并且更易于手动测试。


I've been trying to integrate code-coverage in my Django application.. The build is successfull and all the tests are successfull but when i check coveralls.io or codecov.io there is no data.. I have searched everything, added a .coveragerc but still nothing helps.

Dockerfile

FROM python:3.7-alpine
MAINTAINER abhie-lp

ENV PYTHONUNBUFFERED 1
COPY ./requirements.txt /requirements.txt

RUN apk add --update --no-cache jpeg-dev
RUN apk add --update --no-cache --virtual .tmp-build-deps \
        gcc libc-dev musl-dev zlib zlib-dev
RUN pip install -r /requirements.txt
RUN apk del .tmp-build-deps

RUN mkdir /app
WORKDIR /app
COPY ./app /app

RUN mkdir -p /vol/web/media
RUN mkdir -p /vol/web/static
RUN adduser -D ABHIE
RUN chown -R ABHIE:ABHIE /vol/
RUN chmod -R 755 /vol/web
USER ABHIE

docker-compose.yml

version: "3"

services:
  app:
    build:
      context: .
    ports:
      - "8000:8000"
    volumes:
      - ./app:/app
    command: >
      sh -c "python manage.py wait_for_db && 
             python manage.py migrate && 
             python manage.py runserver 0.0.0.0:8000"

.travis.yml

language: python
python:
  - "3.6"

services:
  - docker

before_script:
  - pip install docker-compose
  - pip install coveralls
  - pip install codecov
  - docker-compose run --user='root' app chmod -R 777 .

script:
  - docker-compose run app sh -c "coverage run --source=. manage.py test"
  - docker-compose run app sh -c "flake8"

after_success:
  - coveralls
  - codecov

.coveragerc

[run]
source = /home/travis/build/abhie-lp/recipe-app-api/app
parallel = True
data_file = /home/travis/build/abhie-lp/recipe-app-api/app/.coverage

[paths]
source = 
  /home/travis/build/abhie-lp/recipe-app-api
  /app/

解决方案

There are three major problems in the test setup you show:

  1. The volumes: declaration in the docker-compose.yml file hides the contents of the /app tree in your image, which means that your test setup is not testing the image that it built.

  2. Your pip install commands install additional packages in the host's Python environment, but these will not be visible inside the Docker container.

  3. Each docker-compose run command launches a new container with a new ephemeral filesystem, so after you docker-compose run coverage, the temporary container filesystem that had the coverage report is lost.

For basic test coverage metrics, hopefully your unit-test setup is not especially sensitive to being run in Docker, or deployed on a different path, or from a different developer's workstation. The setup I've used successfully is to run unit tests and things like code coverage outside of Docker, and only build and publish a Docker image as a final step. While it's worthwhile to run some integration tests against your built image, you should be able to drive these from outside of Docker without needing any changes in the image itself.

If it's important to you to run these tests from inside Docker, you need to either add these development-only tools to your production image, or do everything (install the extra tools, run the tests, and extract the results) from a single docker-compose run command. A one-liner could look like

docker-compose run \
  -v $PWD:/coverage \
  sh -c 'pip install coverage && COVERAGE_FILE=/coverage/.coverage coverage run --source=. manage.py test'

You could also break this out into a script that is either COPYed or bind-mounted into your container

docker-compose run -v $PWD:/coverage /coverage/cov-pytest

which might be more maintainable and a little easier to manually test.

这篇关于工作服:错误-使用Docker容器的应用程序中无源的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆