Added tests for deleting PNOR and BMC images

Resolves openbmc/openbmc-test-automation#832

Change-Id: I0efd585f9403cb5140039c0e38149ebda6961864
Signed-off-by: Charles Paul Hofer <Charles.Hofer@ibm.com>
diff --git a/extended/code_update/code_update.robot b/extended/code_update/code_update.robot
index 5398d7e..f365dfe 100644
--- a/extended/code_update/code_update.robot
+++ b/extended/code_update/code_update.robot
@@ -16,6 +16,7 @@
 
 Library           ../../lib/code_update_utils.py
 Library           OperatingSystem
+Library           String
 Variables         ../../data/variables.py
 Resource          ../lib/rest_client.robot
 Resource          ../lib/openbmc_ffdc.robot
@@ -23,6 +24,7 @@
 Resource          ../../lib/boot_utils.robot
 Resource          ../../lib/utils.robot
 Resource          code_update_utils.robot
+Resource          ../../lib/state_manager.robot
 
 Test Teardown     Code Update Teardown
 
@@ -95,6 +97,27 @@
     Priority          ${127}
 
 
+Delete Host Image
+    [Documentation]  Delete a PNOR image from the BMC and PNOR flash chip.
+    [Tags]  Delete_Host_Image
+    [Setup]  Initiate Host PowerOff
+
+    ${software_objects}=  Get Software Objects
+    ...  version_type=${VERSION_PURPOSE_HOST}
+    ${num_images}=  Get Length  ${software_objects}
+    Should Be True  0 < ${num_images}
+    ...  msg=There are no PNOR images on the BMC to delete.
+    Delete Image And Verify  @{software_objects}[0]  ${VERSION_PURPOSE_HOST}
+
+
+Delete BMC Image
+    [Documentation]  Delete a BMC image from the BMC flash chip.
+    [Tags]  Delete_BMC_Image
+
+    ${software_object}=  Get Non Running BMC Software Object
+    Delete Image And Verify  ${software_object}  ${VERSION_PURPOSE_BMC}
+
+
 *** Keywords ***
 
 Set PNOR Attribute
@@ -128,10 +151,6 @@
 Code Update Teardown
     [Documentation]  Do code update test case teardown.
 
-    #TODO: Use the Delete interface instead once delivered
-    Open Connection And Log In
-    Execute Command On BMC  rm -rf /tmp/images/*
-
     Close All Connections
     FFDC On Test Case Fail
 
@@ -145,3 +164,28 @@
     ${version}= Execute Command On BMC
     ...  "grep \"extended_version=\" " + ${path}
     [return] ${version.split(",")}
+
+Delete Image And Verify
+    [Documentation]  Delete an image from the BMC and verify that it was
+    ...              removed from software and the /tmp/images directory.
+    [Arguments]  ${software_object}  ${version_type}
+
+    # Description of argument(s):
+    # software_object        The URI of the software object to delete.
+    # version_type  The type of the software object, e.g.
+    #               xyz.openbmc_project.Software.Version.VersionPurpose.Host
+    #               or xyz.openbmc_project.Software.Version.VersionPurpose.BMC.
+
+    # Delete the image.
+    Delete Software Object  ${software_object}
+    # TODO: If/when we don't have to delete twice anymore, take this out
+    Run Keyword And Ignore Error  Delete Software Object  ${software_object}
+
+    # Verify that it's gone from software.
+    ${software_objects}=  Get Software Objects  version_type=${version_type}
+    Should Not Contain  ${software_objects}  ${software_object}
+
+    # Check that there is no file in the /tmp/images directory.
+    ${image_id}=  Fetch From Right  ${software_object}  /
+    BMC Execute Command
+    ...  [ ! -d "/tmp/images/${image_id}" ]
diff --git a/lib/code_update_utils.py b/lib/code_update_utils.py
index a785fe6..44a3ea0 100644
--- a/lib/code_update_utils.py
+++ b/lib/code_update_utils.py
@@ -20,6 +20,31 @@
 from robot.libraries.BuiltIn import BuiltIn
 
 ###############################################################################
+def get_non_running_bmc_software_object():
+
+    r"""
+    Get the URI to a BMC image from software that is not running on the BMC.
+    """
+
+    # Get the version of the image currently running on the BMC.
+    _, cur_img_version = keyword.run_key("Get BMC Version")
+    # Remove the surrounding double quotes from the version.
+    cur_img_version = cur_img_version.replace('"', '')
+
+    _, images = keyword.run_key("Read Properties  "
+                                + var.SOFTWARE_VERSION_URI + "enumerate")
+
+    for image_name in images:
+        _, image_properties = keyword.run_key(
+                "Get Host Software Property  " + image_name)
+        if image_properties['Version'] != cur_img_version:
+            return image_name
+    BuiltIn().fail("Did not find any non-running BMC images.")
+
+###############################################################################
+
+
+###############################################################################
 def delete_all_pnor_images():
 
     r"""
diff --git a/lib/code_update_utils.robot b/lib/code_update_utils.robot
index 44eaa1f..ce1eb85 100644
--- a/lib/code_update_utils.robot
+++ b/lib/code_update_utils.robot
@@ -58,3 +58,15 @@
     ${args}=  Create Dictionary  data=${data}
     Write Attribute  ${host_object}  ${sw_attribute}  data=${args}
 
+Delete Software Object
+    [Documentation]  Deletes an image from the BMC.
+    [Arguments]  ${software_object}
+
+    # Description of argument(s):
+    # software_object  The URI to the software image to delete.
+
+    ${arglist}=  Create List
+    ${args}=  Create Dictionary  data=${arglist}
+    ${resp}=  OpenBMC Post Request  ${software_object}/action/delete
+    ...  data=${args}
+    Should Be Equal As Strings  ${resp.status_code}  ${HTTP_OK}