summaryrefslogtreecommitdiff
path: root/cloudinit
diff options
context:
space:
mode:
authorDaniel Watkins <oddbloke@ubuntu.com>2020-05-12 14:09:40 -0400
committerGitHub <noreply@github.com>2020-05-12 14:09:40 -0400
commitc8f20b31cd57443b1bef17579dfceca432420c94 (patch)
tree15801b7d0d08dc130c4e71024ad4481b32aa63e8 /cloudinit
parentd10ce3ecfe89d7ec2a69bdbd109b100090698ded (diff)
downloadvyos-cloud-init-c8f20b31cd57443b1bef17579dfceca432420c94.tar.gz
vyos-cloud-init-c8f20b31cd57443b1bef17579dfceca432420c94.zip
conftest: add docs and tests regarding CiTestCase's subp functionality (#343)
And raise TypeError when subp called with no args, which more accurately mirrors normal behaviour: >>> from cloudinit.util import subp >>> subp() Traceback (most recent call last): File "<stdin>", line 1, in <module> TypeError: subp() missing 1 required positional argument: 'args'
Diffstat (limited to 'cloudinit')
-rw-r--r--cloudinit/conftest.py4
-rw-r--r--cloudinit/tests/helpers.py3
-rw-r--r--cloudinit/tests/test_conftest.py22
3 files changed, 28 insertions, 1 deletions
diff --git a/cloudinit/conftest.py b/cloudinit/conftest.py
index 6a1c63e9..af458c31 100644
--- a/cloudinit/conftest.py
+++ b/cloudinit/conftest.py
@@ -21,7 +21,9 @@ def disable_subp_usage(request):
util.subp(["whoami"])
This fixture (roughly) mirrors the functionality of
- CiTestCase.allowed_subp.
+ CiTestCase.allowed_subp. N.B. While autouse fixtures do affect non-pytest
+ tests, CiTestCase's allowed_subp does take precedence (and we have
+ TestDisableSubpUsageInTestSubclass to confirm that).
TODO:
* Enable select subp usage (i.e. allowed_subp=[...])
diff --git a/cloudinit/tests/helpers.py b/cloudinit/tests/helpers.py
index 477e14c2..b0af54c1 100644
--- a/cloudinit/tests/helpers.py
+++ b/cloudinit/tests/helpers.py
@@ -144,6 +144,9 @@ class CiTestCase(TestCase):
if 'args' in kwargs:
cmd = kwargs['args']
else:
+ if not args:
+ raise TypeError(
+ "subp() missing 1 required positional argument: 'args'")
cmd = args[0]
if not isinstance(cmd, str):
diff --git a/cloudinit/tests/test_conftest.py b/cloudinit/tests/test_conftest.py
index 62a5361d..11b91327 100644
--- a/cloudinit/tests/test_conftest.py
+++ b/cloudinit/tests/test_conftest.py
@@ -1,9 +1,11 @@
import pytest
from cloudinit import util
+from cloudinit.tests.helpers import CiTestCase
class TestDisableSubpUsage:
+ """Test that the disable_subp_usage fixture behaves as expected."""
def test_using_subp_raises_assertion_error(self):
with pytest.raises(AssertionError):
@@ -16,3 +18,23 @@ class TestDisableSubpUsage:
@pytest.mark.parametrize('disable_subp_usage', [False], indirect=True)
def test_subp_usage_can_be_reenabled(self):
util.subp(['whoami'])
+
+
+class TestDisableSubpUsageInTestSubclass(CiTestCase):
+ """Test that disable_subp_usage doesn't impact CiTestCase's subp logic."""
+
+ def test_using_subp_raises_exception(self):
+ with pytest.raises(Exception):
+ util.subp(["some", "args"])
+
+ def test_typeerrors_on_incorrect_usage(self):
+ with pytest.raises(TypeError):
+ util.subp()
+
+ def test_subp_usage_can_be_reenabled(self):
+ _old_allowed_subp = self.allow_subp
+ self.allowed_subp = True
+ try:
+ util.subp(['bash', '-c', 'true'])
+ finally:
+ self.allowed_subp = _old_allowed_subp