]> review.fuel-infra Code Review - openstack-build/cinder-build.git/commitdiff
Fix Python 3 issues in Hitachi HNAS tests
authorVictor Stinner <vstinner@redhat.com>
Tue, 30 Jun 2015 14:59:21 +0000 (16:59 +0200)
committerVictor Stinner <vstinner@redhat.com>
Sat, 25 Jul 2015 01:10:31 +0000 (03:10 +0200)
* Replace __builtin__ with six.moves.builtins.
* Replace tempfile.mkstemp() with tempfile.NamedTemporaryFile() to get a
  text file instead of a binary file.
* tox.ini: add the following tests to Python 3.4

  - cinder.tests.unit.test_hitachi_hnas_backend
  - cinder.tests.unit.test_hitachi_hnas_iscsi
  - cinder.tests.unit.test_hitachi_hnas_nfs

Blueprint cinder-python3
Change-Id: I9a26b4e67033a443271e8f13bcaea5e122ec865a

cinder/tests/unit/test_hitachi_hnas_backend.py
cinder/tests/unit/test_hitachi_hnas_iscsi.py
cinder/tests/unit/test_hitachi_hnas_nfs.py
tox.ini

index 26e9b973530b66f4b175dd322f103f2be6b5aecc..7c8a4940dacee3c645fbfe04d18e739362b1ecd4 100644 (file)
@@ -281,7 +281,7 @@ class HDSHNASBendTest(test.TestCase):
         super(HDSHNASBendTest, self).setUp()
         self.hnas_bend = hnas_backend.HnasBackend(DRV_CONF)
 
-    @mock.patch('__builtin__.open')
+    @mock.patch('six.moves.builtins.open')
     @mock.patch('os.path.isfile', return_value=True)
     @mock.patch('paramiko.RSAKey.from_private_key_file')
     @mock.patch('paramiko.SSHClient')
index 59dbed844c9773871c6c19271c1b4b5c7c7d2f0b..b388422901343f61f93441d807a54681a87473ff 100644 (file)
@@ -268,19 +268,16 @@ class HNASiSCSIDriverTest(test.TestCase):
         self.backend = SimulatedHnasBackend()
         _factory_bend.return_value = self.backend
 
-        (handle, self.config_file) = tempfile.mkstemp('.xml')
-        os.write(handle, HNASCONF)
-        os.close(handle)
+        self.config_file = tempfile.NamedTemporaryFile("w+", suffix='.xml')
+        self.addCleanup(self.config_file.close)
+        self.config_file.write(HNASCONF)
+        self.config_file.flush()
 
         self.configuration = mock.Mock(spec=conf.Configuration)
-        self.configuration.hds_hnas_iscsi_config_file = self.config_file
+        self.configuration.hds_hnas_iscsi_config_file = self.config_file.name
         self.configuration.hds_svc_iscsi_chap_enabled = True
         self.driver = iscsi.HDSISCSIDriver(configuration=self.configuration)
         self.driver.do_setup("")
-        self.addCleanup(self._clean)
-
-    def _clean(self):
-        os.remove(self.config_file)
 
     def _create_volume(self):
         loc = self.driver.create_volume(_VOLUME)
@@ -288,7 +285,7 @@ class HNASiSCSIDriverTest(test.TestCase):
         vol['provider_location'] = loc['provider_location']
         return vol
 
-    @mock.patch('__builtin__.open')
+    @mock.patch('six.moves.builtins.open')
     @mock.patch.object(os, 'access')
     def test_read_config(self, m_access, m_open):
         # Test exception when file is not found
index e17bc0e3d992a7350aab9793e6ad3595c07fa1c2..ac01c8d066292195fb4d93af360632310c2f004c 100644 (file)
@@ -158,16 +158,19 @@ class HDSNFSDriverTest(test.TestCase):
         self.backend = SimulatedHnasBackend()
         m_factory_bend.return_value = self.backend
 
-        (handle, self.config_file) = tempfile.mkstemp('.xml')
-        os.write(handle, HNASCONF)
-        os.close(handle)
-        (handle, self.shares_file) = tempfile.mkstemp('')
-        os.write(handle, SHARESCONF)
-        os.close(handle)
+        self.config_file = tempfile.NamedTemporaryFile("w+", suffix='.xml')
+        self.addCleanup(self.config_file.close)
+        self.config_file.write(HNASCONF)
+        self.config_file.flush()
+
+        self.shares_file = tempfile.NamedTemporaryFile("w+", suffix='.xml')
+        self.addCleanup(self.shares_file.close)
+        self.shares_file.write(SHARESCONF)
+        self.shares_file.flush()
 
         self.configuration = mock.Mock(spec=conf.Configuration)
-        self.configuration.hds_hnas_nfs_config_file = self.config_file
-        self.configuration.nfs_shares_config = self.shares_file
+        self.configuration.hds_hnas_nfs_config_file = self.config_file.name
+        self.configuration.nfs_shares_config = self.shares_file.name
         self.configuration.nfs_mount_point_base = '/opt/stack/cinder/mnt'
         self.configuration.nfs_mount_options = None
         self.configuration.nas_ip = None
@@ -176,13 +179,8 @@ class HDSNFSDriverTest(test.TestCase):
 
         self.driver = nfs.HDSNFSDriver(configuration=self.configuration)
         self.driver.do_setup("")
-        self.addCleanup(self._clean)
-
-    def _clean(self):
-        os.remove(self.config_file)
-        os.remove(self.shares_file)
 
-    @mock.patch('__builtin__.open')
+    @mock.patch('six.moves.builtins.open')
     @mock.patch.object(os, 'access')
     def test_read_config(self, m_access, m_open):
         # Test exception when file is not found
diff --git a/tox.ini b/tox.ini
index 7f850bcb0e0b981f902a261329ef0798b2aa007b..76e54e6905bd9641da3636cc9ad23f5f0754bc3c 100644 (file)
--- a/tox.ini
+++ b/tox.ini
@@ -65,6 +65,9 @@ commands =
     cinder.tests.unit.test_hitachi_hbsd_horcm_fc \
     cinder.tests.unit.test_hitachi_hbsd_snm2_fc \
     cinder.tests.unit.test_hitachi_hbsd_snm2_iscsi \
+    cinder.tests.unit.test_hitachi_hnas_backend \
+    cinder.tests.unit.test_hitachi_hnas_iscsi \
+    cinder.tests.unit.test_hitachi_hnas_nfs \
     cinder.tests.unit.test_hp_xp_fc \
     cinder.tests.unit.test_hplefthand \
     cinder.tests.unit.test_huawei_drivers \