From 00dd22ab35324f582b9ab2bb387a93f036ede1b1 Mon Sep 17 00:00:00 2001 From: monokai <2536818783@qq.com> Date: Fri, 11 Jan 2019 11:33:38 +0800 Subject: [PATCH] Add metrics resource for RSD 2.2 Change-Id: I5a175f9fd4f7a835104911e5e90a12c97bed1c7a --- .../v2_2/ethernet_switch/ethernet_switch.py | 25 ++++++++-- .../resources/v2_2/ethernet_switch/metrics.py | 30 +++++++++++ .../v2_2/ethernet_switch_metrics.json | 9 ++++ .../ethernet_switch/test_ethernet_switch.py | 50 ++++++++++++++++++- .../v2_2/ethernet_switch/test_metrics.py | 45 +++++++++++++++++ 5 files changed, 154 insertions(+), 5 deletions(-) create mode 100644 rsd_lib/resources/v2_2/ethernet_switch/metrics.py create mode 100644 rsd_lib/tests/unit/json_samples/v2_2/ethernet_switch_metrics.json create mode 100644 rsd_lib/tests/unit/resources/v2_2/ethernet_switch/test_metrics.py diff --git a/rsd_lib/resources/v2_2/ethernet_switch/ethernet_switch.py b/rsd_lib/resources/v2_2/ethernet_switch/ethernet_switch.py index 345ee6c..7d747e8 100644 --- a/rsd_lib/resources/v2_2/ethernet_switch/ethernet_switch.py +++ b/rsd_lib/resources/v2_2/ethernet_switch/ethernet_switch.py @@ -1,4 +1,4 @@ -# Copyright 2018 Intel, Inc. +# Copyright 2019 Intel, Inc. # All Rights Reserved. # # Licensed under the Apache License, Version 2.0 (the "License"); you may @@ -13,13 +13,14 @@ # License for the specific language governing permissions and limitations # under the License. -from sushy.resources import base -from sushy import utils - from rsd_lib.resources.v2_1.ethernet_switch import ethernet_switch \ as v2_1_ethernet_switch +from rsd_lib.resources.v2_2.ethernet_switch import metrics from rsd_lib.resources.v2_2.ethernet_switch import port +from sushy.resources import base +from sushy import utils + class EthernetSwitch(v2_1_ethernet_switch.EthernetSwitch): @@ -35,6 +36,22 @@ class EthernetSwitch(v2_1_ethernet_switch.EthernetSwitch): self._conn, self._get_port_collection_path(), redfish_version=self.redfish_version) + def _get_metrics_path(self): + """Helper function to find the Metrics path""" + return utils.get_sub_resource_path_by(self, 'Metrics') + + @property + @utils.cache_it + def metrics(self): + """Property to provide reference to `Metrics` instance + + It is calculated once when it is queried for the first time. On + refresh, this property is reset. + """ + return metrics.Metrics( + self._conn, self._get_metrics_path(), + redfish_version=self.redfish_version) + class EthernetSwitchCollection(base.ResourceCollectionBase): diff --git a/rsd_lib/resources/v2_2/ethernet_switch/metrics.py b/rsd_lib/resources/v2_2/ethernet_switch/metrics.py new file mode 100644 index 0000000..6421145 --- /dev/null +++ b/rsd_lib/resources/v2_2/ethernet_switch/metrics.py @@ -0,0 +1,30 @@ +# Copyright 2019 Intel, Inc. +# All Rights Reserved. +# +# Licensed under the Apache License, Version 2.0 (the "License"); you may +# not use this file except in compliance with the License. You may obtain +# a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT +# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the +# License for the specific language governing permissions and limitations +# under the License. + +from sushy.resources import base + + +class Metrics(base.ResourceBase): + name = base.Field("Name") + """The metrics name""" + + description = base.Field("Description") + """The description of metrics""" + + identity = base.Field("Id") + """The id of metrics""" + + health = base.Field("Health") + """The health status of metrics""" diff --git a/rsd_lib/tests/unit/json_samples/v2_2/ethernet_switch_metrics.json b/rsd_lib/tests/unit/json_samples/v2_2/ethernet_switch_metrics.json new file mode 100644 index 0000000..0a8bf0e --- /dev/null +++ b/rsd_lib/tests/unit/json_samples/v2_2/ethernet_switch_metrics.json @@ -0,0 +1,9 @@ +{ + "@odata.context": "/redfish/v1/$metadata#EthernetSwitchMetrics.EthernetSwitchMetrics", + "@odata.id": "/redfish/v1/EthernetSwitches/Switch1/Metrics", + "@odata.type": "#EthernetSwitchMetrics.v1_0_0.EthernetSwitchMetrics", + "Name": "EthernetSwitch Metrics for Switch1", + "Description": "description-as-string", + "Id": "Metrics for Switch1", + "Health": "OK" +} \ No newline at end of file diff --git a/rsd_lib/tests/unit/resources/v2_2/ethernet_switch/test_ethernet_switch.py b/rsd_lib/tests/unit/resources/v2_2/ethernet_switch/test_ethernet_switch.py index add59ef..2bca85a 100644 --- a/rsd_lib/tests/unit/resources/v2_2/ethernet_switch/test_ethernet_switch.py +++ b/rsd_lib/tests/unit/resources/v2_2/ethernet_switch/test_ethernet_switch.py @@ -15,11 +15,13 @@ import json import mock -import testtools from rsd_lib.resources.v2_2.ethernet_switch import ethernet_switch +from rsd_lib.resources.v2_2.ethernet_switch import metrics from rsd_lib.resources.v2_2.ethernet_switch import port +import testtools + class EthernetSwitchTestCase(testtools.TestCase): @@ -80,6 +82,52 @@ class EthernetSwitchTestCase(testtools.TestCase): self.assertIsInstance(self.ethernet_switch_inst.ports, port.PortCollection) + def test_metrics(self): + # | GIVEN | + self.conn.get.return_value.json.reset_mock() + with open('rsd_lib/tests/unit/json_samples/v2_2/' + 'ethernet_switch_metrics.json', 'r') as f: + self.conn.get.return_value.json.return_value = json.loads(f.read()) + # | WHEN | + actual_metrics = self.ethernet_switch_inst.metrics + # | THEN | + self.assertIsInstance(actual_metrics, + metrics.Metrics) + 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_metrics, + self.ethernet_switch_inst.metrics) + self.conn.get.return_value.json.assert_not_called() + + def test_metrics_on_refresh(self): + # | GIVEN | + with open('rsd_lib/tests/unit/json_samples/v2_2/' + 'ethernet_switch_metrics.json', 'r') as f: + self.conn.get.return_value.json.return_value = json.loads(f.read()) + # | WHEN & THEN | + self.assertIsInstance(self.ethernet_switch_inst.metrics, + metrics.Metrics) + + # On refreshing the metrics instance... + with open('rsd_lib/tests/unit/json_samples/v2_2/' + 'ethernet_switch.json', 'r') as f: + self.conn.get.return_value.json.return_value = json.loads(f.read()) + + self.ethernet_switch_inst.invalidate() + self.ethernet_switch_inst.refresh(force=False) + + # | GIVEN | + with open('rsd_lib/tests/unit/json_samples/v2_2/' + 'ethernet_switch_metrics.json', 'r') as f: + self.conn.get.return_value.json.return_value = json.loads(f.read()) + # | WHEN & THEN | + self.assertIsInstance(self.ethernet_switch_inst.metrics, + metrics.Metrics) + class EthernetSwitchCollectionTestCase(testtools.TestCase): diff --git a/rsd_lib/tests/unit/resources/v2_2/ethernet_switch/test_metrics.py b/rsd_lib/tests/unit/resources/v2_2/ethernet_switch/test_metrics.py new file mode 100644 index 0000000..b1d1ba6 --- /dev/null +++ b/rsd_lib/tests/unit/resources/v2_2/ethernet_switch/test_metrics.py @@ -0,0 +1,45 @@ +# Copyright 2019 Intel, Inc. +# All Rights Reserved. +# +# Licensed under the Apache License, Version 2.0 (the "License"); you may +# not use this file except in compliance with the License. You may obtain +# a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT +# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the +# License for the specific language governing permissions and limitations +# under the License. + +import json +import mock +from rsd_lib.resources.v2_2.ethernet_switch import metrics +import testtools + + +class MetricsTestCase(testtools.TestCase): + + def setUp(self): + super(MetricsTestCase, self).setUp() + self.conn = mock.Mock() + with open( + 'rsd_lib/tests/unit/json_samples/v2_2/' + 'ethernet_switch_metrics.json', + 'r') as f: + self.conn.get.return_value.json.return_value = json.loads(f.read()) + + self.metrics_inst = metrics.Metrics(self.conn, + '/redfish/v1/EthernetSwitches/' + 'Switch1/Metrics', + redfish_version='1.0.2') + + def test__parse_attributes(self): + self.metrics_inst._parse_attributes() + self.assertEqual('EthernetSwitch Metrics for Switch1', + self.metrics_inst.name) + self.assertEqual('description-as-string', + self.metrics_inst.description) + self.assertEqual('Metrics for Switch1', self.metrics_inst.identity) + self.assertEqual('OK', self.metrics_inst.health)