Add option to skip code update if image is already on the BMC

Resolves openbmc/openbmc-test-automation#1008

Change-Id: I87764de485bb9d78918b8f44b486a175a34dea55
Signed-off-by: Charles Paul Hofer <Charles.Hofer@ibm.com>
diff --git a/extended/code_update/bmc_code_update.robot b/extended/code_update/bmc_code_update.robot
index 99be4ef..f9e0b15 100644
--- a/extended/code_update/bmc_code_update.robot
+++ b/extended/code_update/bmc_code_update.robot
@@ -19,6 +19,7 @@
 ${QUIET}                          ${1}
 ${IMAGE_FILE_PATH}                ${EMPTY}
 ${ALTERNATE_IMAGE_FILE_PATH}      ${EMPTY}
+${SKIP_UPDATE_IF_ACTIVE}          false
 
 *** Test Cases ***
 
@@ -27,7 +28,7 @@
     [Tags]  REST_BMC_Code_Update
     [Setup]  Code Update Setup
 
-    Upload And Activate Image  ${IMAGE_FILE_PATH}
+    Upload And Activate Image  ${IMAGE_FILE_PATH}  ${SKIP_UPDATE_IF_ACTIVE}
     OBMC Reboot (off)
 
 
diff --git a/lib/code_update_utils.robot b/lib/code_update_utils.robot
index 82fbc94..e166b0f 100644
--- a/lib/code_update_utils.robot
+++ b/lib/code_update_utils.robot
@@ -136,12 +136,14 @@
 
 Upload And Activate Image
     [Documentation]  Upload an image to the BMC and activate it with REST.
-    [Arguments]  ${image_file_path}  ${wait}=${1}
+    [Arguments]  ${image_file_path}  ${wait}=${1}  ${skip_if_active}=false
 
     # Description of argument(s):
     # image_file_path     The path to the image tarball to upload and activate.
     # wait                Indicates that this keyword should wait for host or
     #                     BMC activation is completed.
+    # skip_if_active      If set to true, will skip the code update if this
+    #                     image is already on the BMC.
 
     OperatingSystem.File Should Exist  ${image_file_path}
     ${image_version}=  Get Version Tar  ${image_file_path}
@@ -151,8 +153,13 @@
     ${ret}  ${version_id}=  Verify Image Upload  ${image_version}
     Should Be True  ${ret}
 
-    # Verify the image is 'READY' to be activated.
+    # Verify the image is 'READY' to be activated or if it's already active,
+    # set priority to 0 and reboot the BMC.
     ${software_state}=  Read Properties  ${SOFTWARE_VERSION_URI}${version_id}
+    ${activation}=  Set Variable  &{software_state}[Activation]
+    Run Keyword If
+    ...  '${skip_if_active}' == 'true' and '${activation}' == '${ACTIVE}'
+    ...  Switch To Active Image And Pass  ${SOFTWARE_VERSION_URI}${version_id}
     Should Be Equal As Strings  &{software_state}[Activation]  ${READY}
 
     # Request the image to be activated.
@@ -172,6 +179,20 @@
     Should Be Equal As Strings  &{software_state}[Activation]  ${ACTIVE}
 
 
+Switch To Active Image And Pass
+    [Documentation]  Make the given active image the image running on the BMC
+    ...              and pass the test.
+    [Arguments]  ${software_object}
+
+    # Description of argument(s):
+    # software_object  Software object path.
+    #                  (e.g. "/xyz/openbmc_project/software/f3b29aa8").
+
+    Set Host Software Property  ${software_object}  Priority  ${0}
+    OBMC Reboot (off)
+    Pass Execution  ${software_object} was already on the BMC.
+
+
 Activate Image And Verify No Duplicate Priorities
     [Documentation]  Upload an image, and then check that no images have the
     ...              same priority.
diff --git a/lib/resource.txt b/lib/resource.txt
index 6a3690d..34db7a0 100755
--- a/lib/resource.txt
+++ b/lib/resource.txt
@@ -56,6 +56,7 @@
 ${PNOR_IMAGE_FILE_PATH}         ${EMPTY}
 ${BMC_IMAGE_FILE_PATH}          ${EMPTY}
 ${BAD_IMAGES_DIR_PATH}          ${EMPTY}
+${SKIP_UPDATE_IF_ACTIVE}        false
 
 # The caller must set this to the string "true" in order to delete images. The
 # code is picky.
diff --git a/tools/generate_argumentfile.sh b/tools/generate_argumentfile.sh
index 8fd4cea..81ac987 100755
--- a/tools/generate_argumentfile.sh
+++ b/tools/generate_argumentfile.sh
@@ -30,6 +30,7 @@
 echo "--variable PNOR_IMAGE_FILE_PATH:$PNOR_IMAGE_FILE_PATH" >> $ARG_FILE
 echo "--variable BMC_IMAGE_FILE_PATH:$BMC_IMAGE_FILE_PATH" >> $ARG_FILE
 echo "--variable BAD_IMAGES_DIR_PATH:$BAD_IMAGES_DIR_PATH" >> $ARG_FILE
+echo "--variable SKIP_UPDATE_IF_ACTIVE:$SKIP_UPDATE_IF_ACTIVE" >> $ARG_FILE
 echo "--variable DELETE_OLD_PNOR_IMAGES:$DELETE_OLD_PNOR_IMAGES" >> $ARG_FILE
 echo "--variable DELETE_OLD_GUARD_FILE:$DELETE_OLD_GUARD_FILE" >> $ARG_FILE
 echo "--variable LAST_KNOWN_GOOD_VERSION:$LAST_KNOWN_GOOD_VERSION" >> $ARG_FILE