]> review.fuel-infra Code Review - openstack-build/cinder-build.git/commitdiff
Targets test refactoring
authorAnton Arefiev <aarefiev@mirantis.com>
Thu, 30 Apr 2015 13:54:23 +0000 (16:54 +0300)
committerAnton Arefiev <aarefiev@mirantis.com>
Thu, 30 Apr 2015 15:02:57 +0000 (18:02 +0300)
The unit tests for iser inherit from the tgtAdm, it means that any
time run tests for iser, tgtAdm tests run too. This chache add
TargetFixture to tests, it allows to decouple tests and delete
duplicated code.

Other changes:
 -  deleted  __init__ in test's classes; __init__ may work as a
replacement for setUp, but  setUp should be used instead because
it is part of the protocol for writing tests. It also has a
counterpart, tearDown, which __init__ does no;
 - replaced stubs to mock.

Change-Id: I8aa9f756efb0b4fc4a3a0ea960c6f2d8e20ec18a
Closes-Bug: #1420009

cinder/tests/unit/targets/targets_fixture.py [new file with mode: 0644]
cinder/tests/unit/targets/test_base_iscsi_driver.py
cinder/tests/unit/targets/test_cxt_driver.py
cinder/tests/unit/targets/test_iet_driver.py
cinder/tests/unit/targets/test_iser_driver.py
cinder/tests/unit/targets/test_lio_driver.py
cinder/tests/unit/targets/test_scst_driver.py
cinder/tests/unit/targets/test_tgt_driver.py

diff --git a/cinder/tests/unit/targets/targets_fixture.py b/cinder/tests/unit/targets/targets_fixture.py
new file mode 100644 (file)
index 0000000..0638b56
--- /dev/null
@@ -0,0 +1,105 @@
+#    Licensed under the Apache License, Version 2.0 (the "License"); you may
+#    not use this file except in compliance with the License. You may obtain
+#    a copy of the License at
+#
+#         http://www.apache.org/licenses/LICENSE-2.0
+#
+#    Unless required by applicable law or agreed to in writing, software
+#    distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+#    WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
+#    License for the specific language governing permissions and limitations
+#    under the License.
+
+import os
+import shutil
+import tempfile
+
+import mock
+from oslo_utils import timeutils
+
+from cinder.openstack.common import fileutils
+from cinder import test
+from cinder.volume import configuration as conf
+
+
+class TargetDriverFixture(test.TestCase):
+    def setUp(self):
+        super(TargetDriverFixture, self).setUp()
+        self.configuration = conf.Configuration(None)
+        self.configuration.append_config_values = mock.Mock(return_value=0)
+        self.configuration.safe_get = mock.Mock(side_effect=self.fake_safe_get)
+        self.configuration.iscsi_ip_address = '10.9.8.7'
+
+        self.fake_volumes_dir = tempfile.mkdtemp()
+        fileutils.ensure_tree(self.fake_volumes_dir)
+
+        self.fake_project_id = 'ed2c1fd4-5fc0-11e4-aa15-123b93f75cba'
+        self.fake_project_id_2 = 'ed2c1fd4-5fc0-11e4-aa15-123b93f75cba'
+        self.fake_volume_id = 'ed2c2222-5fc0-11e4-aa15-123b93f75cba'
+
+        self.addCleanup(self._cleanup)
+
+        self.testvol =\
+            {'project_id': self.fake_project_id,
+             'name': 'testvol',
+             'size': 1,
+             'id': self.fake_volume_id,
+             'volume_type_id': None,
+             'provider_location': '10.10.7.1:3260 '
+                                  'iqn.2010-10.org.openstack:'
+                                  'volume-%s 0' % self.fake_volume_id,
+             'provider_auth': 'CHAP stack-1-a60e2611875f40199931f2'
+                              'c76370d66b 2FE0CQ8J196R',
+             'provider_geometry': '512 512',
+             'created_at': timeutils.utcnow(),
+             'host': 'fake_host@lvm#lvm'}
+
+        self.iscsi_target_prefix = 'iqn.2010-10.org.openstack:'
+        self.target_string = ('127.0.0.1:3260,1 ' +
+                              self.iscsi_target_prefix +
+                              'volume-%s' % self.testvol['id'])
+
+        self.testvol_2 =\
+            {'project_id': self.fake_project_id_2,
+             'name': 'testvol2',
+             'size': 1,
+             'id': self.fake_volume_id,
+             'volume_type_id': None,
+             'provider_location': '10.9.8.7:3260 '
+                                  'iqn.2010-10.org.openstack:'
+                                  'volume-%s 2' % self.fake_volume_id,
+             'provider_auth': 'CHAP stack-1-a60e2611875f40199931f2'
+                              'c76370d66b 2FE0CQ8J196R',
+             'provider_geometry': '512 512',
+             'created_at': timeutils.utcnow(),
+             'host': 'fake_host@lvm#lvm'}
+
+        self.expected_iscsi_properties = \
+            {'auth_method': 'CHAP',
+             'auth_password': '2FE0CQ8J196R',
+             'auth_username': 'stack-1-a60e2611875f40199931f2c76370d66b',
+             'encrypted': False,
+             'logical_block_size': '512',
+             'physical_block_size': '512',
+             'target_discovered': False,
+             'target_iqn': 'iqn.2010-10.org.openstack:volume-%s' %
+                           self.fake_volume_id,
+             'target_lun': 0,
+             'target_portal': '10.10.7.1:3260',
+             'volume_id': self.fake_volume_id}
+
+        self.volume_name = 'volume-83c2e877-feed-46be-8435-77884fe55b45'
+        self.test_vol = (self.iscsi_target_prefix +
+                         self.volume_name)
+
+    def _cleanup(self):
+        if os.path.exists(self.fake_volumes_dir):
+            shutil.rmtree(self.fake_volumes_dir)
+
+    def fake_safe_get(self, value):
+        if value == 'volumes_dir':
+            return self.fake_volumes_dir
+        elif value == 'iscsi_protocol':
+            return self.configuration.iscsi_protocol
+        elif value == 'iscsi_target_prefix':
+            return self.iscsi_target_prefix
index 9bd5d752cf0f156a3d228f5f62ed396ed58b3767..5ab0ddc0b14c08366a53258fc207a5b28535a5ab 100644 (file)
 
 import mock
 from oslo_config import cfg
-from oslo_utils import timeutils
 
 from cinder import context
 from cinder import exception
-from cinder import test
+from cinder.tests.unit.targets import targets_fixture as tf
 from cinder import utils
 from cinder.volume import configuration as conf
 from cinder.volume.targets import fake
@@ -28,43 +27,12 @@ class FakeIncompleteDriver(iscsi.ISCSITarget):
         pass
 
 
-class TestBaseISCSITargetDriver(test.TestCase):
+class TestBaseISCSITargetDriver(tf.TargetDriverFixture):
 
     def setUp(self):
         super(TestBaseISCSITargetDriver, self).setUp()
-        self.configuration = conf.Configuration(None)
-        self.fake_project_id = 'ed2c1fd4-5fc0-11e4-aa15-123b93f75cba'
-        self.fake_volume_id = 'ed2c2222-5fc0-11e4-aa15-123b93f75cba'
         self.target = fake.FakeTarget(root_helper=utils.get_root_helper(),
                                       configuration=self.configuration)
-        self.testvol =\
-            {'project_id': self.fake_project_id,
-             'name': 'testvol',
-             'size': 1,
-             'id': self.fake_volume_id,
-             'volume_type_id': None,
-             'provider_location': '10.10.7.1:3260 '
-                                  'iqn.2010-10.org.openstack:'
-                                  'volume-%s 0' % self.fake_volume_id,
-             'provider_auth': 'CHAP stack-1-a60e2611875f40199931f2'
-                              'c76370d66b 2FE0CQ8J196R',
-             'provider_geometry': '512 512',
-             'created_at': timeutils.utcnow(),
-             'host': 'fake_host@lvm#lvm'}
-
-        self.expected_iscsi_properties = \
-            {'auth_method': 'CHAP',
-             'auth_password': '2FE0CQ8J196R',
-             'auth_username': 'stack-1-a60e2611875f40199931f2c76370d66b',
-             'encrypted': False,
-             'logical_block_size': '512',
-             'physical_block_size': '512',
-             'target_discovered': False,
-             'target_iqn': 'iqn.2010-10.org.openstack:volume-%s' %
-                           self.fake_volume_id,
-             'target_lun': 0,
-             'target_portal': '10.10.7.1:3260',
-             'volume_id': self.fake_volume_id}
 
     def test_abc_methods_not_present_fails(self):
         configuration = conf.Configuration(cfg.StrOpt('iscsi_target_prefix',
@@ -101,26 +69,12 @@ class TestBaseISCSITargetDriver(test.TestCase):
                                                            'chap-password'))
 
     def test_do_iscsi_discovery(self):
