Skip to content

Commit eeb9a9f

Browse files
author
Eric Fried
committed
Fix mock specs set to strings
The behavior of Mock(spec=foo) or Mock(spec_set=foo) when foo is not a list is to interpret foo as a class or instance and derive the set of attributes you're allowed to get (spec) and/or set (spec_set) based on dir(foo) [1]. Thus if foo is a string, your mock will be set up to look like a string (with attributes like index, find, startswith...). So for example, if you see: Mock(spec='nova.objects.Instance') ...what was almost certainly intended was: Mock(spec=nova.objects.Instance) This commit fixes all such cases in the nova codebase, making those specs point to the actual classes as intended. A subsequent commit will introduce a hacking rule to ensure this mistake isn't repeated in the future. [1] https://docs.python.org/3/library/unittest.mock.html#the-mock-class Change-Id: I924edd474a798aa32bb8fb88ba6d84815a8bf93e
1 parent a991980 commit eeb9a9f

File tree

3 files changed

+16
-18
lines changed

3 files changed

+16
-18
lines changed

nova/tests/fixtures.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@
2626
import warnings
2727

2828
import fixtures
29+
import futurist
30+
from keystoneauth1 import adapter as ksa_adap
2931
import mock
3032
from neutronclient.common import exceptions as neutron_client_exc
3133
import os_resource_classes as orc
@@ -1116,7 +1118,7 @@ def setUp(self):
11161118

11171119
def fake_submit(_self, fn, *args, **kwargs):
11181120
result = fn(*args, **kwargs)
1119-
future = mock.Mock(spec='futurist.Future')
1121+
future = mock.Mock(spec=futurist.Future)
11201122
future.return_value.result.return_value = result
11211123
return future
11221124
self.useFixture(fixtures.MonkeyPatch(
@@ -1462,7 +1464,7 @@ def setUp(self):
14621464
self.test.stub_out(
14631465
'nova.network.neutronv2.api._get_ksa_client',
14641466
lambda *args, **kwargs: mock.Mock(
1465-
spec='keystoneauth1.adapter.Adapter'))
1467+
spec=ksa_adap.Adapter))
14661468
self.test.stub_out(
14671469
'nova.network.neutronv2.api.API._create_port_binding',
14681470
self.fake_create_port_binding)

nova/tests/unit/network/test_neutronv2.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5340,7 +5340,7 @@ def test_update_ports_for_instance_fails_rollback_ports_and_vifs(self,
53405340
mock_populate_ext_values):
53415341
"""Makes sure we rollback ports and VIFs if we fail updating ports"""
53425342
instance = fake_instance.fake_instance_obj(self.context)
5343-
ntrn = mock.Mock(spec='neutronclient.v2_0.client.Client')
5343+
ntrn = mock.Mock(spec=client.Client)
53445344
# we have two requests, one with a preexisting port and one where nova
53455345
# created the port (on the same network)
53465346
requests_and_created_ports = [

nova/tests/unit/virt/libvirt/test_driver.py

Lines changed: 11 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -8009,10 +8009,9 @@ def test_extend_volume(self):
80098009

80108010
new_size_in_kb = 20 * 1024 * 1024
80118011

8012-
guest = mock.Mock(spec='nova.virt.libvirt.guest.Guest')
8012+
guest = mock.Mock(spec=libvirt_guest.Guest)
80138013
# block_device
8014-
block_device = mock.Mock(
8015-
spec='nova.virt.libvirt.guest.BlockDevice')
8014+
block_device = mock.Mock(spec=libvirt_guest.BlockDevice)
80168015
block_device.resize = mock.Mock()
80178016
guest.get_block_device = mock.Mock(return_value=block_device)
80188017
drvr._host.get_guest = mock.Mock(return_value=guest)
@@ -8083,11 +8082,10 @@ def test_extend_volume_with_libvirt_error(self):
80838082
}
80848083
new_size_in_kb = 20 * 1024 * 1024
80858084

8086-
guest = mock.Mock(spec='nova.virt.libvirt.guest.Guest')
8085+
guest = mock.Mock(spec=libvirt_guest.Guest)
80878086
guest.get_power_state = mock.Mock(return_value=power_state.RUNNING)
80888087
# block_device
8089-
block_device = mock.Mock(
8090-
spec='nova.virt.libvirt.guest.BlockDevice')
8088+
block_device = mock.Mock(spec=libvirt_guest.BlockDevice)
80918089
block_device.resize = mock.Mock(
80928090
side_effect=fakelibvirt.libvirtError('ERR'))
80938091
guest.get_block_device = mock.Mock(return_value=block_device)
@@ -8111,13 +8109,12 @@ def test_extend_volume_with_no_device_path_attribute(self):
81118109
}
81128110
new_size_in_kb = 20 * 1024 * 1024
81138111

8114-
guest = mock.Mock(spec='nova.virt.libvirt.guest.Guest')
8112+
guest = mock.Mock(spec=libvirt_guest.Guest)
81158113
# block_device
8116-
block_device = mock.Mock(
8117-
spec='nova.virt.libvirt.guest.BlockDevice')
8114+
block_device = mock.Mock(spec=libvirt_guest.BlockDevice)
81188115
block_device.resize = mock.Mock()
81198116
disk = mock.Mock(
8120-
spec='nova.virt.libvirt.config.LibvirtConfigGuestDisk',
8117+
spec=vconfig.LibvirtConfigGuestDisk,
81218118
serial='58a84f6d-3f0c-4e19-a0af-eb657b790657',
81228119
target_dev='vdb')
81238120
guest.get_block_device = mock.Mock(return_value=block_device)
@@ -8148,13 +8145,12 @@ def test_extend_volume_no_disk_found_by_serial(self):
81488145
}
81498146
new_size_in_kb = 20 * 1024 * 1024
81508147

8151-
guest = mock.Mock(spec='nova.virt.libvirt.guest.Guest')
8148+
guest = mock.Mock(spec=libvirt_guest.Guest)
81528149
# block_device
8153-
block_device = mock.Mock(
8154-
spec='nova.virt.libvirt.guest.BlockDevice')
8150+
block_device = mock.Mock(spec=libvirt_guest.BlockDevice)
81558151
block_device.resize = mock.Mock()
81568152
disk = mock.Mock(
8157-
spec='nova.virt.libvirt.config.LibvirtConfigGuestDisk',
8153+
spec=vconfig.LibvirtConfigGuestDisk,
81588154
serial='12345678-abcd-abcd-abcd-0123456789012',
81598155
target_dev='vdb')
81608156
guest.get_block_device = mock.Mock(return_value=block_device)
@@ -20064,7 +20060,7 @@ def test_detach_interface_device_not_found(self, mock_log):
2006420060
# found on the guest after a libvirt error during detach.
2006520061
instance = self._create_instance()
2006620062
vif = _fake_network_info(self, 1)[0]
20067-
guest = mock.Mock(spec='nova.virt.libvirt.guest.Guest')
20063+
guest = mock.Mock(spec=libvirt_guest.Guest)
2006820064
guest.get_power_state = mock.Mock()
2006920065
self.drvr._host.get_guest = mock.Mock(return_value=guest)
2007020066
error = fakelibvirt.libvirtError(

0 commit comments

Comments
 (0)