diff options
author | Daniel Watkins <oddbloke@ubuntu.com> | 2020-04-23 15:34:07 -0400 |
---|---|---|
committer | GitHub <noreply@github.com> | 2020-04-23 15:34:07 -0400 |
commit | 845a393d8bb641216b1d311b09486b0aa19f1e9c (patch) | |
tree | d5811f87013b893e021c112a908068e36fa69d82 /cloudinit/conftest.py | |
parent | 1b049e6e51a9ac1ca02bf33629ede5c47c5b1941 (diff) | |
download | vyos-cloud-init-845a393d8bb641216b1d311b09486b0aa19f1e9c.tar.gz vyos-cloud-init-845a393d8bb641216b1d311b09486b0aa19f1e9c.zip |
conftest: introduce disable_subp_usage autouse fixture (#304)
This mirrors the behaviour of CiTestCase.allowed_subp, by causing all
calls to util.subp to raise an AssertionError.
Diffstat (limited to 'cloudinit/conftest.py')
-rw-r--r-- | cloudinit/conftest.py | 35 |
1 files changed, 35 insertions, 0 deletions
diff --git a/cloudinit/conftest.py b/cloudinit/conftest.py new file mode 100644 index 00000000..6a1c63e9 --- /dev/null +++ b/cloudinit/conftest.py @@ -0,0 +1,35 @@ +from unittest import mock + +import pytest + + +@pytest.yield_fixture(autouse=True) +def disable_subp_usage(request): + """ + Across all (pytest) tests, ensure that util.subp is not invoked. + + Note that this can only catch invocations where the util module is imported + and ``util.subp(...)`` is called. ``from cloudinit.util import subp`` + imports happen before the patching here (or the CiTestCase monkey-patching) + happens, so are left untouched. + + To allow a particular test method or class to use util.subp you can set the + parameter passed to this fixture to False using pytest.mark.parametrize:: + + @pytest.mark.parametrize("disable_subp_usage", [False], indirect=True) + def test_whoami(self): + util.subp(["whoami"]) + + This fixture (roughly) mirrors the functionality of + CiTestCase.allowed_subp. + + TODO: + * Enable select subp usage (i.e. allowed_subp=[...]) + """ + should_disable = getattr(request, "param", True) + if should_disable: + with mock.patch('cloudinit.util.subp', autospec=True) as m_subp: + m_subp.side_effect = AssertionError("Unexpectedly used util.subp") + yield + else: + yield |