Bad firmware update for BMC/Host

Resolves openbmc/openbmc-test-automation#1923
Resolves openbmc/openbmc-test-automation#1934

Change-Id: I85358a27c8a323ce1ded2682c104ce89475e1c4c
Signed-off-by: Sushil Singh <susilsi7@in.ibm.com>
diff --git a/lib/rest_client.robot b/lib/rest_client.robot
index 7076ef8..6a5cdaa 100644
--- a/lib/rest_client.robot
+++ b/lib/rest_client.robot
@@ -338,17 +338,25 @@
     ...  timeout=${timeout}  quiet=${quiet}  &{kwargs}
     [Return]     ${resp}
 
+
 Upload Image To BMC
-    [Documentation]  Upload image to BMC device using REST POST operation.
-    [Arguments]  ${uri}  ${timeout}=10  ${quiet}=${1}  &{kwargs}
+    [Documentation]  Upload image to BMC via REST and return status code.
+    [Arguments]  ${uri}  ${timeout}=10  ${quiet}=${1}
+    ...  ${valid_status_codes}=[${HTTP_OK}]  &{kwargs}
 
     # Description of argument(s):
-    # uri             URI for uploading image via REST e.g. "/upload/image".
-    # timeout         Time allocated for the REST command to return status
-    #                 (specified in Robot Framework Time Format e.g. "3 mins").
-    # quiet           If enabled, turns off logging to console.
-    # kwargs          A dictionary keys/values to be passed directly to
-    #                 Post Request.
+    # uri                           URI for uploading image via REST e.g.
+    #                               "/upload/image".
+    # timeout                       Time allocated for the REST command to
+    #                               return status (specified in Robot
+    #                               Framework Time Format e.g. "3 mins").
+    # quiet                         If enabled, turns off logging to console.
+    # valid_status_codes            A list of status codes that are valid for
+    #                               the REST post command. This can be
+    #                               specified as a string the evaluates to a
+    #                               python object (e.g. [${HTTP_OK}]).
+    # kwargs                        A dictionary keys/values to be passed
+    #                               directly to Post Request.
 
     Initialize OpenBMC  ${timeout}  quiet=${quiet}
     ${base_uri}=  Catenate  SEPARATOR=  ${DBUS_PREFIX}  ${uri}
@@ -359,5 +367,8 @@
     ...  base_uri=${base_uri}  args=&{kwargs}
     ${ret}=  Post Request  openbmc  ${base_uri}  &{kwargs}  timeout=${timeout}
     Run Keyword If  '${quiet}' == '${0}'  Log Response  ${ret}
-    Should Be Equal As Strings  ${ret.status_code}  ${HTTP_OK}
+    Valid Value  ret.status_code  ${valid_status_codes}
     Delete All Sessions
