Add test cases for field mode

Resolves openbmc/openbmc-test-automation#1032

Change-Id: Idaa467b79241fc135ae01a8eaa7a4103ba4689c9
Signed-off-by: Charles Paul Hofer <Charles.Hofer@ibm.com>
diff --git a/extended/code_update/test_field_mode.robot b/extended/code_update/test_field_mode.robot
new file mode 100644
index 0000000..6bba58f
--- /dev/null
+++ b/extended/code_update/test_field_mode.robot
@@ -0,0 +1,75 @@
+*** Settings ***
+Documentation       Test BMC field mode.
+
+Variables           ../../data/variables.py
+Resource            ../../lib/boot_utils.robot
+Resource            ../../lib/rest_client.robot
+Resource            ../../lib/openbmc_ffdc.robot
+
+Suite Setup         Suite Setup Execution
+Suite Teardown      Suite Teardown Execution
+
+Test Teardown       FFDC On Test Case Fail
+
+*** Test Cases ***
+
+Enable Field Mode And Verify Unmount
+    [Documentation]  Enable field mode and check that /usr/local is unmounted.
+    [Tags]  Enable_Field_Mode
+
+    # After running, /xyz/openbmc_project/software should look like this:
+    # /xyz/openbmc_project/software
+    # {
+    #     "FieldModeEnabled": 1,
+    #     "associations": [
+    #         [
+    #             "active",
+    #             "software_version",
+    #             "/xyz/openbmc_project/software/fcf8e182"
+    #         ],
+    #         [
+    #             "functional",
+    #             "software_version",
+    #             "/xyz/openbmc_project/software/fcf8e182"
+    #         ]
+    #     ]
+    # }
+
+    ${args}=  Create Dictionary  data=${1}
+    Write Attribute  ${SOFTWARE_VERSION_URI}  FieldModeEnabled  data=${args}
+    BMC Execute Command  [ ! -d "/usr/local/share" ]
+
+
+Attempt To Disable Field Mode Via REST And Veify
+    [Documentation]  Attempt to disable field mode with REST and verify that
+    ...              it remains enabled.
+    [Tags]  Attempt_To_Disable_Field_Mode_Via_REST
+
+    # This test case doesn't actually disable field mode. It attempts to, but
+    # verifies that the FieldModeEnabled property keeps its value of '1'
+    # after trying to set it to '0'. Field mode is disabled in suite teardown.
+
+    ${args}=  Create Dictionary  data=${0}
+    Write Attribute  ${SOFTWARE_VERSION_URI}  FieldModeEnabled
+    ...  verify=${TRUE}  expected_value=${1}  data=${args}
+
+
+*** Keywords ***
+
+Suite Setup Execution
+    [Documentation]  Do suite setup tasks.
+
+    # Check that /usr/local is mounted
+    BMC Execute Command  [ -d "/usr/local/share" ]
+
+
+Suite Teardown Execution
+    [Documentation]  Do suite teardown tasks.
+
+    # 1. Disable field mode
+    # 2. Check that /usr/local is mounted
+
+    BMC Execute Command  /sbin/fw_setenv fieldmode
+    BMC Execute Command  /bin/systemctl unmask usr-local.mount
+    OBMC Reboot (off)  quiet=${1}
+    BMC Execute Command  [ -d "/usr/local/share" ]
\ No newline at end of file
diff --git a/lib/rest_client.robot b/lib/rest_client.robot
index 83fb049..67170d9 100644
--- a/lib/rest_client.robot
+++ b/lib/rest_client.robot
@@ -178,13 +178,41 @@
     ${content}=     To Json    ${resp.content}
     [Return]    ${content["data"]}
 
+
 Write Attribute
-    [Arguments]    ${uri}      ${attr}    ${timeout}=10    &{kwargs}
-    ${base_uri}=    Catenate    SEPARATOR=    ${DBUS_PREFIX}    ${uri}
-    ${resp}=  openbmc put request  ${base_uri}/attr/${attr}
+    [Documentation]  Write a D-Bus attribute with REST.
+    [Arguments]  ${uri}  ${attr}  ${timeout}=10  ${verify}=${FALSE}
+    ...  ${expected_value}=${EMPTY}  &{kwargs}
+
+    # Description of argument(s):
+    # uri               URI of the object that the attribute lives on
+    #                   (e.g. '/xyz/openbmc_project/software/').
+    # attr              Name of the attribute (e.g. 'FieldModeEnabled').
+    # timeout           Timeout for the REST call.
+    # verify            If set to ${TRUE}, the attribute will be read back to
+    #                   ensure that its value is set to ${verify_attr}.
+    # expected_value    Only used if verify is set to ${TRUE}. The value that
+    #                   ${attr} should be set to. This defaults to
+    #                   ${kwargs['data']. There are cases where the caller
+    #                   expects some other value in which case this value can
+    #                   be explicitly specified.
+    # kwargs            Arguments passed to the REST call. This should always
+    #                   contain the value to set the property to at the 'data'
+    #                   key (e.g. data={"data": 1}).
+
+    ${base_uri}=  Catenate  SEPARATOR=  ${DBUS_PREFIX}  ${uri}
+    ${resp}=  Openbmc Put Request  ${base_uri}/attr/${attr}
     ...  timeout=${timeout}  &{kwargs}
-    should be equal as strings      ${resp.status_code}     ${HTTP_OK}
-    ${json}=   to json         ${resp.content}
+    Should Be Equal As Strings  ${resp.status_code}  ${HTTP_OK}
+
+    # Verify the attribute was set correctly if the caller requested it.
+    Return From Keyword If  ${verify} == ${FALSE}
+
+    ${expected_value}=  Set Variable If  '${expected_value}' == '${EMPTY}'
+    ...  ${kwargs['data']['data']}
+    ${value}=  Read Attribute  ${uri}  ${attr}
+    Should Be Equal  ${value}  ${expected_value}
+
 
 Read Properties
     [Arguments]  ${uri}  ${timeout}=10  ${quiet}=${QUIET}