From d03795e74d229793b50f4d605eac479f73d965b2 Mon Sep 17 00:00:00 2001 From: kesper Date: Wed, 14 Jun 2017 07:37:27 +0000 Subject: [PATCH] Redfish: Adding the ability to get one time boot This commit adds new function 'get_one_time_boot' to get the one time boot option for server. Change-Id: Ibc2779680a1f740e8ef2eaa221657a4e8670b93e --- proliantutils/redfish/redfish.py | 23 +++++++ .../tests/redfish/json_samples/system.json | 65 +++++++++++++++++++ .../tests/redfish/resources/test_system.py | 3 +- proliantutils/tests/redfish/test_redfish.py | 27 +++++++- 4 files changed, 116 insertions(+), 2 deletions(-) diff --git a/proliantutils/redfish/redfish.py b/proliantutils/redfish/redfish.py index fd965f2..af7ce08 100644 --- a/proliantutils/redfish/redfish.py +++ b/proliantutils/redfish/redfish.py @@ -39,6 +39,16 @@ POWER_RESET_MAP = { 'OFF': sushy.RESET_FORCE_OFF, } +DEVICE_COMMON_TO_REDFISH = { + 'NETWORK': sushy.BOOT_SOURCE_TARGET_PXE, + 'HDD': sushy.BOOT_SOURCE_TARGET_HDD, + 'CDROM': sushy.BOOT_SOURCE_TARGET_CD, + 'ISCSI': sushy.BOOT_SOURCE_TARGET_UEFI_TARGET +} + +DEVICE_REDFISH_TO_COMMON = {v: k for k, v in DEVICE_COMMON_TO_REDFISH.items()} + + # Assuming only one sushy_system present as part of collection, # as we are dealing with iLO's here. PROLIANT_SYSTEM_ID = '1' @@ -215,3 +225,16 @@ class RedfishOperations(operations.IloOperations): {'error': str(e)}) LOG.debug(msg) raise exception.IloError(msg) + + def get_one_time_boot(self): + """Retrieves the current setting for the one time boot. + + :returns: Returns boot device that would be used in next + boot. Returns 'Normal' if no device is set. + """ + sushy_system = self._get_sushy_system(PROLIANT_SYSTEM_ID) + if (sushy_system.boot.enabled == sushy.BOOT_SOURCE_ENABLED_ONCE): + return DEVICE_REDFISH_TO_COMMON.get(sushy_system.boot.target) + else: + # value returned by RIBCL if one-time boot setting are absent + return 'Normal' diff --git a/proliantutils/tests/redfish/json_samples/system.json b/proliantutils/tests/redfish/json_samples/system.json index d187aaf..c59ab1c 100644 --- a/proliantutils/tests/redfish/json_samples/system.json +++ b/proliantutils/tests/redfish/json_samples/system.json @@ -1,4 +1,5 @@ { + "Default": { "@odata.context": "/redfish/v1/$metadata#Systems/Members/$entity", "@odata.etag": "W/\"0E79655D\"", "@odata.id": "/redfish/v1/Systems/1/", @@ -223,4 +224,68 @@ } ], "UUID": "00000000-0000-0000-0000-000000000000" + }, + + "System_op_for_one_time_boot_cdrom": { + "@odata.context": "/redfish/v1/$metadata#Systems/Members/$entity", + "@odata.etag": "W/\"0E79655D\"", + "@odata.id": "/redfish/v1/Systems/1/", + "@odata.type": "#ComputerSystem.v1_2_0.ComputerSystem", + "Actions": { + "#ComputerSystem.Reset": { + "ResetType@Redfish.AllowableValues": [ + "On", + "ForceOff", + "ForceRestart", + "Nmi", + "PushPowerButton" + ], + "target": "/redfish/v1/Systems/1/Actions/ComputerSystem.Reset/" + } + }, + "HostName": "", + "Id": "1", + "IndicatorLED": "Off", + "Manufacturer": "HPE", + "Name": "Computer System", + "PowerState": "On", + "Boot": { + "BootSourceOverrideEnabled": "Once", + "BootSourceOverrideMode": "UEFI", + "BootSourceOverrideTarget": "Cd", + "BootSourceOverrideTarget@Redfish.AllowableValues": [ + "None", + "Cd", + "Hdd", + "Usb", + "SDCard", + "Utilities", + "Diags", + "BiosSetup", + "Pxe", + "UefiShell", + "UefiHttp", + "UefiTarget" + ], + "UefiTargetBootSourceOverride": "None", + "UefiTargetBootSourceOverride@Redfish.AllowableValues": [ + "HD(1,GPT,4029CE09-5C2D-464D-9C2D-EC6E607F06A1,0x800,0x100000)/\\EFI\\redhat\\shim.efi", + "HD(1,GPT,4029CE09-5C2D-464D-9C2D-EC6E607F06A1,0x800,0x100000)/\\EFI\\ubuntu\\shimx64.efi", + "HD(1,GPT,7F14DF43-6600-420A-9950-C028836F6A5D,0x800,0x64000)/\\EFI\\centos\\shim.efi", + "PciRoot(0x0)/Pci(0x17,0x0)/Sata(0x3,0x0,0x0)", + "UsbClass(0xFFFF,0xFFFF,0xFF,0xFF,0xFF)" + ] + }, + "SerialNumber": " ", + "SKU": " ", + "SystemType": "Physical", + "UUID": "00000000-0000-0000-0000-000000000000", + "MemorySummary": { + "Status": { + "HealthRollup": "OK" + }, + "TotalSystemMemoryGiB": 8 + } + + } } diff --git a/proliantutils/tests/redfish/resources/test_system.py b/proliantutils/tests/redfish/resources/test_system.py index 2bb518d..fba8c7b 100644 --- a/proliantutils/tests/redfish/resources/test_system.py +++ b/proliantutils/tests/redfish/resources/test_system.py @@ -30,7 +30,8 @@ class HPESystemTestCase(testtools.TestCase): self.conn = mock.MagicMock() with open('proliantutils/tests/redfish/' 'json_samples/system.json', 'r') as f: - self.conn.get.return_value.json.return_value = json.loads(f.read()) + system_json = json.loads(f.read()) + self.conn.get.return_value.json.return_value = system_json['Default'] self.sys_inst = system.HPESystem( self.conn, '/redfish/v1/Systems/1', diff --git a/proliantutils/tests/redfish/test_redfish.py b/proliantutils/tests/redfish/test_redfish.py index dbf3b1b..9f7ec59 100644 --- a/proliantutils/tests/redfish/test_redfish.py +++ b/proliantutils/tests/redfish/test_redfish.py @@ -23,6 +23,7 @@ from proliantutils import exception from proliantutils.redfish import main from proliantutils.redfish import redfish from proliantutils.redfish.resources.system import constants as sys_cons +from sushy.resources.system import system class RedfishOperationsTestCase(testtools.TestCase): @@ -72,7 +73,8 @@ class RedfishOperationsTestCase(testtools.TestCase): 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()) + system_json = json.loads(f.read()) + self.sushy.get_system().json = system_json['Default'] product_name = self.rf_client.get_product_name() self.assertEqual('ProLiant DL180 Gen10', product_name) @@ -149,3 +151,26 @@ class RedfishOperationsTestCase(testtools.TestCase): exception.IloError, 'The Redfish controller failed to press and hold power button', self.rf_client.hold_pwr_btn) + + def test_get_one_time_boot_not_set(self): + with open('proliantutils/tests/redfish/' + 'json_samples/system.json', 'r') as f: + system_json = json.loads(f.read()) + self.sushy.get_system().json = system_json['Default'] + boot = self.rf_client.get_one_time_boot() + self.assertEqual('Normal', boot) + + @mock.patch.object(redfish.RedfishOperations, '_get_sushy_system') + def test_get_one_time_boot_set_cdrom(self, get_system_mock): + with open('proliantutils/tests/redfish/' + 'json_samples/system.json', 'r') as f: + system_json = json.loads(f.read()) + self.conn = mock.Mock() + self.conn.get.return_value.json.return_value = system_json[ + 'System_op_for_one_time_boot_cdrom'] + self.sys_inst = system.System(self.conn, + '/redfish/v1/Systems/437XR1138R2', + redfish_version='1.0.2') + get_system_mock.return_value = self.sys_inst + ret = self.rf_client.get_one_time_boot() + self.assertEqual(ret, 'CDROM')