]> review.fuel-infra Code Review - openstack-build/cinder-build.git/commitdiff
Targets tests: Clean up long lines
authorEric Harney <eharney@redhat.com>
Fri, 12 Jun 2015 19:47:30 +0000 (15:47 -0400)
committerEric Harney <eharney@redhat.com>
Mon, 15 Jun 2015 16:58:54 +0000 (12:58 -0400)
Clean up long lines in unit tests, mostly by
using a variable for volume UUID and shortening
unnecessarily long CHAP credentials.

This is not intended to be complete, just
a good start that greatly reduces #noqa and
improves readability.

Change-Id: Iddc26a5722731d76e6d9e4b0ff493ed806bf148d

cinder/tests/unit/targets/targets_fixture.py
cinder/tests/unit/targets/test_cxt_driver.py
cinder/tests/unit/targets/test_lio_driver.py
cinder/tests/unit/targets/test_tgt_driver.py

index 902dd6dbe710cfb884d3ada91fd6293bda50c795..c679740204823d280985d762bfb7e54faaacbe06 100644 (file)
@@ -91,9 +91,10 @@ class TargetDriverFixture(test.TestCase):
              'target_portal': '10.10.7.1:3260',
              'volume_id': self.fake_volume_id}
 
-        self.volume_name = 'volume-83c2e877-feed-46be-8435-77884fe55b45'
+        self.VOLUME_ID = '83c2e877-feed-46be-8435-77884fe55b45'
+        self.VOLUME_NAME = 'volume-' + self.VOLUME_ID
         self.test_vol = (self.iscsi_target_prefix +
-                         self.volume_name)
+                         self.VOLUME_NAME)
 
     def _cleanup(self):
         if os.path.exists(self.fake_volumes_dir):
index 9134fc3fe5e8c81a21367259060ea42c9f81814c..9a844ec3ecd300d9ea87fe05151d2fb6b466a954 100644 (file)
@@ -31,38 +31,45 @@ class TestCxtAdmDriver(tf.TargetDriverFixture):
         self.cxt_subdir = cxt.CxtAdm.cxt_subdir
         self.target = cxt.CxtAdm(root_helper=utils.get_root_helper(),
                                  configuration=self.configuration)
-        self.fake_iscsi_scan =\
+        self.VG = 'stack-volumes-lvmdriver-1'
+        self.fake_iscsi_scan = \
             ('\n'
-             'TARGET: iqn.2010-10.org.openstack:%s, id=1, login_ip=0\n'  # noqa
+             'TARGET: iqn.2010-10.org.openstack:%(vol)s, id=1, login_ip=0\n'
              '        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.volume_name, self.volume_name))
+             '        TargetDevice=/dev/%(vg)s/%(vol)s'
+             ',BLK,PROD=CHISCSI '
+             'Target,SN=0N0743000000000,ID=0D074300000000000000000,'
+             'WWN=:W00743000000000\n'
+             % {'vol': self.VOLUME_NAME, 'vg': self.VG})
 
     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.assertEqual(
+                '1',
+                self.target._get_target(
+                    'iqn.2010-10.org.openstack:volume-%s' % self.VOLUME_ID
+                )
+            )
             self.assertTrue(m_exec.called)
 
     def test_get_target_chap_auth(self):
         tmp_file = StringIO.StringIO()
         tmp_file.write(
             'target:\n'
-            '        TargetName=iqn.2010-10.org.openstack:volume-83c2e877-feed-46be-8435-77884fe55b45\n'  # noqa
-            '        TargetDevice=/dev/stack-volumes-lvmdriver-1/volume-83c2e877-feed-46be-8435-77884fe55b45\n'  # noqa
+            '        TargetName=iqn.2010-10.org.openstack:volume-%(id)s\n'
+            '        TargetDevice=/dev/%(vg)s/volume-%(id)s\n'
             '        PortalGroup=1@10.9.8.7:3260\n'
             '        AuthMethod=CHAP\n'
             '        Auth_CHAP_Policy=Oneway\n'
-            '        Auth_CHAP_Initiator="otzLy2UYbYfnP4zXLG5z":"234Zweo38VGBBvrpK9nt"\n'  # noqa
+            '        Auth_CHAP_Initiator="otzL":"234Z"\n' %
+            {'id': self.VOLUME_ID, 'vg': self.VG}
         )
         tmp_file.seek(0)
 
