From 53f756d0650a815cbb9eeac2fa7bcfa2334c674a Mon Sep 17 00:00:00 2001
From: Sam Wan <sam.wan@emc.com>
Date: Wed, 5 Aug 2020 04:06:08 -0400
Subject: [PATCH] Fix wrong capacity in pool_stat for DellEMC manila drivers.

DellEMC manila drivers have bugs in returning capacity in pool_stat.
The current powermax/vnx manila return size in MB and unity manila
returns size in bytes.

Change-Id: I1a2c28548d02eee6632313a41a5c4d8ab6ad9532
Closes-bug: #1890372
Closes-bug: #1890375
Closes-bug: #1890376
---
 .../drivers/dell_emc/common/enas/utils.py      |  9 +++++++++
 .../dell_emc/plugins/powermax/connection.py    |  5 +++--
 .../dell_emc/plugins/unity/connection.py       | 10 ++++++----
 .../drivers/dell_emc/plugins/vnx/connection.py |  5 +++--
 .../drivers/dell_emc/common/enas/test_utils.py | 16 ++++++++++++++++
 .../plugins/powermax/test_connection.py        |  9 ++++++---
 .../dell_emc/plugins/unity/test_connection.py  | 18 +++++++++++-------
 .../dell_emc/plugins/vnx/test_connection.py    |  9 ++++++---
 ...c-fix-capacity-report-25f75a6c96e12b40.yaml | 10 ++++++++++
 9 files changed, 70 insertions(+), 21 deletions(-)
 create mode 100644 releasenotes/releasenotes/notes/dellemc-fix-capacity-report-25f75a6c96e12b40.yaml

diff --git a/manila/share/drivers/dell_emc/common/enas/utils.py b/manila/share/drivers/dell_emc/common/enas/utils.py
index 5a22fbecad..a1faf6e193 100644
--- a/manila/share/drivers/dell_emc/common/enas/utils.py
+++ b/manila/share/drivers/dell_emc/common/enas/utils.py
@@ -20,6 +20,7 @@ from oslo_log import log
 from oslo_utils import fnmatch
 from oslo_utils import netutils
 from oslo_utils import timeutils
+from oslo_utils import units
 import ssl
 
 CONF = cfg.CONF
@@ -179,3 +180,11 @@ def export_unc_path(ip_addr):
     if netutils.is_valid_ipv6(ip_addr):
         ip_addr = ip_addr.replace(':', '-') + unc_suffix
     return ip_addr
