diff --git a/cinder/tests/unit/targets/test_base_iscsi_driver.py b/cinder/tests/unit/targets/test_base_iscsi_driver.py index 6cb11e68e52..8c935a2e4d8 100644 --- a/cinder/tests/unit/targets/test_base_iscsi_driver.py +++ b/cinder/tests/unit/targets/test_base_iscsi_driver.py @@ -34,7 +34,8 @@ class TestBaseISCSITargetDriver(tf.TargetDriverFixture): self.target = fake.FakeTarget(root_helper=utils.get_root_helper(), configuration=self.configuration) self.target.db = mock.MagicMock( - volume_get=lambda x, y: {'provider_auth': 'CHAP otzL 234Z'}) + volume_get=mock.MagicMock(return_value={'provider_auth': + 'CHAP otzL 234Z'})) def test_abc_methods_not_present_fails(self): configuration = conf.Configuration(cfg.StrOpt('iscsi_target_prefix', @@ -161,4 +162,6 @@ class TestBaseISCSITargetDriver(tf.TargetDriverFixture): ctxt = context.get_admin_context() self.assertEqual(('otzL', '234Z'), self.target._get_target_chap_auth(ctxt, - self.test_vol)) + self.testvol)) + self.target.db.volume_get.assert_called_once_with( + ctxt, self.testvol['id']) diff --git a/cinder/volume/targets/iscsi.py b/cinder/volume/targets/iscsi.py index e4917a9de57..5fbed1157c7 100644 --- a/cinder/volume/targets/iscsi.py +++ b/cinder/volume/targets/iscsi.py @@ -192,7 +192,7 @@ class ISCSITarget(driver.Target): # Verify we haven't setup a CHAP creds file already # if DNE no big deal, we'll just create it - chap_auth = self._get_target_chap_auth(context, iscsi_name) + chap_auth = self._get_target_chap_auth(context, volume) if not chap_auth: chap_auth = (vutils.generate_username(), vutils.generate_password()) @@ -251,7 +251,7 @@ class ISCSITarget(driver.Target): # Verify we haven't setup a CHAP creds file already # if DNE no big deal, we'll just create it - chap_auth = self._get_target_chap_auth(context, iscsi_name) + chap_auth = self._get_target_chap_auth(context, volume) if not chap_auth: LOG.info(_LI("Skipping ensure_export. No iscsi_target " "provision for volume: %s"), volume['id']) @@ -321,17 +321,16 @@ class ISCSITarget(driver.Target): if tid is None: raise exception.NotFound() - def _get_target_chap_auth(self, context, iscsi_name): + def _get_target_chap_auth(self, context, volume): """Get the current chap auth username and password.""" try: - # 'iscsi_name': 'iqn.2010-10.org.openstack:volume-00000001' - vol_id = iscsi_name.split(':volume-')[1] - volume_info = self.db.volume_get(context, vol_id) + # Query DB to get latest state of volume + volume_info = self.db.volume_get(context, volume['id']) # 'provider_auth': 'CHAP user_id password' if volume_info['provider_auth']: return tuple(volume_info['provider_auth'].split(' ', 3)[1:]) except exception.NotFound: - LOG.debug('Failed to get CHAP auth from DB for %s.', vol_id) + LOG.debug('Failed to get CHAP auth from DB for %s.', volume['id']) @abc.abstractmethod def _get_target_and_lun(self, context, volume): @@ -392,7 +391,7 @@ class SanISCSITarget(ISCSITarget): def _get_target_and_lun(self, context, volume): pass - def _get_target_chap_auth(self, context, iscsi_name): + def _get_target_chap_auth(self, context, volume): pass def create_iscsi_target(self, name, tid, lun, path, diff --git a/cinder/volume/targets/scst.py b/cinder/volume/targets/scst.py index 906713d9fa2..f533fe89b32 100644 --- a/cinder/volume/targets/scst.py +++ b/cinder/volume/targets/scst.py @@ -236,13 +236,22 @@ class SCSTAdm(iscsi.ISCSITarget): return "%s:%s,%s %s %s" % (ip, self.configuration.iscsi_port, target, iqn, lun) + def _get_iscsi_name(self, volume): + if self.target_name is None: + return "%s%s" % (self.configuration.iscsi_target_prefix, + volume['name']) + else: + return self.target_name + def _get_iscsi_target(self, context, vol_id): # FIXME(jdg): Need to implement abc method pass - def _get_target_chap_auth(self, context, iscsi_name): + def _get_target_chap_auth(self, context, volume): # FIXME(jdg): Need to implement abc method + iscsi_name = self._get_iscsi_name(volume) + if self._get_target(iscsi_name) is None: return None (out, _err) = self.scst_execute('-list_tgt_attr', iscsi_name, @@ -262,16 +271,12 @@ class SCSTAdm(iscsi.ISCSITarget): def ensure_export(self, context, volume, volume_path): iscsi_target, lun = self._get_target_and_lun(context, volume) - if self.target_name is None: - iscsi_name = "%s%s" % (self.configuration.iscsi_target_prefix, - volume['name']) - else: - iscsi_name = self.target_name + iscsi_name = self._get_iscsi_name(volume) if self.chap_username and self.chap_password: chap_auth = (self.chap_username, self.chap_password) else: - chap_auth = self._get_target_chap_auth(context, iscsi_name) + chap_auth = self._get_target_chap_auth(context, volume) self.create_iscsi_target(iscsi_name, volume['id'], iscsi_target, lun, volume_path, chap_auth) @@ -279,16 +284,12 @@ class SCSTAdm(iscsi.ISCSITarget): def create_export(self, context, volume, volume_path): """Creates an export for a logical volume.""" iscsi_target, lun = self._get_target_and_lun(context, volume) - if self.target_name is None: - iscsi_name = "%s%s" % (self.configuration.iscsi_target_prefix, - volume['name']) - else: - iscsi_name = self.target_name + iscsi_name = self._get_iscsi_name(volume) if self.chap_username and self.chap_password: chap_auth = (self.chap_username, self.chap_password) else: - chap_auth = self._get_target_chap_auth(context, iscsi_name) + chap_auth = self._get_target_chap_auth(context, volume) if not chap_auth: chap_auth = (vutils.generate_username(), vutils.generate_password())