Use stub_out and mock to remove mox:part 3

This change replaces the use of stubs in a few files
with stub_out, which was introduced in test.py.
Also, remove mox usage in some of the files.

unit/test_block_device.py
unit/test_context.py
unit/test_crypto.py
unit/test_metadata.py
unit/test_notifications.py

Part of bp:remove-mox

Change-Id: Ibb8a87b34d5f238e08469883361297757a467765
This commit is contained in:
jichenjc 2015-12-19 19:39:52 +08:00
parent 5d6e0086b5
commit 1b7462a5a7
5 changed files with 34 additions and 51 deletions

View File

@ -364,11 +364,11 @@ class TestBlockDeviceDict(test.NoDBTestCase):
def fake_validate(obj, dct):
pass
self.stubs.Set(block_device.BlockDeviceDict, '_fields',
self.stub_out('nova.block_device.BlockDeviceDict._fields',
set(['field1', 'field2']))
self.stubs.Set(block_device.BlockDeviceDict, '_db_only_fields',
self.stub_out('nova.block_device.BlockDeviceDict._db_only_fields',
set(['db_field1', 'db_field2']))
self.stubs.Set(block_device.BlockDeviceDict, '_validate',
self.stub_out('nova.block_device.BlockDeviceDict._validate',
fake_validate)
# Make sure db fields are not picked up if they are not

View File

@ -83,7 +83,7 @@ class ContextTestCase(test.NoDBTestCase):
def fake_warn(log_msg):
info['log_msg'] = log_msg
self.stubs.Set(context.LOG, 'warning', fake_warn)
self.stub_out('nova.context.LOG.warning', fake_warn)
c = context.RequestContext('user', 'project',
extra_arg1='meow', extra_arg2='wuff')
@ -128,7 +128,7 @@ class ContextTestCase(test.NoDBTestCase):
a = a[0]
warns.append(str(msg) % a)
self.stubs.Set(context.LOG, 'warn', stub_warn)
self.stub_out('nova.context.LOG.warn', stub_warn)
ctxt = context.RequestContext('111',
'222',

View File

@ -22,13 +22,11 @@ import uuid
from cryptography.hazmat import backends
from cryptography.hazmat.primitives import serialization
import mock
from mox3 import mox
from oslo_concurrency import processutils
import paramiko
import six
from nova import crypto
from nova import db
from nova import exception
from nova import test
from nova import utils
@ -95,7 +93,8 @@ class X509Test(test.TestCase):
class RevokeCertsTest(test.TestCase):
def test_revoke_certs_by_user_and_project(self):
@mock.patch('nova.crypto.revoke_cert')
def test_revoke_certs_by_user_and_project(self, mock_revoke):
user_id = 'test_user'
project_id = 2
file_name = 'test_file'
@ -107,17 +106,15 @@ class RevokeCertsTest(test.TestCase):
return [{"user_id": user_id, "project_id": project_id,
"file_name": file_name}]
self.stubs.Set(db, 'certificate_get_all_by_user_and_project',
mock_certificate_get_all_by_user_and_project)
self.mox.StubOutWithMock(crypto, 'revoke_cert')
crypto.revoke_cert(project_id, file_name)
self.mox.ReplayAll()
self.stub_out('nova.db.certificate_get_all_by_user_and_project',
mock_certificate_get_all_by_user_and_project)
crypto.revoke_certs_by_user_and_project(user_id, project_id)
def test_revoke_certs_by_user(self):
mock_revoke.assert_called_once_with(project_id, file_name)
@mock.patch('nova.crypto.revoke_cert')
def test_revoke_certs_by_user(self, mock_revoke):
user_id = 'test_user'
project_id = 2
file_name = 'test_file'
@ -127,17 +124,14 @@ class RevokeCertsTest(test.TestCase):
return [{"user_id": user_id, "project_id": project_id,
"file_name": file_name}]
self.stubs.Set(db, 'certificate_get_all_by_user',
mock_certificate_get_all_by_user)
self.mox.StubOutWithMock(crypto, 'revoke_cert')
crypto.revoke_cert(project_id, mox.IgnoreArg())
self.mox.ReplayAll()
self.stub_out('nova.db.certificate_get_all_by_user',
mock_certificate_get_all_by_user)
crypto.revoke_certs_by_user(user_id)
mock_revoke.assert_called_once_with(project_id, mock.ANY)
def test_revoke_certs_by_project(self):
@mock.patch('nova.crypto.revoke_cert')
def test_revoke_certs_by_project(self, mock_revoke):
user_id = 'test_user'
project_id = 2
file_name = 'test_file'
@ -147,15 +141,11 @@ class RevokeCertsTest(test.TestCase):
return [{"user_id": user_id, "project_id": project_id,
"file_name": file_name}]
self.stubs.Set(db, 'certificate_get_all_by_project',
mock_certificate_get_all_by_project)
self.mox.StubOutWithMock(crypto, 'revoke_cert')
crypto.revoke_cert(project_id, mox.IgnoreArg())
self.mox.ReplayAll()
self.stub_out('nova.db.certificate_get_all_by_project',
mock_certificate_get_all_by_project)
crypto.revoke_certs_by_project(project_id)
mock_revoke.assert_called_once_with(project_id, mock.ANY)
@mock.patch.object(utils, 'execute',
side_effect=processutils.ProcessExecutionError)

View File

@ -39,7 +39,6 @@ from nova import block_device
from nova.compute import flavors
from nova.conductor import api as conductor_api
from nova import context
from nova import db
from nova import exception
from nova.network import api as network_api
from nova.network import model as network_model
@ -249,7 +248,7 @@ class MetadataTestCase(test.TestCase):
'delete_on_termination': None,
'device_name': '/dev/sdb'})]
self.stubs.Set(db, 'block_device_mapping_get_all_by_instance',
self.stub_out('nova.db.block_device_mapping_get_all_by_instance',
fake_bdm_get)
expected = {'ami': 'sda1',
@ -312,15 +311,14 @@ class MetadataTestCase(test.TestCase):
self.assertTrue(md._check_version('2009-04-04', '2009-04-04'))
def test_InstanceMetadata_uses_passed_network_info(self):
@mock.patch('nova.virt.netutils.get_injected_network_template')
def test_InstanceMetadata_uses_passed_network_info(self, mock_get):
network_info = []
self.mox.StubOutWithMock(netutils, "get_injected_network_template")
netutils.get_injected_network_template(network_info).AndReturn(False)
self.mox.ReplayAll()
mock_get.return_value = False
base.InstanceMetadata(fake_inst_obj(self.context),
network_info=network_info)
mock_get.assert_called_once_with(network_info)
@mock.patch.object(netutils, "get_network_metadata", autospec=True)
def test_InstanceMetadata_gets_network_metadata(self, mock_netutils):
@ -337,17 +335,13 @@ class MetadataTestCase(test.TestCase):
for (path, value) in inst_md.metadata_for_config_drive():
self.assertIsNotNone(path)
def test_InstanceMetadata_queries_network_API_when_needed(self):
@mock.patch('nova.virt.netutils.get_injected_network_template')
def test_InstanceMetadata_queries_network_API_when_needed(self, mock_get):
network_info_from_api = []
self.mox.StubOutWithMock(netutils, "get_injected_network_template")
netutils.get_injected_network_template(
network_info_from_api).AndReturn(False)
self.mox.ReplayAll()
mock_get.return_value = False
base.InstanceMetadata(fake_inst_obj(self.context))
mock_get.assert_called_once_with(network_info_from_api)
def test_local_ipv4(self):
nw_info = fake_network.fake_get_instance_nw_info(self.stubs,
@ -779,7 +773,7 @@ class MetadataHandlerTestCase(test.TestCase):
self.assertTrue(response_ctype.startswith("application/json"))
def test_user_data_non_existing_fixed_address(self):
self.stubs.Set(network_api.API, 'get_fixed_ip_by_address',
self.stub_out('nova.network.api.get_fixed_ip_by_address',
return_non_existing_address)
response = fake_request(None, self.mdinst, "/2009-04-04/user-data",
"127.1.1.1")

View File

@ -27,7 +27,6 @@ from nova.compute import task_states
from nova.compute import vm_states
from nova import context
from nova import exception
from nova.network import api as network_api
from nova import notifications
from nova import objects
from nova.objects import base as obj_base
@ -52,7 +51,7 @@ class NotificationsTestCase(test.TestCase):
self.assertTrue(ctxt.is_admin)
return self.net_info
self.stubs.Set(network_api.API, 'get_instance_nw_info',
self.stub_out('nova.network.api.API.get_instance_nw_info',
fake_get_nw_info)
fake_network.set_stub_network_methods(self.stubs)
@ -420,7 +419,7 @@ class NotificationsTestCase(test.TestCase):
def sending_no_state_change(context, instance, **kwargs):
called[0] = True
self.stubs.Set(notifications, '_send_instance_update_notification',
self.stub_out('nova.notifications._send_instance_update_notification',
sending_no_state_change)
notifications.send_update(self.context, self.instance, self.instance)
self.assertTrue(called[0])
@ -428,7 +427,7 @@ class NotificationsTestCase(test.TestCase):
def test_fail_sending_update(self):
def fail_sending(context, instance, **kwargs):
raise Exception('failed to notify')
self.stubs.Set(notifications, '_send_instance_update_notification',
self.stub_out('nova.notifications._send_instance_update_notification',
fail_sending)
notifications.send_update(self.context, self.instance, self.instance)