-        expected = ('otzLy2UYbYfnP4zXLG5z', '234Zweo38VGBBvrpK9nt')
+        expected = ('otzL', '234Z')
         with mock.patch('__builtin__.open') as mock_open:
             ctx = context.get_admin_context()
             mock_open.return_value = contextlib.closing(tmp_file)
index 79213cb31a8f602dc44468255d7b7a3fc9c71a4e..dd01fd9a635cd4f6bb1c0733b2724c6179310168 100644 (file)
@@ -79,7 +79,7 @@ class TestLioAdmDriver(tf.TargetDriverFixture):
                 1,
                 0,
                 self.fake_volumes_dir))
-        mpersist_cfg.assert_called_once_with(self.volume_name)
+        mpersist_cfg.assert_called_once_with(self.VOLUME_NAME)
         mexecute.assert_called_once_with(
             'cinder-rtstool',
             'create',
@@ -96,15 +96,13 @@ class TestLioAdmDriver(tf.TargetDriverFixture):
     @mock.patch.object(lio.LioAdm, '_get_target', return_value=1)
     def test_create_iscsi_target_port_ip(self, mget_target, mexecute,
                                          mpersist_cfg, mlock_exec):
-        test_vol = 'iqn.2010-10.org.openstack:'\
-                   'volume-83c2e877-feed-46be-8435-77884fe55b45'
         ip = '10.0.0.15'
         port = 3261
 
         self.assertEqual(
             1,
             self.target.create_iscsi_target(
-                name=test_vol,
+                name=self.test_vol,
                 tid=1,
                 lun=0,
                 path=self.fake_volumes_dir,
@@ -114,7 +112,7 @@ class TestLioAdmDriver(tf.TargetDriverFixture):
             'cinder-rtstool',
             'create',
             self.fake_volumes_dir,
-            test_vol,
+            self.test_vol,
             '',
             '',
             self.target.iscsi_protocol == 'iser',
@@ -123,7 +121,7 @@ class TestLioAdmDriver(tf.TargetDriverFixture):
 
         mlock_exec.assert_any_call(*expected_args, run_as_root=True)
         mexecute.assert_any_call(*expected_args, run_as_root=True)
-        mpersist_cfg.assert_called_once_with(self.volume_name)
+        mpersist_cfg.assert_called_once_with(self.VOLUME_NAME)
 
     @mock.patch.object(lio.LioAdm, '_execute', side_effect=lio.LioAdm._execute)
     @mock.patch.object(lio.LioAdm, '_persist_configuration')
@@ -131,8 +129,7 @@ class TestLioAdmDriver(tf.TargetDriverFixture):
     @mock.patch.object(lio.LioAdm, '_get_target', return_value=1)
     def test_create_iscsi_target_port_ips(self, mget_target, mexecute,
                                           mpersist_cfg, mlock_exec):
-        test_vol = 'iqn.2010-10.org.openstack:'\
-                   'volume-83c2e877-feed-46be-8435-77884fe55b45'
+        test_vol = 'iqn.2010-10.org.openstack:' + self.VOLUME_NAME
         ips = ['10.0.0.15', '127.0.0.1']
         port = 3261
 
@@ -158,7 +155,7 @@ class TestLioAdmDriver(tf.TargetDriverFixture):
 
         mlock_exec.assert_any_call(*expected_args, run_as_root=True)
         mexecute.assert_any_call(*expected_args, run_as_root=True)
-        mpersist_cfg.assert_called_once_with(self.volume_name)
+        mpersist_cfg.assert_called_once_with(self.VOLUME_NAME)
 
     @mock.patch.object(lio.LioAdm, '_execute', side_effect=lio.LioAdm._execute)
     @mock.patch.object(lio.LioAdm, '_persist_configuration')
index 83ed96097ebff226ddcde5a2292330872aaa7e5b..e9455af85c89bf3401fe8f142baa7d8497d1f1a2 100644 (file)
@@ -31,11 +31,10 @@ class TestTgtAdmDriver(tf.TargetDriverFixture):
         self.target = tgt.TgtAdm(root_helper=utils.get_root_helper(),
                                  configuration=self.configuration)
         self.testvol_path = \
-            '/dev/stack-volumes-lvmdriver-1/'\
-            'volume-83c2e877-feed-46be-8435-77884fe55b45'
+            '/dev/stack-volumes-lvmdriver-1/%s' % self.VOLUME_NAME
 
         self.fake_iscsi_scan =\
-            ('Target 1: iqn.2010-10.org.openstack:volume-83c2e877-feed-46be-8435-77884fe55b45\n'  # noqa
+            ('Target 1: %(test_vol)s\n'
              '    System information:\n'
              '        Driver: iscsi\n'
              '        State: ready\n'
@@ -67,12 +66,13 @@ class TestTgtAdmDriver(tf.TargetDriverFixture):
              '            SWP: No\n'
              '            Thin-provisioning: No\n'
              '            Backing store type: rdwr\n'
-             '            Backing store path: /dev/stack-volumes-lvmdriver-1/volume-83c2e877-feed-46be-8435-77884fe55b45\n'  # noqa
+             '            Backing store path: %(bspath)s\n'
              '            Backing store flags:\n'
              '    Account information:\n'
              '        mDVpzk8cZesdahJC9h73\n'
              '    ACL information:\n'
-             '        ALL"\n')
+             '        ALL"\n' % {'test_vol': self.test_vol,
+                                 'bspath': self.testvol_path})
 
     def test_iscsi_protocol(self):
         self.assertEqual(self.target.iscsi_protocol, 'iscsi')
@@ -80,30 +80,22 @@ class TestTgtAdmDriver(tf.TargetDriverFixture):
     def test_get_target(self):
         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'))
+            iqn = self.test_vol
+            self.assertEqual('1', self.target._get_target(iqn))
 
     def test_verify_backing_lun(self):
+        iqn = self.test_vol
+
         with mock.patch('cinder.utils.execute',
                         return_value=(self.fake_iscsi_scan, None)):
-
-            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, '1'))
 
         # Test the failure case
         bad_scan = self.fake_iscsi_scan.replace('LUN: 1', 'LUN: 3')
 
         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'))
+            self.assertFalse(self.target._verify_backing_lun(iqn, '1'))
 
     @mock.patch.object(time, 'sleep')
     @mock.patch('cinder.utils.execute')
@@ -116,8 +108,7 @@ class TestTgtAdmDriver(tf.TargetDriverFixture):
         expected_command = ('tgtadm', '--lld', 'iscsi', '--op', 'new',
                             '--mode', 'logicalunit', '--tid', '1',
                             '--lun', '1', '-b',
-                            '/dev/stack-volumes-lvmdriver-1/'
-                            'volume-83c2e877-feed-46be-8435-77884fe55b45')
+                            self.testvol_path)
 
         mock_execute.assert_called_once_with(*expected_command,
                                              run_as_root=True)
