Merge "Add missing 'autospec' statements to unit test mocks"

This commit is contained in:
Jenkins
2017-03-23 22:17:18 +00:00
committed by Gerrit Code Review
9 changed files with 36 additions and 36 deletions

View File

@@ -230,7 +230,7 @@ class CommonFiltersTest(test_utils.BaseTestCase):
self.assertEqual(['fields=a,b,c'], result) self.assertEqual(['fields=a,b,c'], result)
@mock.patch.object(subprocess, 'Popen') @mock.patch.object(subprocess, 'Popen', autospec=True)
class MakeConfigDriveTest(test_utils.BaseTestCase): class MakeConfigDriveTest(test_utils.BaseTestCase):
def setUp(self): def setUp(self):
@@ -256,14 +256,14 @@ class MakeConfigDriveTest(test_utils.BaseTestCase):
stdout=subprocess.PIPE) stdout=subprocess.PIPE)
fake_process.communicate.assert_called_once_with() fake_process.communicate.assert_called_once_with()
@mock.patch.object(os, 'access') @mock.patch.object(os, 'access', autospec=True)
def test_make_configdrive_non_readable_dir(self, mock_access, mock_popen): def test_make_configdrive_non_readable_dir(self, mock_access, mock_popen):
mock_access.return_value = False mock_access.return_value = False
self.assertRaises(exc.CommandError, utils.make_configdrive, 'fake-dir') self.assertRaises(exc.CommandError, utils.make_configdrive, 'fake-dir')
mock_access.assert_called_once_with('fake-dir', os.R_OK) mock_access.assert_called_once_with('fake-dir', os.R_OK)
self.assertFalse(mock_popen.called) self.assertFalse(mock_popen.called)
@mock.patch.object(os, 'access') @mock.patch.object(os, 'access', autospec=True)
def test_make_configdrive_oserror(self, mock_access, mock_popen): def test_make_configdrive_oserror(self, mock_access, mock_popen):
mock_access.return_value = True mock_access.return_value = True
mock_popen.side_effect = OSError('boom') mock_popen.side_effect = OSError('boom')
@@ -274,7 +274,7 @@ class MakeConfigDriveTest(test_utils.BaseTestCase):
stderr=subprocess.PIPE, stderr=subprocess.PIPE,
stdout=subprocess.PIPE) stdout=subprocess.PIPE)
@mock.patch.object(os, 'access') @mock.patch.object(os, 'access', autospec=True)
def test_make_configdrive_non_zero_returncode(self, mock_access, def test_make_configdrive_non_zero_returncode(self, mock_access,
mock_popen): mock_popen):
fake_process = mock.Mock(returncode=123) fake_process = mock.Mock(returncode=123)

View File

@@ -281,7 +281,7 @@ class ClientTest(utils.BaseTestCase):
self._test_loader_arguments_passed_correctly( self._test_loader_arguments_passed_correctly(
passed_kwargs=passed_kwargs, expected_kwargs=expected_kwargs) passed_kwargs=passed_kwargs, expected_kwargs=expected_kwargs)
@mock.patch.object(iroclient, 'Client') @mock.patch.object(iroclient, 'Client', autospec=True)
@mock.patch.object(kaloading.session, 'Session', autospec=True) @mock.patch.object(kaloading.session, 'Session', autospec=True)
def test_correct_arguments_passed_to_client_constructor_noauth_mode( def test_correct_arguments_passed_to_client_constructor_noauth_mode(
self, mock_ks_session, mock_client): self, mock_ks_session, mock_client):
@@ -312,7 +312,7 @@ class ClientTest(utils.BaseTestCase):
) )
self.assertFalse(mock_ks_session.called) self.assertFalse(mock_ks_session.called)
@mock.patch.object(iroclient, 'Client') @mock.patch.object(iroclient, 'Client', autospec=True)
@mock.patch.object(kaloading.session, 'Session', autospec=True) @mock.patch.object(kaloading.session, 'Session', autospec=True)
def test_correct_arguments_passed_to_client_constructor_session_created( def test_correct_arguments_passed_to_client_constructor_session_created(
self, mock_ks_session, mock_client): self, mock_ks_session, mock_client):
@@ -337,7 +337,7 @@ class ClientTest(utils.BaseTestCase):
} }
) )
@mock.patch.object(iroclient, 'Client') @mock.patch.object(iroclient, 'Client', autospec=True)
@mock.patch.object(kaloading.session, 'Session', autospec=True) @mock.patch.object(kaloading.session, 'Session', autospec=True)
def test_correct_arguments_passed_to_client_constructor_session_passed( def test_correct_arguments_passed_to_client_constructor_session_passed(
self, mock_ks_session, mock_client): self, mock_ks_session, mock_client):

View File

