Is there a way to test a test program using pytest, especially access to the file system?

I'm interested in performing potentially unreliable tests using pytest in the form of a sandbox, such as dockers, similar to what continuous integration services do.

I understand that in order to work correctly with the sandbox of the python process, you need isolation at the OS level, for example, running tests in a one-time chroot / container, but in my use case I do not need to protect against intentionally malicious code, only against dangerous mating behavior "random" functions with arguments. A less rigorous sandbox may still be acceptable. But I did not find any plugin that allows you to use any sandbox.

What is the best way to run sandbox tests in pytest?

Update : this question is not about the python sandbox at all , since the test code is run by pytest, and I cannot change the way it is executed, use execeither astor whatever. Also, using pypy-sandbox is not an option, unfortunately, since it is “only a prototype” according to the PyPy functions page .

Update 2 : Hoger Krekel on the pytest-dev mailing list suggests using dedicated testuser via pytest-xdist for user level isolation:

py.test --tx ssh=OTHERUSER@localhost --dist=each

which made me realize that for my CI-like use case:

"" , , , , testuser (/home/testuser,/tmp,/var/tmp, ..).

, testuser + xdist , .

pytest-nodev.

+4
2

pytest . , , .

( ) , , :

  • python :
    • root: pytest
    • requirements.txt
    • ,
  • py.test , pytest

, Dockerfile , requirements.txt setup.py:

FROM python:3

# setup pytest user
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 python setup.py develop

# setup entry point
USER pytest
ENTRYPOINT ["py.test"]

docker build -t pytest .

py.test , volume on/home/pytest :

docker run --rm -it -v `pwd`:/home/pytest pytest [USUAL_PYTEST_OPTIONS]

, -v uid 1000, pytest uid, 7357.

.

:. , python pytest, . :

rm -rf .cache/ && find . -name __pycache__  | xargs rm -rf
+7

, - . , , python, . , CI, .

, , , - , :

rm -rf /usr/local/share/ myapp
+2

Source: https://habr.com/ru/post/1628177/


All Articles