+
+
+def bytes_to_gb(size):
+    return round(float(size) / units.Gi, 2)
+
+
+def mb_to_gb(size):
+    return bytes_to_gb(size * units.Mi)
diff --git a/manila/share/drivers/dell_emc/plugins/powermax/connection.py b/manila/share/drivers/dell_emc/plugins/powermax/connection.py
index 8557e96bab..d0a3e7f3fe 100644
--- a/manila/share/drivers/dell_emc/plugins/powermax/connection.py
+++ b/manila/share/drivers/dell_emc/plugins/powermax/connection.py
@@ -634,8 +634,9 @@ class PowerMaxStorageConnection(driver.StorageConnection):
 
                 pool_stat = {
                     'pool_name': pool['name'],
-                    'total_capacity_gb': total_size,
-                    'free_capacity_gb': total_size - used_size,
+                    'total_capacity_gb': enas_utils.mb_to_gb(total_size),
+                    'free_capacity_gb':
+                        enas_utils.mb_to_gb(total_size - used_size),
                     'qos': False,
                     'reserved_percentage': self.reserved_percentage,
                     'snapshot_support': True,
diff --git a/manila/share/drivers/dell_emc/plugins/unity/connection.py b/manila/share/drivers/dell_emc/plugins/unity/connection.py
index 86958786de..5e1d1b1ef3 100644
--- a/manila/share/drivers/dell_emc/plugins/unity/connection.py
+++ b/manila/share/drivers/dell_emc/plugins/unity/connection.py
@@ -581,10 +581,12 @@ class UnityStorageConnection(driver.StorageConnection):
                 pool_stat = {
                     'pool_name': pool.name,
                     'thin_provisioning': True,
-                    'total_capacity_gb': total_size,
-                    'free_capacity_gb': total_size - used_size,
-                    'allocated_capacity_gb': used_size,
-                    'provisioned_capacity_gb': float(pool.size_subscribed),
+                    'total_capacity_gb': enas_utils.bytes_to_gb(total_size),
+                    'free_capacity_gb':
+                        enas_utils.bytes_to_gb(total_size - used_size),
+                    'allocated_capacity_gb': enas_utils.bytes_to_gb(used_size),
+                    'provisioned_capacity_gb':
+                        enas_utils.bytes_to_gb(pool.size_subscribed),
                     'qos': False,
                     'reserved_percentage': self.reserved_percentage,
                     'max_over_subscription_ratio':
diff --git a/manila/share/drivers/dell_emc/plugins/vnx/connection.py b/manila/share/drivers/dell_emc/plugins/vnx/connection.py
index 55335e9d57..2bd663ff29 100644
--- a/manila/share/drivers/dell_emc/plugins/vnx/connection.py
+++ b/manila/share/drivers/dell_emc/plugins/vnx/connection.py
@@ -630,8 +630,9 @@ class VNXStorageConnection(driver.StorageConnection):
 
                 pool_stat = dict(
                     pool_name=pool['name'],
-                    total_capacity_gb=total_size,
-                    free_capacity_gb=total_size - used_size,
+                    total_capacity_gb=enas_utils.mb_to_gb(total_size),
+                    free_capacity_gb=enas_utils.mb_to_gb(
+                        total_size - used_size),
                     qos=False,
                     reserved_percentage=self.reserved_percentage,
                 )
diff --git a/manila/tests/share/drivers/dell_emc/common/enas/test_utils.py b/manila/tests/share/drivers/dell_emc/common/enas/test_utils.py
index 3f908c0b37..28d0534586 100644
--- a/manila/tests/share/drivers/dell_emc/common/enas/test_utils.py
+++ b/manila/tests/share/drivers/dell_emc/common/enas/test_utils.py
@@ -140,3 +140,19 @@ class ExportUncPathTestCase(test.TestCase):
     @ddt.unpack
     def test_invalid_ipv6_addr(self, ip_addr):
         self.assertEqual(ip_addr, utils.export_unc_path(ip_addr))
+
+
+@ddt.ddt
+class SizeToGbTestCase(test.TestCase):
+
+    @ddt.data({'size_in_bytes': 1073741824, 'size_in_gb': 1.0},
+              {'size_in_bytes': 5610301030, 'size_in_gb': 5.22})
+    @ddt.unpack
+    def test_bytes_to_gb(self, size_in_bytes, size_in_gb):
+        self.assertEqual(size_in_gb, utils.bytes_to_gb(size_in_bytes))
+
+    @ddt.data({'size_in_mb': 1024, 'size_in_gb': 1.0},
+              {'size_in_mb': 5346, 'size_in_gb': 5.22})
+    @ddt.unpack
+    def test_mb_to_gb(self, size_in_mb, size_in_gb):
+        self.assertEqual(size_in_gb, utils.mb_to_gb(size_in_mb))
diff --git a/manila/tests/share/drivers/dell_emc/plugins/powermax/test_connection.py b/manila/tests/share/drivers/dell_emc/plugins/powermax/test_connection.py
index 7e25f84c4a..ca9b11bc05 100644
--- a/manila/tests/share/drivers/dell_emc/plugins/powermax/test_connection.py
+++ b/manila/tests/share/drivers/dell_emc/plugins/powermax/test_connection.py
@@ -21,6 +21,7 @@ from oslo_log import log
 
 from manila import exception
 from manila.share.drivers.dell_emc.common.enas import connector
+from manila.share.drivers.dell_emc.common.enas import utils as enas_utils
 from manila.share.drivers.dell_emc.plugins.vnx import connection
 from manila.share.drivers.dell_emc.plugins.vnx import object_manager
 from manila import test
@@ -2221,12 +2222,14 @@ class StorageConnectionTestCase(test.TestCase):
 
         for pool in fakes.STATS['pools']:
             if pool['pool_name'] == fakes.FakeData.pool_name:
-                self.assertEqual(fakes.FakeData.pool_total_size,
-                                 pool['total_capacity_gb'])
+                self.assertEqual(
+                    enas_utils.mb_to_gb(fakes.FakeData.pool_total_size),
+                    pool['total_capacity_gb'])
 
                 free_size = (fakes.FakeData.pool_total_size -
                              fakes.FakeData.pool_used_size)
-                self.assertEqual(free_size, pool['free_capacity_gb'])
+                self.assertEqual(enas_utils.mb_to_gb(free_size),
+                                 pool['free_capacity_gb'])
 
     def test_update_share_stats_without_matched_config_pools(self):
         self.connection.pools = set('fake_pool')
diff --git a/manila/tests/share/drivers/dell_emc/plugins/unity/test_connection.py b/manila/tests/share/drivers/dell_emc/plugins/unity/test_connection.py
index a016f63a47..e17207a408 100644
--- a/manila/tests/share/drivers/dell_emc/plugins/unity/test_connection.py
+++ b/manila/tests/share/drivers/dell_emc/plugins/unity/test_connection.py
@@ -14,17 +14,17 @@
 #    under the License.
 
 import copy
-from unittest import mock
-
 import ddt
-from oslo_utils import units
 import six
 
 from manila import exception
+from manila.share.drivers.dell_emc.common.enas import utils as enas_utils
 from manila import test
 from manila.tests.share.drivers.dell_emc.plugins.unity import fake_exceptions
 from manila.tests.share.drivers.dell_emc.plugins.unity import res_mock
 from manila.tests.share.drivers.dell_emc.plugins.unity import utils
+from oslo_utils import units
+from unittest import mock
 
 
 @ddt.ddt
@@ -293,14 +293,18 @@ class TestConnection(test.TestCase):
         self.assertEqual(5, len(stat_dict))
         pool = stat_dict['pools'][0]
         self.assertEqual('pool_1', pool['pool_name'])
-        self.assertEqual(500000.0, pool['total_capacity_gb'])
+        self.assertEqual(
+            enas_utils.bytes_to_gb(500000.0), pool['total_capacity_gb'])
         self.assertEqual(False, pool['qos'])
-        self.assertEqual(30000.0, pool['provisioned_capacity_gb'])
+        self.assertEqual(
+            enas_utils.bytes_to_gb(30000.0), pool['provisioned_capacity_gb'])
         self.assertEqual(20, pool['max_over_subscription_ratio'])
-        self.assertEqual(10000.0, pool['allocated_capacity_gb'])
+        self.assertEqual(
+            enas_utils.bytes_to_gb(10000.0), pool['allocated_capacity_gb'])
         self.assertEqual(0, pool['reserved_percentage'])
         self.assertTrue(pool['thin_provisioning'])
-        self.assertEqual(490000.0, pool['free_capacity_gb'])
+        self.assertEqual(
+            enas_utils.bytes_to_gb(490000.0), pool['free_capacity_gb'])
 
     @res_mock.patch_connection
     def test_update_share_stats__nonexistent_pools(self, connection):
diff --git a/manila/tests/share/drivers/dell_emc/plugins/vnx/test_connection.py b/manila/tests/share/drivers/dell_emc/plugins/vnx/test_connection.py
index b0d7318b18..5395b93dc0 100644
--- a/manila/tests/share/drivers/dell_emc/plugins/vnx/test_connection.py
+++ b/manila/tests/share/drivers/dell_emc/plugins/vnx/test_connection.py
@@ -21,6 +21,7 @@ from oslo_log import log
 
 from manila import exception
 from manila.share.drivers.dell_emc.common.enas import connector
+from manila.share.drivers.dell_emc.common.enas import utils as enas_utils
 from manila.share.drivers.dell_emc.plugins.vnx import connection
 from manila.share.drivers.dell_emc.plugins.vnx import object_manager
 from manila import test
@@ -2221,12 +2222,14 @@ class StorageConnectionTestCase(test.TestCase):
 
         for pool in fakes.STATS['pools']:
             if pool['pool_name'] == fakes.FakeData.pool_name:
-                self.assertEqual(fakes.FakeData.pool_total_size,
-                                 pool['total_capacity_gb'])
+                self.assertEqual(
+                    enas_utils.mb_to_gb(fakes.FakeData.pool_total_size),
+                    pool['total_capacity_gb'])
 
                 free_size = (fakes.FakeData.pool_total_size -
                              fakes.FakeData.pool_used_size)
-                self.assertEqual(free_size, pool['free_capacity_gb'])
+                self.assertEqual(
+                    enas_utils.mb_to_gb(free_size), pool['free_capacity_gb'])
 
     def test_update_share_stats_without_matched_config_pools(self):
         self.connection.pools = set('fake_pool')
diff --git a/releasenotes/releasenotes/notes/dellemc-fix-capacity-report-25f75a6c96e12b40.yaml b/releasenotes/releasenotes/notes/dellemc-fix-capacity-report-25f75a6c96e12b40.yaml
new file mode 100644
index 0000000000..2ab67dac7a
--- /dev/null
+++ b/releasenotes/releasenotes/notes/dellemc-fix-capacity-report-25f75a6c96e12b40.yaml
@@ -0,0 +1,10 @@
+---
+fixes:
+  - |
+    Dell EMC Manila Driver: Fixes wrong capacity in pool_stat.
+    `bug 1890372 <https://bugs.launchpad.net/manila/+bug/1890372>`_
+     powermax manila return size in MB,
+    `bug 1890375 <https://bugs.launchpad.net/manila/+bug/1890375>`_
+     vnx manila return size in MB,
+    `bug 1890376 <https://bugs.launchpad.net/manila/+bug/1890376>`_
+     unity manila return size in bytes.