@@ -21,7 +21,7 @@ from ironicclient import exc
from ironicclient.tests.unit import utils as test_utils from ironicclient.tests.unit import utils as test_utils
@mock.patch.object(exceptions, 'from_response') @mock.patch.object(exceptions, 'from_response', autospec=True)
class ExcTest(test_utils.BaseTestCase): class ExcTest(test_utils.BaseTestCase):
def setUp(self): def setUp(self):

View File

@@ -153,10 +153,10 @@ class ShellTest(utils.BaseTestCase):
self.make_env(exclude='OS_USERNAME') self.make_env(exclude='OS_USERNAME')
self.test_help() self.test_help()
@mock.patch.object(client, 'get_client', @mock.patch.object(client, 'get_client', autospec=True,
side_effect=keystone_exc.ConnectFailure) side_effect=keystone_exc.ConnectFailure)
@mock.patch('sys.stdin', side_effect=mock.MagicMock) @mock.patch('sys.stdin', side_effect=mock.MagicMock, autospec=True)
@mock.patch('getpass.getpass', return_value='password') @mock.patch('getpass.getpass', return_value='password', autospec=True)
def test_password_prompted(self, mock_getpass, mock_stdin, mock_client): def test_password_prompted(self, mock_getpass, mock_stdin, mock_client):
self.make_env(exclude='OS_PASSWORD') self.make_env(exclude='OS_PASSWORD')
# We will get a ConnectFailure because there is no keystone. # We will get a ConnectFailure because there is no keystone.
@@ -181,9 +181,9 @@ class ShellTest(utils.BaseTestCase):
# Make sure we are actually prompted. # Make sure we are actually prompted.
mock_getpass.assert_called_with('OpenStack Password: ') mock_getpass.assert_called_with('OpenStack Password: ')
@mock.patch.object(client, 'get_client', @mock.patch.object(client, 'get_client', autospec=True,
side_effect=keystone_exc.ConnectFailure) side_effect=keystone_exc.ConnectFailure)
@mock.patch('getpass.getpass', return_value='password') @mock.patch('getpass.getpass', return_value='password', autospec=True)
def test_token_auth(self, mock_getpass, mock_client): def test_token_auth(self, mock_getpass, mock_client):
self.make_env(environ_dict=FAKE_ENV_KEYSTONE_V2_TOKEN) self.make_env(environ_dict=FAKE_ENV_KEYSTONE_V2_TOKEN)
# We will get a ConnectFailure because there is no keystone. # We will get a ConnectFailure because there is no keystone.
@@ -209,8 +209,8 @@ class ShellTest(utils.BaseTestCase):
mock_client.assert_called_once_with(1, **expected_kwargs) mock_client.assert_called_once_with(1, **expected_kwargs)
self.assertFalse(mock_getpass.called) self.assertFalse(mock_getpass.called)
@mock.patch('sys.stdin', side_effect=mock.MagicMock) @mock.patch('sys.stdin', side_effect=mock.MagicMock, autospec=True)
@mock.patch('getpass.getpass', side_effect=EOFError) @mock.patch('getpass.getpass', side_effect=EOFError, autospec=True)
def test_password_prompted_ctrlD(self, mock_getpass, mock_stdin): def test_password_prompted_ctrlD(self, mock_getpass, mock_stdin):
self.make_env(exclude='OS_PASSWORD') self.make_env(exclude='OS_PASSWORD')
# We should get Command Error because we mock Ctl-D. # We should get Command Error because we mock Ctl-D.
@@ -219,7 +219,7 @@ class ShellTest(utils.BaseTestCase):
# Make sure we are actually prompted. # Make sure we are actually prompted.
mock_getpass.assert_called_with('OpenStack Password: ') mock_getpass.assert_called_with('OpenStack Password: ')
@mock.patch('sys.stdin') @mock.patch('sys.stdin', autospec=True)
def test_no_password_no_tty(self, mock_stdin): def test_no_password_no_tty(self, mock_stdin):
# delete the isatty attribute so that we do not get # delete the isatty attribute so that we do not get
# prompted when manually running the tests # prompted when manually running the tests

View File

@@ -208,7 +208,7 @@ class LoadFromFileTest(utils.BaseTestCase):
'must have .json or .yaml extension', 'must have .json or .yaml extension',
create_resources.load_from_file, fname) create_resources.load_from_file, fname)
@mock.patch.object(__builtin__, 'open') @mock.patch.object(__builtin__, 'open', autospec=True)
def test_load_ioerror(self, mock_open): def test_load_ioerror(self, mock_open):
mock_open.side_effect = IOError('file does not exist') mock_open.side_effect = IOError('file does not exist')
fname = 'abc.json' fname = 'abc.json'

View File

