From c60c47f3138e551355af8418543b064a40fc32f9 Mon Sep 17 00:00:00 2001 From: Rodrigo Barbieri Date: Fri, 6 Feb 2015 10:27:05 -0200 Subject: [PATCH] Fix exception error on HNAS drivers This patch fixes the error thrown on the loading of HNAS drivers (NFS and iSCSI). The bug was added by commit 450a35e2. Also, it adds unit tests to cover exception situations. Change-Id: I00957c4d39117a50d2ac2753f853841eedbe86a0 Closes-bug: #1418645 --- cinder/tests/test_hds_iscsi.py | 49 ++++++++++++++++++++++++++++++ cinder/tests/test_hds_nfs.py | 49 ++++++++++++++++++++++++++++++ cinder/volume/drivers/hds/iscsi.py | 10 +++--- cinder/volume/drivers/hds/nfs.py | 8 ++--- 4 files changed, 107 insertions(+), 9 deletions(-) diff --git a/cinder/tests/test_hds_iscsi.py b/cinder/tests/test_hds_iscsi.py index 3b8e1cf01..e5f491a5d 100644 --- a/cinder/tests/test_hds_iscsi.py +++ b/cinder/tests/test_hds_iscsi.py @@ -19,10 +19,12 @@ Self test for Hitachi Unified Storage (HUS-HNAS) platform. """ import os +from StringIO import StringIO import tempfile import mock +from cinder import exception from cinder.openstack.common import log as logging from cinder import test from cinder.volume import configuration as conf @@ -49,6 +51,33 @@ HNASCONF = """ """ +HNAS_WRONG_CONF1 = """ + + ssc + 172.17.44.15 + supervisor + supervisor + default + 172.17.39.132:/cinder + + +""" + +HNAS_WRONG_CONF2 = """ + + ssc + 172.17.44.15 + supervisor + supervisor + + default + + + silver + + +""" + # The following information is passed on to tests, when creating a volume _VOLUME = {'name': 'testvol', 'volume_id': '1234567890', 'size': 128, 'volume_type': None, 'provider_location': None, 'id': 'abcdefg'} @@ -291,6 +320,26 @@ class HNASiSCSIDriverTest(test.TestCase): vol['provider_location'] = loc['provider_location'] return vol + @mock.patch('__builtin__.open') + @mock.patch.object(os, 'access') + def test_read_config(self, m_access, m_open): + # Test exception when file is not found + m_access.return_value = False + m_open.return_value = StringIO(HNASCONF) + self.assertRaises(exception.NotFound, iscsi._read_config, '') + + # Test exception when config file has parsing errors + # due to missing tag + m_access.return_value = True + m_open.return_value = StringIO(HNAS_WRONG_CONF1) + self.assertRaises(exception.ConfigNotFound, iscsi._read_config, '') + + # Test exception when config file has parsing errors + # due to missing tag + m_open.return_value = StringIO(HNAS_WRONG_CONF2) + self.configuration.hds_hnas_iscsi_config_file = '' + self.assertRaises(exception.ParameterNotFound, iscsi._read_config, '') + def test_create_volume(self): loc = self.driver.create_volume(_VOLUME) self.assertNotEqual(loc, None) diff --git a/cinder/tests/test_hds_nfs.py b/cinder/tests/test_hds_nfs.py index 22dc68d47..7cdbfc939 100644 --- a/cinder/tests/test_hds_nfs.py +++ b/cinder/tests/test_hds_nfs.py @@ -15,10 +15,12 @@ # import os +from StringIO import StringIO import tempfile import mock +from cinder import exception from cinder.openstack.common import log as logging from cinder import test from cinder.volume import configuration as conf @@ -46,6 +48,33 @@ HNASCONF = """ """ +HNAS_WRONG_CONF1 = """ + + ssc + 172.17.44.15 + supervisor + supervisor + default + 172.17.39.132:/cinder + + +""" + +HNAS_WRONG_CONF2 = """ + + ssc + 172.17.44.15 + supervisor + supervisor + + default + + + silver + + +""" + # The following information is passed on to tests, when creating a volume _SHARE = '172.17.39.132:/cinder' _EXPORT = '/cinder' @@ -135,6 +164,26 @@ class HDSNFSDriverTest(test.TestCase): os.remove(self.config_file) os.remove(self.shares_file) + @mock.patch('__builtin__.open') + @mock.patch.object(os, 'access') + def test_read_config(self, m_access, m_open): + # Test exception when file is not found + m_access.return_value = False + m_open.return_value = StringIO(HNASCONF) + self.assertRaises(exception.NotFound, nfs._read_config, '') + + # Test exception when config file has parsing errors + # due to missing tag + m_access.return_value = True + m_open.return_value = StringIO(HNAS_WRONG_CONF1) + self.assertRaises(exception.ConfigNotFound, nfs._read_config, '') + + # Test exception when config file has parsing errors + # due to missing tag + m_open.return_value = StringIO(HNAS_WRONG_CONF2) + self.configuration.hds_hnas_iscsi_config_file = '' + self.assertRaises(exception.ParameterNotFound, nfs._read_config, '') + @mock.patch.object(nfs.HDSNFSDriver, '_id_to_vol') @mock.patch.object(nfs.HDSNFSDriver, '_get_provider_location') @mock.patch.object(nfs.HDSNFSDriver, '_get_export_path') diff --git a/cinder/volume/drivers/hds/iscsi.py b/cinder/volume/drivers/hds/iscsi.py index 9ebab41ff..60e9209cb 100644 --- a/cinder/volume/drivers/hds/iscsi.py +++ b/cinder/volume/drivers/hds/iscsi.py @@ -25,7 +25,7 @@ from oslo_utils import excutils from oslo_utils import units from cinder import exception -from cinder.i18n import _LE, _LI, _LW +from cinder.i18n import _, _LE, _LI, _LW from cinder.openstack.common import log as logging from cinder.volume import driver from cinder.volume.drivers.hds.hnas_backend import HnasBackend @@ -89,14 +89,14 @@ def _read_config(xml_config_file): """Read hds driver specific xml config file.""" if not os.access(xml_config_file, os.R_OK): - raise exception.NotFound(_LE("Can't open config file: %s"), - xml_config_file) + msg = (_("Can't open config file: %s") % xml_config_file) + raise exception.NotFound(message=msg) try: root = ETree.parse(xml_config_file).getroot() except Exception: - raise exception.ConfigNotFound(_LE("Error parsing config file: %s"), - xml_config_file) + msg = (_("Error parsing config file: %s") % xml_config_file) + raise exception.ConfigNotFound(message=msg) # mandatory parameters config = {} diff --git a/cinder/volume/drivers/hds/nfs.py b/cinder/volume/drivers/hds/nfs.py index 4c2d905d1..2faa8a942 100644 --- a/cinder/volume/drivers/hds/nfs.py +++ b/cinder/volume/drivers/hds/nfs.py @@ -81,14 +81,14 @@ def _read_config(xml_config_file): """ if not os.access(xml_config_file, os.R_OK): - raise exception.NotFound(_LE("Can't open config file: %s"), - xml_config_file) + msg = (_("Can't open config file: %s") % xml_config_file) + raise exception.NotFound(message=msg) try: root = ETree.parse(xml_config_file).getroot() except Exception: - raise exception.ConfigNotFound(_LE("Error parsing config file: %s"), - xml_config_file) + msg = (_("Error parsing config file: %s") % xml_config_file) + raise exception.ConfigNotFound(message=msg) # mandatory parameters config = {} -- 2.45.2