From de9c02a4c09d603bdfe5d23478e6c050223d79b6 Mon Sep 17 00:00:00 2001 From: Daniel Watkins Date: Thu, 21 May 2020 10:24:18 -0400 Subject: conftest: implement partial disable_subp_usage (#371) This allows tests to be configured to permit some commands to be run via util.subp, while still rejecting any unexpected calls. See the documentation for further details. --- cloudinit/tests/test_conftest.py | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) (limited to 'cloudinit/tests') diff --git a/cloudinit/tests/test_conftest.py b/cloudinit/tests/test_conftest.py index b39637d8..773ef8fe 100644 --- a/cloudinit/tests/test_conftest.py +++ b/cloudinit/tests/test_conftest.py @@ -21,6 +21,25 @@ class TestDisableSubpUsage: def test_subp_usage_can_be_reenabled(self): util.subp(['whoami']) + @pytest.mark.parametrize( + 'disable_subp_usage', [['whoami'], 'whoami'], indirect=True) + def test_subp_usage_can_be_conditionally_reenabled(self): + # The two parameters test each potential invocation with a single + # argument + with pytest.raises(AssertionError) as excinfo: + util.subp(["some", "args"]) + assert "allowed: whoami" in str(excinfo.value) + util.subp(['whoami']) + + @pytest.mark.parametrize( + 'disable_subp_usage', [['whoami', 'bash']], indirect=True) + def test_subp_usage_can_be_conditionally_reenabled_for_multiple_cmds(self): + with pytest.raises(AssertionError) as excinfo: + util.subp(["some", "args"]) + assert "allowed: whoami,bash" in str(excinfo.value) + util.subp(['bash', '-c', 'true']) + util.subp(['whoami']) + class TestDisableSubpUsageInTestSubclass(CiTestCase): """Test that disable_subp_usage doesn't impact CiTestCase's subp logic.""" -- cgit v1.2.3