+
+    [Return]  ${ret.status_code}
+
diff --git a/redfish/update_service/test_redfish_image_upload.robot b/redfish/update_service/test_redfish_image_upload.robot
new file mode 100644
index 0000000..98e9376
--- /dev/null
+++ b/redfish/update_service/test_redfish_image_upload.robot
@@ -0,0 +1,118 @@
+*** Settings ***
+Documentation         Test upload image with invalid images.
+...                   This test expects the following bad tarball image files
+...                   to exist in the BAD_IMAGES_DIR_PATH:
+...                       bmc_bad_manifest.ubi.mtd.tar
+...                       bmc_nokernel_image.ubi.mtd.tar
+...                       bmc_invalid_key.ubi.mtd.tar
+...                       pnor_bad_manifest.pnor.squashfs.tar
+...                       pnor_nokernel_image.pnor.squashfs.tar
+...                       pnor_invalid_key.pnor.squashfs.tar
+
+# Test Parameters:
+# OPENBMC_HOST         The BMC host name or IP address.
+# OPENBMC_USERNAME     The OS login userid.
+# OPENBMC_PASSWORD     The password for the OS login.
+# BAD_IMAGES_DIR_PATH  The path to the directory which contains the bad image files.
+
+Resource               ../../lib/connection_client.robot
+Resource               ../../lib/rest_client.robot
+Resource               ../../lib/openbmc_ffdc.robot
+Resource               ../../lib/bmc_redfish_resource.robot
+Resource               ../../lib/code_update_utils.robot
+Library                OperatingSystem
+Library                ../../lib/code_update_utils.py
+Library                ../../lib/gen_robot_valid.py
+
+Suite Setup            Suite Setup Execution
+Suite Teardown         Redfish.Logout
+Test Setup             Printn
+
+Force Tags  Upload_Test
+
+*** Variables ***
+${timeout}             20
+${QUIET}               ${1}
+${image_id}            ${EMPTY}
+
+*** Test Cases ***
+
+Redfish Failure to Upload BMC Image With Bad Manifest
+    [Documentation]  Upload a BMC firmware with a bad MANFIEST file.
+    [Tags]  Redfish_Failure_To_Upload_BMC_Image_With_Bad_Manifest
+    [Template]  Redfish Bad Firmware Update
+
+    # Image File Name
+    bmc_bad_manifest.ubi.mtd.tar
+
+
+Redfish Failure to Upload Empty BMC Image
+    [Documentation]  Upload a BMC firmware with no kernel image.
+    [Tags]  Redfish_Failure_To_Upload_Empty_BMC_Image
+    [Template]  Redfish Bad Firmware Update
+
+    # Image File Name
+    bmc_nokernel_image.ubi.mtd.tar
+
+
+Redfish Failure to Upload Host Image With Bad Manifest
+    [Documentation]  Upload a PNOR firmware with a bad MANIFEST file.
+    [Tags]  Redfish_Failure_To_Upload_Host_Image_With_Bad_Manifest
+    [Template]  Redfish Bad Firmware Update
+
+    # Image File Name
+    pnor_bad_manifest.pnor.squashfs.tar
+
+
+Redfish Failure to Upload Empty Host Image
+    [Documentation]  Upload a PNOR firmware with no kernel Image.
+    [Tags]  Redfish_Failure_To_Upload_Empty_Host_Image
+    [Template]  Redfish Bad Firmware Update
+
+    # Image File Name
+    pnor_nokernel_image.pnor.squashfs.tar
+
+
+*** Keywords ***
+
+Suite Setup Execution
+    [Documentation]  Do the suite setup.
+
+    Redfish.Login
+    Valid Dir Path  BAD_IMAGES_DIR_PATH
+    Delete All BMC Dump
+    Redfish Purge Event Log
+
+
+Redfish Bad Firmware Update
+    [Documentation]  Redfish firmware update.
+    [Arguments]  ${image_file_name}
+    [Teardown]  Test Teardown Execution
+
+    # Description of argument(s):
+    # image_file_name  The file name of the image.
+
+    ${image_file_path}=  OperatingSystem.Join Path  ${BAD_IMAGES_DIR_PATH}
+    ...  ${image_file_name}
+    Valid File Path  image_file_path
+    Set ApplyTime  policy=OnReset
+    ${image_data}=  OperatingSystem.Get Binary File  ${image_file_path}
+    ${status_code}=  Upload Image To BMC
+    ...  ${REDFISH_BASE_URI}UpdateService
+    ...  ${timeout}
+    ...  valid_status_codes=[${HTTP_OK}, ${HTTP_INTERNAL_SERVER_ERROR}]
+    ...  data=${image_data}
+
+    Return From Keyword If  ${status_code} == ${HTTP_INTERNAL_SERVER_ERROR}
+
+    ${image_id}=  Get Latest Image ID
+    Rprint Vars  image_id
+    Check Image Update Progress State
+    ...  match_state='Updating', 'Disabled'  image_id=${image_id}
+
+Test Teardown Execution
+    [Documentation]  Do the post test teardown.
+
+    FFDC On Test Case Fail
+    Run Keyword If  '${image_id}'  Delete Software Object
+    ...  /xyz/openbmc_project/software/${image_id}