-        target_string = '127.0.0.1:3260,1 '\
-                        'iqn.2010-10.org.openstack:'\
-                        'volume-%s' % self.testvol['id']
-
-        def _fake_execute(*args, **kwargs):
-            return target_string, None
-
-        def _fake_safe_get(val):
-            return '127.0.0.1'
-
-        self.stubs.Set(self.configuration,
-                       'safe_get',
-                       _fake_safe_get)
-
-        self.stubs.Set(utils,
-                       'execute',
-                       _fake_execute)
-
-        self.assertEqual(target_string,
-                         self.target._do_iscsi_discovery(self.testvol))
+        with mock.patch.object(self.configuration,
+                               'safe_get', return_value='127.0.0.1'),\
+                mock.patch('cinder.utils.execute',
+                           return_value=(self.target_string, '')):
+            self.assertEqual(self.target_string,
+                             self.target._do_iscsi_discovery(self.testvol))
 
     def test_remove_export(self):
 
index 01aaba7fa744e4e2e3cfd5e195a34d6684a5244a..30511caf62af4dc19a7751f5c8561caf5a952ef5 100644 (file)
 #    under the License.
 
 import contextlib
-import os
-import shutil
 import StringIO
-import tempfile
 
 import mock
-from oslo_utils import timeutils
 
 from cinder import context
-from cinder.openstack.common import fileutils
-from cinder import test
+from cinder.tests.unit.targets import targets_fixture as tf
 from cinder import utils
-from cinder.volume import configuration as conf
 from cinder.volume.targets import cxt
 
 
-class TestCxtAdmDriver(test.TestCase):
-
-    def __init__(self, *args, **kwargs):
-        super(TestCxtAdmDriver, self).__init__(*args, **kwargs)
-        self.configuration = conf.Configuration(None)
-        self.configuration.append_config_values = mock.Mock(return_value=0)
-        self.configuration.iscsi_ip_address = '10.9.8.7'
+class TestCxtAdmDriver(tf.TargetDriverFixture):
+    def setUp(self):
+        super(TestCxtAdmDriver, self).setUp()
         self.cxt_subdir = cxt.CxtAdm.cxt_subdir
-        self.fake_id_1 = 'ed2c1fd4-5fc0-11e4-aa15-123b93f75cba'
-        self.fake_id_2 = 'ed2c2222-5fc0-11e4-aa15-123b93f75cba'
         self.target = cxt.CxtAdm(root_helper=utils.get_root_helper(),
                                  configuration=self.configuration)
-        self.fake_volume = 'volume-83c2e877-feed-46be-8435-77884fe55b45'
-        self.testvol_1 =\
-            {'project_id': self.fake_id_1,
-             'name': 'testvol',
-             'size': 1,
-             'id': self.fake_id_2,
-             'volume_type_id': None,
-             'provider_location': '10.9.8.7:3260 '
-                                  'iqn.2010-10.org.openstack:'
-                                  'volume-%s 0' % self.fake_id_2,
-             'provider_auth': 'CHAP stack-1-a60e2611875f40199931f2'
-                              'c76370d66b 2FE0CQ8J196R',
-             'provider_geometry': '512 512',
-             'created_at': timeutils.utcnow(),
-             'host': 'fake_host@lvm#lvm'}
-
-        self.expected_iscsi_properties = \
-            {'auth_method': 'CHAP',
-             'auth_password': '2FE0CQ8J196R',
-             'auth_username': 'stack-1-a60e2611875f40199931f2c76370d66b',
-             'encrypted': False,
-             'logical_block_size': '512',
-             'physical_block_size': '512',
-             'target_discovered': False,
-             'target_iqn': 'iqn.2010-10.org.openstack:volume-%s' %
-                           self.fake_id_2,
-             'target_lun': 0,
-             'target_portal': '10.10.7.1:3260',
-             'volume_id': self.fake_id_2}
-
         self.fake_iscsi_scan =\
             ('\n'
              'TARGET: iqn.2010-10.org.openstack:%s, id=1, login_ip=0\n'  # noqa
              '        PortalGroup=1@10.9.8.7:3260,timeout=0\n'
              '        TargetDevice=/dev/stack-volumes-lvmdriver-1/%s,BLK,PROD=CHISCSI Target,SN=0N0743000000000,ID=0D074300000000000000000,WWN=:W00743000000000\n'  # noqa
-             % (self.fake_volume, self.fake_volume))
-
-    def setUp(self):
-        super(TestCxtAdmDriver, self).setUp()
-        self.fake_base_dir = tempfile.mkdtemp()
-        self.fake_volumes_dir = os.path.join(self.fake_base_dir,
-                                             self.cxt_subdir)
-        fileutils.ensure_tree(self.fake_volumes_dir)
-        self.addCleanup(self._cleanup)
+             % (self.volume_name, self.volume_name))
 
-        self.exec_patcher = mock.patch.object(utils, 'execute')
-        self.mock_execute = self.exec_patcher.start()
-        self.addCleanup(self.exec_patcher.stop)
-
-    def _cleanup(self):
-        if os.path.exists(self.fake_base_dir):
-            shutil.rmtree(self.fake_base_dir)
-
-    @mock.patch('cinder.utils.execute')
-    def test_get_target(self, mock_execute):
-        mock_execute.return_value = (self.fake_iscsi_scan, None)
-        with mock.patch.object(self.target, '_get_volumes_dir') as mock_get:
-            mock_get.return_value = self.fake_volumes_dir
+    def test_get_target(self):
+        with mock.patch.object(self.target, '_get_volumes_dir',
+                               return_value=self.fake_volumes_dir),\
+            mock.patch('cinder.utils.execute',
+                       return_value=(self.fake_iscsi_scan, None)) as m_exec:
             self.assertEqual('1',
                              self.target._get_target(
                                                      'iqn.2010-10.org.openstack:volume-83c2e877-feed-46be-8435-77884fe55b45'  # noqa
                                                     ))
-            self.assertTrue(mock_execute.called)
+            self.assertTrue(m_exec.called)
 
     def test_get_target_chap_auth(self):
         tmp_file = StringIO.StringIO()
@@ -118,20 +60,17 @@ class TestCxtAdmDriver(test.TestCase):
             '        Auth_CHAP_Initiator="otzLy2UYbYfnP4zXLG5z":"234Zweo38VGBBvrpK9nt"\n'  # noqa
         )
         tmp_file.seek(0)
-        test_vol = ('iqn.2010-10.org.openstack:'
-                    'volume-83c2e877-feed-46be-8435-77884fe55b45')
+
         expected = ('otzLy2UYbYfnP4zXLG5z', '234Zweo38VGBBvrpK9nt')
         with mock.patch('__builtin__.open') as mock_open:
             ctx = context.get_admin_context()
             mock_open.return_value = contextlib.closing(tmp_file)
             self.assertEqual(expected,
-                             self.target._get_target_chap_auth(ctx, test_vol))
+                             self.target._get_target_chap_auth(ctx,
+                                                               self.test_vol))
             self.assertTrue(mock_open.called)
 
     def test_get_target_chap_auth_negative(self):
-        test_vol =\
-            'iqn.2010-10.org.openstack:'\
-            'volume-83c2e877-feed-46be-8435-77884fe55b45'
         with mock.patch('__builtin__.open') as mock_open:
             e = IOError()
             e.errno = 123
@@ -139,11 +78,11 @@ class TestCxtAdmDriver(test.TestCase):
             ctxt = context.get_admin_context()
             self.assertRaises(IOError,
                               self.target._get_target_chap_auth,
-                              ctxt, test_vol)
+                              ctxt, self.test_vol)
             mock_open.side_effect = StandardError()
             self.assertRaises(StandardError,
                               self.target._get_target_chap_auth,
-                              ctxt, test_vol)
+                              ctxt, self.test_vol)
 
     @mock.patch('cinder.volume.targets.cxt.CxtAdm._get_target',
                 return_value=1)
@@ -152,12 +91,10 @@ class TestCxtAdmDriver(test.TestCase):
         mock_execute.return_value = ('', '')
         with mock.patch.object(self.target, '_get_volumes_dir') as mock_get:
             mock_get.return_value = self.fake_volumes_dir
-            test_vol = 'iqn.2010-10.org.openstack:'\
-                       'volume-83c2e877-feed-46be-8435-77884fe55b45'
             self.assertEqual(
                 1,
                 self.target.create_iscsi_target(
-                    test_vol,
+                    self.test_vol,
                     1,
                     0,
                     self.fake_volumes_dir))
