Code update Applytime - onreset, immediate

Resolves openbmc/openbmc-test-automation#1869

Change-Id: I99cd33ac68ef94a42cb3eeaf532c4cde3679c35f
Signed-off-by: Sushil Singh <susilsi7@in.ibm.com>
diff --git a/redfish/update_service/redfish_bmc_code_update.robot b/redfish/update_service/redfish_bmc_code_update.robot
index aa6d999..d87ba60 100644
--- a/redfish/update_service/redfish_bmc_code_update.robot
+++ b/redfish/update_service/redfish_bmc_code_update.robot
@@ -1,5 +1,13 @@
 *** Settings ***
-Documentation     Update the BMC code on a target BMC via Redifsh.
+Documentation     Update firmware on a target BMC via Redifsh.
+
+# Test Parameters:
+# IMAGE_FILE_PATH    The path to the BMC image file.
+#
+# Firmware update states:
+#     Enabled        Image is installed and either functional or active.
+#     Disabled       Image installation failed or ready for activation.
+#     Updating       Image installation currently in progress.
 
 Resource          ../../lib/resource.robot
 Resource          ../../lib/bmc_redfish_resource.robot
@@ -8,7 +16,9 @@
 Resource          ../../lib/code_update_utils.robot
 Resource          ../../lib/dump_utils.robot
 Resource          ../../lib/logging_utils.robot
+Resource          ../../lib/redfish_code_update_utils.robot
 Library           ../../lib/gen_robot_valid.py
+Library           ../../lib/tftp_update_utils.py
 
 Suite Setup       Suite Setup Execution
 Suite Teardown    Redfish.Logout
@@ -23,31 +33,22 @@
 
 *** Test Cases ***
 
-Redfish BMC Code Update
-    [Documentation]  Do a BMC code update by uploading image on BMC via redfish.
-    [Tags]  Redfish_BMC_Code_Update
+Redfish Code Update With ApplyTime OnReset
+    [Documentation]  Update the firmaware image with ApplyTime of OnReset.
+    [Tags]  Redfish_Code_Update_With_ApplyTime_OnReset
+    [Template]  Redfish Update Firmware
 
-    Set ApplyTime  policy=${onreset}
+    # policy
+    ${onreset}
 
-    ${base_redfish_uri}=  Set Variable  ${REDFISH_BASE_URI}UpdateService
 
-    Redfish Upload Image  ${base_redfish_uri}  ${IMAGE_FILE_PATH}
+Redfish Code Update With ApplyTime Immediate
+    [Documentation]  Update the firmaware image with ApplyTime of Immediate.
+    [Tags]  Redfish_Code_Update_With_ApplyTime_Immediate
+    [Template]  Redfish Update Firmware
 
-    ${image_id}=  Get Latest Image ID
-    Rprint Vars  image_id
-
-    Check Image Update Progress State  match_state='Disabled', 'Updating'  image_id=${image_id}
-
-    # Wait a few seconds to check if the update progress started.
-    Sleep  5s
-    Check Image Update Progress State  match_state='Updating'  image_id=${image_id}
-
-    Wait Until Keyword Succeeds  5 min  20 sec
-    ...  Check Image Update Progress State  match_state='Enabled'  image_id=${image_id}
-
-    Redfish OBMC Reboot (off)
-    Redfish.Login
-    Redfish Verify BMC Version  ${IMAGE_FILE_PATH}
+    # policy
+    ${immediate}
 
 
 *** Keywords ***
@@ -64,3 +65,57 @@
     # Checking for file existence.
     OperatingSystem.File Should Exist  ${IMAGE_FILE_PATH}
 
+
+Redfish Update Firmware
+    [Documentation]  Code update with ApplyTime and verify installation.
+    [Arguments]  ${apply_time}
+
+    # Description of argument(s):
+    # policy     ApplyTime allowed values (e.g. "OnReset", "Immediate").
+
+    ${state}=  Get Pre Reboot State
+    Rprint Vars  state
+
+    Set ApplyTime  policy=${apply_time}
+
+    Redfish Upload Image  ${REDFISH_BASE_URI}UpdateService  ${IMAGE_FILE_PATH}
+
+    ${image_id}=  Get Latest Image ID
+    Rprint Vars  image_id
+
+    Check Image Update Progress State
+    ...  match_state='Disabled', 'Updating'  image_id=${image_id}
+
+    # Wait a few seconds to check if the update progress started.
+    Sleep  5s
+    Check Image Update Progress State
+    ...  match_state='Updating'  image_id=${image_id}
+
+    Wait Until Keyword Succeeds  8 min  20 sec
+    ...  Check Image Update Progress State
+    ...    match_state='Enabled'  image_id=${image_id}
+
+    Reboot BMC And Verify BMC Image
+    ...  ${apply_time}  start_boot_seconds=${state['epoch_seconds']}
+
+
+Reboot BMC And Verify BMC Image
+    [Documentation]  Reboot or wait for BMC standby post reboot and
+    ...  verify installed image is functional.
+    [Arguments]  ${apply_time}  ${start_boot_seconds}
+
+    # Description of argument(s):
+    # policy                ApplyTime allowed values (e.g. "OnReset", "Immediate").
+    # start_boot_seconds    See 'Wait For Reboot' for details.
+
+    Run Keyword if  'OnReset' == '${apply_time}'
+    ...  Run Keywords
+    ...      Redfish OBMC Reboot (off)  AND
+    ...      Redfish.Login  AND
+    ...      Redfish Verify BMC Version  ${IMAGE_FILE_PATH}
+    ...  ELSE
+    ...    Run Keywords
+    ...        Wait For Reboot  start_boot_seconds=${start_boot_seconds}  AND
+    ...        Redfish.Login  AND
+    ...        Redfish Verify BMC Version  ${IMAGE_FILE_PATH}
+