Skip to content

Commit cb380e8

Browse files
Jenkinsopenstack-gerrit
authored andcommitted
Merge "Move vbd plug/unplug into session object"
2 parents f11071b + 355e724 commit cb380e8

File tree

9 files changed

+63
-58
lines changed

9 files changed

+63
-58
lines changed

nova/tests/virt/xenapi/client/test_objects.py

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
import mock
1717

1818
from nova.tests.virt.xenapi import stubs
19+
from nova import utils
1920
from nova.virt.xenapi.client import objects
2021

2122

@@ -89,3 +90,31 @@ def test_pool(self):
8990
pool = objects.Pool(self.session)
9091
pool.get_X("ref")
9192
self.session.call_xenapi.assert_called_once_with("pool.get_X", "ref")
93+
94+
95+
class VBDTestCase(stubs.XenAPITestBaseNoDB):
96+
def setUp(self):
97+
super(VBDTestCase, self).setUp()
98+
self.session = mock.Mock()
99+
self.session.VBD = objects.VBD(self.session)
100+
101+
def test_plug(self):
102+
self.session.VBD.plug("vbd_ref", "vm_ref")
103+
self.session.call_xenapi.assert_called_once_with("VBD.plug", "vbd_ref")
104+
105+
@mock.patch.object(utils, 'synchronized')
106+
def test_vbd_plug_check_synchronized(self, mock_synchronized):
107+
session = mock.Mock()
108+
self.session.VBD.plug("vbd_ref", "vm_ref")
109+
mock_synchronized.assert_called_once_with("xenapi-vbd-vm_ref")
110+
111+
def test_unplug(self):
112+
self.session.VBD.unplug("vbd_ref", "vm_ref")
113+
self.session.call_xenapi.assert_called_once_with("VBD.unplug",
114+
"vbd_ref")
115+
116+
@mock.patch.object(utils, 'synchronized')
117+
def test_vbd_plug_check_synchronized(self, mock_synchronized):
118+
session = mock.Mock()
119+
self.session.VBD.unplug("vbd_ref", "vm_ref")
120+
mock_synchronized.assert_called_once_with("xenapi-vbd-vm_ref")

nova/tests/virt/xenapi/test_vm_utils.py

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -887,7 +887,7 @@ def test_attach_cd(self):
887887
class UnplugVbdTestCase(VMUtilsTestBase):
888888
@mock.patch.object(greenthread, 'sleep')
889889
def test_unplug_vbd_works(self, mock_sleep):
890-
session = mock.Mock()
890+
session = _get_fake_session()
891891
vbd_ref = "vbd_ref"
892892
vm_ref = 'vm_ref'
893893

@@ -897,7 +897,7 @@ def test_unplug_vbd_works(self, mock_sleep):
897897
self.assertEqual(0, mock_sleep.call_count)
898898