@@ -167,18 +104,15 @@ class TestCxtAdmDriver(test.TestCase):
 
     @mock.patch('cinder.volume.targets.cxt.CxtAdm._get_target',
                 return_value=1)
-    @mock.patch('cinder.utils.execute')
+    @mock.patch('cinder.utils.execute', return_value=('fake out', 'fake err'))
     def test_create_iscsi_target_already_exists(self, mock_execute,
                                                 mock_get_targ):
-        mock_execute.return_value = ('fake out', 'fake err')
         with mock.patch.object(self.target, '_get_volumes_dir') as mock_get:
             mock_get.return_value = self.fake_volumes_dir
-            test_vol = 'iqn.2010-10.org.openstack:'\
-                       'volume-83c2e877-feed-46be-8435-77884fe55b45'
             self.assertEqual(
                 1,
                 self.target.create_iscsi_target(
-                    test_vol,
+                    self.test_vol,
                     1,
                     0,
                     self.fake_volumes_dir))
@@ -206,7 +140,7 @@ class TestCxtAdmDriver(test.TestCase):
             ctxt = context.get_admin_context()
             self.assertEqual(expected_result,
                              self.target.create_export(ctxt,
-                                                       self.testvol_1,
+                                                       self.testvol,
                                                        self.fake_volumes_dir))
             self.assertTrue(mock_get.called)
             self.assertTrue(mock_execute.called)
@@ -218,7 +152,7 @@ class TestCxtAdmDriver(test.TestCase):
         ctxt = context.get_admin_context()
         with mock.patch.object(self.target, 'create_iscsi_target'):
             self.target.ensure_export(ctxt,
-                                      self.testvol_1,
+                                      self.testvol,
                                       self.fake_volumes_dir)
             self.target.create_iscsi_target.assert_called_once_with(
                 'iqn.2010-10.org.openstack:testvol',
index 762cca472a4f1ec8e75da561582cdf52ab75503b..707b256b56d95ad93399ce22c920e387c35e18ba 100644 (file)
 #    under the License.
 
 import contextlib
-import os
-import shutil
+
 import StringIO
-import tempfile
+
 
 import mock
 from oslo_concurrency import processutils as putils
-from oslo_utils import timeutils
 
 from cinder import context
 from cinder import exception
-from cinder.openstack.common import fileutils
-from cinder import test
+
+from cinder.tests.unit.targets import targets_fixture as tf
 from cinder import utils
-from cinder.volume import configuration as conf
 from cinder.volume.targets import iet
 
 
-class TestIetAdmDriver(test.TestCase):
-
-    def __init__(self, *args, **kwargs):
-        super(TestIetAdmDriver, self).__init__(*args, **kwargs)
-        self.configuration = conf.Configuration(None)
-        self.configuration.append_config_values = mock.Mock(return_value=0)
-        self.configuration.iscsi_ip_address = '10.9.8.7'
-        self.fake_project_id = 'ed2c1fd4-5fc0-11e4-aa15-123b93f75cba'
-        self.fake_volume_id = '83c2e877-feed-46be-8435-77884fe55b45'
-        self.target = iet.IetAdm(root_helper=utils.get_root_helper(),
-                                 configuration=self.configuration)
-        self.testvol =\
-            {'project_id': self.fake_project_id,
-             'name': 'testvol',
-             'size': 1,
-             'id': self.fake_volume_id,
-             'volume_type_id': None,
-             'provider_location': '10.9.8.7:3260 '
-                                  'iqn.2010-10.org.openstack:'
-                                  'volume-%s 0' % self.fake_volume_id,
-             'provider_auth': 'CHAP stack-1-a60e2611875f40199931f2'
-                              'c76370d66b 2FE0CQ8J196R',
-             'provider_geometry': '512 512',
-             'created_at': timeutils.utcnow(),
-             'host': 'fake_host@lvm#lvm'}
-
-        self.expected_iscsi_properties = \
-            {'auth_method': 'CHAP',
-             'auth_password': '2FE0CQ8J196R',
-             'auth_username': 'stack-1-a60e2611875f40199931f2c76370d66b',
-             'encrypted': False,
-             'logical_block_size': '512',
-             'physical_block_size': '512',
-             'target_discovered': False,
-             'target_iqn': 'iqn.2010-10.org.openstack:volume-%s' %
-                           self.fake_volume_id,
-             'target_lun': 0,
-             'target_portal': '10.10.7.1:3260',
-             'volume_id': self.fake_volume_id}
-
+class TestIetAdmDriver(tf.TargetDriverFixture):
     def setUp(self):
         super(TestIetAdmDriver, self).setUp()
-        self.fake_volumes_dir = tempfile.mkdtemp()
-        fileutils.ensure_tree(self.fake_volumes_dir)
-        self.addCleanup(self._cleanup)
-
-        self.exec_patcher = mock.patch.object(utils, 'execute')
-        self.mock_execute = self.exec_patcher.start()
-        self.addCleanup(self.exec_patcher.stop)
-
-    def _cleanup(self):
-        if os.path.exists(self.fake_volumes_dir):
-            shutil.rmtree(self.fake_volumes_dir)
+        self.target = iet.IetAdm(root_helper=utils.get_root_helper(),
+                                 configuration=self.configuration)
 
     def test_get_target(self):
         tmp_file = StringIO.StringIO()
@@ -113,14 +62,13 @@ class TestIetAdmDriver(test.TestCase):
             '    Lun 0 Path=/dev/stack-volumes-lvmdriver-1/volume-83c2e877-feed-46be-8435-77884fe55b45,Type=fileio\n'  # noqa
         )
         tmp_file.seek(0)
-        test_vol = ('iqn.2010-10.org.openstack:'
-                    'volume-83c2e877-feed-46be-8435-77884fe55b45')
         expected = ('otzLy2UYbYfnP4zXLG5z', '234Zweo38VGBBvrpK9nt')
         with mock.patch('__builtin__.open') as mock_open:
             ictx = context.get_admin_context()
             mock_open.return_value = contextlib.closing(tmp_file)
             self.assertEqual(expected,
-                             self.target._get_target_chap_auth(ictx, test_vol))
+                             self.target._get_target_chap_auth(ictx,
+                                                               self.test_vol))
             self.assertTrue(mock_open.called)
 
             # Test the failure case: Failed to handle the config file
@@ -128,7 +76,7 @@ class TestIetAdmDriver(test.TestCase):
             self.assertRaises(StandardError,
                               self.target._get_target_chap_auth,
                               ictx,
-                              test_vol)
+                              self.test_vol)
 
     @mock.patch('cinder.volume.targets.iet.IetAdm._get_target',
                 return_value=0)
@@ -139,14 +87,12 @@ class TestIetAdmDriver(test.TestCase):
                                  mock_execute, mock_get_targ):
         mock_execute.return_value = ('', '')
         tmp_file = StringIO.StringIO()
-        test_vol = ('iqn.2010-10.org.openstack:'
-                    'volume-83c2e877-feed-46be-8435-77884fe55b45')
         with mock.patch('__builtin__.open') as mock_open:
             mock_open.return_value = contextlib.closing(tmp_file)
             self.assertEqual(
                 0,
                 self.target.create_iscsi_target(
-                    test_vol,
+                    self.test_vol,
                     0,
                     0,
                     self.fake_volumes_dir))
@@ -158,7 +104,7 @@ class TestIetAdmDriver(test.TestCase):
             mock_open.side_effect = putils.ProcessExecutionError
             self.assertRaises(exception.ISCSITargetCreateFailed,
                               self.target.create_iscsi_target,
-                              test_vol,
+                              self.test_vol,
                               0,
                               0,
                               self.fake_volumes_dir)
@@ -167,7 +113,7 @@ class TestIetAdmDriver(test.TestCase):
             mock_execute.side_effect = putils.ProcessExecutionError
             self.assertRaises(exception.ISCSITargetCreateFailed,
                               self.target.create_iscsi_target,
-                              test_vol,
+                              self.test_vol,
                               0,
                               0,
                               self.fake_volumes_dir)
@@ -175,15 +121,12 @@ class TestIetAdmDriver(test.TestCase):
     @mock.patch('cinder.utils.execute')
     @mock.patch('os.path.exists', return_value=True)
     def test_update_config_file_failure(self, mock_exists, mock_execute):
-        test_vol = ('iqn.2010-10.org.openstack:'
-                    'volume-83c2e877-feed-46be-8435-77884fe55b45')
-
         # Test the failure case: conf file does not exist
         mock_exists.return_value = False
         mock_execute.side_effect = putils.ProcessExecutionError
         self.assertRaises(exception.ISCSITargetCreateFailed,
                           self.target.update_config_file,
-                          test_vol,
+                          self.test_vol,
                           0,
                           self.fake_volumes_dir,
                           "foo bar")
