summaryrefslogtreecommitdiff
path: root/ironic_python_agent/tests/unit/base.py
diff options
context:
space:
mode:
authorJohn L. Villalovos <john.l.villalovos@intel.com>2017-05-17 11:00:38 -0700
committerJohn L. Villalovos <john.l.villalovos@intel.com>2017-10-23 08:44:23 -0700
commitef838dd6bbb8d3c830a69ff2bf6a21b90b9adfaf (patch)
treeb14b5c68c5068df3710a67cca00f0650f1a301dc /ironic_python_agent/tests/unit/base.py
parent265a072b7297b1516eb0db7f9eb821518b68a725 (diff)
downloadironic-python-agent-ef838dd6bbb8d3c830a69ff2bf6a21b90b9adfaf.tar.gz
Improve the catching of calls to 'execute' related functions
Improve the catching of calls to 'execute' related functions in unit tests. Before we only caught calls to utils.execute(). Now we catch calls to: ironic_lib.utils.execute() processutils.execute() subprocess.call() subprocess.check_call() subprocess.check_output() utils.execute() Change-Id: If4720ebed00f15c2a19cb8badbe4dc3c808eeece
Diffstat (limited to 'ironic_python_agent/tests/unit/base.py')
-rw-r--r--ironic_python_agent/tests/unit/base.py31
1 files changed, 24 insertions, 7 deletions
diff --git a/ironic_python_agent/tests/unit/base.py b/ironic_python_agent/tests/unit/base.py
index 11fff16b..97d1bfe2 100644
--- a/ironic_python_agent/tests/unit/base.py
+++ b/ironic_python_agent/tests/unit/base.py
@@ -15,8 +15,11 @@
"""Common utilities and classes across all unit tests."""
-import mock
+import subprocess
+import ironic_lib
+import mock
+from oslo_concurrency import processutils
from oslotest import base as test_base
from ironic_python_agent import utils
@@ -25,19 +28,33 @@ from ironic_python_agent import utils
class IronicAgentTest(test_base.BaseTestCase):
"""Extends the base test to provide common features across agent tests."""
+ # By default block execution of utils.execute() and related functions.
+ block_execute = True
+
def setUp(self):
super(IronicAgentTest, self).setUp()
- """Add a blanket ban on running external processes via utils.execute().
+ """Ban running external processes via 'execute' like functions
`self` will grow a property called _exec_patch which is the Mock
- that replaces utils.execute.
+ that replaces all the 'execute' related functions.
If the mock is called, an exception is raised to warn the tester.
"""
# NOTE(bigjools): Not using a decorator on tests because I don't
# want to force every test method to accept a new arg. Instead, they
# can override or examine this self._exec_patch Mock as needed.
- self._exec_patch = mock.Mock()
- self._exec_patch.side_effect = Exception(
- "Don't call utils.execute in tests!")
- self.patch(utils, 'execute', self._exec_patch)
+ if self.block_execute:
+ self._exec_patch = mock.Mock()
+ self._exec_patch.side_effect = Exception(
+ "Don't call ironic_lib.utils.execute() / "
+ "processutils.execute() or similar functions in tests!")
+
+ # NOTE(jlvillal): pyudev.Context() calls ctypes.find_library()
+ # which calls subprocess.Popen(). So not blocking
+ # subprocess.Popen()
+ self.patch(ironic_lib.utils, 'execute', self._exec_patch)
+ self.patch(processutils, 'execute', self._exec_patch)
+ self.patch(subprocess, 'call', self._exec_patch)
+ self.patch(subprocess, 'check_call', self._exec_patch)
+ self.patch(subprocess, 'check_output', self._exec_patch)
+ self.patch(utils, 'execute', self._exec_patch)