From: Victor Stinner Date: Tue, 30 Jun 2015 14:59:21 +0000 (+0200) Subject: Fix Python 3 issues in Hitachi HNAS tests X-Git-Url: https://review.fuel-infra.org/gitweb?a=commitdiff_plain;h=d24d07524253e31748bd436b9c1be76d18490c3d;p=openstack-build%2Fcinder-build.git Fix Python 3 issues in Hitachi HNAS tests * 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 --- diff --git a/cinder/tests/unit/test_hitachi_hnas_backend.py b/cinder/tests/unit/test_hitachi_hnas_backend.py index 26e9b9735..7c8a4940d 100644 --- a/cinder/tests/unit/test_hitachi_hnas_backend.py +++ b/cinder/tests/unit/test_hitachi_hnas_backend.py @@ -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') diff --git a/cinder/tests/unit/test_hitachi_hnas_iscsi.py b/cinder/tests/unit/test_hitachi_hnas_iscsi.py index 59dbed844..b38842290 100644 --- a/cinder/tests/unit/test_hitachi_hnas_iscsi.py +++ b/cinder/tests/unit/test_hitachi_hnas_iscsi.py @@ -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 diff --git a/cinder/tests/unit/test_hitachi_hnas_nfs.py b/cinder/tests/unit/test_hitachi_hnas_nfs.py index e17bc0e3d..ac01c8d06 100644 --- a/cinder/tests/unit/test_hitachi_hnas_nfs.py +++ b/cinder/tests/unit/test_hitachi_hnas_nfs.py @@ -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 7f850bcb0..76e54e690 100644 --- 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 \