@@ -22,7 +22,7 @@ class TestCreateResourcesShell(utils.BaseTestCase):
super(TestCreateResourcesShell, self).setUp() super(TestCreateResourcesShell, self).setUp()
self.client = mock.MagicMock(autospec=True) self.client = mock.MagicMock(autospec=True)
@mock.patch.object(create_resources, 'create_resources') @mock.patch.object(create_resources, 'create_resources', autospec=True)
def test_create_shell(self, mock_create_resources): def test_create_shell(self, mock_create_resources):
args = mock.MagicMock() args = mock.MagicMock()
files = ['file1', 'file2', 'file3'] files = ['file1', 'file2', 'file3']

View File

@@ -131,7 +131,7 @@ class DriverManagerTest(testtools.TestCase):
'/v1/drivers/%s/raid/logical_disk_properties' % DRIVER2['name']) '/v1/drivers/%s/raid/logical_disk_properties' % DRIVER2['name'])
self.assertEqual({}, properties) self.assertEqual({}, properties)
@mock.patch.object(driver.DriverManager, 'update') @mock.patch.object(driver.DriverManager, 'update', autospec=True)
def test_vendor_passthru_update(self, update_mock): def test_vendor_passthru_update(self, update_mock):
# For now just mock the tests because vendor-passthru doesn't return # For now just mock the tests because vendor-passthru doesn't return
# anything to verify. # anything to verify.
@@ -146,12 +146,12 @@ class DriverManagerTest(testtools.TestCase):
for http_method in ('POST', 'PUT', 'PATCH'): for http_method in ('POST', 'PUT', 'PATCH'):
kwargs['http_method'] = http_method kwargs['http_method'] = http_method
self.mgr.vendor_passthru(**kwargs) self.mgr.vendor_passthru(**kwargs)
update_mock.assert_called_once_with(final_path, update_mock.assert_called_once_with(mock.ANY, final_path,
vendor_passthru_args, vendor_passthru_args,
http_method=http_method) http_method=http_method)
update_mock.reset_mock() update_mock.reset_mock()
@mock.patch.object(driver.DriverManager, 'get') @mock.patch.object(driver.DriverManager, 'get', autospec=True)
def test_vendor_passthru_get(self, get_mock): def test_vendor_passthru_get(self, get_mock):
kwargs = { kwargs = {
'driver_name': 'driver_name', 'driver_name': 'driver_name',
@@ -161,9 +161,9 @@ class DriverManagerTest(testtools.TestCase):
final_path = 'driver_name/vendor_passthru/method' final_path = 'driver_name/vendor_passthru/method'
self.mgr.vendor_passthru(**kwargs) self.mgr.vendor_passthru(**kwargs)
get_mock.assert_called_once_with(final_path) get_mock.assert_called_once_with(mock.ANY, final_path)
@mock.patch.object(driver.DriverManager, 'delete') @mock.patch.object(driver.DriverManager, 'delete', autospec=True)
def test_vendor_passthru_delete(self, delete_mock): def test_vendor_passthru_delete(self, delete_mock):
kwargs = { kwargs = {
'driver_name': 'driver_name', 'driver_name': 'driver_name',
@@ -173,9 +173,9 @@ class DriverManagerTest(testtools.TestCase):
final_path = 'driver_name/vendor_passthru/method' final_path = 'driver_name/vendor_passthru/method'
self.mgr.vendor_passthru(**kwargs) self.mgr.vendor_passthru(**kwargs)
delete_mock.assert_called_once_with(final_path) delete_mock.assert_called_once_with(mock.ANY, final_path)
@mock.patch.object(driver.DriverManager, 'delete') @mock.patch.object(driver.DriverManager, 'delete', autospec=True)
def test_vendor_passthru_unknown_http_method(self, delete_mock): def test_vendor_passthru_unknown_http_method(self, delete_mock):
kwargs = { kwargs = {
'driver_name': 'driver_name', 'driver_name': 'driver_name',

View File

@@ -73,7 +73,7 @@ class DriverShellTest(utils.BaseTestCase):
d_shell.do_driver_properties(client_mock, args) d_shell.do_driver_properties(client_mock, args)
client_mock.driver.properties.assert_called_once_with("driver_name") client_mock.driver.properties.assert_called_once_with("driver_name")
@mock.patch('ironicclient.common.cliutils.print_dict') @mock.patch('ironicclient.common.cliutils.print_dict', autospec=True)
def test_do_driver_properties_with_wrap_default(self, mock_print_dict): def test_do_driver_properties_with_wrap_default(self, mock_print_dict):
client_mock = self.client_mock client_mock = self.client_mock
client_mock.driver.properties.return_value = { client_mock.driver.properties.return_value = {
@@ -91,7 +91,7 @@ class DriverShellTest(utils.BaseTestCase):
json_flag=False, json_flag=False,
wrap=0) wrap=0)
@mock.patch('ironicclient.common.cliutils.print_dict') @mock.patch('ironicclient.common.cliutils.print_dict', autospec=True)
def test_do_driver_properties_with_wrap(self, mock_print_dict): def test_do_driver_properties_with_wrap(self, mock_print_dict):
client_mock = self.client_mock client_mock = self.client_mock
client_mock.driver.properties.return_value = { client_mock.driver.properties.return_value = {
@@ -109,7 +109,7 @@ class DriverShellTest(utils.BaseTestCase):
json_flag=False, json_flag=False,
wrap=80) wrap=80)
@mock.patch('ironicclient.common.cliutils.print_dict') @mock.patch('ironicclient.common.cliutils.print_dict', autospec=True)
def _test_do_driver_raid_logical_disk(self, print_dict_mock, wrap=0): def _test_do_driver_raid_logical_disk(self, print_dict_mock, wrap=0):
cli_mock = self.client_mock cli_mock = self.client_mock
cli_mock.driver.raid_logical_disk_properties.return_value = { cli_mock.driver.raid_logical_disk_properties.return_value = {

View File

@@ -1013,7 +1013,7 @@ class NodeManagerTest(testtools.TestCase):
] ]
self.assertEqual(expect, self.api.calls) self.assertEqual(expect, self.api.calls)
@mock.patch.object(common_utils, 'make_configdrive') @mock.patch.object(common_utils, 'make_configdrive', autospec=True)
def test_node_set_provision_state_with_configdrive_dir(self, def test_node_set_provision_state_with_configdrive_dir(self,
mock_configdrive): mock_configdrive):
mock_configdrive.return_value = 'fake-configdrive' mock_configdrive.return_value = 'fake-configdrive'
@@ -1081,7 +1081,7 @@ class NodeManagerTest(testtools.TestCase):
self.assertEqual(expect, self.api.calls) self.assertEqual(expect, self.api.calls)
self.assertEqual(CONSOLE_DATA_DISABLED, info) self.assertEqual(CONSOLE_DATA_DISABLED, info)
@mock.patch.object(node.NodeManager, 'update') @mock.patch.object(node.NodeManager, 'update', autospec=True)
def test_vendor_passthru_update(self, update_mock): def test_vendor_passthru_update(self, update_mock):
# For now just mock the tests because vendor-passthru doesn't return # For now just mock the tests because vendor-passthru doesn't return
# anything to verify. # anything to verify.
@@ -1096,12 +1096,12 @@ class NodeManagerTest(testtools.TestCase):
for http_method in ('POST', 'PUT', 'PATCH'): for http_method in ('POST', 'PUT', 'PATCH'):
kwargs['http_method'] = http_method kwargs['http_method'] = http_method
self.mgr.vendor_passthru(**kwargs) self.mgr.vendor_passthru(**kwargs)
update_mock.assert_called_once_with(final_path, update_mock.assert_called_once_with(mock.ANY, final_path,
vendor_passthru_args, vendor_passthru_args,
http_method=http_method) http_method=http_method)
update_mock.reset_mock() update_mock.reset_mock()
@mock.patch.object(node.NodeManager, 'get') @mock.patch.object(node.NodeManager, 'get', autospec=True)
def test_vendor_passthru_get(self, get_mock): def test_vendor_passthru_get(self, get_mock):
kwargs = { kwargs = {
'node_id': 'node_uuid', 'node_id': 'node_uuid',
@@ -1111,9 +1111,9 @@ class NodeManagerTest(testtools.TestCase):
final_path = 'node_uuid/vendor_passthru/method' final_path = 'node_uuid/vendor_passthru/method'
self.mgr.vendor_passthru(**kwargs) self.mgr.vendor_passthru(**kwargs)
get_mock.assert_called_once_with(final_path) get_mock.assert_called_once_with(mock.ANY, final_path)
@mock.patch.object(node.NodeManager, 'delete') @mock.patch.object(node.NodeManager, 'delete', autospec=True)
def test_vendor_passthru_delete(self, delete_mock): def test_vendor_passthru_delete(self, delete_mock):
kwargs = { kwargs = {
'node_id': 'node_uuid', 'node_id': 'node_uuid',
@@ -1123,9 +1123,9 @@ class NodeManagerTest(testtools.TestCase):
final_path = 'node_uuid/vendor_passthru/method' final_path = 'node_uuid/vendor_passthru/method'
self.mgr.vendor_passthru(**kwargs) self.mgr.vendor_passthru(**kwargs)
delete_mock.assert_called_once_with(final_path) delete_mock.assert_called_once_with(mock.ANY, final_path)
@mock.patch.object(node.NodeManager, 'delete') @mock.patch.object(node.NodeManager, 'delete', autospec=True)
def test_vendor_passthru_unknown_http_method(self, delete_mock): def test_vendor_passthru_unknown_http_method(self, delete_mock):
kwargs = { kwargs = {
'node_id': 'node_uuid', 'node_id': 'node_uuid',