@@ -194,12 +137,10 @@ class TestIetAdmDriver(test.TestCase):
     def test_create_iscsi_target_already_exists(self, mock_execute,
                                                 mock_get_targ):
         mock_execute.return_value = ('fake out', 'fake err')
-        test_vol = 'iqn.2010-10.org.openstack:'\
-                   'volume-83c2e877-feed-46be-8435-77884fe55b45'
         self.assertEqual(
             1,
             self.target.create_iscsi_target(
-                test_vol,
+                self.test_vol,
                 1,
                 0,
                 self.fake_volumes_dir))
@@ -209,7 +150,7 @@ class TestIetAdmDriver(test.TestCase):
     @mock.patch('cinder.volume.targets.iet.IetAdm._find_sid_cid_for_target',
                 return_value=None)
     @mock.patch('os.path.exists', return_value=False)
-    @mock.patch.object(utils, 'execute')
+    @mock.patch('cinder.utils.execute')
     def test_remove_iscsi_target(self, mock_execute, mock_exists, mock_find):
 
         # Test the normal case
@@ -268,9 +209,11 @@ class TestIetAdmDriver(test.TestCase):
                                                    self.fake_volumes_dir))
         self.assertTrue(mock_execute.called)
 
+    @mock.patch('cinder.volume.targets.iet.IetAdm._get_target_chap_auth',
+                return_value=None)
     @mock.patch('cinder.volume.targets.iet.IetAdm._get_target',
                 return_value=1)
-    def test_ensure_export(self, mock_get_target):
+    def test_ensure_export(self, mock_get_targetm, mock_get_chap):
         ctxt = context.get_admin_context()
         with mock.patch.object(self.target, 'create_iscsi_target'):
             self.target.ensure_export(ctxt,
index 291f0c0fc22c0c2adb51f4864e3d08951b45a786..53635321eff7faf4efbe4614e1981208eb0c31ad 100644 (file)
 
 import mock
 
-from cinder.tests.unit.targets import test_lio_driver as test_lio
-from cinder.tests.unit.targets import test_tgt_driver as test_tgt
+from cinder.tests.unit.targets import targets_fixture as tf
 from cinder import utils
 from cinder.volume.targets import iser
 from cinder.volume.targets import lio
 from cinder.volume.targets import tgt
 
 
-class TestIserAdmDriver(test_tgt.TestTgtAdmDriver):
+class TestIserAdmDriver(tf.TargetDriverFixture):
     """Unit tests for the deprecated ISERTgtAdm flow
     """
 
     def setUp(self):
         super(TestIserAdmDriver, self).setUp()
-        self.configuration.iser_ip_address = '10.9.8.7'
-        self.configuration.iser_target_prefix = 'iqn.2010-10.org.openstack:'
         self.target = iser.ISERTgtAdm(root_helper=utils.get_root_helper(),
                                       configuration=self.configuration)
 
@@ -48,7 +45,7 @@ class TestIserAdmDriver(test_tgt.TestTgtAdmDriver):
         self.assertEqual(self.target.iscsi_protocol, 'iser')
 
 
-class TestIserTgtDriver(test_tgt.TestTgtAdmDriver):
+class TestIserTgtDriver(tf.TargetDriverFixture):
     """Unit tests for the iSER TGT flow
     """
 
@@ -74,7 +71,7 @@ class TestIserTgtDriver(test_tgt.TestTgtAdmDriver):
                                                            connector))
 
 
-class TestIserLioAdmDriver(test_lio.TestLioAdmDriver):
+class TestIserLioAdmDriver(tf.TargetDriverFixture):
     """Unit tests for the iSER LIO flow
     """
     def setUp(self):
@@ -89,7 +86,7 @@ class TestIserLioAdmDriver(test_lio.TestLioAdmDriver):
     def test_iscsi_protocol(self):
         self.assertEqual(self.target.iscsi_protocol, 'iser')
 
-    @mock.patch.object(utils, 'execute')
+    @mock.patch('cinder.utils.execute')
     @mock.patch.object(lio.LioAdm, '_get_iscsi_properties')
     def test_initialize_connection(self, mock_get_iscsi, mock_execute):
 
index 478d276f70b95c0860f0ec186a713d5ccc4951e3..e3fbca11702c9a9eca2ab4b46b36bd44464a00f6 100644 (file)
 
 import mock
 from oslo_concurrency import processutils as putils
-from oslo_utils import timeutils
 
 from cinder import context
 from cinder import exception
-from cinder import test
+from cinder.tests.unit.targets import targets_fixture as tf
 from cinder import utils
-from cinder.volume import configuration as conf
 from cinder.volume.targets import lio
 
 
-class TestLioAdmDriver(test.TestCase):
+class TestLioAdmDriver(tf.TargetDriverFixture):
 
     def setUp(self):
         super(TestLioAdmDriver, self).setUp()
-        self.configuration = conf.Configuration(None)
-        self.configuration.append_config_values = mock.Mock(return_value=0)
-        self.configuration.safe_get = mock.Mock(side_effect=self.fake_safe_get)
-        self.configuration.iscsi_ip_address = '10.9.8.7'
-        self.fake_volumes_dir = '/tmp/tmpfile'
-        self.iscsi_target_prefix = 'iqn.2010-10.org.openstack:'
-        self.fake_project_id = 'ed2c1fd4-5fc0-11e4-aa15-123b93f75cba'
-        self.fake_volume_id = '83c2e877-feed-46be-8435-77884fe55b45'
+
         with mock.patch.object(lio.LioAdm, '_verify_rtstool'):
             self.target = lio.LioAdm(root_helper=utils.get_root_helper(),
                                      configuration=self.configuration)
-        self.fake_iscsi_scan = ('iqn.2010-10.org.openstack:'
-                                'volume-83c2e877-feed-46be-8435-77884fe55b45')
         self.target.db = mock.MagicMock(
             volume_get=lambda x, y: {'provider_auth': 'IncomingUser foo bar'})
 
-        self.testvol =\
-            {'project_id': self.fake_project_id,
-             'name': 'volume-%s' % self.fake_volume_id,
-             'size': 1,
-             'id': self.fake_volume_id,
-             'volume_type_id': None,
-             'provider_location': '10.9.8.7:3260 '
-                                  'iqn.2010-10.org.openstack:'
-                                  'volume-%s 0' % self.fake_volume_id,
-             'provider_auth': 'CHAP c76370d66b 2FE0CQ8J196R',
-             'provider_geometry': '512 512',
-             'created_at': timeutils.utcnow(),
-             'host': 'fake_host@lvm#lvm'}
-
-    def fake_safe_get(self, value):
-        if value == 'volumes_dir':
-            return self.fake_volumes_dir
-        elif value == 'iscsi_protocol':
-            return self.configuration.iscsi_protocol
-        elif value == 'iscsi_target_prefix':
-            return self.iscsi_target_prefix
-
     def test_get_target(self):
-
-        def _fake_execute(*args, **kwargs):
-            return self.fake_iscsi_scan, None
-
-        self.stubs.Set(utils,
-                       'execute',
-                       _fake_execute)
-
-        self.assertEqual('iqn.2010-10.org.openstack:'
-                         'volume-83c2e877-feed-46be-8435-77884fe55b45',
-                         self.target._get_target('iqn.2010-10.org.openstack:'
-                                                 'volume-83c2e877-feed-46be-'
-                                                 '8435-77884fe55b45'))
+        with mock.patch('cinder.utils.execute',
+                        return_value=(self.test_vol, None)):
+            self.assertEqual(self.test_vol,
+                             self.target._get_target(self.test_vol))
 
     def test_get_iscsi_target(self):
         ctxt = context.get_admin_context()
@@ -96,43 +54,36 @@ class TestLioAdmDriver(test.TestCase):
 
     def test_get_target_chap_auth(self):
         ctxt = context.get_admin_context()
-        test_vol = 'iqn.2010-10.org.openstack:'\
-                   'volume-83c2e877-feed-46be-8435-77884fe55b45'
-
         self.assertEqual(('foo', 'bar'),
-                         self.target._get_target_chap_auth(ctxt, test_vol))
+                         self.target._get_target_chap_auth(ctxt,
+                                                           self.test_vol))
 
     @mock.patch.object(lio.LioAdm, '_persist_configuration')
-    @mock.patch.object(utils, 'execute')
+    @mock.patch('cinder.utils.execute')
     @mock.patch.object(lio.LioAdm, '_get_target')
     def test_create_iscsi_target(self, mget_target, mexecute, mpersist_cfg):
 
         mget_target.return_value = 1
         # create_iscsi_target sends volume_name instead of volume_id on error
