]> review.fuel-infra Code Review - openstack-build/cinder-build.git/commitdiff
Adds brick helpers to cinder utils
authorWalter A. Boring IV <walter.boring@hp.com>
Wed, 21 Aug 2013 21:00:13 +0000 (14:00 -0700)
committerWalter A. Boring IV <walter.boring@hp.com>
Wed, 21 Aug 2013 23:45:19 +0000 (16:45 -0700)
This patch adds some cinder utils functions
that wrap calls into brick to automatically
populate cinder's root wrap helper.  This
prevents propogating the recreation of the
sudo cinder-rootwrap string.

Change-Id: I3ae2add4f38a40c57ca197b231e09a9856d7e809

cinder/tests/test_coraid.py
cinder/tests/test_utils.py
cinder/utils.py
cinder/volume/driver.py

index ab3a849c57b35f591ea540d4b6587e85671c61ba..3797498f3f1a0fd65c8dd500b1af7c9e41c06a9c 100644 (file)
@@ -26,6 +26,7 @@ from cinder.openstack.common import jsonutils
 from cinder.openstack.common import log as logging
 from cinder import test
 from cinder import units
+from cinder import utils
 from cinder.volume import configuration as conf
 from cinder.volume.drivers import coraid
 from cinder.volume import volume_types
@@ -779,12 +780,12 @@ class CoraidDriverImageTestCases(CoraidDriverTestCase):
         connector.get_connector_properties(root_helper).\
             AndReturn({})
 
-        self.mox.StubOutWithMock(connector.InitiatorConnector, 'factory')
+        self.mox.StubOutWithMock(utils, 'brick_get_connector')
 
         aoe_initiator = self.mox.CreateMockAnything()
 
-        connector.InitiatorConnector.factory('aoe', root_helper,
-                                             use_multipath=False).\
+        utils.brick_get_connector('aoe',
+                                  use_multipath=False).\
             AndReturn(aoe_initiator)
 
         aoe_initiator\
index 4468ce91615d3225905278ff5db337764b64b91b..0a50d3f5e93a1c10860f61ca2a0885322c24c5e1 100644 (file)
@@ -20,6 +20,7 @@ import datetime
 import hashlib
 import os
 import paramiko
+import socket
 import StringIO
 import tempfile
 import uuid
@@ -28,7 +29,10 @@ import mox
 from oslo.config import cfg
 
 import cinder
+from cinder.brick.initiator import connector
+from cinder.brick.initiator import linuxfc
 from cinder import exception
+from cinder.openstack.common import processutils as putils
 from cinder.openstack.common import timeutils
 from cinder import test
 from cinder import utils
@@ -721,3 +725,60 @@ class SSHPoolTestCase(test.TestCase):
             third_id = ssh.id
 
         self.assertNotEqual(first_id, third_id)
+
+
+class BrickUtils(test.TestCase):
+    """Unit test to test the brick utility
+    wrapper functions.
+    """
+
+    def test_brick_get_connector_properties(self):
+
+        self.mox.StubOutWithMock(socket, 'gethostname')
+        socket.gethostname().AndReturn('fakehost')
+
+        self.mox.StubOutWithMock(connector.ISCSIConnector, 'get_initiator')
+        connector.ISCSIConnector.get_initiator().AndReturn('fakeinitiator')
+
+        self.mox.StubOutWithMock(linuxfc.LinuxFibreChannel, 'get_fc_wwpns')
+        linuxfc.LinuxFibreChannel.get_fc_wwpns().AndReturn(None)
+
+        self.mox.StubOutWithMock(linuxfc.LinuxFibreChannel, 'get_fc_wwnns')
+        linuxfc.LinuxFibreChannel.get_fc_wwnns().AndReturn(None)
+
+        props = {'initiator': 'fakeinitiator',
+                 'host': 'fakehost',
+                 'ip': CONF.my_ip,
+                 }
+
+        self.mox.ReplayAll()
+        props_actual = utils.brick_get_connector_properties()
+        self.assertEqual(props, props_actual)
+        self.mox.VerifyAll()
+
+    def test_brick_get_connector(self):
+
+        root_helper = utils.get_root_helper()
+
+        self.mox.StubOutClassWithMocks(connector, 'ISCSIConnector')
+        connector.ISCSIConnector(execute=putils.execute,
+                                 driver=None,
+                                 root_helper=root_helper,
+                                 use_multipath=False)
+
+        self.mox.StubOutClassWithMocks(connector, 'FibreChannelConnector')
+        connector.FibreChannelConnector(execute=putils.execute,
+                                        driver=None,
+                                        root_helper=root_helper,
+                                        use_multipath=False)
+
+        self.mox.StubOutClassWithMocks(connector, 'AoEConnector')
+        connector.AoEConnector(execute=putils.execute,
+                               driver=None,
+                               root_helper=root_helper)
+
+        self.mox.ReplayAll()
+        utils.brick_get_connector('iscsi')
+        utils.brick_get_connector('fibre_channel')
+        utils.brick_get_connector('aoe')
+        self.mox.VerifyAll()
index f1aac57296c8f964e041244eaf28dc5fb3949f6a..9068d92567119b61950c614c0b5932814cf9d289 100644 (file)
@@ -46,6 +46,7 @@ from eventlet import pools
 
 from oslo.config import cfg
 