@@ -147,18 +138,19 @@ class TestTgtAdmDriver(tf.TargetDriverFixture):
 
     def test_get_target_chap_auth(self):
         persist_file =\
-            '<target iqn.2010-10.org.openstack:volume-83c2e877-feed-46be-8435-77884fe55b45>\n'\
-            '    backing-store /dev/stack-volumes-lvmdriver-1/volume-83c2e877-feed-46be-8435-77884fe55b45\n'\
+            '<target iqn.2010-10.org.openstack:volume-%(id)s>\n'\
+            '    backing-store %(bspath)s\n'\
             '    driver iscsi\n'\
-            '    incominguser otzLy2UYbYfnP4zXLG5z 234Zweo38VGBBvrpK9nt\n'\
+            '    incominguser otzL 234Z\n'\
             '    write-cache on\n'\
-            '</target>'
+            '</target>' % {'id': self.VOLUME_ID,
+                           'bspath': self.testvol_path}
         with open(os.path.join(self.fake_volumes_dir,
                                self.test_vol.split(':')[1]),
                   'wb') as tmp_file:
             tmp_file.write(persist_file)
         ctxt = context.get_admin_context()
-        expected = ('otzLy2UYbYfnP4zXLG5z', '234Zweo38VGBBvrpK9nt')
+        expected = ('otzL', '234Z')
         self.assertEqual(expected,
                          self.target._get_target_chap_auth(ctxt,
                                                            self.test_vol))
