Add drive instance in storage service

Change-Id: I8d0036698e0e0168095cd7f44cbe12e471dc4941
This commit is contained in:
Lin Yang 2018-05-07 16:05:45 -07:00
parent ea8902d432
commit bcd579f5bd
2 changed files with 91 additions and 4 deletions

View File

@ -18,6 +18,7 @@ import logging
from sushy import exceptions
from sushy.resources import base
from rsd_lib.resources.v2_3.storage_service import drive
from rsd_lib.resources.v2_3.storage_service import storage_pool
from rsd_lib.resources.v2_3.storage_service import volume
from rsd_lib import utils
@ -45,9 +46,11 @@ class StorageService(base.ResourceBase):
status = StatusField('Status')
"""The storage service status"""
_volumes = None # ref to Volumes instance
_volumes = None # ref to Volumes collection
_storage_pools = None # ref to StoragePools instance
_storage_pools = None # ref to StoragePool collection
_drives = None # ref to Drive collection
def __init__(self, connector, identity, redfish_version=None):
"""A class representing a StorageService
@ -83,7 +86,7 @@ class StorageService(base.ResourceBase):
return self._volumes
def _get_storage_pool_collection_path(self):
"""Helper function to find the StoragePoolsCollection path"""
"""Helper function to find the StoragePoolCollection path"""
storage_pool_col = self.json.get('StoragePools')
if not storage_pool_col:
raise exceptions.MissingAttributeError(attribute='StoragePools',
@ -92,7 +95,7 @@ class StorageService(base.ResourceBase):
@property
def storage_pools(self):
"""Property to provide reference to `StoragePoolsCollection` instance
"""Property to provide reference to `StoragePoolCollection` instance
It is calculated once when it is queried for the first time. On
refresh, this property is reset.
@ -104,10 +107,33 @@ class StorageService(base.ResourceBase):
return self._storage_pools
def _get_drive_collection_path(self):
"""Helper function to find the DriveCollection path"""
drive_col = self.json.get('Drives')
if not drive_col:
raise exceptions.MissingAttributeError(attribute='Drives',
resource=self._path)
return utils.get_resource_identity(drive_col)
@property
def drives(self):
"""Property to provide reference to `DriveCollection` instance
It is calculated once when it is queried for the first time. On
refresh, this property is reset.
"""
if self._drives is None:
self._drives = drive.DriveCollection(
self._conn, self._get_drive_collection_path(),
redfish_version=self.redfish_version)
return self._drives
def refresh(self):
super(StorageService, self).refresh()
self._volumes = None
self._storage_pools = None
self._drives = None
class StorageServiceCollection(base.ResourceCollectionBase):

View File

@ -19,6 +19,7 @@ import testtools
from sushy import exceptions
from rsd_lib.resources.v2_3.storage_service import drive
from rsd_lib.resources.v2_3.storage_service import storage_pool
from rsd_lib.resources.v2_3.storage_service import storage_service
from rsd_lib.resources.v2_3.storage_service import volume
@ -169,6 +170,66 @@ class StorageServiceTestCase(testtools.TestCase):
self.assertIsInstance(self.storage_service_inst.storage_pools,
storage_pool.StoragePoolCollection)
def test__get_drive_collection_path(self):
expected = '/redfish/v1/StorageServices/NVMeoE1/Drives'
result = self.storage_service_inst._get_drive_collection_path()
self.assertEqual(expected, result)
def test__get_drive_collection_path_missing_processors_attr(self):
self.storage_service_inst._json.pop('Drives')
self.assertRaisesRegex(
exceptions.MissingAttributeError, 'attribute Drives',
self.storage_service_inst._get_drive_collection_path)
def test_drives(self):
# check for the underneath variable value
self.assertIsNone(self.storage_service_inst._drives)
# | GIVEN |
self.conn.get.return_value.json.reset_mock()
with open('rsd_lib/tests/unit/json_samples/v2_3/'
'drive_collection.json', 'r') as f:
self.conn.get.return_value.json.return_value = json.loads(f.read())
# | WHEN |
actual_drives = self.storage_service_inst.drives
# | THEN |
self.assertIsInstance(actual_drives,
drive.DriveCollection)
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_drives,
self.storage_service_inst.drives)
self.conn.get.return_value.json.assert_not_called()
def test_drives_on_refresh(self):
# | GIVEN |
with open('rsd_lib/tests/unit/json_samples/v2_3/'
'drive_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.drives,
drive.DriveCollection)
# 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._drives)
# | GIVEN |
with open('rsd_lib/tests/unit/json_samples/v2_3/'
'drive_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.drives,
drive.DriveCollection)
class StorageServiceCollectionTestCase(testtools.TestCase):