Add zone instance in fabric in RSD 2.3

Change-Id: If272e36756e1d64ca7e380e6b2f7f8f78a95b1ba
This commit is contained in:
Lin Yang 2018-06-12 21:50:02 -07:00
parent f7824e2586
commit 517275b24f
2 changed files with 87 additions and 0 deletions

View File

@ -19,6 +19,7 @@ from sushy import exceptions
from sushy.resources import base
from rsd_lib.resources.v2_3.fabric import endpoint
from rsd_lib.resources.v2_3.fabric import zone
from rsd_lib import utils as rsd_lib_utils
LOG = logging.getLogger(__name__)
@ -51,6 +52,8 @@ class Fabric(base.ResourceBase):
_endpoints = None # ref to EndpointCollection instance
_zones = None # ref to ZoneCollection instance
def __init__(self, connector, identity, redfish_version=None):
"""A class representing a Fabric
@ -84,9 +87,32 @@ class Fabric(base.ResourceBase):
return self._endpoints
def _get_zone_collection_path(self):
"""Helper function to find the ZoneCollection path"""
zone_col = self.json.get('Zones')
if not zone_col:
raise exceptions.MissingAttributeError(attribute='Zones',
resource=self._path)
return rsd_lib_utils.get_resource_identity(zone_col)
@property
def zones(self):
"""Property to provide reference to `ZoneCollection` instance
It is calculated once when it is queried for the first time. On
refresh, this property is reset.
"""
if self._zones is None:
self._zones = zone.ZoneCollection(
self._conn, self._get_zone_collection_path(),
redfish_version=self.redfish_version)
return self._zones
def refresh(self):
super(Fabric, self).refresh()
self._endpoints = None
self._zones = None
class FabricCollection(base.ResourceCollectionBase):

View File

@ -21,6 +21,7 @@ from sushy import exceptions
from rsd_lib.resources.v2_3.fabric import endpoint
from rsd_lib.resources.v2_3.fabric import fabric
from rsd_lib.resources.v2_3.fabric import zone
class FabricTestCase(testtools.TestCase):
@ -107,6 +108,66 @@ class FabricTestCase(testtools.TestCase):
self.assertIsInstance(self.fabric_inst.endpoints,
endpoint.EndpointCollection)
def test__get_zone_collection_path(self):
expected = '/redfish/v1/Fabrics/NVMeoE/Zones'
result = self.fabric_inst._get_zone_collection_path()
self.assertEqual(expected, result)
def test__get_zone_collection_path_missing_attr(self):
self.fabric_inst._json.pop('Zones')
self.assertRaisesRegex(
exceptions.MissingAttributeError, 'attribute Zones',
self.fabric_inst._get_zone_collection_path)
def test_zones(self):
# check for the underneath variable value
self.assertIsNone(self.fabric_inst._zones)
# | GIVEN |
self.conn.get.return_value.json.reset_mock()
with open('rsd_lib/tests/unit/json_samples/v2_3/'
'zone_collection.json', 'r') as f:
self.conn.get.return_value.json.return_value = json.loads(f.read())
# | WHEN |
actual_zones = self.fabric_inst.zones
# | THEN |
self.assertIsInstance(actual_zones,
zone.ZoneCollection)
self.conn.get.return_value.json.assert_called_once_with()
# reset mock
self.conn.get.return_value.json.reset_mock()
# | WHEN & THEN |
# tests for same object on invoking subsequently
self.assertIs(actual_zones,
self.fabric_inst.zones)
self.conn.get.return_value.json.assert_not_called()
def test_zones_on_refresh(self):
# | GIVEN |
with open('rsd_lib/tests/unit/json_samples/v2_3/'
'zone_collection.json', 'r') as f:
self.conn.get.return_value.json.return_value = json.loads(f.read())
# | WHEN & THEN |
self.assertIsInstance(self.fabric_inst.zones,
zone.ZoneCollection)
# On refreshing the fabric instance...
with open('rsd_lib/tests/unit/json_samples/v2_3/'
'fabric.json', 'r') as f:
self.conn.get.return_value.json.return_value = json.loads(f.read())
self.fabric_inst.refresh()
# | WHEN & THEN |
self.assertIsNone(self.fabric_inst._zones)
# | GIVEN |
with open('rsd_lib/tests/unit/json_samples/v2_3/'
'zone_collection.json', 'r') as f:
self.conn.get.return_value.json.return_value = json.loads(f.read())
# | WHEN & THEN |
self.assertIsInstance(self.fabric_inst.zones,
zone.ZoneCollection)
class FabricCollectionTestCase(testtools.TestCase):