summaryrefslogtreecommitdiff
path: root/cloudinit/config/tests/test_snap.py
diff options
context:
space:
mode:
authorScott Moser <smoser@ubuntu.com>2018-09-05 16:02:25 +0000
committerServer Team CI Bot <josh.powers+server-team-bot@canonical.com>2018-09-05 16:02:25 +0000
commita8dcad9ac62bb1d2a4f7489960395bad6cac9382 (patch)
tree837df7fce2bdc2c05ce7c3bf497fc8a5be5cb99f /cloudinit/config/tests/test_snap.py
parentdb50bc0d999e3a90136864a774f85e4e15b144e8 (diff)
downloadvyos-cloud-init-a8dcad9ac62bb1d2a4f7489960395bad6cac9382.tar.gz
vyos-cloud-init-a8dcad9ac62bb1d2a4f7489960395bad6cac9382.zip
tests: Disallow use of util.subp except for where needed.
In many cases, cloud-init uses 'util.subp' to run a subprocess. This is not really desirable in our unit tests as it makes the tests dependent upon existance of those utilities. The change here is to modify the base test case class (CiTestCase) to raise exception any time subp is called. Then, fix all callers. For cases where subp is necessary or actually desired, we can use it via   a.) context hander CiTestCase.allow_subp(value)   b.) class level self.allowed_subp = value Both cases the value is a list of acceptable executable names that will be called (essentially argv[0]). Some cleanups in AltCloud were done as the code was being updated.
Diffstat (limited to 'cloudinit/config/tests/test_snap.py')
-rw-r--r--cloudinit/config/tests/test_snap.py7
1 files changed, 5 insertions, 2 deletions
diff --git a/cloudinit/config/tests/test_snap.py b/cloudinit/config/tests/test_snap.py
index 34c80f1e..3c472891 100644
--- a/cloudinit/config/tests/test_snap.py
+++ b/cloudinit/config/tests/test_snap.py
@@ -162,6 +162,7 @@ class TestAddAssertions(CiTestCase):
class TestRunCommands(CiTestCase):
with_logs = True
+ allowed_subp = [CiTestCase.SUBP_SHELL_TRUE]
def setUp(self):
super(TestRunCommands, self).setUp()
@@ -424,8 +425,10 @@ class TestHandle(CiTestCase):
'snap': {'commands': ['echo "HI" >> %s' % outfile,
'echo "MOM" >> %s' % outfile]}}
mock_path = 'cloudinit.config.cc_snap.sys.stderr'
- with mock.patch(mock_path, new_callable=StringIO):
- handle('snap', cfg=cfg, cloud=None, log=self.logger, args=None)
+ with self.allow_subp([CiTestCase.SUBP_SHELL_TRUE]):
+ with mock.patch(mock_path, new_callable=StringIO):
+ handle('snap', cfg=cfg, cloud=None, log=self.logger, args=None)
+
self.assertEqual('HI\nMOM\n', util.load_file(outfile))
@mock.patch('cloudinit.config.cc_snap.util.subp')