From 3e37cb6f8cf489480e264b434290064a61c4a782 Mon Sep 17 00:00:00 2001 From: Eric Harney Date: Wed, 19 Apr 2017 14:00:51 -0400 Subject: [PATCH] LIO: Fix terminate_connection AttributeError "provider_location" is not guaranteed to contain a value here. Closes-Bug: #1684243 Change-Id: I964917d13d9415223845ac17eb804ee7faceaf6f --- cinder/tests/unit/targets/targets_fixture.py | 4 ++++ cinder/tests/unit/targets/test_lio_driver.py | 17 +++++++++++++++++ cinder/volume/targets/lio.py | 5 +++++ 3 files changed, 26 insertions(+) diff --git a/cinder/tests/unit/targets/targets_fixture.py b/cinder/tests/unit/targets/targets_fixture.py index 0657943ddd0..1d1bf206699 100644 --- a/cinder/tests/unit/targets/targets_fixture.py +++ b/cinder/tests/unit/targets/targets_fixture.py @@ -10,6 +10,7 @@ # License for the specific language governing permissions and limitations # under the License. +import copy import os import shutil import tempfile @@ -55,6 +56,9 @@ class TargetDriverFixture(test.TestCase): 'created_at': timeutils.utcnow(), 'host': 'fake_host@lvm#lvm'} + self.testvol_no_prov_loc = copy.copy(self.testvol) + self.testvol_no_prov_loc['provider_location'] = None + self.iscsi_target_prefix = 'iqn.2010-10.org.openstack:' self.target_string = ('127.0.0.1:3260,1 ' + self.iscsi_target_prefix + diff --git a/cinder/tests/unit/targets/test_lio_driver.py b/cinder/tests/unit/targets/test_lio_driver.py index f582efae4a2..a542aa90822 100644 --- a/cinder/tests/unit/targets/test_lio_driver.py +++ b/cinder/tests/unit/targets/test_lio_driver.py @@ -283,6 +283,23 @@ class TestLioAdmDriver(tf.TargetDriverFixture): mock_execute.assert_called_once_with(*expected_args, run_as_root=True) mpersist_cfg.assert_called_once_with(self.fake_volume_id) + @mock.patch.object(lio.LioAdm, '_execute', side_effect=lio.LioAdm._execute) + @mock.patch.object(lio.LioAdm, '_persist_configuration') + @mock.patch('cinder.utils.execute') + def test_terminate_connection_no_prov_loc(self, + mock_execute, + mpersist_cfg, + mlock_exec): + """terminate_connection does nothing if provider_location is None""" + + connector = {'initiator': 'fake_init'} + self.target.terminate_connection(self.testvol_no_prov_loc, + connector) + + mlock_exec.assert_not_called() + mock_execute.assert_not_called() + mpersist_cfg.assert_not_called() + @mock.patch.object(lio.LioAdm, '_execute', side_effect=lio.LioAdm._execute) @mock.patch.object(lio.LioAdm, '_persist_configuration') @mock.patch('cinder.utils.execute') diff --git a/cinder/volume/targets/lio.py b/cinder/volume/targets/lio.py index 85d1f220523..37d32698cbd 100644 --- a/cinder/volume/targets/lio.py +++ b/cinder/volume/targets/lio.py @@ -186,6 +186,11 @@ class LioAdm(iscsi.ISCSITarget): return super(LioAdm, self).initialize_connection(volume, connector) def terminate_connection(self, volume, connector, **kwargs): + if volume['provider_location'] is None: + LOG.debug('No provider_location for volume %s.', + volume['id']) + return + volume_iqn = volume['provider_location'].split(' ')[1] # Delete initiator iqns from target ACL