From 9a178f525405a68dc162e443c1a1c126260be945 Mon Sep 17 00:00:00 2001
From: Alyson Rosa <alyson.rodrigues.rosa@gmail.com>
Date: Mon, 8 Aug 2016 08:49:15 -0300
Subject: [PATCH] Add dedupe report in HNAS driver

HNAS backend has support for enabling dedupe in file systems. This
commit adds a report for this capability in Manila HNAS driver.

DocImpact
Closes-Bug: #1610956
Change-Id: I6f83da1e51cfdcd8d20b607619c131d13f9e513f
---
 ...hare_back_ends_feature_support_mapping.rst |  2 +-
 manila/share/drivers/hitachi/hds_hnas.py      |  5 ++--
 manila/share/drivers/hitachi/ssh.py           |  4 ++-
 .../share/drivers/hitachi/test_hds_hnas.py    |  7 ++---
 .../tests/share/drivers/hitachi/test_ssh.py   | 26 ++++++++++++++++++-
 ...-support-hnas-driver-017d2f2a93a8b487.yaml |  5 ++++
 6 files changed, 41 insertions(+), 8 deletions(-)
 create mode 100644 releasenotes/notes/dedupe-support-hnas-driver-017d2f2a93a8b487.yaml

diff --git a/doc/source/devref/share_back_ends_feature_support_mapping.rst b/doc/source/devref/share_back_ends_feature_support_mapping.rst
index b631849321..8329b9a148 100644
--- a/doc/source/devref/share_back_ends_feature_support_mapping.rst
+++ b/doc/source/devref/share_back_ends_feature_support_mapping.rst
@@ -180,7 +180,7 @@ Mapping of share drivers and common capabilities
 +----------------------------------------+-----------+------------+--------+-------------+-------------------+--------------------+-----+
 |                  HDFS                  |     \-    |      K     |   \-   |      \-     |        \-         |          L         | \-  |
 +----------------------------------------+-----------+------------+--------+-------------+-------------------+--------------------+-----+
-|              Hitachi HNAS              |     \-    |      L     |   \-   |      \-     |         L         |         \-         | \-  |
+|              Hitachi HNAS              |     \-    |      L     |   N    |      \-     |         L         |         \-         | \-  |
 +----------------------------------------+-----------+------------+--------+-------------+-------------------+--------------------+-----+
 |                HPE 3PAR                |     L     |      K     |   L    |      \-     |         L         |          L         | \-  |
 +----------------------------------------+-----------+------------+--------+-------------+-------------------+--------------------+-----+
diff --git a/manila/share/drivers/hitachi/hds_hnas.py b/manila/share/drivers/hitachi/hds_hnas.py
index 6f67b1b43c..bfb930552d 100644
--- a/manila/share/drivers/hitachi/hds_hnas.py
+++ b/manila/share/drivers/hitachi/hds_hnas.py
@@ -420,7 +420,7 @@ class HDSHNASDriver(driver.ShareDriver):
 
         self._check_fs_mounted()
 
-        total_space, free_space = self.hnas.get_stats()
+        total_space, free_space, dedupe = self.hnas.get_stats()
 
         reserved = self.configuration.safe_get('reserved_share_percentage')
 
@@ -435,6 +435,7 @@ class HDSHNASDriver(driver.ShareDriver):
             'reserved_percentage': reserved,
             'qos': False,
             'thin_provisioning': True,
