From 2c49f5ddfcb358c04bf581fc78662877c7d22738 Mon Sep 17 00:00:00 2001
From: Victor Stinner <vstinner@redhat.com>
Date: Sun, 8 Nov 2015 22:04:45 +0100
Subject: [PATCH] Port vzstorage to Python 3

* vzstorage: replace a/b with a//b to use integer division on
  Python 3.
* fix os.path.exists mock: only override return for path /fake
  (shares config)
* tests-py3.txt: add cinder.tests.unit.test_vzstorage

Note: remove also spaces in parameters in two functions calls to
respect the PEP 8.

Partial-Implements: blueprint cinder-python3
Change-Id: I7cee7009d8bc87cd0294e90cb2967f4560276994
---
 cinder/tests/unit/test_vzstorage.py | 29 +++++++++++++++++++++--------
 cinder/volume/drivers/vzstorage.py  |  2 +-
 tests-py3.txt                       |  1 +
 3 files changed, 23 insertions(+), 9 deletions(-)

diff --git a/cinder/tests/unit/test_vzstorage.py b/cinder/tests/unit/test_vzstorage.py
index 50332ae4674..cb05922eebe 100644
--- a/cinder/tests/unit/test_vzstorage.py
+++ b/cinder/tests/unit/test_vzstorage.py
@@ -27,6 +27,9 @@ from cinder import test
 from cinder.volume.drivers import vzstorage
 
 
+_orig_path_exists = os.path.exists
+
+
 class VZStorageTestCase(test.TestCase):
 
     _FAKE_SHARE = "10.0.0.1,10.0.0.2:/cluster123:123123"
@@ -69,21 +72,31 @@ class VZStorageTestCase(test.TestCase):
         self._vz_driver._execute = mock.Mock()
         self._vz_driver.base = self._FAKE_MNT_BASE
 
+    def _path_exists(self, path):
+        if path.startswith(self._FAKE_VZ_CONFIG.vzstorage_shares_config):
+            return True
+        return _orig_path_exists(path)
+
+    def _path_dont_exists(self, path):
+        if path.startswith('/fake'):
+            return False
+        return _orig_path_exists(path)
+
     @mock.patch('os.path.exists')
     def test_setup_ok(self, mock_exists):
-        mock_exists.return_value = True
+        mock_exists.side_effect = self._path_exists
         self._vz_driver.do_setup(mock.sentinel.context)
 
     @mock.patch('os.path.exists')
     def test_setup_missing_shares_conf(self, mock_exists):
-        mock_exists.return_value = False
+        mock_exists.side_effect = self._path_dont_exists
         self.assertRaises(exception.VzStorageException,
                           self._vz_driver.do_setup,
                           mock.sentinel.context)
 
     @mock.patch('os.path.exists')
     def test_setup_invalid_usage_ratio(self, mock_exists):
-        mock_exists.return_value = True
+        mock_exists.side_effect = self._path_exists
         self._vz_driver.configuration.vzstorage_used_ratio = 1.2
         self.assertRaises(exception.VzStorageException,
                           self._vz_driver.do_setup,
@@ -91,7 +104,7 @@ class VZStorageTestCase(test.TestCase):
 
     @mock.patch('os.path.exists')
     def test_setup_invalid_usage_ratio2(self, mock_exists):
-        mock_exists.return_value = True
+        mock_exists.side_effect = self._path_exists
         self._vz_driver.configuration.vzstorage_used_ratio = 0
         self.assertRaises(exception.VzStorageException,
                           self._vz_driver.do_setup,
@@ -99,7 +112,7 @@ class VZStorageTestCase(test.TestCase):
 
     @mock.patch('os.path.exists')
     def test_setup_invalid_mount_point_base(self, mock_exists):
-        mock_exists.return_value = True
+        mock_exists.side_effect = self._path_exists
         conf = copy.copy(self._FAKE_VZ_CONFIG)
         conf.vzstorage_mount_point_base = './tmp'
         vz_driver = vzstorage.VZStorageDriver(configuration=conf)
@@ -109,7 +122,7 @@ class VZStorageTestCase(test.TestCase):
 
     @mock.patch('os.path.exists')
     def test_setup_no_vzstorage(self, mock_exists):
-        mock_exists.return_value = True
+        mock_exists.side_effect = self._path_exists
         exc = OSError()
         exc.errno = errno.ENOENT
         self._vz_driver._execute.side_effect = exc
@@ -166,7 +179,7 @@ class VZStorageTestCase(test.TestCase):
         drv = self._vz_driver
         cap_info = (100 * units.Gi, 40 * units.Gi, 60 * units.Gi)
         with mock.patch.object(drv, '_get_capacity_info',
-                               return_value = cap_info):
+                               return_value=cap_info):
             ret = drv._is_share_eligible(self._FAKE_SHARE, 50)
             self.assertFalse(ret)
 
@@ -174,7 +187,7 @@ class VZStorageTestCase(test.TestCase):
         drv = self._vz_driver
         cap_info = (100 * units.Gi, 40 * units.Gi, 60 * units.Gi)
         with mock.patch.object(drv, '_get_capacity_info',
-                               return_value = cap_info):
+                               return_value=cap_info):
             ret = drv._is_share_eligible(self._FAKE_SHARE, 30)
             self.assertTrue(ret)
 
diff --git a/cinder/volume/drivers/vzstorage.py b/cinder/volume/drivers/vzstorage.py
index f1659004f08..aaa304e0fed 100644
--- a/cinder/volume/drivers/vzstorage.py
+++ b/cinder/volume/drivers/vzstorage.py
@@ -219,7 +219,7 @@ class VZStorageDriver(remotefs_drv.RemoteFSSnapDriver):
 
         total_size, available, allocated = self._get_capacity_info(vz_share)
 
-        if (allocated + volume_size) / total_size > used_ratio:
+        if (allocated + volume_size) // total_size > used_ratio:
             LOG.debug('_is_share_eligible: %s is above '
                       'vzstorage_used_ratio.', vz_share)
             return False
diff --git a/tests-py3.txt b/tests-py3.txt
index 8e07e9e3cb1..179e93af11d 100644
--- a/tests-py3.txt
+++ b/tests-py3.txt
@@ -120,6 +120,7 @@ cinder.tests.unit.test_volume_transfer
 cinder.tests.unit.test_volume_types
 cinder.tests.unit.test_volume_types_extra_specs
 cinder.tests.unit.test_volume_utils
+cinder.tests.unit.test_vzstorage
 cinder.tests.unit.volume.drivers.emc.scaleio
 cinder.tests.unit.volume.flows.test_create_volume_flow
 cinder.tests.unit.windows.test_smbfs