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
This commit is contained in:
Victor Stinner
2015-06-30 16:59:21 +02:00
parent ba1ae4ada0
commit d24d075242
4 changed files with 22 additions and 24 deletions

View 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')

View 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

View 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

View File

@@ -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 \