TFTP download and install for BMC and Host image

Changes:
   - New test suite to test TFTP image installation.

Change-Id: Ib65b5dd82b905211963cc3e6504d4ab59e465f92
Signed-off-by: George Keishing <gkeishin@in.ibm.com>
diff --git a/lib/code_update_utils.robot b/lib/code_update_utils.robot
index 406d2cf..cea4721 100644
--- a/lib/code_update_utils.robot
+++ b/lib/code_update_utils.robot
@@ -473,3 +473,67 @@
 
     Directory Should Exist  ${ACTIVATION_DIR_PATH}
     ...  msg=${ACTIVATION_DIR_PATH} does not exist. Therefore, the image is not signed.
+
+
+Get Latest Image ID
+    [Documentation]  Return the ID of the most recently extracted image.
+    # Note: This keyword will fail if there is no such file.
+
+    # Example: # ls /tmp/images/
+    #            1b714fb7
+    ${image_id}=  Get Latest File  /tmp/images/
+    Rvalid Value  image_id
+
+    # Though an image sub-directory was found, it really isn't valid unless
+    # the MANIFEST file is present.
+    BMC Execute Command  ls -l /tmp/images/${image_id}/MANIFEST
+
+    [Return]  ${image_id}
+
+
+Check Image Update Progress State
+    [Documentation]  Check that the image update progress state matches the specified state.
+    [Arguments]  ${match_state}  ${image_id}
+
+    # Description of argument(s):
+    # match_state    The expected state. This may be one or more comma-separated values
+    #                (e.g. "Disabled", "Disabled, Updating"). If the actual state matches
+    #                any of the states named in this argument, this keyword passes.
+    # image_id       The image ID (e.g. "1b714fb7").
+
+    ${state}=  Get Image Update Progress State  image_id=${image_id}
+    Rvalid Value  state  valid_values=[${match_state}]
+
+
+Get Image Update Progress State
+    [Documentation]  Return the current state of the image update.
+    [Arguments]  ${image_id}
+
+    # Description of argument(s):
+    # image_id         The image ID (e.g. "1b714fb7").
+
+    # In this example, this keyword would return the value "Enabled".
+    #  "Status": {
+    #              "Health": "OK",
+    #              "HealthRollup": "OK",
+    #              "State": "Enabled"
+    #            },
+    ${status}=  Redfish.Get Attribute  /redfish/v1/UpdateService/FirmwareInventory/${image_id}  Status
+    Rprint Vars  status
+
+    [Return]  ${status["State"]}
+
+
+Get Firmware Image Version
+    [Documentation]  Get the version of the currently installed firmware and return it.
+    [Arguments]  ${image_id}
+
+    # Description of argument(s):
+    # image_id      The image ID (e.g. "1b714fb7").
+
+    # Example of a version returned by this keyword:
+    # 2.8.0-dev-19-g6d5764b33
+    ${version}=  Redfish.Get Attribute  /redfish/v1/UpdateService/FirmwareInventory/${image_id}  Version
+    Rprint Vars  version
+
+    [Return]  ${version}
diff --git a/redfish/update_service/test_firmware_tftp_upload_image.robot b/redfish/update_service/test_firmware_tftp_upload_image.robot
new file mode 100644
index 0000000..3a4e278
--- /dev/null
+++ b/redfish/update_service/test_firmware_tftp_upload_image.robot
@@ -0,0 +1,78 @@
+*** Settings ***
+Documentation    Firmware image (BMC and Host) upload test using TFTP protocol.
+
+# Test Parameters:
+# TFTP_SERVER        The TFTP server host name or IP address.
+# IMAGE_FILE_NAME    The BMC or Host image file name.
+#
+# 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/boot_utils.robot
+Resource         ../../lib/bmc_redfish_resource.robot
+Resource         ../../lib/openbmc_ffdc.robot
+Resource         ../../lib/code_update_utils.robot
+Library          ../../lib/code_update_utils.py
+Library          ../../lib/gen_robot_valid.py
+
+Suite Setup      Suite Setup Execution
+Suite Teardown   Redfish.Logout
+Test Setup       Printn
+Test Teardown    FFDC On Test Case Fail
+
+Force Tags       tftp_update
+
+*** Test Cases ***
+
+TFTP Download Install With ApplyTime OnReset Policy
+    [Documentation]  Download image to BMC using TFTP with OnReset policy and verify installation.
+    [Tags]  TFTP_Download_Install_With_ApplyTime_OnReset_Policy
+
+    # Set and verify the firmware OnReset policy.
+    Redfish.Patch  ${REDFISH_BASE_URI}UpdateService  body={'ApplyTime' : 'OnReset'}
+    ${apply_time}=  Read Attribute   ${SOFTWARE_VERSION_URI}apply_time  RequestedApplyTime
+    Rvalid Value  apply_time  valid_values=['xyz.openbmc_project.Software.ApplyTime.RequestedApplyTimes.OnReset']
+    Rprint Vars  apply_time
+
+    # Download image from TFTP server to BMC.
+    Redfish.Post  /redfish/v1/UpdateService/Actions/UpdateService.SimpleUpdate
+    ...  body={"TransferProtocol" : "TFTP", "ImageURI" : "${TFTP_SERVER}/${IMAGE_FILE_NAME}"}
+
+    # Wait for image tar file to download complete.
+    ${image_id}=  Wait Until Keyword Succeeds  60 sec  10 sec  Get Latest Image ID
+    Rprint Vars  image_id
+
+    # Let the image get extracted and it should not fail.
+    Sleep  5s
+    Check Image Update Progress State  match_state='Disabled', 'Updating'  image_id=${image_id}
+
+    # Get image version currently installation in progress.
+    ${install_version}=  Get Firmware Image Version  image_id=${image_id}
+    Rprint Vars  install_version
+
+    Check Image Update Progress State  match_state='Updating'  image_id=${image_id}
+
+    # Wait for the image to install complete.
+    Wait Until Keyword Succeeds  5 min  15 sec
+    ...  Check Image Update Progress State  match_state='Enabled'  image_id=${image_id}
+
+    Redfish OBMC Reboot (off)
+
+    # Verify the image is installed and functional.
+    ${cmd}=  Set Variable  grep ^VERSION_ID= /etc/os-release | cut -f 2 -d '=' | sed 's/"//g'
+    ${functional_version}  ${stderr}  ${rc}=  BMC Execute Command  ${cmd}
+    Rvalid Value  functional_version  valid_values=['${install_version}']
+    Rprint Vars  functional_version
+
+
+*** Keywords ***
+
+Suite Setup Execution
+    [Documentation]  Do the suite setup.
+
+    Redfish.Login
+    Rvalid Value  TFTP_SERVER
+    Rvalid Value  IMAGE_FILE_NAME