-        volume_name = 'volume-83c2e877-feed-46be-8435-77884fe55b45'
-        test_vol = 'iqn.2010-10.org.openstack:' + volume_name
         self.assertEqual(
             1,
             self.target.create_iscsi_target(
-                test_vol,
+                self.test_vol,
                 1,
                 0,
                 self.fake_volumes_dir))
-        mpersist_cfg.assert_called_once_with(volume_name)
+        mpersist_cfg.assert_called_once_with(self.volume_name)
 
     @mock.patch.object(lio.LioAdm, '_persist_configuration')
-    @mock.patch.object(utils, 'execute')
+    @mock.patch('cinder.utils.execute',
+                side_effect=putils.ProcessExecutionError)
     @mock.patch.object(lio.LioAdm, '_get_target')
     def test_create_iscsi_target_already_exists(self, mget_target, mexecute,
                                                 mpersist_cfg):
-        mexecute.side_effect = putils.ProcessExecutionError
-
-        test_vol = 'iqn.2010-10.org.openstack:'\
-                   'volume-83c2e877-feed-46be-8435-77884fe55b45'
         chap_auth = ('foo', 'bar')
         self.assertRaises(exception.ISCSITargetCreateFailed,
                           self.target.create_iscsi_target,
-                          test_vol,
+                          self.test_vol,
                           1,
                           0,
                           self.fake_volumes_dir,
@@ -140,12 +91,8 @@ class TestLioAdmDriver(test.TestCase):
         self.assertEqual(0, mpersist_cfg.call_count)
 
     @mock.patch.object(lio.LioAdm, '_persist_configuration')
-    @mock.patch.object(utils, 'execute')
+    @mock.patch('cinder.utils.execute')
     def test_remove_iscsi_target(self, mexecute, mpersist_cfg):
-
-        volume_id = '83c2e877-feed-46be-8435-77884fe55b45'
-        test_vol = 'iqn.2010-10.org.openstack:volume-' + volume_id
-
         # Test the normal case
         self.target.remove_iscsi_target(0,
                                         0,
@@ -153,10 +100,11 @@ class TestLioAdmDriver(test.TestCase):
                                         self.testvol['name'])
         mexecute.assert_called_once_with('cinder-rtstool',
                                          'delete',
-                                         test_vol,
+                                         self.iscsi_target_prefix +
+                                         self.testvol['name'],
                                          run_as_root=True)
 
-        mpersist_cfg.assert_called_once_with(volume_id)
+        mpersist_cfg.assert_called_once_with(self.fake_volume_id)
 
         # Test the failure case: putils.ProcessExecutionError
         mexecute.side_effect = putils.ProcessExecutionError
@@ -179,21 +127,19 @@ class TestLioAdmDriver(test.TestCase):
         self.target.ensure_export(ctxt,
                                   self.testvol,
                                   self.fake_volumes_dir)
-        test_vol = 'iqn.2010-10.org.openstack:'\
-                   'volume-83c2e877-feed-46be-8435-77884fe55b45'
+
         _mock_create.assert_called_once_with(
-            test_vol,
+            self.iscsi_target_prefix + 'testvol',
             0, 0, self.fake_volumes_dir, ('foo', 'bar'),
             check_exit_code=False,
             old_name=None)
 
     @mock.patch.object(lio.LioAdm, '_persist_configuration')
-    @mock.patch.object(utils, 'execute')
+    @mock.patch('cinder.utils.execute')
     @mock.patch.object(lio.LioAdm, '_get_iscsi_properties')
     def test_initialize_connection(self, mock_get_iscsi, mock_execute,
                                    mpersist_cfg):
-        volume_id = '83c2e877-feed-46be-8435-77884fe55b45'
-        target_id = 'iqn.2010-10.org.openstack:volume-' + volume_id
+        target_id = self.iscsi_target_prefix + 'volume-' + self.fake_volume_id
         connector = {'initiator': 'fake_init'}
 
         # Test the normal case
@@ -206,11 +152,11 @@ class TestLioAdmDriver(test.TestCase):
 
         mock_execute.assert_called_once_with(
             'cinder-rtstool', 'add-initiator', target_id,
-            'c76370d66b', '2FE0CQ8J196R',
-            connector['initiator'],
+            self.expected_iscsi_properties['auth_username'],
+            '2FE0CQ8J196R', connector['initiator'],
             run_as_root=True)
 
-        mpersist_cfg.assert_called_once_with(volume_id)
+        mpersist_cfg.assert_called_once_with(self.fake_volume_id)
 
         # Test the failure case: putils.ProcessExecutionError
         mock_execute.side_effect = putils.ProcessExecutionError
@@ -220,11 +166,10 @@ class TestLioAdmDriver(test.TestCase):
                           connector)
 
     @mock.patch.object(lio.LioAdm, '_persist_configuration')
-    @mock.patch.object(utils, 'execute')
+    @mock.patch('cinder.utils.execute')
     def test_terminate_connection(self, _mock_execute, mpersist_cfg):
 
-        volume_id = '83c2e877-feed-46be-8435-77884fe55b45'
-        target_id = 'iqn.2010-10.org.openstack:volume-' + volume_id
+        target_id = self.iscsi_target_prefix + 'volume-' + self.fake_volume_id
 
         connector = {'initiator': 'fake_init'}
         self.target.terminate_connection(self.testvol,
@@ -234,10 +179,10 @@ class TestLioAdmDriver(test.TestCase):
             connector['initiator'],
             run_as_root=True)
 
-        mpersist_cfg.assert_called_once_with(volume_id)
+        mpersist_cfg.assert_called_once_with(self.fake_volume_id)
 
     @mock.patch.object(lio.LioAdm, '_persist_configuration')
-    @mock.patch.object(utils, 'execute')
+    @mock.patch('cinder.utils.execute')
     def test_terminate_connection_fail(self, _mock_execute, mpersist_cfg):
 
         _mock_execute.side_effect = putils.ProcessExecutionError
index f8947c16791f1fe16ab7346113869423ec870a87..c45a56a0b941b8acdd8683e311ac42bec5b69983 100644 (file)
@@ -9,63 +9,22 @@
 #    WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 #    License for the specific language governing permissions and limitations
 #    under the License.
-import tempfile
 
 import mock
-from oslo_utils import timeutils
 
 from cinder import context
-from cinder import test
+from cinder.tests.unit.targets import targets_fixture as tf
 from cinder import utils
-from cinder.volume import configuration as conf
 from cinder.volume.targets import scst
 from cinder.volume import utils as vutils
 
 
-class TestSCSTAdmDriver(test.TestCase):
+class TestSCSTAdmDriver(tf.TargetDriverFixture):
 
     def setUp(self):
         super(TestSCSTAdmDriver, self).setUp()
-        self.configuration = conf.Configuration(None)
-        self.configuration.append_config_values = mock.Mock(return_value=0)
-        self.configuration.iscsi_ip_address = '10.9.8.7'
-        self.fake_volumes_dir = tempfile.mkdtemp()
-        self.fake_id_1 = 'ed2c1fd4-5fc0-11e4-aa15-123b93f75cba'
-        self.fake_id_2 = 'ed2c2222-5fc0-11e4-aa15-123b93f75cba'
-        self.fake_id_3 = 'ed2c3333-5fc0-11e4-aa15-123b93f75cba'
-        self.fake_id_4 = 'ed2c4444-5fc0-11e4-aa15-123b93f75cba'
-        self.stubs.Set(self.configuration, 'safe_get', self.fake_safe_get)
-
         self.target = scst.SCSTAdm(root_helper=utils.get_root_helper(),
                                    configuration=self.configuration)
