Fix issue with double mocking of utils.execute functions
An issue was discovered if we mock an already mock-ed function. This was happening in our execute() detection code. Change it to not use a mock and instead a function. Add unit tests to show it works as intended. Change-Id: Ia116b355e7adb3cbfb8ae34f4d59bf527f2889bb
This commit is contained in:
parent
831576c906
commit
fc3de87469
@ -18,7 +18,6 @@
|
||||
import subprocess
|
||||
|
||||
import ironic_lib
|
||||
import mock
|
||||
from oslo_concurrency import processutils
|
||||
from oslo_config import cfg
|
||||
from oslo_config import fixture as config_fixture
|
||||
@ -37,32 +36,27 @@ class IronicAgentTest(test_base.BaseTestCase):
|
||||
|
||||
def setUp(self):
|
||||
super(IronicAgentTest, self).setUp()
|
||||
"""Ban running external processes via 'execute' like functions
|
||||
|
||||
`self` will grow a property called _exec_patch which is the Mock
|
||||
that replaces all the 'execute' related functions.
|
||||
|
||||
If the mock is called, an exception is raised to warn the tester.
|
||||
"""
|
||||
self._set_config()
|
||||
# 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!")
|
||||
|
||||
# Ban running external processes via 'execute' like functions. If the
|
||||
# patched function is called, an exception is raised to warn the
|
||||
# tester.
|
||||
if self.block_execute:
|
||||
# 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)
|
||||
|
||||
# NOTE(jlvillal): Intentionally not using mock as if you mock a
|
||||
# mock it causes things to not work correctly. As doing an
|
||||
# autospec=True causes strangeness. By using a simple function we
|
||||
# can then mock it without issue.
|
||||
self.patch(ironic_lib.utils, 'execute', do_not_call)
|
||||
self.patch(processutils, 'execute', do_not_call)
|
||||
self.patch(subprocess, 'call', do_not_call)
|
||||
self.patch(subprocess, 'check_call', do_not_call)
|
||||
self.patch(subprocess, 'check_output', do_not_call)
|
||||
self.patch(utils, 'execute', do_not_call)
|
||||
|
||||
def _set_config(self):
|
||||
self.cfg_fixture = self.useFixture(config_fixture.Config(CONF))
|
||||
@ -76,3 +70,10 @@ class IronicAgentTest(test_base.BaseTestCase):
|
||||
group = kw.pop('group', None)
|
||||
for o, v in kw.items():
|
||||
self.cfg_fixture.set_default(o, v, group=group)
|
||||
|
||||
|
||||
def do_not_call(*args, **kwargs):
|
||||
"""Helper function to raise an exception if it is called"""
|
||||
raise Exception(
|
||||
"Don't call ironic_lib.utils.execute() / "
|
||||
"processutils.execute() or similar functions in tests!")
|
||||
|
88
ironic_python_agent/tests/unit/test_base.py
Normal file
88
ironic_python_agent/tests/unit/test_base.py
Normal file
@ -0,0 +1,88 @@
|
||||
# Licensed under the Apache License, Version 2.0 (the "License");
|
||||
# you may not use this file except in compliance with the License.
|
||||
# You may obtain a copy of the License at
|
||||
#
|
||||
# http://www.apache.org/licenses/LICENSE-2.0
|
||||
#
|
||||
# Unless required by applicable law or agreed to in writing, software
|
||||
# distributed under the License is distributed on an "AS IS" BASIS,
|
||||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
||||
# implied.
|
||||
# See the License for the specific language governing permissions and
|
||||
# limitations under the License.
|
||||
|
||||
import subprocess
|
||||
|
||||
import ironic_lib
|
||||
import mock
|
||||
from oslo_concurrency import processutils
|
||||
|
||||
from ironic_python_agent.tests.unit import base as ironic_agent_base
|
||||
from ironic_python_agent import utils
|
||||
|
||||
|
||||
class BlockExecuteTestCase(ironic_agent_base.IronicAgentTest):
|
||||
"""Test to ensure we block access to the 'execute' type functions"""
|
||||
|
||||
def test_exception_raised_for_execute(self):
|
||||
execute_functions = (ironic_lib.utils.execute, processutils.execute,
|
||||
subprocess.call, subprocess.check_call,
|
||||
subprocess.check_output, utils.execute)
|
||||
|
||||
for function_name in execute_functions:
|
||||
exc = self.assertRaises(
|
||||
Exception,
|
||||
function_name,
|
||||
["echo", "%s" % function_name]) # noqa
|
||||
# Have to use 'noqa' as we are raising plain Exception and we will
|
||||
# get H202 error in 'pep8' check.
|
||||
|
||||
self.assertEqual(
|
||||
"Don't call ironic_lib.utils.execute() / "
|
||||
"processutils.execute() or similar functions in tests!",
|
||||
"%s" % exc)
|
||||
|
||||
@mock.patch.object(utils, "execute", autospec=True)
|
||||
def test_can_mock_execute(self, mock_exec):
|
||||
# NOTE(jlvillal): We had discovered an issue where mocking wasn't
|
||||
# working because we had used a mock to block access to the execute
|
||||
# functions. This caused us to "mock a mock" and didn't work correctly.
|
||||
# We want to make sure that we can mock our execute functions even with
|
||||
# our "block execute" code.
|
||||
utils.execute("ls")
|
||||
utils.execute("echo")
|
||||
self.assertEqual(2, mock_exec.call_count)
|
||||
|
||||
@mock.patch.object(ironic_lib.utils, "execute", autospec=True)
|
||||
def test_exception_raised_for_execute_parent_mocked(self, mock_exec):
|
||||
# Make sure that even if we mock the parent execute function, that we
|
||||
# still get an exception for a child. So in this case utils.execute()
|
||||
# calls ironic_lib.utils.execute(). Make sure an exception is raised
|
||||
# even though we mocked ironic_lib.utils.execute()
|
||||
exc = self.assertRaises(
|
||||
Exception,
|
||||
utils.execute,
|
||||
"ls") # noqa
|
||||
# Have to use 'noqa' as we are raising plain Exception and we will get
|
||||
# H202 error in 'pep8' check.
|
||||
|
||||
self.assertEqual(
|
||||
"Don't call ironic_lib.utils.execute() / "
|
||||
"processutils.execute() or similar functions in tests!",
|
||||
"%s" % exc)
|
||||
|
||||
|
||||
class DontBlockExecuteTestCase(ironic_agent_base.IronicAgentTest):
|
||||
"""Ensure we can turn off blocking access to 'execute' type functions"""
|
||||
|
||||
# Don't block the execute function
|
||||
block_execute = False
|
||||
|
||||
@mock.patch.object(ironic_lib.utils, "execute", autospec=True)
|
||||
def test_no_exception_raised_for_execute(self, mock_exec):
|
||||
# Make sure we can call utils.execute() even though we didn't mock it.
|
||||
# We do mock ironic_lib.utils.execute() so we don't actually execute
|
||||
# anything.
|
||||
utils.execute("ls")
|
||||
utils.execute("echo")
|
||||
self.assertEqual(2, mock_exec.call_count)
|
Loading…
Reference in New Issue
Block a user