Added test cases for PNOR code upload

Resolves openbmc/openbmc-test-automation#766

Change-Id: I1f530b4f0ca57dd83962de2e7dfad20dbab3b526
Signed-off-by: Charles P. Hofer <charles.hofer@ibm.com>
diff --git a/extended/test_uploadimage.py b/extended/test_uploadimage.py
index 44a0963..e4ac71b 100644
--- a/extended/test_uploadimage.py
+++ b/extended/test_uploadimage.py
@@ -171,3 +171,30 @@
         return False
 
 ###############################################################################
+
+
+###############################################################################
+def verify_image_not_in_bmc_uploads_dir(image_version):
+
+    r"""
+    Check that an image with the given version is not unpacked inside of the
+    BMCs image uploads directory. If no image is found, retry every 30 seconds
+    for 3 minutes in case the BMC takes time unpacking the image.
+
+    Description of argument(s):
+    image_version  The version of the image to look for on the BMC.
+    """
+
+    grk.run_key('Open Connection And Log In')
+    upload_dir_path = BuiltIn().get_variable_value("${UPLOAD_DIR_PATH}")
+    for i in range(6):
+        stat, grep_res = grk.run_key('Execute Command On BMC  '
+                + 'ls ' + upload_dir_path + '*/MANIFEST 2>/dev/null '
+                + '| xargs grep -rl "version=' + image_version + '"')
+        image_dir = os.path.dirname(grep_res.split('\n')[0])
+        if '' != image_dir:
+            grk.run_key('Execute Command On BMC  rm -rf ' + image_dir)
+            BuiltIn().fail('Found invalid BMC Image: ' + image_dir)
+        time.sleep(30)
+
+###############################################################################
diff --git a/extended/test_uploadimage.robot b/extended/test_uploadimage.robot
index 46a4829..008596c 100644
--- a/extended/test_uploadimage.robot
+++ b/extended/test_uploadimage.robot
@@ -1,10 +1,14 @@
 *** Settings ***