@@ -240,25 +232,22 @@ class TestTgtAdmDriver(tf.TargetDriverFixture):
 
         mock_exec.side_effect = _fake_execute
 
-        test_vol_id = '83c2e877-feed-46be-8435-77884fe55b45'
-        test_vol_name = 'volume-83c2e877-feed-46be-8435-77884fe55b45'
-
         with mock.patch.object(self.target, '_get_target', return_value=False):
             self.assertEqual(
                 None,
                 self.target.remove_iscsi_target(
                     1,
                     0,
-                    test_vol_id,
-                    test_vol_name))
+                    self.VOLUME_ID,
+                    self.VOLUME_NAME))
 
             mock_exec.side_effect = _fake_execute_wrong_message
             self.assertRaises(exception.ISCSITargetRemoveFailed,
                               self.target.remove_iscsi_target,
                               1,
                               0,
-                              test_vol_id,
-                              test_vol_name)
+                              self.VOLUME_ID,
+                              self.VOLUME_NAME)
 
     @mock.patch('os.path.isfile', return_value=True)
     @mock.patch('os.path.exists', return_value=True)
@@ -285,25 +274,22 @@ class TestTgtAdmDriver(tf.TargetDriverFixture):
 
         mock_exec.side_effect = _fake_execute
 
-        test_vol_id = '83c2e877-feed-46be-8435-77884fe55b45'
-        test_vol_name = 'volume-83c2e877-feed-46be-8435-77884fe55b45'
-
         with mock.patch.object(self.target, '_get_target', return_value=False):
             self.assertEqual(
                 None,
                 self.target.remove_iscsi_target(
                     1,
                     0,
-                    test_vol_id,
-                    test_vol_name))
+                    self.VOLUME_ID,
+                    self.VOLUME_NAME))
 
             mock_exec.side_effect = _fake_execute_wrong_message
             self.assertRaises(exception.ISCSITargetRemoveFailed,
                               self.target.remove_iscsi_target,
                               1,
                               0,
-                              test_vol_id,
-                              test_vol_name)
+                              self.VOLUME_ID,
+                              self.VOLUME_NAME)
 
     @mock.patch.object(tgt.TgtAdm, '_get_iscsi_properties')
     def test_initialize_connection(self, mock_get_iscsi):
@@ -359,8 +345,7 @@ class TestTgtAdmDriver(tf.TargetDriverFixture):
         expected_result = {'location': '10.9.8.7:3260,1 ' +
                            self.iscsi_target_prefix +
                            self.testvol['name'] + ' 1',
-                           'auth': 'CHAP '
-                           'QZJbisG9AL954FNF4D P68eE7u9eFqDGexd28DQ'}
+                           'auth': 'CHAP QZJb P68e'}
 
         with mock.patch('cinder.utils.execute', return_value=('', '')),\
                 mock.patch.object(self.target, '_get_target',
@@ -370,9 +355,9 @@ class TestTgtAdmDriver(tf.TargetDriverFixture):
                 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'),\
+                                  side_effect=lambda: 'QZJb'),\
                 mock.patch.object(vutils, 'generate_password',
-                                  side_effect=lambda: 'P68eE7u9eFqDGexd28DQ'):
+                                  side_effect=lambda: 'P68e'):
 
             ctxt = context.get_admin_context()
             self.assertEqual(expected_result,
@@ -380,12 +365,9 @@ class TestTgtAdmDriver(tf.TargetDriverFixture):
                                                        self.testvol,
                                                        self.fake_volumes_dir))
 
-            m_chap.side_effect = lambda x, y: ('otzLy2UYbYfnP4zXLG5z',
-                                               '234Zweo38VGBBvrpK9nt')
+            m_chap.side_effect = lambda x, y: ('otzL', '234Z')
 
-            expected_result['auth'] = ('CHAP '
-                                       'otzLy2UYbYfnP4zXLG5z '
-                                       '234Zweo38VGBBvrpK9nt')
+            expected_result['auth'] = ('CHAP otzL 234Z')
 
             self.assertEqual(expected_result,
                              self.target.create_export(ctxt,