-        self.testvol_1 =\
-            {'project_id': self.fake_id_1,
-             'name': 'testvol',
-             'size': 1,
-             'id': self.fake_id_2,
-             'volume_type_id': None,
-             'provider_location': '10.9.8.7:3260 '
-                                  'iqn.2010-10.org.openstack:'
-                                  'volume-%s 1' % self.fake_id_2,
-             'provider_auth': 'CHAP stack-1-a60e2611875f40199931f2'
-                              'c76370d66b 2FE0CQ8J196R',
-             'provider_geometry': '512 512',
-             'created_at': timeutils.utcnow(),
-             'host': 'fake_host@lvm#lvm'}
-        self.testvol_2 =\
-            {'project_id': self.fake_id_3,
-             'name': 'testvol2',
-             'size': 1,
-             'id': self.fake_id_4,
-             'volume_type_id': None,
-             'provider_location': '10.9.8.7:3260 '
-                                  'iqn.2010-10.org.openstack:'
-                                  'volume-%s 2' % self.fake_id_4,
-             'provider_auth': 'CHAP stack-1-a60e2611875f40199931f2'
-                              'c76370d66b 2FE0CQ8J196R',
-             'provider_geometry': '512 512',
-             'created_at': timeutils.utcnow(),
-             'host': 'fake_host@lvm#lvm'}
 
         self.fake_iscsi_scan = \
             ('Collecting current configuration: done.\n'
@@ -111,10 +70,6 @@ class TestSCSTAdmDriver(test.TestCase):
         self.target.db = mock.MagicMock(
             volume_get=lambda x, y: {'provider_auth': 'IncomingUser foo bar'})
 
-    def fake_safe_get(self, value):
-        if value == 'volumes_dir':
-            return self.fake_volumes_dir
-
     @mock.patch.object(utils, 'execute')
     @mock.patch.object(scst.SCSTAdm, '_target_attribute')
     @mock.patch.object(scst.SCSTAdm, 'scst_execute')
@@ -138,7 +93,7 @@ class TestSCSTAdmDriver(test.TestCase):
     def test_single_lun_get_target_and_lun(self):
         ctxt = context.get_admin_context()
         self.assertEqual((0, 1), self.target._get_target_and_lun(
-            ctxt, self.testvol_1))
+            ctxt, self.testvol))
 
     @mock.patch.object(utils, 'execute')
     @mock.patch.object(scst.SCSTAdm, '_get_group')
@@ -148,13 +103,12 @@ class TestSCSTAdmDriver(test.TestCase):
         mock_execute.return_value = (self.fake_list_group, None)
         mock_get_group.return_value = self.fake_list_group
 
-        self.stubs.Set(self.target,
-                       'target_name',
-                       'iqn.2010-10.org.openstack:volume-vedams')
         ctxt = context.get_admin_context()
-
-        self.assertEqual((0, 3), self.target._get_target_and_lun(
-            ctxt, self.testvol_1))
+        with mock.patch.object(self.target, 'target_name',
+                               return_value='iqn.2010-10.org.openstack:'
+                                            'volume-vedams'):
+            self.assertEqual((0, 3), self.target._get_target_and_lun(
+                ctxt, self.testvol))
 
     @mock.patch.object(utils, 'execute')
     @mock.patch.object(scst.SCSTAdm, '_get_target')
@@ -191,44 +145,34 @@ class TestSCSTAdmDriver(test.TestCase):
         def _fake_get_target_chap_auth(*args, **kwargs):
             return ('QZJbisGmn9AL954FNF4D', 'P68eE7u9eFqDGexd28DQ')
 
-        self.stubs.Set(self.target,
-                       '_get_target_and_lun',
-                       _fake_get_target_and_lun)
-
-        self.stubs.Set(self.target,
-                       'initiator_iqn',
-                       'iqn.1993-08.org.debian:01:626bf14ebdc')
-        self.stubs.Set(self.target,
-                       '_iscsi_location',
-                       _fake_iscsi_location)
-        self.stubs.Set(self.target,
-                       '_get_target_chap_auth',
-                       _fake_get_target_chap_auth)
-        self.stubs.Set(self.target,
-                       'target_driver',
-                       'iscsi')
-        self.stubs.Set(self.target,
-                       'initiator_iqn',
-                       'iqn.1993-08.org.debian:01:626bf14ebdc')
-        self.stubs.Set(vutils,
-                       'generate_username',
-                       lambda: 'QZJbisGmn9AL954FNF4D')
-        self.stubs.Set(vutils,
-                       'generate_password',
-                       lambda: 'P68eE7u9eFqDGexd28DQ')
-
         ctxt = context.get_admin_context()
         expected_result = {'location': '10.9.8.7:3260,1 '
                            'iqn.2010-10.org.openstack:'
                            'volume-ed2c2222-5fc0-11e4-aa15-123b93f75cba 1',
                            'auth': 'CHAP '
                            'QZJbisGmn9AL954FNF4D P68eE7u9eFqDGexd28DQ'}
-        self.assertEqual(expected_result,
-                         self.target.create_export(ctxt,
-                                                   self.testvol_1,
-                                                   self.fake_volumes_dir))
 
