I'm interested in executing potentially untrusted tests with pytest in some kind of sandbox, like docker, similarly to what continuous integration services do.
I understand that to properly sandbox a python process you need OS-level isolation, like running the tests in a disposable chroot/container, but in my use case I don't need to protect against intentionally malicious code, only from dangerous behaviour of pairing "randomly" functions with arguments. So lesser strict sandboxing may still be acceptable. But I didn't find any plugin that enables any form of sandboxing.
What is the best way to sandbox tests execution in pytest?
Update: This question is not about python sandboxing in general as the tests' code is run by pytest and I can't change the way it is executed to use exec
or ast
or whatever. Also using pypy-sandbox is not an option unfortunately as it is "a prototype only" as per the PyPy feature page.
Update 2: Hoger Krekel on the pytest-dev mailing list suggests using a dedicated testuser via pytest-xdist for user-level isolation:
py.test --tx ssh=OTHERUSER@localhost --dist=each
which made me realise that for my CI-like use case:
having a "disposable" environment is as important as having a isolated one, so that every test or every session runs from the same initial state and it is not influenced by what older sessions might have left on folders writable by the testuser (/home/testuser, /tmp, /var/tmp, etc).
So the testuser+xdist is close to a solution, but not quite there.
Just for context I need isolation to run pytest-nodev.