From a30a6b23207e8affe649f0849e05e9d0c938b85d Mon Sep 17 00:00:00 2001 From: Lin Yang Date: Mon, 17 Dec 2018 17:15:17 -0800 Subject: [PATCH] Add PowerZone reference instance in chassis Change-Id: I3fe84d3f2dbbbfc112a0d3d7e6739912ead75edf --- rsd_lib/resources/v2_1/chassis/chassis.py | 17 ++++++ .../resources/v2_1/chassis/test_chassis.py | 60 ++++++++++++++++++- 2 files changed, 76 insertions(+), 1 deletion(-) diff --git a/rsd_lib/resources/v2_1/chassis/chassis.py b/rsd_lib/resources/v2_1/chassis/chassis.py index 81e5bc7..1a86b88 100644 --- a/rsd_lib/resources/v2_1/chassis/chassis.py +++ b/rsd_lib/resources/v2_1/chassis/chassis.py @@ -16,6 +16,7 @@ from sushy.resources import base from sushy import utils +from rsd_lib.resources.v2_1.chassis import power_zone from rsd_lib import utils as rsd_lib_utils @@ -132,6 +133,22 @@ class Chassis(base.ResourceBase): """ super(Chassis, self).__init__(connector, identity, redfish_version) + def _get_power_zone_collection_path(self): + """Helper function to find the PowerZoneCollection path""" + return utils.get_sub_resource_path_by(self, 'PowerZones') + + @property + @utils.cache_it + def power_zones(self): + """Property to provide reference to `PowerZoneCollection` instance + + It is calculated once when it is queried for the first time. On + refresh, this property is reset. + """ + return power_zone.PowerZoneCollection( + self._conn, self._get_power_zone_collection_path(), + redfish_version=self.redfish_version) + def update(self, asset_tag=None, location_id=None): """Update AssetTag and Location->Id properties diff --git a/rsd_lib/tests/unit/resources/v2_1/chassis/test_chassis.py b/rsd_lib/tests/unit/resources/v2_1/chassis/test_chassis.py index 650f76d..7d8a941 100644 --- a/rsd_lib/tests/unit/resources/v2_1/chassis/test_chassis.py +++ b/rsd_lib/tests/unit/resources/v2_1/chassis/test_chassis.py @@ -11,12 +11,13 @@ # under the License. import json - import mock +from sushy import exceptions from sushy.tests.unit import base from rsd_lib.resources.v2_1.chassis import chassis +from rsd_lib.resources.v2_1.chassis import power_zone class TestChassis(base.TestCase): @@ -77,6 +78,63 @@ class TestChassis(base.TestCase): self.assertEqual('Unique ID', self.chassis_inst.oem.uuid) self.assertEqual('54.348103, 18.645172', self.chassis_inst.oem.geo_tag) + def test__get_power_zone_collection_path(self): + expected = '/redfish/v1/Chassis/Rack1/PowerZones' + result = self.chassis_inst._get_power_zone_collection_path() + self.assertEqual(expected, result) + + def test__get_power_zone_collection_path_missing_processors_attr(self): + self.chassis_inst._json.pop('PowerZones') + self.assertRaisesRegex( + exceptions.MissingAttributeError, 'attribute PowerZones', + self.chassis_inst._get_power_zone_collection_path) + + def test_power_zones(self): + # | GIVEN | + self.conn.get.return_value.json.reset_mock() + with open('rsd_lib/tests/unit/json_samples/v2_1/' + 'power_zone_collection.json', 'r') as f: + self.conn.get.return_value.json.return_value = json.loads(f.read()) + # | WHEN | + actual_power_zones = self.chassis_inst.power_zones + # | THEN | + self.assertIsInstance(actual_power_zones, + power_zone.PowerZoneCollection) + 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_power_zones, + self.chassis_inst.power_zones) + self.conn.get.return_value.json.assert_not_called() + + def test_power_zones_on_refresh(self): + # | GIVEN | + with open('rsd_lib/tests/unit/json_samples/v2_1/' + 'power_zone_collection.json', 'r') as f: + self.conn.get.return_value.json.return_value = json.loads(f.read()) + # | WHEN & THEN | + self.assertIsInstance(self.chassis_inst.power_zones, + power_zone.PowerZoneCollection) + + # On refreshing the chassis instance... + with open('rsd_lib/tests/unit/json_samples/v2_1/' + 'chassis.json', 'r') as f: + self.conn.get.return_value.json.return_value = json.loads(f.read()) + + self.chassis_inst.invalidate() + self.chassis_inst.refresh(force=False) + + # | GIVEN | + with open('rsd_lib/tests/unit/json_samples/v2_1/' + 'power_zone_collection.json', 'r') as f: + self.conn.get.return_value.json.return_value = json.loads(f.read()) + # | WHEN & THEN | + self.assertIsInstance(self.chassis_inst.power_zones, + power_zone.PowerZoneCollection) + def test_update(self): self.chassis_inst.update(asset_tag='Rack#1', location_id='1234') self.chassis_inst._conn.patch.assert_called_once_with(