Update unit tests to use six.moves.builtins.open

Change-Id: I724a42d22d4c9011feb8aa09726e5186686f3729
This commit is contained in:
Moshe Levi 2016-02-17 14:08:42 +02:00
parent 559b25f56e
commit 52fc4f891f
3 changed files with 15 additions and 33 deletions
ironic_python_agent/tests/unit

@ -17,16 +17,10 @@ import os
import mock
from oslo_concurrency import processutils
from oslotest import base as test_base
import six
from ironic_python_agent import errors
from ironic_python_agent.extensions import standby
if six.PY2:
OPEN_FUNCTION_NAME = '__builtin__.open'
else:
OPEN_FUNCTION_NAME = 'builtins.open'
def _build_fake_image_info():
return {
@ -97,7 +91,7 @@ class TestStandbyExtension(test_base.BaseTestCase):
location = standby._image_location(image_info)
self.assertEqual('/tmp/fake_id', location)
@mock.patch(OPEN_FUNCTION_NAME, autospec=True)
@mock.patch('six.moves.builtins.open', autospec=True)
@mock.patch('ironic_python_agent.utils.execute', autospec=True)
def test_write_image(self, execute_mock, open_mock):
image_info = _build_fake_image_info()
@ -137,7 +131,7 @@ class TestStandbyExtension(test_base.BaseTestCase):
write_mock.assert_called_once_with('data', 'filename')
@mock.patch('gzip.GzipFile', autospec=True)
@mock.patch(OPEN_FUNCTION_NAME, autospec=True)
@mock.patch('six.moves.builtins.open', autospec=True)
@mock.patch('base64.b64decode', autospec=True)
def test_write_configdrive_to_file(self, b64_mock, open_mock, gzip_mock):
open_mock.return_value.__enter__ = lambda s: s
@ -157,7 +151,7 @@ class TestStandbyExtension(test_base.BaseTestCase):
@mock.patch(('ironic_python_agent.extensions.standby.'
'_write_configdrive_to_file'),
autospec=True)
@mock.patch(OPEN_FUNCTION_NAME, autospec=True)
@mock.patch('six.moves.builtins.open', autospec=True)
@mock.patch('ironic_python_agent.utils.execute', autospec=True)
def test_write_configdrive_to_partition(self, execute_mock, open_mock,
configdrive_mock, stat_mock):
@ -186,7 +180,7 @@ class TestStandbyExtension(test_base.BaseTestCase):
@mock.patch(('ironic_python_agent.extensions.standby.'
'_write_configdrive_to_file'),
autospec=True)
@mock.patch(OPEN_FUNCTION_NAME, autospec=True)
@mock.patch('six.moves.builtins.open', autospec=True)
@mock.patch('ironic_python_agent.utils.execute', autospec=True)
def test_write_configdrive_too_large(self, execute_mock, open_mock,
configdrive_mock, stat_mock):
@ -200,7 +194,7 @@ class TestStandbyExtension(test_base.BaseTestCase):
device)
@mock.patch('hashlib.md5')
@mock.patch(OPEN_FUNCTION_NAME)
@mock.patch('six.moves.builtins.open')
@mock.patch('requests.get')
def test_download_image(self, requests_mock, open_mock, md5_mock):
image_info = _build_fake_image_info()
@ -222,7 +216,7 @@ class TestStandbyExtension(test_base.BaseTestCase):
self.assertEqual(2, write.call_count)
@mock.patch('hashlib.md5')
@mock.patch(OPEN_FUNCTION_NAME)
@mock.patch('six.moves.builtins.open')
@mock.patch('requests.get')
@mock.patch.dict(os.environ, {})
def test_download_image_proxy(
@ -261,7 +255,7 @@ class TestStandbyExtension(test_base.BaseTestCase):
image_info)
@mock.patch('hashlib.md5', autospec=True)
@mock.patch(OPEN_FUNCTION_NAME, autospec=True)
@mock.patch('six.moves.builtins.open', autospec=True)
@mock.patch('requests.get', autospec=True)
def test_download_image_verify_fails(self, requests_mock, open_mock,
md5_mock):
@ -573,7 +567,7 @@ class TestStandbyExtension(test_base.BaseTestCase):
write_mock.assert_called_once_with(image_info, device)
@mock.patch('hashlib.md5')
@mock.patch(OPEN_FUNCTION_NAME)
@mock.patch('six.moves.builtins.open')
@mock.patch('requests.get')
def test_stream_raw_image_onto_device(self, requests_mock, open_mock,
md5_mock):
@ -595,7 +589,7 @@ class TestStandbyExtension(test_base.BaseTestCase):
file_mock.write.assert_has_calls(expected_calls)
@mock.patch('hashlib.md5')
@mock.patch(OPEN_FUNCTION_NAME)
@mock.patch('six.moves.builtins.open')
@mock.patch('requests.get')
def test_stream_raw_image_onto_device_write_error(self, requests_mock,
open_mock, md5_mock):

