Add volume reference instance in storage service

Change-Id: I3e625b40f7b1a4b396ca059b0e069196c80ca073
This commit is contained in:
Lin Yang 2018-04-09 23:08:23 -07:00
parent 59b8cf2546
commit cd30367e1c
2 changed files with 96 additions and 1 deletions

View File

@ -15,8 +15,12 @@
import logging
from sushy import exceptions
from sushy.resources import base
from rsd_lib.resources.v2_3.storage_service import volume
from rsd_lib import utils
LOG = logging.getLogger(__name__)
@ -40,6 +44,8 @@ class StorageService(base.ResourceBase):
status = StatusField('Status')
"""The storage service status"""
_volumes = None # ref to Volumes instance
def __init__(self, connector, identity, redfish_version=None):
"""A class representing a StorageService
@ -51,6 +57,32 @@ class StorageService(base.ResourceBase):
super(StorageService, self).__init__(connector, identity,
redfish_version)
def _get_volume_collection_path(self):
"""Helper function to find the VolumeCollection path"""
volume_col = self.json.get('Volumes')
if not volume_col:
raise exceptions.MissingAttributeError(attribute='Volumes',
resource=self._path)
return utils.get_resource_identity(volume_col)
@property
def volumes(self):
"""Property to provide reference to `VolumeCollection` instance
It is calculated once when it is queried for the first time. On
refresh, this property is reset.
"""
if self._volumes is None:
self._volumes = volume.VolumeCollection(
self._conn, self._get_volume_collection_path(),
redfish_version=self.redfish_version)
return self._volumes
def refresh(self):
super(StorageService, self).refresh()
self._volumes = None
class StorageServiceCollection(base.ResourceCollectionBase):

View File

@ -14,11 +14,13 @@
# under the License.
import json
import mock
import testtools
from sushy import exceptions
from rsd_lib.resources.v2_3.storage_service import storage_service
from rsd_lib.resources.v2_3.storage_service import volume
class StorageServiceTestCase(testtools.TestCase):
@ -44,6 +46,67 @@ class StorageServiceTestCase(testtools.TestCase):
self.assertEqual('Enabled', self.storage_service_inst.status.state)
self.assertEqual('OK', self.storage_service_inst.status.health)
self.assertEqual('OK', self.storage_service_inst.status.health_rollup)
self.assertIsNone(self.storage_service_inst._volumes)
def test__get_volume_collection_path(self):
expected = '/redfish/v1/StorageServices/1/Volumes'
result = self.storage_service_inst._get_volume_collection_path()
self.assertEqual(expected, result)
def test__get_volume_collection_path_missing_processors_attr(self):
self.storage_service_inst._json.pop('Volumes')
self.assertRaisesRegex(
exceptions.MissingAttributeError, 'attribute Volumes',
self.storage_service_inst._get_volume_collection_path)
def test_volumes(self):
# check for the underneath variable value
self.assertIsNone(self.storage_service_inst._volumes)
# | GIVEN |
self.conn.get.return_value.json.reset_mock()
with open('rsd_lib/tests/unit/json_samples/v2_3/'
'volume_collection.json', 'r') as f:
self.conn.get.return_value.json.return_value = json.loads(f.read())
# | WHEN |
actual_volumes = self.storage_service_inst.volumes
# | THEN |
self.assertIsInstance(actual_volumes,
volume.VolumeCollection)
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_volumes,
self.storage_service_inst.volumes)
self.conn.get.return_value.json.assert_not_called()
def test_volumes_on_refresh(self):
# | GIVEN |
with open('rsd_lib/tests/unit/json_samples/v2_3/'
'volume_collection.json', 'r') as f:
self.conn.get.return_value.json.return_value = json.loads(f.read())
# | WHEN & THEN |
self.assertIsInstance(self.storage_service_inst.volumes,
volume.VolumeCollection)
# On refreshing the storage service instance...
with open('rsd_lib/tests/unit/json_samples/v2_3/'
'storage_service.json', 'r') as f:
self.conn.get.return_value.json.return_value = json.loads(f.read())
self.storage_service_inst.refresh()
# | WHEN & THEN |
self.assertIsNone(self.storage_service_inst._volumes)
# | GIVEN |
with open('rsd_lib/tests/unit/json_samples/v2_3/'
'volume_collection.json', 'r') as f:
self.conn.get.return_value.json.return_value = json.loads(f.read())
# | WHEN & THEN |
self.assertIsInstance(self.storage_service_inst.volumes,
volume.VolumeCollection)
class StorageServiceCollectionTestCase(testtools.TestCase):