+from cinder.brick.initiator import connector
 from cinder import exception
 from cinder.openstack.common import excutils
 from cinder.openstack.common import gettextutils
@@ -143,8 +144,7 @@ def fetchfile(url, target):
 def execute(*cmd, **kwargs):
     """Convenience wrapper around oslo's execute() method."""
     if 'run_as_root' in kwargs and not 'root_helper' in kwargs:
-        kwargs['root_helper'] =\
-            'sudo cinder-rootwrap %s' % CONF.rootwrap_config
+        kwargs['root_helper'] = get_root_helper()
     try:
         (stdout, stderr) = processutils.execute(*cmd, **kwargs)
     except processutils.ProcessExecutionError as ex:
@@ -162,8 +162,7 @@ def execute(*cmd, **kwargs):
 def trycmd(*args, **kwargs):
     """Convenience wrapper around oslo's trycmd() method."""
     if 'run_as_root' in kwargs and not 'root_helper' in kwargs:
-        kwargs['root_helper'] =\
-            'sudo cinder-rootwrap %s' % CONF.rootwrap_config
+        kwargs['root_helper'] = get_root_helper()
     try:
         (stdout, stderr) = processutils.trycmd(*args, **kwargs)
     except processutils.ProcessExecutionError as ex:
@@ -1013,3 +1012,31 @@ class UndoManager(object):
                 LOG.exception(msg, **kwargs)
 
             self._rollback()
+
+
+def get_root_helper():
+    return 'sudo cinder-rootwrap %s' % CONF.rootwrap_config
+
+
+def brick_get_connector_properties():
+    """wrapper for the brick calls to automatically set
+    the root_helper needed for cinder.
+    """
+
+    root_helper = get_root_helper()
+    return connector.get_connector_properties(root_helper)
+
+
+def brick_get_connector(protocol, driver=None,
+                        execute=processutils.execute,
+                        use_multipath=False):
+    """Wrapper to get a brick connector object.
+    This automatically populates the required protocol as well
+    as the root_helper needed to execute commands.
+    """
+
+    root_helper = get_root_helper()
+    return connector.InitiatorConnector.factory(protocol, root_helper,
+                                                driver=driver,
+                                                execute=execute,
+                                                use_multipath=use_multipath)
index 7d0837943bb1c9aed36d8ece0c459d308b9e125e..6620f584dae05f6ad89cafe7dfc5f98f5f89d5f2 100644 (file)
@@ -236,8 +236,7 @@ class VolumeDriver(object):
         LOG.debug(_('copy_data_between_volumes %(src)s -> %(dest)s.')
                   % {'src': src_vol['name'], 'dest': dest_vol['name']})
 
-        root_helper = 'sudo cinder-rootwrap %s' % CONF.rootwrap_config
-        properties = initiator.get_connector_properties(root_helper)
+        properties = utils.brick_get_connector_properties()
         dest_remote = True if remote in ['dest', 'both'] else False
         dest_orig_status = dest_vol['status']
         try:
@@ -291,8 +290,7 @@ class VolumeDriver(object):
         """Fetch the image from image_service and write it to the volume."""
         LOG.debug(_('copy_image_to_volume %s.') % volume['name'])
 
-        root_helper = 'sudo cinder-rootwrap %s' % CONF.rootwrap_config
-        properties = initiator.get_connector_properties(root_helper)
+        properties = utils.brick_get_connector_properties()
         attach_info = self._attach_volume(context, volume, properties)
 
         try:
@@ -308,8 +306,7 @@ class VolumeDriver(object):
         """Copy the volume to the specified image."""
         LOG.debug(_('copy_volume_to_image %s.') % volume['name'])
 
-        root_helper = 'sudo cinder-rootwrap %s' % CONF.rootwrap_config
-        properties = initiator.get_connector_properties(root_helper)
+        properties = utils.brick_get_connector_properties()
         attach_info = self._attach_volume(context, volume, properties)
 
         try:
@@ -332,9 +329,8 @@ class VolumeDriver(object):
         # Use Brick's code to do attach/detach
         use_multipath = self.configuration.use_multipath_for_image_xfer
         protocol = conn['driver_volume_type']
-        root_helper = 'sudo cinder-rootwrap %s' % CONF.rootwrap_config
-        connector = initiator.InitiatorConnector.factory(
-            protocol, root_helper, use_multipath=use_multipath)
+        connector = utils.brick_get_connector(protocol,
+                                              use_multipath=use_multipath)
         device = connector.connect_volume(conn['data'])
         host_device = device['path']