@ -19,18 +19,12 @@ from oslo_concurrency import processutils
from oslo_utils import units
from oslotest import base as test_base
import pyudev
import six
from stevedore import extension
from ironic_python_agent import errors
from ironic_python_agent import hardware
from ironic_python_agent import utils
if six.PY2:
OPEN_FUNCTION_NAME = '__builtin__.open'
else:
OPEN_FUNCTION_NAME = 'builtins.open'
HDPARM_INFO_TEMPLATE = (
'/dev/sda:\n'
'\n'
@ -262,7 +256,7 @@ class TestGenericHardwareManager(test_base.BaseTestCase):
@mock.patch('netifaces.ifaddresses')
@mock.patch('os.listdir')
@mock.patch('os.path.exists')
@mock.patch(OPEN_FUNCTION_NAME)
@mock.patch('six.moves.builtins.open')
def test_list_network_interfaces(self,
mocked_open,
mocked_exists,
@ -392,7 +386,8 @@ class TestGenericHardwareManager(test_base.BaseTestCase):
def test__get_device_vendor(self):
fileobj = mock.mock_open(read_data='fake-vendor')
with mock.patch(OPEN_FUNCTION_NAME, fileobj, create=True) as mock_open:
with mock.patch(
'six.moves.builtins.open', fileobj, create=True) as mock_open:
vendor = hardware._get_device_vendor('/dev/sdfake')
mock_open.assert_called_once_with(
'/sys/class/block/sdfake/device/vendor', 'r')

@ -23,18 +23,11 @@ import testtools
import mock
from oslo_concurrency import processutils
from oslotest import base as test_base
import six
from ironic_python_agent import errors
from ironic_python_agent import utils
if six.PY2:
OPEN_FUNCTION_NAME = '__builtin__.open'
else:
OPEN_FUNCTION_NAME = 'builtins.open'
class ExecuteTestCase(testtools.TestCase):
"""This class is a copy of the same class in openstack/ironic."""
@ -135,13 +128,13 @@ grep foo
class GetAgentParamsTestCase(test_base.BaseTestCase):
@mock.patch('oslo_log.log.getLogger')
@mock.patch(OPEN_FUNCTION_NAME)
@mock.patch('six.moves.builtins.open')
def test__read_params_from_file_fail(self, logger_mock, open_mock):
open_mock.side_effect = Exception
params = utils._read_params_from_file('file-path')
self.assertEqual({}, params)
@mock.patch(OPEN_FUNCTION_NAME)
@mock.patch('six.moves.builtins.open')
def test__read_params_from_file(self, open_mock):
kernel_line = 'api-url=http://localhost:9999 baz foo=bar\n'
open_mock.return_value.__enter__ = lambda s: s
@ -201,7 +194,7 @@ class GetAgentParamsTestCase(test_base.BaseTestCase):
self.assertEqual(expected_params, returned_params)
self.assertEqual(0, set_cache_mock.call_count)
@mock.patch(OPEN_FUNCTION_NAME)
@mock.patch('six.moves.builtins.open')
@mock.patch.object(glob, 'glob')
def test__get_vmedia_device(self, glob_mock, open_mock):