Merge "Prevent tests from using utils.execute()"

This commit is contained in:
Jenkins 2017-08-04 16:36:51 +00:00 committed by Gerrit Code Review
commit 3d4545b345

View File

@ -23,12 +23,16 @@ inline callbacks.
import copy import copy
import os import os
import subprocess
import sys import sys
import tempfile import tempfile
import eventlet import eventlet
eventlet.monkey_patch(os=False) eventlet.monkey_patch(os=False)
import fixtures import fixtures
from ironic_lib import utils
import mock
from oslo_concurrency import processutils
from oslo_config import fixture as config_fixture from oslo_config import fixture as config_fixture
from oslo_log import log as logging from oslo_log import log as logging
from oslo_serialization import jsonutils from oslo_serialization import jsonutils
@ -73,6 +77,9 @@ class TestingException(Exception):
class TestCase(testtools.TestCase): class TestCase(testtools.TestCase):
"""Test case base class for all unit tests.""" """Test case base class for all unit tests."""
# By default block execution of utils.execute() and related functions.
block_execute = True
def setUp(self): def setUp(self):
"""Run before each test method to initialize test environment.""" """Run before each test method to initialize test environment."""
super(TestCase, self).setUp() super(TestCase, self).setUp()
@ -117,6 +124,23 @@ class TestCase(testtools.TestCase):
for factory in driver_factory._INTERFACE_LOADERS.values(): for factory in driver_factory._INTERFACE_LOADERS.values():
factory._extension_manager = None factory._extension_manager = None
# Block access to utils.execute() and related functions.
# 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.
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!")
self.patch(processutils, 'execute', self._exec_patch)
self.patch(subprocess, 'Popen', 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)
def _set_config(self): def _set_config(self):
self.cfg_fixture = self.useFixture(config_fixture.Config(CONF)) self.cfg_fixture = self.useFixture(config_fixture.Config(CONF))
self.config(use_stderr=False, self.config(use_stderr=False,