# Copyright 2017 Hewlett Packard Enterprise Development LP # 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 sushy import testtools from proliantutils import exception from proliantutils.redfish import redfish class RedfishOperationsTestCase(testtools.TestCase): @mock.patch.object(sushy, 'Sushy', autospec=True) def setUp(self, sushy_mock): super(RedfishOperationsTestCase, self).setUp() self.sushy = mock.MagicMock() sushy_mock.return_value = self.sushy with open('proliantutils/tests/redfish/' 'json_samples/root.json', 'r') as f: self.sushy.json = json.loads(f.read()) self.rf_client = redfish.RedfishOperations( '1.2.3.4', username='foo', password='bar') sushy_mock.assert_called_once_with( 'https://1.2.3.4', 'foo', 'bar', '/redfish/v1/', False) @mock.patch.object(sushy, 'Sushy', autospec=True) def test_sushy_init_fail(self, sushy_mock): sushy_mock.side_effect = sushy.exceptions.SushyError self.assertRaisesRegex( exception.IloConnectionError, 'The Redfish controller at "https://1.2.3.4" has thrown error', redfish.RedfishOperations, '1.2.3.4', username='foo', password='bar') def test__get_system_collection_path(self): self.assertEqual('/redfish/v1/Systems/', self.rf_client._get_system_collection_path()) def test__get_system_collection_path_missing_systems_attr(self): self.rf_client._sushy.json.pop('Systems') self.assertRaisesRegex( exception.MissingAttributeError, 'The attribute Systems is missing', self.rf_client._get_system_collection_path) def test__get_sushy_system_fail(self): self.rf_client._sushy.get_system.side_effect = ( sushy.exceptions.SushyError) self.assertRaisesRegex( exception.IloError, 'The Redfish System "apple" was not found.', self.rf_client._get_sushy_system, 'apple') def test_get_product_name(self): with open('proliantutils/tests/redfish/' 'json_samples/system.json', 'r') as f: self.sushy.get_system().json = json.loads(f.read()) product_name = self.rf_client.get_product_name() self.assertEqual('ProLiant DL180 Gen10', product_name) def test_get_host_power_status(self): self.sushy.get_system().power_state = sushy.SYSTEM_POWER_STATE_ON power_state = self.rf_client.get_host_power_status() self.assertEqual('ON', power_state)