+            'dedupe': dedupe,
         }
 
         LOG.info(_LI("HNAS Capabilities: %(data)s."),
@@ -661,7 +662,7 @@ class HDSHNASDriver(driver.ShareDriver):
         self._ensure_share(share, hnas_share_id)
 
         old_size = share['size']
-        total, available_space = self.hnas.get_stats()
+        available_space = self.hnas.get_stats()[1]
 
         LOG.debug("Available space in filesystem: %(space)sG.",
                   {'space': available_space})
diff --git a/manila/share/drivers/hitachi/ssh.py b/manila/share/drivers/hitachi/ssh.py
index 83d0cd2eec..8682fc2471 100644
--- a/manila/share/drivers/hitachi/ssh.py
+++ b/manila/share/drivers/hitachi/ssh.py
@@ -53,6 +53,7 @@ class HNASSSHBackend(object):
         :returns:
         fs_capacity.size = Total size from filesystem.
         available_space = Free space currently on filesystem.
+        dedupe = True if dedupe is enabled on filesystem.
         """
         command = ['df', '-a', '-f', self.fs_name]
         output, err = self._execute(command)
@@ -60,7 +61,7 @@ class HNASSSHBackend(object):
         line = output.split('\n')
         fs = Filesystem(line[3])
         available_space = fs.size - fs.used
-        return fs.size, available_space
+        return fs.size, available_space, fs.dedupe
 
     def nfs_export_add(self, share_id):
         path = '/shares/' + share_id
@@ -612,6 +613,7 @@ class Filesystem(object):
                 self.used_measure = items[6]
                 if self.used_measure == 'TB':
                     self.used = self.used * units.Ki
+            self.dedupe = 'dedupe enabled' in data
 
 
 class Quota(object):
diff --git a/manila/tests/share/drivers/hitachi/test_hds_hnas.py b/manila/tests/share/drivers/hitachi/test_hds_hnas.py
index 9c6f9d462e..ba50eef52d 100644
--- a/manila/tests/share/drivers/hitachi/test_hds_hnas.py
+++ b/manila/tests/share/drivers/hitachi/test_hds_hnas.py
@@ -547,7 +547,7 @@ class HDSHNASTestCase(test.TestCase):
 
     def test_extend_share(self):
         self.mock_object(ssh.HNASSSHBackend, "get_stats", mock.Mock(
-            return_value=(500, 200)))
+            return_value=(500, 200, True)))
         self.mock_object(ssh.HNASSSHBackend, "modify_quota", mock.Mock())
 
         self._driver.extend_share(share_nfs, 150)
@@ -558,7 +558,7 @@ class HDSHNASTestCase(test.TestCase):
 
     def test_extend_share_with_no_available_space_in_fs(self):
         self.mock_object(ssh.HNASSSHBackend, "get_stats", mock.Mock(
-            return_value=(500, 200)))
+            return_value=(500, 200, False)))
         self.mock_object(ssh.HNASSSHBackend, "modify_quota", mock.Mock())
 
         self.assertRaises(exception.HNASBackendException,
@@ -729,10 +729,11 @@ class HDSHNASTestCase(test.TestCase):
             'reserved_percentage': hds_hnas.CONF.reserved_share_percentage,
             'qos': False,
             'thin_provisioning': True,
+            'dedupe': True,
         }
 
         self.mock_object(ssh.HNASSSHBackend, 'get_stats', mock.Mock(
-            return_value=(1000, 200)))
+            return_value=(1000, 200, True)))
         self.mock_object(hds_hnas.HDSHNASDriver, "_check_fs_mounted",
                          mock.Mock())
         self.mock_object(manila.share.driver.ShareDriver,
diff --git a/manila/tests/share/drivers/hitachi/test_ssh.py b/manila/tests/share/drivers/hitachi/test_ssh.py
index 39ddcd2005..c96fb1da22 100644
--- a/manila/tests/share/drivers/hitachi/test_ssh.py
+++ b/manila/tests/share/drivers/hitachi/test_ssh.py
@@ -363,6 +363,15 @@ HNAS_RESULT_df_tb = """
 18.3 GB (25%)    No                       4 KB,WFS-2,128 DSBs
 """
 
+HNAS_RESULT_df_dedupe_on = """
+  ID          Label  EVS      Size            Used  Snapshots  Deduped  \
+          Avail  Thin  ThinSize  ThinAvail              FS Type
+----  -------------  ---  --------  --------------  ---------  -------  \
+-------------  ----  --------  ---------  -------------------
+1051  FS-ManilaDev1    3.00  7.00 TB  2 TB (75%)       NA     0 B (0%)  \
+18.3 GB (25%)    No                       4 KB,WFS-2,128 DSBs,dedupe enabled
+"""
+
 HNAS_RESULT_df_unmounted = """
   ID          Label  EVS      Size            Used  Snapshots  Deduped  \
           Avail  Thin  ThinSize  ThinAvail              FS Type
@@ -505,11 +514,26 @@ class HNASSSHTestCase(test.TestCase):
         self.mock_object(ssh.HNASSSHBackend, '_execute',
                          mock.Mock(return_value=(HNAS_RESULT_df_tb, "")))
 
-        total, free = self._driver_ssh.get_stats()
+        total, free, dedupe = self._driver_ssh.get_stats()
 
         ssh.HNASSSHBackend._execute.assert_called_with(fake_list_command)
         self.assertEqual(7168.0, total)
         self.assertEqual(5120.0, free)
+        self.assertFalse(dedupe)
+
+    def test_get_stats_dedupe_on(self):
+        fake_list_command = ['df', '-a', '-f', self.fs_name]
+
+        self.mock_object(
+            ssh.HNASSSHBackend, '_execute',
+            mock.Mock(return_value=(HNAS_RESULT_df_dedupe_on, "")))
+
+        total, free, dedupe = self._driver_ssh.get_stats()
+
+        ssh.HNASSSHBackend._execute.assert_called_with(fake_list_command)
+        self.assertEqual(7168.0, total)
+        self.assertEqual(5120.0, free)
+        self.assertTrue(dedupe)
 
     def test_nfs_export_add(self):
         fake_nfs_command = ['nfs-export', 'add', '-S', 'disable', '-c',
diff --git a/releasenotes/notes/dedupe-support-hnas-driver-017d2f2a93a8b487.yaml b/releasenotes/notes/dedupe-support-hnas-driver-017d2f2a93a8b487.yaml
new file mode 100644
index 0000000000..0049f0470f
--- /dev/null
+++ b/releasenotes/notes/dedupe-support-hnas-driver-017d2f2a93a8b487.yaml
@@ -0,0 +1,5 @@
+---
+fixes:
+  - Hitachi HNAS driver now reports ``dedupe`` capability and
+    it can be used in extra-specs to choose a HNAS file system
+    that has dedupe enabled when creating a manila share on HNAS.