From 3ced15397a089ca506b48e33054abdaaa9c58656 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Matej=20Arta=C4=8D?= Date: Mon, 15 Jul 2019 15:46:33 +0200 Subject: [PATCH] Add initial Redfish unit tests Added unit tests for Redfish system. These unit tests employ `unittest.mock`, which is a Python 3 feature only. --- requirements.txt | 2 +- tests/test_redfish.py | 153 ++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 154 insertions(+), 1 deletion(-) create mode 100644 tests/test_redfish.py diff --git a/requirements.txt b/requirements.txt index e05b2819..ad03e0a4 100644 --- a/requirements.txt +++ b/requirements.txt @@ -26,7 +26,7 @@ python-novaclient==7.1.2 python-heatclient pyvcloud==19.1.2 py3winrm==0.0.1 -redfish-client==0.1.0 +redfish-client==0.2.1 requests six tzlocal diff --git a/tests/test_redfish.py b/tests/test_redfish.py new file mode 100644 index 00000000..fe9adae2 --- /dev/null +++ b/tests/test_redfish.py @@ -0,0 +1,153 @@ +# coding: utf-8 +# Unit tests for the Redfish API +from unittest import mock, TestCase + +from wrapanapi.exceptions import InvalidValueException +from wrapanapi.systems import RedfishSystem +from wrapanapi.systems.redfish import RedfishChassis, RedfishRack, RedfishServer + + +@mock.patch("redfish_client.connect") +class TestRedfishSystemSetup: + def test_connect_non_ssl(self, mock_connector): + RedfishSystem("dummy", "user", "pass", "Non-SSL", "8000") + mock_connector.assert_called_with("http://dummy:8000/", "user", "pass") + + def test_connect_ssl(self, mock_connector): + RedfishSystem("dummy", "user", "pass", "SSL", "443") + mock_connector.assert_called_with("https://dummy:443/", "user", "pass") + + +@mock.patch("redfish_client.Root.login") +@mock.patch("redfish_client.CachingConnector") +class TestRedfishSystem(TestCase): + def test_find_resource(self, mock_connector, mock_root_login): + mock_connector.return_value.get.return_value.status = 200 + mock_connector.return_value.get.return_value.json = { + "@odata.id": "id" + } + + rf = RedfishSystem("dummy", "user", "pass", "Non-SSL", "8000") + resource = rf.find("/redfish/v1/BlankResource") + self.assertEqual(resource.raw, {"@odata.id": "id"}) + + def test_get_server(self, mock_connector, mock_root_login): + mock_connector.return_value.get.return_value.status = 200 + mock_connector.return_value.get.return_value.json = { + "@odata.id": "/redfish/v1/Systems/System-1-2-1-1", + } + + rf = RedfishSystem("dummy", "user", "pass", "Non-SSL", "8000") + rf_server = rf.get_server("/redfish/v1/Systems/System-1-2-1-1") + self.assertEqual(type(rf_server), RedfishServer) + + def test_server_simple_properties(self, mock_connector, mock_root_login): + mock_connector.return_value.get.return_value.status = 200 + mock_connector.return_value.get.return_value.json = { + "@odata.id": "/redfish/v1/Systems/System-1-2-1-1", + "Description": "A server", + "Id": "System-1-2-1-1", + "Manufacturer": "Dell Inc.", + "Name": "System", + "PowerState": "On", + "SerialNumber": "945hjf0927mf", + } + + rf = RedfishSystem("dummy", "user", "pass", "Non-SSL", "8000") + rf_server = rf.get_server("/redfish/v1/Systems/System-1-2-1-1") + self.assertEqual(rf_server.name, "Dell Inc. System (945hjf0927mf)") + self.assertEqual(rf_server.description, "A server") + self.assertEqual(rf_server.state, "On") + self.assertEqual(rf_server._identifying_attrs, + {"odata_id": "/redfish/v1/Systems/System-1-2-1-1"}) + self.assertEqual(rf_server.uuid(), "System-1-2-1-1") + + def test_server_name_no_sn(self, mock_connector, mock_root_login): + mock_connector.return_value.get.return_value.status = 200 + mock_connector.return_value.get.return_value.json = { + "@odata.id": "/redfish/v1/Systems/System-1-2-1-1", + "Id": "System-1-2-1-1", + "Manufacturer": "Dell Inc.", + "Name": "System", + } + + rf = RedfishSystem("dummy", "user", "pass", "Non-SSL", "8000") + rf_server = rf.get_server("/redfish/v1/Systems/System-1-2-1-1") + self.assertEqual(rf_server.name, "Dell Inc. System") + + def test_get_chassis(self, mock_connector, mock_root_login): + mock_connector.return_value.get.return_value.status = 200 + mock_connector.return_value.get.return_value.json = { + "@odata.id": "/redfish/v1/Chassis/Sled-1-2-1", + } + + rf = RedfishSystem("dummy", "user", "pass", "Non-SSL", "8000") + rf_chassis = rf.get_chassis("/redfish/v1/Chassis/Sled-1-2-1") + self.assertEqual(type(rf_chassis), RedfishChassis) + + def test_get_chassis_simple_properties(self, mock_connector, mock_root_login): + mock_connector.return_value.get.return_value.status = 200 + mock_connector.return_value.get.return_value.json = { + "@odata.id": "/redfish/v1/Chassis/Sled-1-2-1", + "ChassisType": "Sled", + "Description": "G5 Sled-Level Enclosure", + "Id": "Sled-1-2-1", + "IndicatorLED": "Blinking", + "Manufacturer": "Dell", + "Name": "G5_Sled", + "SerialNumber": "5555A", + } + + rf = RedfishSystem("dummy", "user", "pass", "Non-SSL", "8000") + rf_chassis = rf.get_chassis("/redfish/v1/Chassis/Sled-1-2-1") + self.assertEqual(rf_chassis.chassis_type, "Sled") + self.assertEqual(rf_chassis.name, "Dell G5_Sled (5555A)") + self.assertEqual(rf_chassis.description, "G5 Sled-Level Enclosure") + self.assertEqual(rf_chassis.led_state, "Blinking") + self.assertEqual(rf_chassis._identifying_attrs, + {"odata_id": "/redfish/v1/Chassis/Sled-1-2-1"}) + self.assertEqual(rf_chassis.uuid(), "Sled-1-2-1") + + def test_get_rack(self, mock_connector, mock_root_login): + mock_connector.return_value.get.return_value.status = 200 + mock_connector.return_value.get.return_value.json = { + "@odata.id": "/redfish/v1/Chassis/Rack-1", + "ChassisType": "Rack", + } + + rf = RedfishSystem("dummy", "user", "pass", "Non-SSL", "8000") + rf_rack = rf.get_rack("/redfish/v1/Chassis/Rack-1") + self.assertEqual(type(rf_rack), RedfishRack) + + def test_get_rack_bad(self, mock_connector, mock_root_login): + mock_connector.return_value.get.return_value.status = 200 + mock_connector.return_value.get.return_value.json = { + "@odata.id": "/redfish/v1/Chassis/Sled-1-2-1", + "ChassisType": "Sled", + } + + rf = RedfishSystem("dummy", "user", "pass", "Non-SSL", "8000") + with self.assertRaises(InvalidValueException, + msg="Chassis type Sled does not match that of a Rack"): + rf.get_rack("/redfish/v1/Chassis/Sled-1-2-1") + + def test_get_rack_simple_properties(self, mock_connector, mock_root_login): + mock_connector.return_value.get.return_value.status = 200 + mock_connector.return_value.get.return_value.json = { + "@odata.id": "/redfish/v1/Chassis/Rack-1", + "ChassisType": "Rack", + "Description": "Redfish Rack", + "Id": "Rack-1", + "Manufacturer": "Dell", + "Name": "G5_Rack", + "SerialNumber": "1ABC", + } + + rf = RedfishSystem("dummy", "user", "pass", "Non-SSL", "8000") + rf_rack = rf.get_rack("/redfish/v1/Chassis/Rack-1") + self.assertEqual(rf_rack.chassis_type, "Rack") + self.assertEqual(rf_rack.name, "Rack-1") + self.assertEqual(rf_rack.description, "Redfish Rack") + self.assertEqual(rf_rack.uuid(), "Rack-1") + self.assertEqual(rf_rack._identifying_attrs, {"odata_id": + "/redfish/v1/Chassis/Rack-1"})