diff --git a/drivers/FileSR.py b/drivers/FileSR.py index 9027679f2..1313c8c94 100755 --- a/drivers/FileSR.py +++ b/drivers/FileSR.py @@ -1103,21 +1103,23 @@ def _check_hardlinks(self): self._raise_hardlink_error) os.link(test_name, link_name) - self.session.xenapi.SR.remove_from_sm_config( - self.sr_ref, SharedFileSR.NO_HARDLINK_SUPPORT) + if self.session: + self.session.xenapi.SR.remove_from_sm_config( + self.sr_ref, SharedFileSR.NO_HARDLINK_SUPPORT) except OSError: msg = "File system for SR %s does not support hardlinks, crash " \ "consistency of snapshots cannot be assured" % self.uuid util.SMlog(msg, priority=util.LOG_WARNING) - try: - self.session.xenapi.SR.add_to_sm_config( - self.sr_ref, SharedFileSR.NO_HARDLINK_SUPPORT, 'True') - self.session.xenapi.message.create( - "sr_does_not_support_hardlinks", 2, "SR", self.uuid, - msg) - except XenAPI.Failure: - # Might already be set and checking has TOCTOU issues - pass + if self.session: + try: + self.session.xenapi.SR.add_to_sm_config( + self.sr_ref, SharedFileSR.NO_HARDLINK_SUPPORT, 'True') + self.session.xenapi.message.create( + "sr_does_not_support_hardlinks", 2, "SR", self.uuid, + msg) + except XenAPI.Failure: + # Might already be set and checking has TOCTOU issues + pass finally: util.force_unlink(link_name) util.force_unlink(test_name) diff --git a/tests/test_FileSR.py b/tests/test_FileSR.py index 72a0e6234..a98b8c2c6 100644 --- a/tests/test_FileSR.py +++ b/tests/test_FileSR.py @@ -478,6 +478,13 @@ def create_test_sr(self): 'sr_ref': TestShareFileSR.TEST_SR_REF} return FakeSharedFileSR(srcmd, self.sr_uuid) + def create_sessionless_test_sr(self): + srcmd = mock.Mock() + srcmd.dconf = {} + srcmd.params = {'command': "some_command", + 'sr_ref': TestShareFileSR.TEST_SR_REF} + return FakeSharedFileSR(srcmd, self.sr_uuid) + def test_attach_success(self): """ Attach SR on FS with expected features @@ -529,6 +536,24 @@ def test_attach_link_fail_already_set(self): self.mock_session.xenapi.SR.add_to_sm_config.assert_called_with( TestShareFileSR.TEST_SR_REF, TestShareFileSR.NO_HARDLINKS, 'True') + def test_attach_success_no_session(self): + test_sr = self.create_sessionless_test_sr() + + with mock.patch('FileSR.open'): + test_sr.attach(self.sr_uuid) + + self.mock_session.xenapi.SR.remove_from_sm_config.assert_not_called() + + def test_attach_link_fail_no_session(self): + test_sr = self.create_sessionless_test_sr() + self.mock_link.side_effect = OSError(524, TestShareFileSR.ERROR_524) + + with mock.patch('FileSR.open'): + test_sr.attach(self.sr_uuid) + + self.mock_session.xenapi.SR.add_to_sm_config.assert_not_called() + self.mock_session.xenapi.message.create.assert_not_called() + def test_attach_fist_active(self): """ Attach SR with FIST point active to set no hardlinks