]> review.fuel-infra Code Review - openstack-build/cinder-build.git/commitdiff
Fix exception error on HNAS drivers
authorRodrigo Barbieri <rodrigo.barbieri@fit-tecnologia.org.br>
Fri, 6 Feb 2015 12:27:05 +0000 (10:27 -0200)
committerRodrigo Barbieri <rodrigo.barbieri@fit-tecnologia.org.br>
Wed, 18 Feb 2015 13:52:11 +0000 (11:52 -0200)
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
cinder/tests/test_hds_nfs.py
cinder/volume/drivers/hds/iscsi.py
cinder/volume/drivers/hds/nfs.py

index 3b8e1cf010d1141d124f2ac07f71eb681651b8c9..e5f491a5d0286a6a3cf1d3168b9fc6adb2e04464 100644 (file)
@@ -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 = """<?xml version="1.0" encoding="UTF-8" ?>
 </config>
 """
 
+HNAS_WRONG_CONF1 = """<?xml version="1.0" encoding="UTF-8" ?>
+<config>
+  <hnas_cmd>ssc</hnas_cmd>
+  <mgmt_ip0>172.17.44.15</mgmt_ip0>
+  <username>supervisor</username>
+  <password>supervisor</password>
+    <volume_type>default</volume_type>
+    <hdp>172.17.39.132:/cinder</hdp>
+  </svc_0>
+</config>
+"""
+
+HNAS_WRONG_CONF2 = """<?xml version="1.0" encoding="UTF-8" ?>
+<config>
+  <hnas_cmd>ssc</hnas_cmd>
+  <mgmt_ip0>172.17.44.15</mgmt_ip0>
+  <username>supervisor</username>
+  <password>supervisor</password>
+  <svc_0>
+    <volume_type>default</volume_type>
+  </svc_0>
+  <svc_1>
+    <volume_type>silver</volume_type>
+  </svc_1>
+</config>
+"""
+
 # 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 <svc> 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 <hdp> 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)
index 22dc68d4746ba8c2bb7ffe2782c2eb4ec0ec9a87..7cdbfc9399d0565a2000e7ffe1bcdeb9e3e27823 100644 (file)
 #
 
 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 = """<?xml version="1.0" encoding="UTF-8" ?>
 </config>
 """
 
+HNAS_WRONG_CONF1 = """<?xml version="1.0" encoding="UTF-8" ?>
+<config>
+  <hnas_cmd>ssc</hnas_cmd>
+  <mgmt_ip0>172.17.44.15</mgmt_ip0>
+  <username>supervisor</username>
+  <password>supervisor</password>
+    <volume_type>default</volume_type>
+    <hdp>172.17.39.132:/cinder</hdp>
+  </svc_0>
+</config>
+"""
+
+HNAS_WRONG_CONF2 = """<?xml version="1.0" encoding="UTF-8" ?>
+<config>
+  <hnas_cmd>ssc</hnas_cmd>
+  <mgmt_ip0>172.17.44.15</mgmt_ip0>
+  <username>supervisor</username>
+  <password>supervisor</password>
+  <svc_0>
+    <volume_type>default</volume_type>
+  </svc_0>
+  <svc_1>
+    <volume_type>silver</volume_type>
+  </svc_1>
+</config>
+"""
+
 # 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 <svc> 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 <hdp> 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')
index 9ebab41ffe927fc2989b07209d4bad7100a917a3..60e9209cbcde90841b650c673e268f3dddb66b49 100644 (file)
@@ -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 = {}
index 4c2d905d1ad8fb76441d473aa7b29497f52b1df9..2faa8a942ed80622f38741443b77d68a8a6b76ac 100644 (file)
@@ -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 = {}