-    @mock.patch.object(utils, 'execute')
+        with mock.patch.object(self.target, '_get_target_and_lun',
+                               side_effect=_fake_get_target_and_lun),\
+                mock.patch.object(self.target, '_get_target_chap_auth',
+                                  side_effect=_fake_get_target_chap_auth),\
+                mock.patch.object(self.target, 'initiator_iqn',
+                                  return_value='iqn.1993-08.org.debian:'
+                                               '01:626bf14ebdc'),\
+                mock.patch.object(self.target, '_iscsi_location',
+                                  side_effect=_fake_iscsi_location),\
+                mock.patch.object(self.target, 'target_driver',
+                                  return_value='iscsi'),\
+                mock.patch.object(vutils, 'generate_username',
+                                  side_effect=lambda: 'QZJbisGmn9AL954FNF4D'),\
+                mock.patch.object(vutils, 'generate_password',
+                                  side_effect=lambda: 'P68eE7u9eFqDGexd28DQ'):
+            self.assertEqual(expected_result,
+                             self.target.create_export(ctxt,
+                                                       self.testvol,
+                                                       self.fake_volumes_dir))
+
+    @mock.patch('cinder.utils.execute')
     @mock.patch.object(scst.SCSTAdm, '_get_target')
     @mock.patch.object(scst.SCSTAdm, 'scst_execute')
     def test_ensure_export(self, mock_execute,
@@ -245,23 +189,20 @@ class TestSCSTAdmDriver(test.TestCase):
         def _fake_get_target_chap_auth(*args, **kwargs):
             return ('QZJbisGmn9AL954FNF4D', 'P68eE7u9eFqDGexd28DQ')
 
-        self.stubs.Set(self.target,
-                       '_get_target_chap_auth',
-                       _fake_get_target_chap_auth)
-        self.stubs.Set(self.target,
-                       '_get_target_and_lun',
-                       _fake_get_target_and_lun)
-
-        with mock.patch.object(self.target, 'create_iscsi_target'):
+        with mock.patch.object(self.target, 'create_iscsi_target'),\
+                mock.patch.object(self.target, '_get_target_chap_auth',
+                                  side_effect=_fake_get_target_chap_auth),\
+                mock.patch.object(self.target, '_get_target_and_lun',
+                                  side_effect=_fake_get_target_and_lun):
             self.target.ensure_export(ctxt,
-                                      self.testvol_1,
+                                      self.testvol,
                                       self.fake_volumes_dir)
             self.target.create_iscsi_target.assert_called_once_with(
                 'iqn.2010-10.org.openstack:testvol',
                 'ed2c2222-5fc0-11e4-aa15-123b93f75cba',
                 0, 1, self.fake_volumes_dir, _fake_get_target_chap_auth())
 
-    @mock.patch.object(utils, 'execute')
+    @mock.patch('cinder.utils.execute')
     @mock.patch.object(scst.SCSTAdm, '_get_target')
     @mock.patch.object(scst.SCSTAdm, 'scst_execute')
     def test_ensure_export_chap(self, mock_execute,
@@ -278,16 +219,13 @@ class TestSCSTAdmDriver(test.TestCase):
         def _fake_get_target_chap_auth(*args, **kwargs):
             return None
 
-        self.stubs.Set(self.target,
-                       '_get_target_chap_auth',
-                       _fake_get_target_chap_auth)
-        self.stubs.Set(self.target,
-                       '_get_target_and_lun',
-                       _fake_get_target_and_lun)
-
-        with mock.patch.object(self.target, 'create_iscsi_target'):
+        with mock.patch.object(self.target, 'create_iscsi_target'),\
+                mock.patch.object(self.target, '_get_target_chap_auth',
+                                  side_effect=_fake_get_target_chap_auth),\
+                mock.patch.object(self.target, '_get_target_and_lun',
+                                  side_effect=_fake_get_target_and_lun):
             self.target.ensure_export(ctxt,
-                                      self.testvol_1,
+                                      self.testvol,
                                       self.fake_volumes_dir)
             self.target.create_iscsi_target.assert_called_once_with(
                 'iqn.2010-10.org.openstack:testvol',
index 1f6286baac36e22d7af3d29b5780f422ccccc0ce..5225894546e6915321fa320e97ee15427424b515 100644 (file)
 #    under the License.
 
 import os
-import tempfile
 import time
 
 import mock
 from oslo_concurrency import processutils as putils
-from oslo_utils import timeutils
 
 from cinder import context
 from cinder import exception
-from cinder import test
+from cinder.tests.unit.targets import targets_fixture as tf
 from cinder import utils
-from cinder.volume import configuration as conf
 from cinder.volume.targets import tgt
 from cinder.volume import utils as vutils
 
 
-class TestTgtAdmDriver(test.TestCase):
+class TestTgtAdmDriver(tf.TargetDriverFixture):
 
     def setUp(self):
         super(TestTgtAdmDriver, self).setUp()
-        self.configuration = conf.Configuration(None)
-        self.configuration.append_config_values = mock.Mock(return_value=0)
-        self.configuration.iscsi_ip_address = '10.9.8.7'
-        self.fake_volumes_dir = tempfile.mkdtemp()
-        self.iscsi_target_prefix = 'iqn.2010-10.org.openstack:'
-        self.fake_project_id = 'ed2c1fd4-5fc0-11e4-aa15-123b93f75cba'
-        self.fake_volume_id = '83c2e877-feed-46be-8435-77884fe55b45'
-        self.stubs.Set(self.configuration, 'safe_get', self.fake_safe_get)
         self.target = tgt.TgtAdm(root_helper=utils.get_root_helper(),
                                  configuration=self.configuration)
-        self.testvol =\
-            {'project_id': self.fake_project_id,
-             'name': 'volume-%s' % self.fake_volume_id,
-             'size': 1,
-             'id': self.fake_volume_id,
-             'volume_type_id': None,
-             'provider_location': '10.9.8.7:3260 '
-                                  'iqn.2010-10.org.openstack:'
-                                  'volume-%s 0' % self.fake_volume_id,
-             'provider_auth': 'CHAP stack-1-a60e2611875f40199931f2'
-                              'c76370d66b 2FE0CQ8J196R',
-             'provider_geometry': '512 512',
-             'created_at': timeutils.utcnow(),
-             'host': 'fake_host@lvm#lvm'}
-
         self.testvol_path = \
             '/dev/stack-volumes-lvmdriver-1/'\
             'volume-83c2e877-feed-46be-8435-77884fe55b45'
 
-        self.expected_iscsi_properties = \
-            {'auth_method': 'CHAP',
-             'auth_password': '2FE0CQ8J196R',
-             'auth_username': 'stack-1-a60e2611875f40199931f2c76370d66b',
-             'encrypted': False,
-             'logical_block_size': '512',
-             'physical_block_size': '512',
-             'target_discovered': False,
-             'target_iqn': 'iqn.2010-10.org.openstack:volume-%s' %
-                           self.fake_volume_id,
-             'target_lun': 0,
-             'target_portal': '10.9.8.7:3260',
-             'volume_id': self.fake_volume_id}
-
         self.fake_iscsi_scan =\
             ('Target 1: iqn.2010-10.org.openstack:volume-83c2e877-feed-46be-8435-77884fe55b45\n'  # noqa
              '    System information:\n'
@@ -114,68 +74,42 @@ class TestTgtAdmDriver(test.TestCase):
              '    ACL information:\n'
              '        ALL"\n')
 
-    def fake_safe_get(self, value):
-        if value == 'volumes_dir':
-            return self.fake_volumes_dir
-        elif value == 'iscsi_protocol':
-            return self.configuration.iscsi_protocol
-        elif value == 'iscsi_target_prefix':
-            return self.iscsi_target_prefix
-
     def test_iscsi_protocol(self):
         self.assertEqual(self.target.iscsi_protocol, 'iscsi')
 
     def test_get_target(self):
-
-        def _fake_execute(*args, **kwargs):
-            return self.fake_iscsi_scan, None
-
-        self.stubs.Set(utils,
-                       'execute',
-                       _fake_execute)
-
-        self.assertEqual('1',
-                         self.target._get_target('iqn.2010-10.org.openstack:'
-                                                 'volume-83c2e877-feed-46be-'
-                                                 '8435-77884fe55b45'))
+        with mock.patch('cinder.utils.execute',
+                        return_value=(self.fake_iscsi_scan, None)):
+            self.assertEqual('1',
+                             self.target._get_target(
+                                 'iqn.2010-10.org.openstack:'
+                                 'volume-83c2e877-feed-46be-'
+                                 '8435-77884fe55b45'))
 
     def test_verify_backing_lun(self):
+        with mock.patch('cinder.utils.execute',
+                        return_value=(self.fake_iscsi_scan, None)):
 
-        def _fake_execute(*args, **kwargs):
-            return self.fake_iscsi_scan, None
-
-        self.stubs.Set(utils,
-                       'execute',
-                       _fake_execute)
-
-        self.assertTrue(self.target._verify_backing_lun(
-            'iqn.2010-10.org.openstack:'
-            'volume-83c2e877-feed-46be-'
-            '8435-77884fe55b45', '1'))
+            self.assertTrue(self.target._verify_backing_lun(
+                'iqn.2010-10.org.openstack:'
+                'volume-83c2e877-feed-46be-'
+                '8435-77884fe55b45', '1'))
 
         # Test the failure case
         bad_scan = self.fake_iscsi_scan.replace('LUN: 1', 'LUN: 3')
 
-        def _fake_execute_bad_lun(*args, **kwargs):
-            return bad_scan, None
-
-        self.stubs.Set(utils,
-                       'execute',
-                       _fake_execute_bad_lun)
-
-        self.assertFalse(self.target._verify_backing_lun(
-            'iqn.2010-10.org.openstack:'
-            'volume-83c2e877-feed-46be-'
-            '8435-77884fe55b45', '1'))
+        with mock.patch('cinder.utils.execute',
+                        return_value=(bad_scan, None)):
+            self.assertFalse(self.target._verify_backing_lun(
+                'iqn.2010-10.org.openstack:'
+                'volume-83c2e877-feed-46be-'
+                '8435-77884fe55b45', '1'))
 
     @mock.patch.object(time, 'sleep')
-    @mock.patch.object(utils, 'execute')
+    @mock.patch('cinder.utils.execute')
     def test_recreate_backing_lun(self, mock_execute, mock_sleep):
-
-        test_vol = 'iqn.2010-10.org.openstack:'\
-                   'volume-83c2e877-feed-46be-8435-77884fe55b45'
         mock_execute.return_value = ('out', 'err')
-        self.target._recreate_backing_lun(test_vol, '1',
+        self.target._recreate_backing_lun(self.test_vol, '1',
                                           self.testvol['name'],
                                           self.testvol_path)
 
@@ -191,7 +125,7 @@ class TestTgtAdmDriver(test.TestCase):
         # Test the failure case
         mock_execute.side_effect = putils.ProcessExecutionError
         self.assertFalse(self.target._recreate_backing_lun(
-            test_vol,
+            self.test_vol,
             '1',
             self.testvol['name'],
             self.testvol_path))
@@ -219,22 +153,17 @@ class TestTgtAdmDriver(test.TestCase):
             '    incominguser otzLy2UYbYfnP4zXLG5z 234Zweo38VGBBvrpK9nt\n'\
             '    write-cache on\n'\
             '</target>'
-        test_vol =\
-            'iqn.2010-10.org.openstack:'\
-            'volume-83c2e877-feed-46be-8435-77884fe55b45'
         with open(os.path.join(self.fake_volumes_dir,
-                               test_vol.split(':')[1]),
+                               self.test_vol.split(':')[1]),
                   'wb') as tmp_file:
             tmp_file.write(persist_file)
         ctxt = context.get_admin_context()
         expected = ('otzLy2UYbYfnP4zXLG5z', '234Zweo38VGBBvrpK9nt')
         self.assertEqual(expected,
-                         self.target._get_target_chap_auth(ctxt, test_vol))
+                         self.target._get_target_chap_auth(ctxt,
+                                                           self.test_vol))
 
     def test_get_target_chap_auth_negative(self):
-        test_vol =\
-            'iqn.2010-10.org.openstack:'\
-            'volume-83c2e877-feed-46be-8435-77884fe55b45'
         with mock.patch('__builtin__.open') as mock_open:
             e = IOError()
             e.errno = 123
@@ -242,38 +171,25 @@ class TestTgtAdmDriver(test.TestCase):
             ctxt = context.get_admin_context()
             self.assertRaises(IOError,
                               self.target._get_target_chap_auth,
-                              ctxt, test_vol)
+                              ctxt, self.test_vol)
             mock_open.side_effect = StandardError()
             self.assertRaises(StandardError,
                               self.target._get_target_chap_auth,
-                              ctxt, test_vol)
+                              ctxt, self.test_vol)
 
     def test_create_iscsi_target(self):