899899
def test_unplug_vbd_raises_unexpected_error(self):
900-
session = mock.Mock()
900+
session = _get_fake_session()
901901
vbd_ref = "vbd_ref"
902902
vm_ref = 'vm_ref'
903903
session.call_xenapi.side_effect = test.TestingException()
@@ -1742,16 +1742,13 @@ class VDIAttachedHere(VMUtilsTestBase):
17421742
@mock.patch.object(vm_utils, 'destroy_vbd')
17431743
@mock.patch.object(vm_utils, '_get_this_vm_ref')
17441744
@mock.patch.object(vm_utils, 'create_vbd')
1745-
@mock.patch.object(volume_utils, 'vbd_plug')
17461745
@mock.patch.object(vm_utils, '_remap_vbd_dev')
17471746
@mock.patch.object(vm_utils, '_wait_for_device')
17481747
@mock.patch.object(utils, 'execute')
1749-
@mock.patch.object(vm_utils, 'unplug_vbd')
1750-
def test_sync_called(self, mock_unplug_vbd, mock_execute,
1751-
mock_wait_for_device, mock_remap_vbd_dev,
1752-
mock_vbd_plug, mock_create_vbd,
1748+
def test_sync_called(self, mock_execute, mock_wait_for_device,
1749+
mock_remap_vbd_dev, mock_create_vbd,
17531750
mock_get_this_vm_ref, mock_destroy_vbd):
1754-
session = mock.Mock()
1751+
session = _get_fake_session()
17551752
with vm_utils.vdi_attached_here(session, 'vdi_ref'):
17561753
pass
17571754
mock_execute.assert_called_with('sync', run_as_root=True)

nova/tests/virt/xenapi/test_volume_utils.py

Lines changed: 0 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -18,34 +18,9 @@
1818
from eventlet import greenthread
1919

2020
from nova.tests.virt.xenapi import stubs
21-
from nova import utils
2221
from nova.virt.xenapi import volume_utils
2322

2423

25-
class CallXenAPIHelpersTestCase(stubs.XenAPITestBaseNoDB):
26-
def test_vbd_plug(self):
27-
session = mock.Mock()
28-
volume_utils.vbd_plug(session, "vbd_ref", "vm_ref:123")
29-
session.call_xenapi.assert_called_once_with("VBD.plug", "vbd_ref")
30-
31-
@mock.patch.object(utils, 'synchronized')
32-
def test_vbd_plug_check_synchronized(self, mock_synchronized):
33-
session = mock.Mock()
34-
volume_utils.vbd_plug(session, "vbd_ref", "vm_ref:123")
35-
mock_synchronized.assert_called_once_with("xenapi-events-vm_ref:123")
36-
37-
def test_vbd_unplug(self):
38-
session = mock.Mock()
39-
volume_utils.vbd_unplug(session, "vbd_ref", "vm_ref:123")
40-
session.call_xenapi.assert_called_once_with("VBD.unplug", "vbd_ref")
41-
42-
@mock.patch.object(utils, 'synchronized')
43-
def test_vbd_unplug_check_synchronized(self, mock_synchronized):
44-
session = mock.Mock()
45-
volume_utils.vbd_unplug(session, "vbd_ref", "vm_ref:123")
46-
mock_synchronized.assert_called_once_with("xenapi-events-vm_ref:123")
47-
48-
4924
class SROps(stubs.XenAPITestBaseNoDB):
5025
def test_find_sr_valid_uuid(self):
5126
self.session = mock.Mock()

nova/tests/virt/xenapi/test_volumeops.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,7 @@ def _test_connect_volume(self, hotplug, vm_running, plugged):
128128
self.mox.StubOutWithMock(volumeops.volume_utils, 'introduce_vdi')
129129
self.mox.StubOutWithMock(volumeops.vm_utils, 'create_vbd')
130130
self.mox.StubOutWithMock(volumeops.vm_utils, 'is_vm_shutdown')
131+
self.mox.StubOutWithMock(ops._session.VBD, 'plug')
131132
self.mox.StubOutWithMock(ops._session, 'call_xenapi')
132133

133134
instance_name = 'instance_1'
@@ -159,7 +160,7 @@ def _test_connect_volume(self, hotplug, vm_running, plugged):
159160
volumeops.vm_utils.is_vm_shutdown(session,
160161
vm_ref).AndReturn(not vm_running)
161162
if plugged:
162-
ops._session.call_xenapi("VBD.plug", vbd_ref)
163+
ops._session.VBD.plug(vbd_ref, vm_ref)
163164
ops._session.call_xenapi("VDI.get_uuid",
164165
vdi_ref).AndReturn(vdi_uuid)
165166

nova/virt/xenapi/client/objects.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@
1212
# License for the specific language governing permissions and limitations
1313
# under the License.
1414

15+
from nova import utils
16+
1517

1618
class XenAPISessionObject(object):
1719
"""Wrapper to make calling and mocking the session easier
@@ -71,6 +73,26 @@ class VBD(XenAPISessionObject):
7173
def __init__(self, session):
7274
super(VBD, self).__init__(session, "VBD")
7375

76+
def plug(self, vbd_ref, vm_ref):
77+
@utils.synchronized('xenapi-vbd-' + vm_ref)
78+
def synchronized_plug():
79+
self._call_method("plug", vbd_ref)
80+
81+
# NOTE(johngarbutt) we need to ensure there is only ever one
82+
# VBD.unplug or VBD.plug happening at once per VM
83+
# due to a bug in XenServer 6.1 and 6.2
84+
synchronized_plug()
85+
86+
def unplug(self, vbd_ref, vm_ref):
87+
@utils.synchronized('xenapi-vbd-' + vm_ref)
88+
def synchronized_unplug():
89+
self._call_method("unplug", vbd_ref)
90+
91+
# NOTE(johngarbutt) we need to ensure there is only ever one
92+
# VBD.unplug or VBD.plug happening at once per VM
93+
# due to a bug in XenServer 6.1 and 6.2
94+
synchronized_unplug()
95+
7496

7597
class VDI(XenAPISessionObject):
7698
"""Virtual disk image."""

nova/virt/xenapi/fake.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@
6363
from nova.openstack.common import log as logging
6464
from nova.openstack.common import timeutils
6565
from nova.openstack.common import units
66+
from nova.virt.xenapi.client import session as xenapi_session
6667

6768

6869
_CLASSES = ['host', 'network', 'session', 'pool', 'SR', 'VBD',
@@ -472,6 +473,7 @@ class SessionBase(object):
472473

473474
def __init__(self, uri):
474475
self._session = None
476+
xenapi_session.apply_session_helpers(self)
475477

476478
def pool_get_default_SR(self, _1, pool_ref):
477479
return _db_content['pool'].values()[0]['default-SR']

nova/virt/xenapi/vm_utils.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -426,7 +426,7 @@ def unplug_vbd(session, vbd_ref, this_vm_ref):
426426
if num_attempt > 1:
427427
greenthread.sleep(1)
428428

429-
volume_utils.vbd_unplug(session, vbd_ref, this_vm_ref)
429+
session.VBD.unplug(vbd_ref, this_vm_ref)
430430
return
431431
except session.XenAPI.Failure as exc:
432432
err = len(exc.details) > 0 and exc.details[0]
@@ -2186,7 +2186,7 @@ def vdi_attached_here(session, vdi_ref, read_only=False):
21862186
read_only=read_only, bootable=False)
21872187
try:
21882188
LOG.debug(_('Plugging VBD %s ... '), vbd_ref)
2189-
volume_utils.vbd_plug(session, vbd_ref, this_vm_ref)
2189+
session.VBD.plug(vbd_ref, this_vm_ref)
21902190
try:
21912191
LOG.debug(_('Plugging VBD %s done.'), vbd_ref)
21922192
orig_dev = session.call_xenapi("VBD.get_device", vbd_ref)

nova/virt/xenapi/volume_utils.py

Lines changed: 0 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@
2626

2727
from nova.openstack.common.gettextutils import _
2828
from nova.openstack.common import log as logging
29-
from nova import utils
3029

3130
xenapi_volume_utils_opts = [
3231
cfg.IntOpt('introduce_vdi_retry_wait',
@@ -312,23 +311,3 @@ def _get_target_port(iscsi_string):
312311
return iscsi_string.split(':')[1]
313312

314313
return CONF.xenserver.target_port
315-
316-
317-
def vbd_plug(session, vbd_ref, vm_ref):
318-
@utils.synchronized('xenapi-events-' + vm_ref)
319-
def synchronized_plug():
320-
session.call_xenapi("VBD.plug", vbd_ref)
321-
322-
# NOTE(johngarbutt) we need to ensure there is only ever one VBD.plug
323-
# happening at once per VM due to a bug in XenServer 6.1 and 6.2
324-
synchronized_plug()
325-
326-
327-
def vbd_unplug(session, vbd_ref, vm_ref):
328-
@utils.synchronized('xenapi-events-' + vm_ref)
329-
def synchronized_unplug():
330-
session.call_xenapi("VBD.unplug", vbd_ref)
331-
332-
# NOTE(johngarbutt) we need to ensure there is only ever one VBD.unplug
333-
# happening at once per VM due to a bug in XenServer 6.1 and 6.2
334-
synchronized_unplug()

nova/virt/xenapi/volumeops.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,7 @@ def _connect_volume(self, connection_info, dev_number=None,
111111

112112
running = not vm_utils.is_vm_shutdown(self._session, vm_ref)
113113
if hotplug and running:
114-
volume_utils.vbd_plug(self._session, vbd_ref, vm_ref)
114+
self._session.VBD.plug(vbd_ref, vm_ref)
115115

116116
vdi_uuid = self._session.call_xenapi("VDI.get_uuid", vdi_ref)
117117
return (sr_uuid, vdi_uuid)

0 commit comments

Comments
 (0)