Mark endpoints in zone as property attribute

Change-Id: I2974c47eebd82bb551c2c01c699ab6c69b28b754
This commit is contained in:
Lin Yang 2018-06-12 17:41:34 -07:00
parent 8b46825c09
commit e16a2af697
2 changed files with 58 additions and 8 deletions

View File

@ -63,13 +63,23 @@ class Zone(base.ResourceBase):
super(Zone, self).__init__(connector, identity,
redfish_version)
def get_endpoints(self):
@property
def endpoints(self):
"""Return a list of Endpoints present in the Zone
:returns: A list of Endpoint objects
It is calculated once when it is queried for the first time. On
refresh, this property is reset.
"""
return [endpoint.Endpoint(self._conn, id_, self.redfish_version) for
id_ in self.links.endpoint_identities]
if self._endpoints is None:
self._endpoints = [
endpoint.Endpoint(self._conn, id_, self.redfish_version)
for id_ in self.links.endpoint_identities]
return self._endpoints
def refresh(self):
super(Zone, self).refresh()
self._endpoints = None
def update(self, endpoints):
"""Add or remove Endpoints from a Zone

View File

@ -48,14 +48,54 @@ class ZoneTestCase(testtools.TestCase):
self.assertEqual('Enabled', self.zone_inst.status.state)
self.assertEqual('OK', self.zone_inst.status.health)
def test_get_endpoints(self):
def test_endpoints(self):
# check for the underneath variable value
self.assertIsNone(self.zone_inst._endpoints)
# | GIVEN |
self.conn.get.return_value.json.reset_mock()
with open('rsd_lib/tests/unit/json_samples/v2_1/'
'endpoint.json', 'r') as f:
self.conn.get.return_value.json.return_value = json.loads(f.read())
endpoints = self.zone_inst.get_endpoints()
self.assertEqual('NVMeDrivePF1', endpoints[0].identity)
self.assertEqual(2, len(endpoints))
# | WHEN |
actual_endpoints = self.zone_inst.endpoints
# | THEN |
self.assertEqual('NVMeDrivePF1', actual_endpoints[0].identity)
self.assertEqual(2, len(actual_endpoints))
self.conn.get.return_value.json.assert_called_with()
# reset mock
self.conn.get.return_value.json.reset_mock()
# | WHEN & THEN |
# tests for same object on invoking subsequently
self.assertIs(actual_endpoints,
self.zone_inst.endpoints)
self.conn.get.return_value.json.assert_not_called()
def test_endpoints_on_refresh(self):
# | GIVEN |
with open('rsd_lib/tests/unit/json_samples/v2_1/'
'endpoint.json', 'r') as f:
self.conn.get.return_value.json.return_value = json.loads(f.read())
# | WHEN & THEN |
self.assertEqual('NVMeDrivePF1', self.zone_inst.endpoints[0].identity)
self.assertEqual(2, len(self.zone_inst.endpoints))
# On refreshing the fabric instance...
with open('rsd_lib/tests/unit/json_samples/v2_1/'
'zone.json', 'r') as f:
self.conn.get.return_value.json.return_value = json.loads(f.read())
self.zone_inst.refresh()
# | WHEN & THEN |
self.assertIsNone(self.zone_inst._endpoints)
# | GIVEN |
with open('rsd_lib/tests/unit/json_samples/v2_1/'
'endpoint.json', 'r') as f:
self.conn.get.return_value.json.return_value = json.loads(f.read())
# | WHEN & THEN |
self.assertEqual('NVMeDrivePF1', self.zone_inst.endpoints[0].identity)
self.assertEqual(2, len(self.zone_inst.endpoints))
def test_update(self):
self.zone_inst.update(