我尝试在具有我感兴趣的代码的docker容器中运行pytest
和pytest -cov
到卷中。在运行应写
make coverage
docker run -a stdin -a stdout -a stderr --rm --mount source=example,target=/home/pytest example_pytest py.test
--cov
Traceback (most recent call last):
File "/usr/local/bin/py.test", line 10, in <module>
sys.exit(main())
File "/usr/local/lib/python3.7/site-packages/_pytest/config/__init__.py", line 61, in main
config = _prepareconfig(args, plugins)
File "/usr/local/lib/python3.7/site-packages/_pytest/config/__init__.py", line 196, in _prepareconfig
pluginmanager=pluginmanager, args=args
File "/usr/local/lib/python3.7/site-packages/pluggy/hooks.py", line 284, in __call__ [26/1974]
return self._hookexec(self, self.get_hookimpls(), kwargs)
File "/usr/local/lib/python3.7/site-packages/pluggy/manager.py", line 68, in _hookexec
return self._inner_hookexec(hook, methods, kwargs)
File "/usr/local/lib/python3.7/site-packages/pluggy/manager.py", line 62, in <lambda>
firstresult=hook.spec.opts.get("firstresult") if hook.spec else False,
File "/usr/local/lib/python3.7/site-packages/pluggy/callers.py", line 203, in _multicall
gen.send(outcome)
File "/usr/local/lib/python3.7/site-packages/_pytest/helpconfig.py", line 93, in pytest_cmdline_parse
config = outcome.get_result()
File "/usr/local/lib/python3.7/site-packages/pluggy/callers.py", line 80, in get_result
raise ex[1].with_traceback(ex[2])
File "/usr/local/lib/python3.7/site-packages/pluggy/callers.py", line 187, in _multicall
res = hook_impl.function(*args)
File "/usr/local/lib/python3.7/site-packages/_pytest/config/__init__.py", line 655, in pytest_cmdline_parse
self.parse(args)
File "/usr/local/lib/python3.7/site-packages/_pytest/config/__init__.py", line 841, in parse
self._preparse(args, addopts=addopts)
File "/usr/local/lib/python3.7/site-packages/_pytest/config/__init__.py", line 797, in _preparse
early_config=self, args=args, parser=self._parser
File "/usr/local/lib/python3.7/site-packages/pluggy/hooks.py", line 284, in __call__
return self._hookexec(self, self.get_hookimpls(), kwargs)
File "/usr/local/lib/python3.7/site-packages/pluggy/manager.py", line 68, in _hookexec return self._inner_hookexec(hook, methods, kwargs)
File "/usr/local/lib/python3.7/site-packages/pluggy/manager.py", line 62, in <lambda>
firstresult=hook.spec.opts.get("firstresult") if hook.spec else False,
File "/usr/local/lib/python3.7/site-packages/pluggy/callers.py", line 208, in _multicall
return outcome.get_result()
File "/usr/local/lib/python3.7/site-packages/pluggy/callers.py", line 80, in get_result
raise ex[1].with_traceback(ex[2])
File "/usr/local/lib/python3.7/site-packages/pluggy/callers.py", line 182, in _multicall
next(gen) # first yield
File "/usr/local/lib/python3.7/site-packages/_pytest/capture.py", line 66, in pytest_load_initial_conftests
capman.start_global_capturing()
File "/usr/local/lib/python3.7/site-packages/_pytest/capture.py", line 110, in start_global_capturing
self._global_capturing = self._getcapture(self._method)
File "/usr/local/lib/python3.7/site-packages/_pytest/capture.py", line 95, in _getcapture
return MultiCapture(out=True, err=True, Capture=FDCapture)
File "/usr/local/lib/python3.7/site-packages/_pytest/capture.py", line 440, in __init__
self.out = Capture(1)
File "/usr/local/lib/python3.7/site-packages/_pytest/capture.py", line 526, in __init__
f = TemporaryFile()
File "/usr/local/lib/python3.7/tempfile.py", line 586, in TemporaryFile
prefix, suffix, dir, output_type = _sanitize_params(prefix, suffix, dir)
File "/usr/local/lib/python3.7/tempfile.py", line 126, in _sanitize_params
dir = gettempdir()
File "/usr/local/lib/python3.7/tempfile.py", line 294, in gettempdir
tempdir = _get_default_tempdir()
File "/usr/local/lib/python3.7/tempfile.py", line 229, in _get_default_tempdir
dirlist)
FileNotFoundError: [Errno 2] No usable temporary directory found in ['/tmp', '/var/tmp', '/usr/tmp', '/home/pytest']
make: *** [coverage] Error 1
# Original inspiration for this file from
# https://stackoverflow.com/questions/35322452/is-there-a-way-to-sandbox-test-execution-with-pytest-especially
ARG SOURCE_IMAGE
FROM ${SOURCE_IMAGE}
RUN adduser --disabled-password --gecos "" --uid 7357 pytest
COPY ./ /home/pytest
WORKDIR /home/pytest
# setup the python and pytest environments
RUN pip install --upgrade pip setuptools pytest
RUN pip install --upgrade -r requirements.txt
RUN pip install --upgrade -r requirements_dev.txt
RUN python setup.py develop
# setup entry point
USER pytest
coverage: ## check code coverage quickly with the default Python
$(DOCKER_RUN_COMMAND) py.test --cov
$(DOCKER_RUN_COMMAND) python -m pytest \
--cov-report annotate \
--cov-report html \
--cov=example \
tests
#$(BROWSER) htmlcov/index.html
env: ## Build a container for running tests and a volume for mounting code.
docker volume create --name example
docker build --no-cache -f Dockerfile \
--build-arg SOURCE_IMAGE=${SOURCE_IMAGE} \ -t ${RUN_IMAGE} .
即使我运行docker system prune -a --force
这是docker image ls
docker image ls
REPOSITORY TAG IMAGE ID CREATED SIZE
example_pytest latest 83fbd031c581 13 seconds ago 1.02GB
<internal_artifactory_uri>/docker-users-virtual/python 3 ac069ebfe1e1 2 days ago 927MB
This ticket要求用户删除Docker.raw
文件,以解决类似问题。根据ls,我的Docker.raw文件为60Gb。
ls -lha ~/Library/Containers/com.docker.docker/Data/vms/0/Docker.raw
-rw-r--r-- 1 alexl HQ\Domain Users 60G Feb 15 00:13 /Users/alexl/Library/Containers/com.docker.docker/Data/vms/0/Docker.raw
但是删除Docker.raw
文件,重新启动服务并重新运行命令会导致新的错误。这个新错误似乎更容易处理,但在运行make目标几次(5-10次)之后,我再次遇到第一个空间不足的问题。
tests/test_example.py . [100%] INTERNALERROR> Traceback (most recent call last):
INTERNALERROR> File "/usr/local/lib/python3.7/site-packages/_pytest/main.py", line 203, in wrap_session
INTERNALERROR> session.exitstatus = doit(config, session) or 0
INTERNALERROR> File "/usr/local/lib/python3.7/site-packages/_pytest/main.py", line 243, in _main
INTERNALERROR> config.hook.pytest_runtestloop(session=session)
INTERNALERROR> File "/usr/local/lib/python3.7/site-packages/pluggy/hooks.py", line 284, in __call__
INTERNALERROR> return self._hookexec(self, self.get_hookimpls(), kwargs)
INTERNALERROR> File "/usr/local/lib/python3.7/site-packages/pluggy/manager.py", line 68, in _hookexec
INTERNALERROR> return self._inner_hookexec(hook, methods, kwargs)
INTERNALERROR> File "/usr/local/lib/python3.7/site-packages/pluggy/manager.py", line 62, in <lambda>
INTERNALERROR> firstresult=hook.spec.opts.get("firstresult") if hook.spec else False,
INTERNALERROR> File "/usr/local/lib/python3.7/site-packages/pluggy/callers.py", line 203, in _multicall
INTERNALERROR> gen.send(outcome)
INTERNALERROR> File "/usr/local/lib/python3.7/site-packages/pytest_cov/plugin.py", line 229, in pytest_runtestloop
INTERNALERROR> self.cov_controller.finish()
INTERNALERROR> File "/usr/local/lib/python3.7/site-packages/pytest_cov/engine.py", line 114, in summary
INTERNALERROR> self.cov.annotate(ignore_errors=True, directory=annotate_dir)
INTERNALERROR> File "/usr/local/lib/python3.7/site-packages/coverage/control.py", line 1062, in annotate
INTERNALERROR> reporter.report(morfs, directory=directory)
INTERNALERROR> File "/usr/local/lib/python3.7/site-packages/coverage/annotate.py", line 52, in report
INTERNALERROR> self.report_files(self.annotate_file, morfs, directory)
INTERNALERROR> File "/usr/local/lib/python3.7/site-packages/coverage/report.py", line 91, in report_files
INTERNALERROR> report_fn(fr, self.coverage._analyze(fr))
INTERNALERROR> File "/usr/local/lib/python3.7/site-packages/coverage/annotate.py", line 72, in annotate_file
INTERNALERROR> with io.open(dest_file, 'w', encoding='utf8') as dest:
INTERNALERROR> PermissionError: [Errno 13] Permission denied: '/home/pytest/example/__init__.py,cover'
===================== 1 passed, 2 warnings in 0.05 seconds =====================
make: *** [coverage] Error 3