-
-        def _fake_execute(*args, **kwargs):
-            return '', ''
-
-        self.stubs.Set(utils,
-                       'execute',
-                       _fake_execute)
-
-        self.stubs.Set(self.target,
-                       '_get_target',
-                       lambda x: 1)
-
-        self.stubs.Set(self.target,
-                       '_verify_backing_lun',
-                       lambda x, y: True)
-
-        test_vol = 'iqn.2010-10.org.openstack:'\
-                   'volume-83c2e877-feed-46be-8435-77884fe55b45'
-        self.assertEqual(
-            1,
-            self.target.create_iscsi_target(
-                test_vol,
+        with mock.patch('cinder.utils.execute', return_value=('', '')),\
+                mock.patch.object(self.target, '_get_target',
+                                  side_effect=lambda x: 1),\
+                mock.patch.object(self.target, '_verify_backing_lun',
+                                  side_effect=lambda x, y: True):
+            self.assertEqual(
                 1,
-                0,
-                self.fake_volumes_dir))
+                self.target.create_iscsi_target(
+                    self.test_vol,
+                    1,
+                    0,
+                    self.fake_volumes_dir))
 
     def test_create_iscsi_target_already_exists(self):
         def _fake_execute(*args, **kwargs):
@@ -286,31 +202,22 @@ class TestTgtAdmDriver(test.TestCase):
             else:
                 return 'fake out', 'fake err'
 
-        self.stubs.Set(utils,
-                       'execute',
-                       _fake_execute)
-
-        self.stubs.Set(self.target,
-                       '_get_target',
-                       lambda x: 1)
-
-        self.stubs.Set(self.target,
-                       '_verify_backing_lun',
-                       lambda x, y: True)
-
-        test_vol = 'iqn.2010-10.org.openstack:'\
-                   'volume-83c2e877-feed-46be-8435-77884fe55b45'
-        self.assertEqual(
-            1,
-            self.target.create_iscsi_target(
-                test_vol,
+        with mock.patch.object(self.target, '_get_target',
+                               side_effect=lambda x: 1),\
+                mock.patch.object(self.target, '_verify_backing_lun',
+                                  side_effect=lambda x, y: True),\
+                mock.patch('cinder.utils.execute', _fake_execute):
+            self.assertEqual(
                 1,
-                0,
-                self.fake_volumes_dir))
+                self.target.create_iscsi_target(
+                    self.test_vol,
+                    1,
+                    0,
+                    self.fake_volumes_dir))
 
     @mock.patch('os.path.isfile', return_value=True)
     @mock.patch('os.path.exists', return_value=True)
-    @mock.patch.object(utils, 'execute')
+    @mock.patch('cinder.utils.execute')
     @mock.patch('os.unlink', return_value=None)
     def test_delete_target_not_found(self,
                                      mock_unlink,
@@ -355,7 +262,7 @@ class TestTgtAdmDriver(test.TestCase):
 
     @mock.patch('os.path.isfile', return_value=True)
     @mock.patch('os.path.exists', return_value=True)
-    @mock.patch.object(utils, 'execute')
+    @mock.patch('cinder.utils.execute')
     @mock.patch('os.unlink', return_value=None)
     def test_delete_target_acl_not_found(self,
                                          mock_unlink,
@@ -411,7 +318,7 @@ class TestTgtAdmDriver(test.TestCase):
                          self.target.initialize_connection(self.testvol,
                                                            connector))
 
-    @mock.patch.object(utils, 'execute')
+    @mock.patch('cinder.utils.execute')
     @mock.patch.object(tgt.TgtAdm, '_get_target')
     @mock.patch.object(os.path, 'exists')
     @mock.patch.object(os.path, 'isfile')
@@ -423,9 +330,6 @@ class TestTgtAdmDriver(test.TestCase):
                                  mock_get_target,
                                  mock_execute):
 
-        test_vol = 'iqn.2010-10.org.openstack:'\
-                   'volume-83c2e877-feed-46be-8435-77884fe55b45'
-
         # Test the failure case: path does not exist
         mock_path_exists.return_value = None
         self.assertEqual(None,
@@ -442,64 +346,51 @@ class TestTgtAdmDriver(test.TestCase):
                                         1,
                                         self.testvol['id'],
                                         self.testvol['name'])
-        calls = [mock.call('tgt-admin', '--force', '--delete', test_vol,
+        calls = [mock.call('tgt-admin', '--force', '--delete',
+                           self.iscsi_target_prefix + self.testvol['name'],
                            run_as_root=True),
-                 mock.call('tgt-admin', '--delete', test_vol,
+                 mock.call('tgt-admin', '--delete',
+                           self.iscsi_target_prefix + self.testvol['name'],
                            run_as_root=True)]
 
         mock_execute.assert_has_calls(calls)
 
     def test_create_export(self):
-
-        def _fake_execute(*args, **kwargs):
-            return '', ''
-
-        self.stubs.Set(utils,
-                       'execute',
-                       _fake_execute)
-
-        self.stubs.Set(self.target,
-                       '_get_target',
-                       lambda x: 1)
-
-        self.stubs.Set(self.target,
-                       '_verify_backing_lun',
-                       lambda x, y: True)
-
-        self.stubs.Set(self.target,
-                       '_get_target_chap_auth',
-                       lambda x, y: None)
-        self.stubs.Set(vutils,
-                       'generate_username',
-                       lambda: 'QZJbisGmn9AL954FNF4D')
-        self.stubs.Set(vutils,
-                       'generate_password',
-                       lambda: 'P68eE7u9eFqDGexd28DQ')
-
-        expected_result = {'location': '10.9.8.7:3260,1 '
-                           'iqn.2010-10.org.openstack:'
-                           'volume-83c2e877-feed-46be-8435-77884fe55b45 1',
+        expected_result = {'location': '10.9.8.7:3260,1 ' +
+                           self.iscsi_target_prefix +
+                           self.testvol['name'] + ' 1',
                            'auth': 'CHAP '
-                           'QZJbisGmn9AL954FNF4D P68eE7u9eFqDGexd28DQ'}
+                           'QZJbisG9AL954FNF4D P68eE7u9eFqDGexd28DQ'}
+
+        with mock.patch('cinder.utils.execute', return_value=('', '')),\
+                mock.patch.object(self.target, '_get_target',
+                                  side_effect=lambda x: 1),\
+                mock.patch.object(self.target, '_verify_backing_lun',
+                                  side_effect=lambda x, y: True),\
+                mock.patch.object(self.target, '_get_target_chap_auth',
+                                  side_effect=lambda x, y: None) as m_chap,\
+                mock.patch.object(vutils, 'generate_username',
+                                  side_effect=lambda: 'QZJbisG9AL954FNF4D'),\
+                mock.patch.object(vutils, 'generate_password',
+                                  side_effect=lambda: 'P68eE7u9eFqDGexd28DQ'):
 
-        ctxt = context.get_admin_context()
-        self.assertEqual(expected_result,
-                         self.target.create_export(ctxt,
-                                                   self.testvol,
-                                                   self.fake_volumes_dir))
+            ctxt = context.get_admin_context()
+            self.assertEqual(expected_result,
+                             self.target.create_export(ctxt,
+                                                       self.testvol,
+                                                       self.fake_volumes_dir))
 
-        self.stubs.Set(self.target,
-                       '_get_target_chap_auth',
-                       lambda x, y: ('otzLy2UYbYfnP4zXLG5z',
-                                     '234Zweo38VGBBvrpK9nt'))
+            m_chap.side_effect = lambda x, y: ('otzLy2UYbYfnP4zXLG5z',
+                                               '234Zweo38VGBBvrpK9nt')
 
-        expected_result['auth'] = ('CHAP '
-                                   'otzLy2UYbYfnP4zXLG5z 234Zweo38VGBBvrpK9nt')
+            expected_result['auth'] = ('CHAP '
+                                       'otzLy2UYbYfnP4zXLG5z '
+                                       '234Zweo38VGBBvrpK9nt')
 
-        self.assertEqual(expected_result,
-                         self.target.create_export(ctxt,
-                                                   self.testvol,
-                                                   self.fake_volumes_dir))
+            self.assertEqual(expected_result,
+                             self.target.create_export(ctxt,
+                                                       self.testvol,
+                                                       self.fake_volumes_dir))
 
     @mock.patch.object(tgt.TgtAdm, '_get_target_chap_auth')
     @mock.patch.object(tgt.TgtAdm, 'create_iscsi_target')
@@ -509,10 +400,9 @@ class TestTgtAdmDriver(test.TestCase):
         self.target.ensure_export(ctxt,
                                   self.testvol,
                                   self.fake_volumes_dir)
-        test_vol = 'iqn.2010-10.org.openstack:'\
-                   'volume-83c2e877-feed-46be-8435-77884fe55b45'
+
         _mock_create.assert_called_once_with(
-            test_vol,
+            self.iscsi_target_prefix + self.testvol['name'],
             0, 1, self.fake_volumes_dir, ('foo', 'bar'),
             check_exit_code=False,
             old_name=None)