Repository: zhmcclient/python-zhmcclient

Issue:
WWPN is not attaching in mock frmework

In mock framework when we create hba then it should also attach 'wwpn'. But as of now it is not attaching.
self.partition.hbas[0].get_property('wwpn')



Discussion:

Patch:
diff --git a/docs/changes.rst b/docs/changes.rst
index 3072f69..7b7c868 100644
--- a/docs/changes.rst
+++ b/docs/changes.rst
@@ -31,6 +31,8 @@ Released: not yet
 
 **Bug fixes:**
 
+* Added WWPN support in mocking framework (issue #212).
+
 **Enhancements:**
 
 **Known Issues:**
diff --git a/zhmcclient_mock/_hmc.py b/zhmcclient_mock/_hmc.py
index 961ee67..450855e 100644
--- a/zhmcclient_mock/_hmc.py
+++ b/zhmcclient_mock/_hmc.py
@@ -774,6 +774,9 @@ class FakedHbaManager(FakedBaseManager):
         if 'device-number' not in new_hba.properties:
             devno = partition.devno_alloc()
             new_hba.properties['device-number'] = devno
+        if 'wwpn' not in new_hba.properties:
+            wwpn = partition.wwpn_alloc()
+            new_hba.properties['wwpn'] = wwpn
         return new_hba
 
     def remove(self, oid):
@@ -793,6 +796,9 @@ class FakedHbaManager(FakedBaseManager):
         devno = hba.properties.get('device-number', None)
         if devno:
             partition.devno_free_if_allocated(devno)
+        wwpn = hba.properties.get('wwpn', None)
+        if wwpn:
+            partition.wwpn_free_if_allocated(wwpn)
         assert 'hba-uris' in partition.properties
         hba_uris = partition.properties['hba-uris']
         hba_uris.remove(hba.uri)
@@ -1049,6 +1055,7 @@ class FakedPartition(FakedBaseResource):
         self._virtual_functions = FakedVirtualFunctionManager(
             hmc=manager.hmc, partition=self)
         self._devno_pool = IdPool(0x8000, 0xFFFF)
+        self._wwpn_pool = IdPool(0x8000, 0xFFFF)
 
     @property
     def nics(self):
@@ -1118,6 +1125,51 @@ class FakedPartition(FakedBaseResource):
         devno_int = int(devno, 16)
         self._devno_pool.free_if_allocated(devno_int)
 
+    def wwpn_alloc(self):
+        """
+        Allocates a WWPN unique to this partition, in the range of
+        0xAFFEAFFE00008000 to 0xAFFEAFFE0000FFFF.
+
+        Returns:
+          string: The WWPN as 16 hexadecimal digits in upper case.
+
+        Raises:
+          ValueError: No more WWPNs available in that range.
+        """
+        wwpn_int = self._wwpn_pool.alloc()
+        wwpn = "AFFEAFFE0000" + "{:04X}".format(wwpn_int)
+        return wwpn
+
+    def wwpn_free(self, wwpn):
+        """
+        Free a WWPN allocated with :meth:`wwpn_alloc`.
+
+        The WWPN must be allocated.
+
+        Parameters:
+          WWPN (string): The WWPN as 16 hexadecimal digits.
+
+        Raises:
+          ValueError: WWPN not in pool range or not currently
+            allocated.
+        """
+        wwpn_int = int(wwpn[-4:], 16)
+        self._wwpn_pool.free(wwpn_int)
+
+    def wwpn_free_if_allocated(self, wwpn):
+
+        """
+        Free a WWPN allocated with :meth:`wwpn_alloc`.
+
+        If the WWPN is not currently allocated or not in the pool
+        range, nothing happens.
+
+        Parameters:
+          WWPN (string): The WWPN as 16 hexadecimal digits.
+        """
+        wwpn_int = int(wwpn[-4:], 16)
+        self._wwpn_pool.free_if_allocated(wwpn_int)
+
 
 class FakedPortManager(FakedBaseManager):
     """


Test patch:
diff --git a/tests/unit/zhmcclient_mock/test_urihandler.py b/tests/unit/zhmcclient_mock/test_urihandler.py
index eecbb13..6febc10 100755
--- a/tests/unit/zhmcclient_mock/test_urihandler.py
+++ b/tests/unit/zhmcclient_mock/test_urihandler.py
@@ -453,7 +453,7 @@ def standard_test_hmc():
                                     'description': 'HBA #1 in Partition #1',
                                     'adapter-port-uri':
                                         '/api/adapters/2/storage-ports/1',
-                                    'wwpn': 'wwpn_1',
+                                    'wwpn': 'CFFEAFFE00008001',
                                     'device-number': '1001',
                                 },
                             },
@@ -782,7 +782,7 @@ class CpcExportPortNamesListHandlerTests(unittest.TestCase):
             ]
         }
         exp_wwpn_list = [
-            'partition_1,2,1001,wwpn_1',
+            'partition_1,2,1001,CFFEAFFE00008001',
         ]
 
         # the function to be tested:
@@ -1108,7 +1108,7 @@ class HbaHandlerTests(unittest.TestCase):
             'name': 'hba_1',
             'description': 'HBA #1 in Partition #1',
             'adapter-port-uri': '/api/adapters/2/storage-ports/1',
-            'wwpn': 'wwpn_1',
+            'wwpn': 'CFFEAFFE00008001',
             'device-number': '1001',
         }
         self.assertEqual(hba1, exp_hba1)
@@ -1138,6 +1138,7 @@ class HbaHandlerTests(unittest.TestCase):
             'name': 'hba_2',
             'adapter-port-uri': '/api/adapters/2/storage-ports/1',
             'device-number': hba2['device-number'],  # auto-generated
+            'wwpn': hba2['wwpn'],  # auto-generated
         }
 
         self.assertEqual(hba2, exp_hba2)