Automated test case to check the firmware version

Test Cases:
       - Primary functional
       - Backup or alternate 
       - Switch to backup 
       - Add code logic to handle template
       
Change-Id: I578c6a5b4d69962f04772af02a260df25b2da2ef
Signed-off-by: Sushil Singh <susilsi7@in.ibm.com>
diff --git a/lib/redfish_code_update_utils.robot b/lib/redfish_code_update_utils.robot
index 8afff2e..0d7fc56 100644
--- a/lib/redfish_code_update_utils.robot
+++ b/lib/redfish_code_update_utils.robot
@@ -145,6 +145,24 @@
     [Return]  ${list_inv_dict}[0]
 
 
+Get Non Functional Firmware List
+    [Documentation]  Get BMC non functional firmware details.
+    [Arguments]  ${sw_inv}  ${functional_state}
+
+    # Description of argument(s):
+    # sw_inv            This dictionary contains all the BMC firmware details.
+    # functional_state  Functional state can be either True or False.
+
+    ${list_inv}=  Create List
+
+    FOR  ${key}  IN  @{sw_inv.keys()}
+      Run Keyword If  '${sw_inv['${key}']['functional']}' == '${functional_state}'
+      ...  Append To List  ${list_inv}  ${sw_inv['${key}']}
+    END
+
+    [Return]  ${list_inv}
+
+
 Redfish Upload Image And Check Progress State
     [Documentation]  Code update with ApplyTime.
 
diff --git a/redfish/update_service/test_firmware_inventory.robot b/redfish/update_service/test_firmware_inventory.robot
index 996939f..2ffd9df 100644
--- a/redfish/update_service/test_firmware_inventory.robot
+++ b/redfish/update_service/test_firmware_inventory.robot
@@ -106,6 +106,7 @@
       Run Keyword If  '${entry}' == '${actual_count}'  Fail  BMC version not there in Firmware Inventory
     END
 
+
 Verify UpdateService Supports TransferProtocol TFTP
     [Documentation]  Verify update service supported values have TFTP protocol.
     [Tags]  Verify_UpdateService_Supports_TransferProtocol_TFTP
@@ -161,34 +162,27 @@
       # "Updateable": true,
 
       Should Be Equal As Strings  ${resp}  True
-
     END
 
 
-Verify Redfish Functional Version Is Same
-    [Documentation]  Verify the redfish firmware inventory path version is same as redfish managers.
-    [Tags]  Verify_Redfish_Functional_Version_Is_Same
+Check Redfish Functional Image Version Is Same
+    [Documentation]  Verify functional image version is same as in Redfish managers.
+    [Tags]  Check_Redfish_Functional_Image_Version_Is_Same
+    [Template]  Verify Redfish Functional Image Version Is Same
 
-    # User defined state for software objects.
-    # Note: "Functional" term refers to firmware which system is currently booted with.
+    # image
+    functional_image
+    backup_image
 
-    # sw_inv_dict:
-    #   [b9101858]:
-    #     [image_type]:                 BMC update
-    #     [image_id]:                   b9101858
-    #     [functional]:                 True
-    #     [version]:                    2.8.0-dev-150-g04508dc9f
 
-    ${sw_inv_dict}=  Get Functional Firmware  BMC image
-    ${sw_inv_dict}=  Get Non Functional Firmware  ${sw_inv_dict}  True
+Check Redfish Backup Image Version Is Same
+    [Documentation]  Switch to backup image and then verify functional image version
+    ...  is same as in Redfish managers..
+    [Tags]  Check_Redfish_Backup_Image_Version_Is_Same
+    [Template]  Verify Redfish Functional Image Version Is Same
 
