From: Gorka Eguileor Date: Wed, 10 Jun 2015 09:57:07 +0000 (+0200) Subject: Use correct rtslib namespace for newer versions X-Git-Url: https://review.fuel-infra.org/gitweb?a=commitdiff_plain;h=6532be7eda2c7eec2984d51f06f0c69311b7b572;p=openstack-build%2Fcinder-build.git Use correct rtslib namespace for newer versions For rtslib-fb versions 2.1.52 and newer its own namespace (rtslib_fb) is recommended instead of the older one (rtslib). On versions with this new namespace a deprecation warning will be thrown: "'rtslib' package name for rtslib-fb is deprecated, please instead import 'rtslib_fb'" This patch uses this new namespace when possible and falls back to the older namespace if not. Change-Id: I984105458da4d7b8736bf7c83919cbe5feb9c85a Closes-Bug: #1460814 --- diff --git a/cinder/cmd/rtstool.py b/cinder/cmd/rtstool.py index bd770d25a..ae2a6d236 100644 --- a/cinder/cmd/rtstool.py +++ b/cinder/cmd/rtstool.py @@ -17,7 +17,13 @@ import sys -import rtslib +# We always use rtslib-fb, but until version 2.1.52 it didn't have its own +# namespace, so we must be backwards compatible. +try: + import rtslib_fb +except ImportError: + import rtslib as rtslib_fb + from cinder import i18n from cinder.i18n import _ @@ -40,8 +46,8 @@ def create(backing_device, name, userid, password, iser_enabled, ips_allow_fail = () try: - rtsroot = rtslib.root.RTSRoot() - except rtslib.utils.RTSLibError: + rtsroot = rtslib_fb.root.RTSRoot() + except rtslib_fb.utils.RTSLibError: print(_('Ensure that configfs is mounted at /sys/kernel/config.')) raise @@ -51,24 +57,25 @@ def create(backing_device, name, userid, password, iser_enabled, # Already exists, use this one return - so_new = rtslib.BlockStorageObject(name=name, - dev=backing_device) + so_new = rtslib_fb.BlockStorageObject(name=name, + dev=backing_device) - target_new = rtslib.Target(rtslib.FabricModule('iscsi'), name, 'create') + target_new = rtslib_fb.Target(rtslib_fb.FabricModule('iscsi'), name, + 'create') - tpg_new = rtslib.TPG(target_new, mode='create') + tpg_new = rtslib_fb.TPG(target_new, mode='create') tpg_new.set_attribute('authentication', '1') - lun_new = rtslib.LUN(tpg_new, storage_object=so_new) + lun_new = rtslib_fb.LUN(tpg_new, storage_object=so_new) if initiator_iqns: initiator_iqns = initiator_iqns.strip(' ') for i in initiator_iqns.split(','): - acl_new = rtslib.NodeACL(tpg_new, i, mode='create') + acl_new = rtslib_fb.NodeACL(tpg_new, i, mode='create') acl_new.chap_userid = userid acl_new.chap_password = password - rtslib.MappedLUN(acl_new, lun_new.lun, lun_new.lun) + rtslib_fb.MappedLUN(acl_new, lun_new.lun, lun_new.lun) tpg_new.enable = 1 @@ -80,9 +87,9 @@ def create(backing_device, name, userid, password, iser_enabled, for ip in portals_ips: try: - portal = rtslib.NetworkPortal(tpg_new, ip, portals_port, - mode='any') - except rtslib.utils.RTSLibError: + portal = rtslib_fb.NetworkPortal(tpg_new, ip, portals_port, + mode='any') + except rtslib_fb.utils.RTSLibError: raise_exc = ip not in ips_allow_fail msg_type = 'Error' if raise_exc else 'Warning' print(_('%(msg_type)s: creating NetworkPortal: ensure port ' @@ -94,7 +101,7 @@ def create(backing_device, name, userid, password, iser_enabled, try: if iser_enabled == 'True': portal.iser = True - except rtslib.utils.RTSLibError: + except rtslib_fb.utils.RTSLibError: print(_('Error enabling iSER for NetworkPortal: please ensure ' 'that RDMA is supported on your iSCSI port %(port)d ' 'on ip %(ip)s.') % {'port': portals_port, 'ip': ip}) @@ -103,8 +110,8 @@ def create(backing_device, name, userid, password, iser_enabled, def _lookup_target(target_iqn, initiator_iqn): try: - rtsroot = rtslib.root.RTSRoot() - except rtslib.utils.RTSLibError: + rtsroot = rtslib_fb.root.RTSRoot() + except rtslib_fb.utils.RTSLibError: print(_('Ensure that configfs is mounted at /sys/kernel/config.')) raise @@ -124,11 +131,11 @@ def add_initiator(target_iqn, initiator_iqn, userid, password): # No further action required return - acl_new = rtslib.NodeACL(tpg, initiator_iqn, mode='create') + acl_new = rtslib_fb.NodeACL(tpg, initiator_iqn, mode='create') acl_new.chap_userid = userid acl_new.chap_password = password - rtslib.MappedLUN(acl_new, 0, tpg_lun=0) + rtslib_fb.MappedLUN(acl_new, 0, tpg_lun=0) def delete_initiator(target_iqn, initiator_iqn): @@ -143,13 +150,13 @@ def delete_initiator(target_iqn, initiator_iqn): def get_targets(): - rtsroot = rtslib.root.RTSRoot() + rtsroot = rtslib_fb.root.RTSRoot() for x in rtsroot.targets: print(x.wwn) def delete(iqn): - rtsroot = rtslib.root.RTSRoot() + rtsroot = rtslib_fb.root.RTSRoot() for x in rtsroot.targets: if x.wwn == iqn: x.delete() @@ -165,9 +172,9 @@ def verify_rtslib(): for member in ['BlockStorageObject', 'FabricModule', 'LUN', 'MappedLUN', 'NetworkPortal', 'NodeACL', 'root', 'Target', 'TPG']: - if not hasattr(rtslib, member): - raise RtstoolImportError(_("rtslib is missing member %s: " - "You may need a newer python-rtslib.") % + if not hasattr(rtslib_fb, member): + raise RtstoolImportError(_("rtslib_fb is missing member %s: You " + "may need a newer python-rtslib-fb.") % member) @@ -188,7 +195,7 @@ def usage(): def save_to_file(destination_file): - rtsroot = rtslib.root.RTSRoot() + rtsroot = rtslib_fb.root.RTSRoot() try: rtsroot.save_to_file(destination_file) except OSError: @@ -277,7 +284,7 @@ def main(argv=None): elif argv[1] == 'verify': # This is used to verify that this script can be called by cinder, - # and that rtslib is new enough to work. + # and that rtslib_fb is new enough to work. verify_rtslib() return 0 diff --git a/cinder/tests/unit/test_cmd.py b/cinder/tests/unit/test_cmd.py index feea1b322..5d0464f82 100644 --- a/cinder/tests/unit/test_cmd.py +++ b/cinder/tests/unit/test_cmd.py @@ -16,7 +16,12 @@ import sys import mock from oslo_config import cfg -import rtslib + +try: + import rtslib_fb +except ImportError: + import rtslib as rtslib_fb + from cinder.cmd import all as cinder_all from cinder.cmd import api as cinder_api @@ -707,12 +712,13 @@ class TestCinderRtstoolCmd(test.TestCase): def tearDown(self): super(TestCinderRtstoolCmd, self).tearDown() - @mock.patch('rtslib.root.RTSRoot') + @mock.patch.object(rtslib_fb.root, 'RTSRoot') def test_create_rtslib_error(self, rtsroot): - rtsroot.side_effect = rtslib.utils.RTSLibError() + rtsroot.side_effect = rtslib_fb.utils.RTSLibError() with mock.patch('sys.stdout', new=six.StringIO()): - self.assertRaises(rtslib.utils.RTSLibError, cinder_rtstool.create, + self.assertRaises(rtslib_fb.utils.RTSLibError, + cinder_rtstool.create, mock.sentinel.backing_device, mock.sentinel.name, mock.sentinel.userid, @@ -720,14 +726,14 @@ class TestCinderRtstoolCmd(test.TestCase): mock.sentinel.iser_enabled) def _test_create_rtslib_error_network_portal(self, ip): - with mock.patch('rtslib.NetworkPortal') as network_portal, \ - mock.patch('rtslib.LUN') as lun, \ - mock.patch('rtslib.TPG') as tpg, \ - mock.patch('rtslib.FabricModule') as fabric_module, \ - mock.patch('rtslib.Target') as target, \ - mock.patch('rtslib.BlockStorageObject') as \ + with mock.patch.object(rtslib_fb, 'NetworkPortal') as network_portal, \ + mock.patch.object(rtslib_fb, 'LUN') as lun, \ + mock.patch.object(rtslib_fb, 'TPG') as tpg, \ + mock.patch.object(rtslib_fb, 'FabricModule') as fabric_module, \ + mock.patch.object(rtslib_fb, 'Target') as target, \ + mock.patch.object(rtslib_fb, 'BlockStorageObject') as \ block_storage_object, \ - mock.patch('rtslib.root.RTSRoot') as rts_root: + mock.patch.object(rtslib_fb.root, 'RTSRoot') as rts_root: root_new = mock.MagicMock(storage_objects=mock.MagicMock()) rts_root.return_value = root_new block_storage_object.return_value = mock.sentinel.so_new @@ -737,8 +743,8 @@ class TestCinderRtstoolCmd(test.TestCase): lun.return_value = mock.sentinel.lun_new if ip == '0.0.0.0': - network_portal.side_effect = rtslib.utils.RTSLibError() - self.assertRaises(rtslib.utils.RTSLibError, + network_portal.side_effect = rtslib_fb.utils.RTSLibError() + self.assertRaises(rtslib_fb.utils.RTSLibError, cinder_rtstool.create, mock.sentinel.backing_device, mock.sentinel.name, @@ -780,14 +786,14 @@ class TestCinderRtstoolCmd(test.TestCase): self._test_create_rtslib_error_network_portal('::0') def _test_create(self, ip): - with mock.patch('rtslib.NetworkPortal') as network_portal, \ - mock.patch('rtslib.LUN') as lun, \ - mock.patch('rtslib.TPG') as tpg, \ - mock.patch('rtslib.FabricModule') as fabric_module, \ - mock.patch('rtslib.Target') as target, \ - mock.patch('rtslib.BlockStorageObject') as \ + with mock.patch.object(rtslib_fb, 'NetworkPortal') as network_portal, \ + mock.patch.object(rtslib_fb, 'LUN') as lun, \ + mock.patch.object(rtslib_fb, 'TPG') as tpg, \ + mock.patch.object(rtslib_fb, 'FabricModule') as fabric_module, \ + mock.patch.object(rtslib_fb, 'Target') as target, \ + mock.patch.object(rtslib_fb, 'BlockStorageObject') as \ block_storage_object, \ - mock.patch('rtslib.root.RTSRoot') as rts_root: + mock.patch.object(rtslib_fb.root, 'RTSRoot') as rts_root: root_new = mock.MagicMock(storage_objects=mock.MagicMock()) rts_root.return_value = root_new block_storage_object.return_value = mock.sentinel.so_new @@ -798,7 +804,7 @@ class TestCinderRtstoolCmd(test.TestCase): def network_portal_exception(*args, **kwargs): if set([tpg_new, '::0', 3260]).issubset(list(args)): - raise rtslib.utils.RTSLibError() + raise rtslib_fb.utils.RTSLibError() else: pass @@ -833,7 +839,7 @@ class TestCinderRtstoolCmd(test.TestCase): def test_create_ipv6(self): self._test_create('::0') - @mock.patch.object(cinder_rtstool, 'rtslib', autospec=True) + @mock.patch.object(cinder_rtstool, 'rtslib_fb', autospec=True) def test_create_ips_and_port(self, mock_rtslib): port = 3261 ips = ['ip1', 'ip2', 'ip3'] @@ -865,19 +871,19 @@ class TestCinderRtstoolCmd(test.TestCase): any_order=True ) - @mock.patch('rtslib.root.RTSRoot') + @mock.patch.object(rtslib_fb.root, 'RTSRoot') def test_add_initiator_rtslib_error(self, rtsroot): - rtsroot.side_effect = rtslib.utils.RTSLibError() + rtsroot.side_effect = rtslib_fb.utils.RTSLibError() with mock.patch('sys.stdout', new=six.StringIO()): - self.assertRaises(rtslib.utils.RTSLibError, + self.assertRaises(rtslib_fb.utils.RTSLibError, cinder_rtstool.add_initiator, mock.sentinel.target_iqn, mock.sentinel.initiator_iqn, mock.sentinel.userid, mock.sentinel.password) - @mock.patch('rtslib.root.RTSRoot') + @mock.patch.object(rtslib_fb.root, 'RTSRoot') def test_add_initiator_rtstool_error(self, rtsroot): rtsroot.targets.return_value = {} @@ -888,9 +894,9 @@ class TestCinderRtstoolCmd(test.TestCase): mock.sentinel.userid, mock.sentinel.password) - @mock.patch('rtslib.MappedLUN') - @mock.patch('rtslib.NodeACL') - @mock.patch('rtslib.root.RTSRoot') + @mock.patch.object(rtslib_fb, 'MappedLUN') + @mock.patch.object(rtslib_fb, 'NodeACL') + @mock.patch.object(rtslib_fb.root, 'RTSRoot') def test_add_initiator_acl_exists(self, rtsroot, node_acl, mapped_lun): target_iqn = mock.MagicMock() target_iqn.tpgs.return_value = \ @@ -909,9 +915,9 @@ class TestCinderRtstoolCmd(test.TestCase): self.assertFalse(node_acl.called) self.assertFalse(mapped_lun.called) - @mock.patch('rtslib.MappedLUN') - @mock.patch('rtslib.NodeACL') - @mock.patch('rtslib.root.RTSRoot') + @mock.patch.object(rtslib_fb, 'MappedLUN') + @mock.patch.object(rtslib_fb, 'NodeACL') + @mock.patch.object(rtslib_fb.root, 'RTSRoot') def test_add_initiator(self, rtsroot, node_acl, mapped_lun): target_iqn = mock.MagicMock() target_iqn.tpgs.return_value = \ @@ -933,7 +939,7 @@ class TestCinderRtstoolCmd(test.TestCase): mode='create') mapped_lun.assert_called_once_with(acl_new, 0, tpg_lun=0) - @mock.patch('rtslib.root.RTSRoot') + @mock.patch.object(rtslib_fb.root, 'RTSRoot') def test_get_targets(self, rtsroot): target = mock.MagicMock() target.dump.return_value = {'wwn': 'fake-wwn'} @@ -944,7 +950,7 @@ class TestCinderRtstoolCmd(test.TestCase): self.assertEqual(str(target.wwn), fake_out.getvalue().strip()) - @mock.patch('rtslib.root.RTSRoot') + @mock.patch.object(rtslib_fb.root, 'RTSRoot') def test_delete(self, rtsroot): target = mock.MagicMock(wwn=mock.sentinel.iqn) storage_object = mock.MagicMock() @@ -958,7 +964,7 @@ class TestCinderRtstoolCmd(test.TestCase): target.delete.assert_called_once_with() storage_object.delete.assert_called_once_with() - @mock.patch.object(cinder_rtstool, 'rtslib', autospec=True) + @mock.patch.object(cinder_rtstool, 'rtslib_fb', autospec=True) def test_save(self, mock_rtslib): filename = mock.sentinel.filename cinder_rtstool.save_to_file(filename) @@ -1111,14 +1117,14 @@ class TestCinderRtstoolCmd(test.TestCase): delete.assert_called_once_with(mock.sentinel.iqn) self.assertEqual(0, rc) - def test_main_verify(self): - with mock.patch('cinder.cmd.rtstool.verify_rtslib') as verify_rtslib: - sys.argv = ['cinder-rtstool', 'verify'] + @mock.patch.object(cinder_rtstool, 'verify_rtslib') + def test_main_verify(self, mock_verify_rtslib): + sys.argv = ['cinder-rtstool', 'verify'] - rc = cinder_rtstool.main() + rc = cinder_rtstool.main() - verify_rtslib.assert_called_once_with() - self.assertEqual(0, rc) + mock_verify_rtslib.assert_called_once_with() + self.assertEqual(0, rc) class TestCinderVolumeUsageAuditCmd(test.TestCase):