-Documentation         Test Upload Image
+Documentation         Test upload image with both valid and invalid images.
+...                   This test expects there to be bad image tarballs named
+...                   pnor_bad_manifest.tar and pnor_no_image.tar on the TFTP
+...                   server and in the directory BAD_IMAGES_DIR_PATH.
 ...                   Execution Method :
 ...                   python -m robot -v OPENBMC_HOST:<hostname>
 ...                   -v TFTP_SERVER:<TFTP server IP>
 ...                   -v TFTP_FILE_NAME:<filename.tar>
 ...                   -v IMAGE_FILE_PATH:<path/*.tar> test_uploadimage.robot
+...                   -v BAD_IMAGES_DIR_PATH:<path>
 
 Resource              ../lib/connection_client.robot
 Resource              ../lib/rest_client.robot
@@ -53,6 +57,62 @@
     ${ret}=  Verify Image Upload
     Should Be True  True == ${ret}
 
+Upload Image With Bad Manifest Via REST
+    [Documentation]  Upload an image with a MANIFEST with an invalid
+    ...              purpose via REST and make sure the BMC does not unpack it.
+    [Tags]  Upload_Image_With_Bad_Manifest_Via_REST
+
+    ${bad_image_file_path}=  OperatingSystem.Join Path  ${BAD_IMAGES_DIR_PATH}
+    ...  pnor_bad_manifest.tar
+    OperatingSystem.File Should Exist  ${bad_image_file_path}
+    ...  msg=Invalid PNOR image pnor_bad_manifest.tar not found
+    ${bad_image_version}=  Get Version Tar  ${bad_image_file_path}
+    ${bad_image_data}=  OperatingSystem.Get Binary File  ${bad_image_file_path}
+    Upload Post Request  /upload/image  data=${bad_image_data}
+    Verify Image Not In BMC Uploads Dir  ${bad_image_version}
+
+Upload Image With Bad Manifest Via TFTP
+    [Documentation]  Upload an image with a MANIFEST with an invalid
+    ...              purpose via TFTP and make sure the BMC does not unpack it.
+    [Tags]  Upload_Image_With_Bad_Manifest_Via_TFTP
+
+    @{image}=  Create List  pnor_bad_manifest.tar  ${TFTP_SERVER}
+    ${data}=  Create Dictionary  data=@{image}
+    ${resp}=  OpenBMC Post Request
+    ...  ${SOFTWARE_VERSION_URI}/action/DownloadViaTFTP  data=${data}
+    Should Be Equal As Strings  ${resp.status_code}  ${HTTP_OK}
+    ${bad_image_version}=  Get Image Version From TFTP Server
+    ...  pnor_bad_manifest.tar
+    Verify Image Not In BMC Uploads Dir  ${bad_image_version}
+
+Upload Image With No Squashfs Via REST
+    [Documentation]  Upload an image with no pnor.xz.suashfs file via REST and
+    ...              make sure the BMC does not unpack it.
+    [Tags]  Upload_Image_With_No_Squashfs_Via_REST
+
+    ${bad_image_file_path}=  OperatingSystem.Join Path  ${BAD_IMAGES_DIR_PATH}
+    ...  pnor_no_image.tar
+    OperatingSystem.File Should Exist  ${bad_image_file_path}
+    ...  msg=Invalid PNOR image pnor_no_image.tar not found
+    ${bad_image_version}=  Get Version Tar  ${bad_image_file_path}
+    ${bad_image_data}=  OperatingSystem.Get Binary File  ${bad_image_file_path}
+    Upload Post Request  /upload/image  data=${bad_image_data}
+    Verify Image Not In BMC Uploads Dir  ${bad_image_version}
+
+Upload Image With No Squashfs Via TFTP
+    [Documentation]  Upload an image with no pnor.xz.suashfs file via TFTP and
+    ...              make sure the BMC does not unpack it.
+    [Tags]  Upload_Image_With_No_Squashfs_Via_TFTP
+
+    @{image}=  Create List  pnor_no_image.tar  ${TFTP_SERVER}
+    ${data}=  Create Dictionary  data=@{image}
+    ${resp}=  OpenBMC Post Request
+    ...  ${SOFTWARE_VERSION_URI}/action/DownloadViaTFTP  data=${data}
+    Should Be Equal As Strings  ${resp.status_code}  ${HTTP_OK}
+    ${bad_image_version}=  Get Image Version From TFTP Server
+    ...  pnor_no_image.tar
+    Verify Image Not In BMC Uploads Dir  ${bad_image_version}
+
 *** Keywords ***
 
 Upload Image Teardown
@@ -65,7 +125,7 @@
 Upload Post Request
     [Arguments]  ${uri}  ${timeout}=10  ${quiet}=${QUIET}  &{kwargs}
 
-    # Description of arguments:
+    # Description of argument(s):
     # uri             URI for uploading image via REST.
     # timeout         Time allocated for the REST command to return status.
     # quiet           If enabled turns off logging to console.
@@ -81,3 +141,22 @@
     ${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}
+
+
+Get Image Version From TFTP Server
+    [Documentation]  Get the version dfound in the MANIFEST file of
+    ...              an image on the given TFTP server.
+    [Arguments]  ${image_file_path}
+
+    # Description of argument(s):
+    # image_file_path  The path to the image on the TFTP server,
+    #                  ommitting a leading /.
+
+    ${rc}=  OperatingSystem.Run And Return RC
+    ...  curl -s tftp://${TFTP_SERVER}/${image_file_path} > bad_image.tar
+    Should Be Equal As Integers  0  ${rc}
+    ...  msg=Could not download image to check version.
+    ${version}=  Get Version Tar  bad_image.tar
+    OperatingSystem.Remove File  bad_image.tar
+    [Return]  ${version}
+
diff --git a/lib/resource.txt b/lib/resource.txt
index d3c3cb1..d457ad6 100755
--- a/lib/resource.txt
+++ b/lib/resource.txt
@@ -51,6 +51,7 @@
 ${TFTP_SERVER}       ${EMPTY}
 ${TFTP_FILE_NAME}    ${EMPTY}
 ${IMAGE_FILE_PATH}   ${EMPTY}
+${BAD_IMAGES_DIR_PATH}    ${EMPTY}
 
 *** Keywords ***
 Get Inventory Schema
diff --git a/tools/generate_argumentfile.sh b/tools/generate_argumentfile.sh
index 0529469..a19aca1 100755
--- a/tools/generate_argumentfile.sh
+++ b/tools/generate_argumentfile.sh
@@ -25,3 +25,4 @@
 echo "--variable TFTP_SERVER:$TFTP_SERVER" >> $ARG_FILE
 echo "--variable TFTP_FILE_NAME:$TFTP_FILE_NAME" >> $ARG_FILE
 echo "--variable IMAGE_FILE_PATH:$IMAGE_FILE_PATH" >> $ARG_FILE
+echo "--variable BAD_IMAGES_DIR_PATH:$BAD_IMAGES_DIR_PATH" >> $ARG_FILE