-    # /redfish/v1/Managers/bmc
-    # "FirmwareVersion": "2.8.0-dev-150-g04508dc9f"
-
-    ${firmware_version}=  Redfish.Get Attribute
-    ...  ${REDFISH_BASE_URI}Managers/bmc  FirmwareVersion
-
-    Should Be Equal  ${sw_inv_dict['version']}  ${firmware_version}
+    # image
+    switch_backup_image
 
 
 Verify Redfish Software Image And Firmware Inventory Are Same
@@ -236,3 +230,86 @@
 
     FFDC On Test Case Fail
     Redfish.Logout
+
+
+Verify Firmware Version Same In Firmware Inventory And Managers
+    [Documentation]  Verify the redfish firmware inventory path version is same as Redfish managers.
+
+    # User defined state for software objects.
+    # Note: "Functional" term refers to firmware which system is currently booted with.
+
+    # sw_inv_dict:
+    #   [b9101858]:
+    #     [image_type]:                 BMC update
+    #     [image_id]:                   b9101858
+    #     [functional]:                 True
+    #     [version]:                    2.8.0-dev-150-g04508dc9f
+
+    ${sw_inv_list}=  Get Functional Firmware  BMC image
+    ${sw_inv_dict}=  Get Non Functional Firmware  ${sw_inv_list}  True
+
+    # /redfish/v1/Managers/bmc
+    # "FirmwareVersion": "2.8.0-dev-150-g04508dc9f"
+
+    ${firmware_version}=  Redfish.Get Attribute
+    ...  ${REDFISH_BASE_URI}Managers/bmc  FirmwareVersion
+
+    Should Be Equal  ${sw_inv_dict['version']}  ${firmware_version}
+
+
+Verify Firmware Version Is Not Same In Firmware Inventory And Managers
+    [Documentation]  Verify the redfish firmware inventory path version is not same as
+    ...  Redfish managers for backup image.
+
+    # User defined state for software objects.
+    # Note: "Functional" term refers to firmware which system is currently booted with.
+
+    # sw_inv_dict:
+    #   [b9101858]:
+    #     [image_type]:                 BMC update
+    #     [image_id]:                   b9101858
+    #     [functional]:                 True
+    #     [version]:                    2.8.0-dev-150-g04508dc9f
+
+    ${sw_inv_list}=  Get Functional Firmware  BMC image
+    ${sw_inv_list}=  Get Non Functional Firmware List  ${sw_inv_list}  False
+
+    # /redfish/v1/Managers/bmc
+    # "FirmwareVersion": "2.8.0-dev-150-g04508dc9f"
+
+    ${firmware_version}=  Redfish.Get Attribute
+    ...  ${REDFISH_BASE_URI}Managers/bmc  FirmwareVersion
+
+    FOR  ${sw_inv}  IN  @{sw_inv_list}
+      Should Not Be Equal  ${sw_inv['version']}  ${firmware_version}
+    END
+
+
+Set Backup Firmware Image As Functional
+    [Documentation]  Switch to the backup firmware image to make functional.
+
+    ${state}=  Get Pre Reboot State
+    Rprint Vars  state
+
+    Switch Backup Firmware Image To Functional
+    Wait For Reboot  start_boot_seconds=${state['epoch_seconds']}
+
+
+Verify Redfish Functional Image Version Is Same
+    [Documentation]  Verify the functional image version is same as in firmware inventory and managers.
+    [Arguments]  ${image}
+
+    # Description of argument(s):
+    # image           Fucntional Image or Backup Image
+
+    Verify Firmware Version Same In Firmware Inventory And Managers
+
+    Run Keyword If  'backup_image' == '${image}'
+    ...  Verify Firmware Version Is Not Same In Firmware Inventory And Managers
+
+    Run Keyword If  'switch_backup_image' == '${image}'
+    ...  Run Keywords  Set Backup Firmware Image As Functional  AND
+    ...    Verify Firmware Version Same In Firmware Inventory And Managers  AND
+    ...    Set Backup Firmware Image As Functional  AND
+    ...    Verify Firmware Version Same In Firmware Inventory And Managers
+