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 ef3e34a..77616db 100644 --- a/rsd_lib/resources/v2_2/ethernet_switch/ethernet_switch.py +++ b/rsd_lib/resources/v2_2/ethernet_switch/ethernet_switch.py @@ -15,30 +15,31 @@ 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 ethernet_switch_port 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): - @property @utils.cache_it def ports(self): - """Property to provide reference to `PortCollection` instance + """Property to provide reference to `EthernetSwitchPortCollection` instance It is calculated once when it is queried for the first time. On refresh, this property is reset. """ - return port.PortCollection( - self._conn, utils.get_sub_resource_path_by(self, 'Metrics'), - redfish_version=self.redfish_version) + return ethernet_switch_port.EthernetSwitchPortCollection( + self._conn, + utils.get_sub_resource_path_by(self, "Metrics"), + 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') + return utils.get_sub_resource_path_by(self, "Metrics") @property @utils.cache_it @@ -49,12 +50,13 @@ class EthernetSwitch(v2_1_ethernet_switch.EthernetSwitch): refresh, this property is reset. """ return metrics.Metrics( - self._conn, self._get_metrics_path(), - redfish_version=self.redfish_version) + self._conn, + self._get_metrics_path(), + redfish_version=self.redfish_version, + ) class EthernetSwitchCollection(base.ResourceCollectionBase): - @property def _resource_type(self): return EthernetSwitch @@ -68,6 +70,6 @@ class EthernetSwitchCollection(base.ResourceCollectionBase): :param redfish_version: The version of RedFish. Used to construct the object according to schema of the given version. """ - super(EthernetSwitchCollection, self).__init__(connector, - path, - redfish_version) + super(EthernetSwitchCollection, self).__init__( + connector, path, redfish_version + ) diff --git a/rsd_lib/resources/v2_2/ethernet_switch/ethernet_switch_port.py b/rsd_lib/resources/v2_2/ethernet_switch/ethernet_switch_port.py new file mode 100644 index 0000000..216ec98 --- /dev/null +++ b/rsd_lib/resources/v2_2/ethernet_switch/ethernet_switch_port.py @@ -0,0 +1,88 @@ +# Copyright 2018 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 +from sushy import utils + +from rsd_lib.resources.v2_1.ethernet_switch import ethernet_switch_port +from rsd_lib.resources.v2_1.system import ethernet_interface +from rsd_lib.resources.v2_2.ethernet_switch import ethernet_switch_port_metrics +from rsd_lib import utils as rsd_lib_utils + + +class LinksField(base.CompositeField): + + primary_vlan = base.Field( + "PrimaryVLAN", adapter=rsd_lib_utils.get_resource_identity + ) + + switch = base.Field("Switch", adapter=rsd_lib_utils.get_resource_identity) + + member_of_port = base.Field( + "MemberOfPort", adapter=rsd_lib_utils.get_resource_identity + ) + + port_members = base.Field( + "PortMembers", adapter=utils.get_members_identities + ) + + active_acls = base.Field( + "ActiveACLs", adapter=utils.get_members_identities + ) + + +class EthernetSwitchPort(ethernet_switch_port.EthernetSwitchPort): + + links = LinksField("Links") + + def _get_metrics_path(self): + """Helper function to find the Port metrics path""" + return utils.get_sub_resource_path_by(self, "Metrics") + + @property + @utils.cache_it + def metrics(self): + """Property to provide reference to `EthernetSwitchPortMetrics` instance + + It is calculated once when it is queried for the first time. On + refresh, this property is reset. + """ + return ethernet_switch_port_metrics.EthernetSwitchPortMetrics( + self._conn, + utils.get_sub_resource_path_by(self, "Metrics"), + redfish_version=self.redfish_version, + ) + + @property + @utils.cache_it + def neighbor_interface(self): + """Property to provide reference to `EthernetInterface` instance + + It is calculated once when it is queried for the first time. On + refresh, this property is reset. + """ + return ethernet_interface.EthernetInterface( + self._conn, + utils.get_sub_resource_path_by(self, "NeighborInterface"), + redfish_version=self.redfish_version, + ) + + +class EthernetSwitchPortCollection( + ethernet_switch_port.EthernetSwitchPortCollection +): + @property + def _resource_type(self): + return EthernetSwitchPort diff --git a/rsd_lib/resources/v2_2/ethernet_switch/ethernet_switch_port_metrics.py b/rsd_lib/resources/v2_2/ethernet_switch/ethernet_switch_port_metrics.py new file mode 100644 index 0000000..52d6e4a --- /dev/null +++ b/rsd_lib/resources/v2_2/ethernet_switch/ethernet_switch_port_metrics.py @@ -0,0 +1,71 @@ +# Copyright 2018 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 + +from rsd_lib import utils as rsd_lib_utils + + +class ReceivedField(base.CompositeField): + packets = base.Field("Packets", adapter=rsd_lib_utils.num_or_none) + dropped_packets = base.Field( + "DroppedPackets", adapter=rsd_lib_utils.num_or_none + ) + error_packets = base.Field( + "ErrorPackets", adapter=rsd_lib_utils.num_or_none + ) + broadcast_packets = base.Field( + "BroadcastPackets", adapter=rsd_lib_utils.num_or_none + ) + multicast_packets = base.Field( + "MulticastPackets", adapter=rsd_lib_utils.num_or_none + ) + errors = base.Field("Errors", adapter=rsd_lib_utils.num_or_none) + received_bytes = base.Field("Bytes", adapter=rsd_lib_utils.num_or_none) + + +class TransmittedField(base.CompositeField): + packets = base.Field("Packets", adapter=rsd_lib_utils.num_or_none) + dropped_packets = base.Field( + "DroppedPackets", adapter=rsd_lib_utils.num_or_none + ) + error_packets = base.Field( + "ErrorPackets", adapter=rsd_lib_utils.num_or_none + ) + broadcast_packets = base.Field( + "BroadcastPackets", adapter=rsd_lib_utils.num_or_none + ) + multicast_packets = base.Field( + "MulticastPackets", adapter=rsd_lib_utils.num_or_none + ) + errors = base.Field("Errors", adapter=rsd_lib_utils.num_or_none) + transmitted_bytes = base.Field("Bytes", adapter=rsd_lib_utils.num_or_none) + + +class EthernetSwitchPortMetrics(base.ResourceBase): + name = base.Field("Name") + """The metrics name""" + + identity = base.Field("Id") + """The metrics identity""" + + received = ReceivedField("Received") + """The received packets status""" + + transmitted = TransmittedField("Transmitted") + """The transmitted packets status""" + + collisions = base.Field("Collisions", adapter=rsd_lib_utils.num_or_none) + """The collisions status""" diff --git a/rsd_lib/resources/v2_2/ethernet_switch/port.py b/rsd_lib/resources/v2_2/ethernet_switch/port.py deleted file mode 100644 index 1716169..0000000 --- a/rsd_lib/resources/v2_2/ethernet_switch/port.py +++ /dev/null @@ -1,57 +0,0 @@ -# Copyright 2018 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 -from sushy import utils - -from rsd_lib.resources.v2_1.ethernet_switch import ethernet_switch_port \ - as v2_1_port -from rsd_lib.resources.v2_2.ethernet_switch import port_metrics - - -class Port(v2_1_port.EthernetSwitchPort): - - def _get_metrics_path(self): - """Helper function to find the Port 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 the first time it is queried. On refresh, - this property is reset. - """ - return port_metrics.PortMetrics( - self._conn, self._get_metrics_path(), - redfish_version=self.redfish_version) - - -class PortCollection(base.ResourceCollectionBase): - - @property - def _resource_type(self): - return Port - - def __init__(self, connector, path, redfish_version=None): - """A class representing an Port - - :param connector: A Connector instance - :param path: The canonical path to the Port collection resource - :param redfish_version: The version of RedFish. Used to construct - the object according to schema of the given version. - """ - super(PortCollection, self).__init__(connector, path, redfish_version) diff --git a/rsd_lib/resources/v2_2/ethernet_switch/port_metrics.py b/rsd_lib/resources/v2_2/ethernet_switch/port_metrics.py deleted file mode 100644 index 5a6632c..0000000 --- a/rsd_lib/resources/v2_2/ethernet_switch/port_metrics.py +++ /dev/null @@ -1,63 +0,0 @@ -# Copyright 2018 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 - -from rsd_lib import utils as rsd_lib_utils - - -class ReceivedField(base.CompositeField): - packets = base.Field('Packets', adapter=rsd_lib_utils.num_or_none) - dropped_packets = base.Field('DroppedPackets', - adapter=rsd_lib_utils.num_or_none) - error_packets = base.Field('ErrorPackets', - adapter=rsd_lib_utils.num_or_none) - broadcast_packets = base.Field('BroadcastPackets', - adapter=rsd_lib_utils.num_or_none) - multicast_packets = base.Field('MulticastPackets', - adapter=rsd_lib_utils.num_or_none) - errors = base.Field('Errors', adapter=rsd_lib_utils.num_or_none) - received_bytes = base.Field('Bytes', adapter=rsd_lib_utils.num_or_none) - - -class TransmittedField(base.CompositeField): - packets = base.Field('Packets', adapter=rsd_lib_utils.num_or_none) - dropped_packets = base.Field('DroppedPackets', - adapter=rsd_lib_utils.num_or_none) - error_packets = base.Field('ErrorPackets', - adapter=rsd_lib_utils.num_or_none) - broadcast_packets = base.Field('BroadcastPackets', - adapter=rsd_lib_utils.num_or_none) - multicast_packets = base.Field('MulticastPackets', - adapter=rsd_lib_utils.num_or_none) - errors = base.Field('Errors', adapter=rsd_lib_utils.num_or_none) - transmitted_bytes = base.Field('Bytes', adapter=rsd_lib_utils.num_or_none) - - -class PortMetrics(base.ResourceBase): - name = base.Field('Name') - """The metrics name""" - - identity = base.Field('Id') - """The metrics identity""" - - received = ReceivedField('Received') - """The received packets status""" - - transmitted = TransmittedField('Transmitted') - """The transmitted packets status""" - - collisions = base.Field('Collisions', adapter=rsd_lib_utils.num_or_none) - """The collisions status""" 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 2bca85a..582f2a5 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 @@ -17,153 +17,184 @@ import json import mock from rsd_lib.resources.v2_2.ethernet_switch import ethernet_switch +from rsd_lib.resources.v2_2.ethernet_switch import ethernet_switch_port 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): - def setUp(self): super(EthernetSwitchTestCase, self).setUp() self.conn = mock.Mock() - with open('rsd_lib/tests/unit/json_samples/v2_2/' - 'ethernet_switch.json', 'r') as f: + 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 = ethernet_switch.EthernetSwitch( - self.conn, '/redfish/v1/EthernetSwitches/Switch1/Ports/Port1', - redfish_version='1.0.2') + self.conn, + "/redfish/v1/EthernetSwitches/Switch1/Ports/Port1", + redfish_version="1.0.2", + ) def test_ports(self): # | GIVEN | self.conn.get.return_value.json.reset_mock() - with open('rsd_lib/tests/unit/json_samples/v2_2/' - 'ethernet_switch_port_collection.json', 'r') as f: + with open( + "rsd_lib/tests/unit/json_samples/v2_2/" + "ethernet_switch_port_collection.json", + "r", + ) as f: self.conn.get.return_value.json.return_value = json.loads(f.read()) # | WHEN | actual_ports = self.ethernet_switch_inst.ports # | THEN | - self.assertIsInstance(actual_ports, - port.PortCollection) + self.assertIsInstance( + actual_ports, ethernet_switch_port.EthernetSwitchPortCollection + ) 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_ports, - self.ethernet_switch_inst.ports) + self.assertIs(actual_ports, self.ethernet_switch_inst.ports) self.conn.get.return_value.json.assert_not_called() def test_ports_on_refresh(self): # | GIVEN | - with open('rsd_lib/tests/unit/json_samples/v2_2/' - 'ethernet_switch_port_collection.json', 'r') as f: + with open( + "rsd_lib/tests/unit/json_samples/v2_2/" + "ethernet_switch_port_collection.json", + "r", + ) as f: self.conn.get.return_value.json.return_value = json.loads(f.read()) # | WHEN & THEN | - self.assertIsInstance(self.ethernet_switch_inst.ports, - port.PortCollection) + self.assertIsInstance( + self.ethernet_switch_inst.ports, + ethernet_switch_port.EthernetSwitchPortCollection, + ) # On refreshing the port instance... - with open('rsd_lib/tests/unit/json_samples/v2_2/' - 'ethernet_switch.json', 'r') as f: + 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_port_collection.json', 'r') as f: + with open( + "rsd_lib/tests/unit/json_samples/v2_2/" + "ethernet_switch_port_collection.json", + "r", + ) as f: self.conn.get.return_value.json.return_value = json.loads(f.read()) # | WHEN & THEN | - self.assertIsInstance(self.ethernet_switch_inst.ports, - port.PortCollection) + self.assertIsInstance( + self.ethernet_switch_inst.ports, + ethernet_switch_port.EthernetSwitchPortCollection, + ) 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: + 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.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.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: + 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) + 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: + 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: + 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) + self.assertIsInstance( + self.ethernet_switch_inst.metrics, metrics.Metrics + ) class EthernetSwitchCollectionTestCase(testtools.TestCase): - def setUp(self): super(EthernetSwitchCollectionTestCase, self).setUp() self.conn = mock.Mock() - with open('rsd_lib/tests/unit/json_samples/v2_2/' - 'ethernet_switch_collection.json', 'r') as f: + with open( + "rsd_lib/tests/unit/json_samples/v2_2/" + "ethernet_switch_collection.json", + "r", + ) as f: self.conn.get.return_value.json.return_value = json.loads(f.read()) self.ethernet_switch_col = ethernet_switch.EthernetSwitchCollection( - self.conn, - 'redfish/v1/EthernetSwitches', - redfish_version='1.0.2') + self.conn, "redfish/v1/EthernetSwitches", redfish_version="1.0.2" + ) def test__parse_attributes(self): self.ethernet_switch_col._parse_attributes() - self.assertEqual('1.0.2', self.ethernet_switch_col.redfish_version) - self.assertEqual('Ethernet Switches Collection', - self.ethernet_switch_col.name) - self.assertEqual(('/redfish/v1/EthernetSwitches/Switch1',), - self.ethernet_switch_col.members_identities) + self.assertEqual("1.0.2", self.ethernet_switch_col.redfish_version) + self.assertEqual( + "Ethernet Switches Collection", self.ethernet_switch_col.name + ) + self.assertEqual( + ("/redfish/v1/EthernetSwitches/Switch1",), + self.ethernet_switch_col.members_identities, + ) - @mock.patch.object(ethernet_switch, 'EthernetSwitch', autospec=True) + @mock.patch.object(ethernet_switch, "EthernetSwitch", autospec=True) def test_get_member(self, mock_ethernet_switch): self.ethernet_switch_col.get_member( - '/redfish/v1/EthernetSwitches/Switch1') + "/redfish/v1/EthernetSwitches/Switch1" + ) mock_ethernet_switch.assert_called_once_with( self.ethernet_switch_col._conn, - '/redfish/v1/EthernetSwitches/Switch1', - redfish_version=self.ethernet_switch_col.redfish_version + "/redfish/v1/EthernetSwitches/Switch1", + redfish_version=self.ethernet_switch_col.redfish_version, ) - @mock.patch.object(ethernet_switch, 'EthernetSwitch', autospec=True) + @mock.patch.object(ethernet_switch, "EthernetSwitch", autospec=True) def test_get_members(self, mock_ethernet_switch): members = self.ethernet_switch_col.get_members() self.assertEqual(mock_ethernet_switch.call_count, 1) diff --git a/rsd_lib/tests/unit/resources/v2_2/ethernet_switch/test_ethernet_switch_port.py b/rsd_lib/tests/unit/resources/v2_2/ethernet_switch/test_ethernet_switch_port.py new file mode 100644 index 0000000..1df50b6 --- /dev/null +++ b/rsd_lib/tests/unit/resources/v2_2/ethernet_switch/test_ethernet_switch_port.py @@ -0,0 +1,169 @@ +# Copyright 2018 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 +import testtools + +from sushy import exceptions + +from rsd_lib.resources.v2_2.ethernet_switch import ethernet_switch_port +from rsd_lib.resources.v2_2.ethernet_switch import ethernet_switch_port_metrics + + +class PortTestCase(testtools.TestCase): + def setUp(self): + super(PortTestCase, self).setUp() + self.conn = mock.Mock() + with open( + "rsd_lib/tests/unit/json_samples/v2_2/" + "ethernet_switch_port.json", + "r", + ) as f: + self.conn.get.return_value.json.return_value = json.loads(f.read()) + + self.port_inst = ethernet_switch_port.EthernetSwitchPort( + self.conn, + "/redfish/v1/EthernetSwitches/Switch1/Ports/Port1", + redfish_version="1.0.2", + ) + + def test__get_metrics_path(self): + self.assertEqual( + "/redfish/v1/EthernetSwitches/Switch1/Ports/Port1/Metrics", + self.port_inst._get_metrics_path(), + ) + + def test__get_metrics_path_missing_ports_attr(self): + self.port_inst._json.pop("Metrics") + with self.assertRaisesRegex( + exceptions.MissingAttributeError, "attribute Metrics" + ): + self.port_inst._get_metrics_path() + + 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_port_metrics.json", + "r", + ) as f: + self.conn.get.return_value.json.return_value = json.loads(f.read()) + # | WHEN | + actual_metrics = self.port_inst.metrics + # | THEN | + self.assertIsInstance( + actual_metrics, + ethernet_switch_port_metrics.EthernetSwitchPortMetrics, + ) + 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.port_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_port_metrics.json", + "r", + ) as f: + self.conn.get.return_value.json.return_value = json.loads(f.read()) + # | WHEN & THEN | + self.assertIsInstance( + self.port_inst.metrics, + ethernet_switch_port_metrics.EthernetSwitchPortMetrics, + ) + + # On refreshing the port instance... + with open( + "rsd_lib/tests/unit/json_samples/v2_2/" + "ethernet_switch_port.json", + "r", + ) as f: + self.conn.get.return_value.json.return_value = json.loads(f.read()) + + self.port_inst.invalidate() + self.port_inst.refresh(force=False) + + # | GIVEN | + with open( + "rsd_lib/tests/unit/json_samples/v2_2/" + "ethernet_switch_port_metrics.json", + "r", + ) as f: + self.conn.get.return_value.json.return_value = json.loads(f.read()) + # | WHEN & THEN | + self.assertIsInstance( + self.port_inst.metrics, + ethernet_switch_port_metrics.EthernetSwitchPortMetrics, + ) + + +class PortCollectionTestCase(testtools.TestCase): + def setUp(self): + super(PortCollectionTestCase, self).setUp() + self.conn = mock.Mock() + with open( + "rsd_lib/tests/unit/json_samples/v2_2/" + "ethernet_switch_port_collection.json", + "r", + ) as f: + self.conn.get.return_value.json.return_value = json.loads(f.read()) + self.port_col = ethernet_switch_port.EthernetSwitchPortCollection( + self.conn, + "/redfish/v1/EthernetSwitches/Switch1/Ports", + redfish_version="1.0.2", + ) + + def test__parse_attributes(self): + self.port_col._parse_attributes() + self.assertEqual("1.0.2", self.port_col.redfish_version) + self.assertEqual("Ethernet Switch Port Collection", self.port_col.name) + self.assertEqual( + ("/redfish/v1/EthernetSwitches/Switch1/Ports/Port1",), + self.port_col.members_identities, + ) + + @mock.patch.object( + ethernet_switch_port, "EthernetSwitchPort", autospec=True + ) + def test_get_member(self, mock_port): + self.port_col.get_member( + "/redfish/v1/EthernetSwitches/Switch1/Ports/Port1" + ) + mock_port.assert_called_once_with( + self.port_col._conn, + "/redfish/v1/EthernetSwitches/Switch1/Ports/Port1", + redfish_version=self.port_col.redfish_version, + ) + + @mock.patch.object( + ethernet_switch_port, "EthernetSwitchPort", autospec=True + ) + def test_get_members(self, mock_port): + members = self.port_col.get_members() + mock_port.assert_called_with( + self.port_col._conn, + "/redfish/v1/EthernetSwitches/Switch1/Ports/Port1", + redfish_version=self.port_col.redfish_version, + ) + self.assertIsInstance(members, list) + self.assertEqual(1, len(members)) diff --git a/rsd_lib/tests/unit/resources/v2_2/ethernet_switch/test_port_metrics.py b/rsd_lib/tests/unit/resources/v2_2/ethernet_switch/test_ethernet_switch_port_metrics.py similarity index 71% rename from rsd_lib/tests/unit/resources/v2_2/ethernet_switch/test_port_metrics.py rename to rsd_lib/tests/unit/resources/v2_2/ethernet_switch/test_ethernet_switch_port_metrics.py index 7e63ffd..78275ae 100644 --- a/rsd_lib/tests/unit/resources/v2_2/ethernet_switch/test_port_metrics.py +++ b/rsd_lib/tests/unit/resources/v2_2/ethernet_switch/test_ethernet_switch_port_metrics.py @@ -18,30 +18,34 @@ import json import mock import testtools -from rsd_lib.resources.v2_2.ethernet_switch import port_metrics +from rsd_lib.resources.v2_2.ethernet_switch import ethernet_switch_port_metrics class PortMetricsTestCase(testtools.TestCase): - def setUp(self): super(PortMetricsTestCase, self).setUp() self.conn = mock.Mock() - with open('rsd_lib/tests/unit/json_samples/v2_2/' - 'ethernet_switch_port_metrics.json', 'r') as f: + with open( + "rsd_lib/tests/unit/json_samples/v2_2/" + "ethernet_switch_port_metrics.json", + "r", + ) as f: self.conn.get.return_value.json.return_value = json.loads(f.read()) - self.port_metrics_inst = port_metrics.PortMetrics( - self.conn, - '/redfish/v1/EthernetSwitches/Switch1/Ports/Port1/Metrics', - redfish_version='1.1.0') + self.port_metrics_inst = ethernet_switch_port_metrics.\ + EthernetSwitchPortMetrics( + self.conn, + "/redfish/v1/EthernetSwitches/Switch1/Ports/Port1/Metrics", + redfish_version="1.1.0", + ) def test__parse_attributes(self): self.port_metrics_inst._parse_attributes() - self.assertEqual('1.1.0', self.port_metrics_inst.redfish_version) - self.assertEqual('Ethernet Switch Port Metrics', - self.port_metrics_inst.name) - self.assertEqual('Metrics', - self.port_metrics_inst.identity) + self.assertEqual("1.1.0", self.port_metrics_inst.redfish_version) + self.assertEqual( + "Ethernet Switch Port Metrics", self.port_metrics_inst.name + ) + self.assertEqual("Metrics", self.port_metrics_inst.identity) self.assertEqual(8, self.port_metrics_inst.received.packets) self.assertEqual(5, self.port_metrics_inst.received.dropped_packets) @@ -52,15 +56,17 @@ class PortMetricsTestCase(testtools.TestCase): self.assertEqual(64, self.port_metrics_inst.received.received_bytes) self.assertEqual(128, self.port_metrics_inst.transmitted.packets) - self.assertEqual( - 1, self.port_metrics_inst.transmitted.dropped_packets) + self.assertEqual(1, self.port_metrics_inst.transmitted.dropped_packets) self.assertEqual(2, self.port_metrics_inst.transmitted.error_packets) self.assertEqual( - 3, self.port_metrics_inst.transmitted.broadcast_packets) + 3, self.port_metrics_inst.transmitted.broadcast_packets + ) self.assertEqual( - 4, self.port_metrics_inst.transmitted.multicast_packets) + 4, self.port_metrics_inst.transmitted.multicast_packets + ) self.assertEqual(5, self.port_metrics_inst.transmitted.errors) self.assertEqual( - 512, self.port_metrics_inst.transmitted.transmitted_bytes) + 512, self.port_metrics_inst.transmitted.transmitted_bytes + ) self.assertEqual(0, self.port_metrics_inst.collisions) diff --git a/rsd_lib/tests/unit/resources/v2_2/ethernet_switch/test_port.py b/rsd_lib/tests/unit/resources/v2_2/ethernet_switch/test_port.py deleted file mode 100644 index e90a3b9..0000000 --- a/rsd_lib/tests/unit/resources/v2_2/ethernet_switch/test_port.py +++ /dev/null @@ -1,137 +0,0 @@ -# Copyright 2018 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 -import testtools - -from sushy import exceptions - -from rsd_lib.resources.v2_2.ethernet_switch import port -from rsd_lib.resources.v2_2.ethernet_switch import port_metrics - - -class PortTestCase(testtools.TestCase): - - def setUp(self): - super(PortTestCase, self).setUp() - self.conn = mock.Mock() - with open('rsd_lib/tests/unit/json_samples/v2_2/' - 'ethernet_switch_port.json', 'r') as f: - self.conn.get.return_value.json.return_value = json.loads(f.read()) - - self.port_inst = port.Port( - self.conn, '/redfish/v1/EthernetSwitches/Switch1/Ports/Port1', - redfish_version='1.0.2') - - def test__get_metrics_path(self): - self.assertEqual( - '/redfish/v1/EthernetSwitches/Switch1/Ports/Port1/Metrics', - self.port_inst._get_metrics_path()) - - def test__get_metrics_path_missing_ports_attr(self): - self.port_inst._json.pop('Metrics') - with self.assertRaisesRegex( - exceptions.MissingAttributeError, 'attribute Metrics'): - self.port_inst._get_metrics_path() - - 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_port_metrics.json', 'r') as f: - self.conn.get.return_value.json.return_value = json.loads(f.read()) - # | WHEN | - actual_metrics = self.port_inst.metrics - # | THEN | - self.assertIsInstance(actual_metrics, - port_metrics.PortMetrics) - 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.port_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_port_metrics.json', 'r') as f: - self.conn.get.return_value.json.return_value = json.loads(f.read()) - # | WHEN & THEN | - self.assertIsInstance(self.port_inst.metrics, - port_metrics.PortMetrics) - - # On refreshing the port instance... - with open('rsd_lib/tests/unit/json_samples/v2_2/' - 'ethernet_switch_port.json', 'r') as f: - self.conn.get.return_value.json.return_value = json.loads(f.read()) - - self.port_inst.invalidate() - self.port_inst.refresh(force=False) - - # | GIVEN | - with open('rsd_lib/tests/unit/json_samples/v2_2/' - 'ethernet_switch_port_metrics.json', 'r') as f: - self.conn.get.return_value.json.return_value = json.loads(f.read()) - # | WHEN & THEN | - self.assertIsInstance(self.port_inst.metrics, - port_metrics.PortMetrics) - - -class PortCollectionTestCase(testtools.TestCase): - - def setUp(self): - super(PortCollectionTestCase, self).setUp() - self.conn = mock.Mock() - with open( - 'rsd_lib/tests/unit/json_samples/v2_2/' - 'ethernet_switch_port_collection.json', 'r') as f: - self.conn.get.return_value.json.return_value = json.loads(f.read()) - self.port_col = port.PortCollection( - self.conn, '/redfish/v1/EthernetSwitches/Switch1/Ports', - redfish_version='1.0.2') - - def test__parse_attributes(self): - self.port_col._parse_attributes() - self.assertEqual('1.0.2', self.port_col.redfish_version) - self.assertEqual( - 'Ethernet Switch Port Collection', - self.port_col.name) - self.assertEqual( - ('/redfish/v1/EthernetSwitches/Switch1/Ports/Port1',), - self.port_col.members_identities) - - @mock.patch.object(port, 'Port', autospec=True) - def test_get_member(self, mock_port): - self.port_col.get_member( - '/redfish/v1/EthernetSwitches/Switch1/Ports/Port1') - mock_port.assert_called_once_with( - self.port_col._conn, - '/redfish/v1/EthernetSwitches/Switch1/Ports/Port1', - redfish_version=self.port_col.redfish_version) - - @mock.patch.object(port, 'Port', autospec=True) - def test_get_members(self, mock_port): - members = self.port_col.get_members() - mock_port.assert_called_with( - self.port_col._conn, - '/redfish/v1/EthernetSwitches/Switch1/Ports/Port1', - redfish_version=self.port_col.redfish_version) - self.assertIsInstance(members, list) - self.assertEqual(1, len(members))