Power on restore test

This test case is broken and had to be fixed to verify the power
policy set using REST interface.

The fix addresses the following:
   1. Fix Template logic error
   2. Fix test template states
   3. Fix state checking post PDU power cycle
   4. Replace poweron/poweroff with BMC utility keywords
   5. Update documentation
   6. and clean up work for coding guidelines

Resolves openbmc/openbmc-test-automation#149

Change-Id: I7907520bc79091710ba3b11f91b6c3e311850df0
Signed-off-by: George Keishing <gkeishin@in.ibm.com>
diff --git a/extended/test_power_restore.robot b/extended/test_power_restore.robot
index 66dc522..47e13aa 100644
--- a/extended/test_power_restore.robot
+++ b/extended/test_power_restore.robot
@@ -1,12 +1,14 @@
 *** Settings ***
-Documentation                This suite will verifiy the power restore policy rest Interfaces
-...                          Details of valid interfaces can be found here...
-...                          https://github.com/openbmc/docs/blob/master/rest-api.md
+Documentation   This suite verifies the power restore policy supported by
+...             REST Interfaces.
+...             Refer here for documentation on the REST interfaces
+...             https://github.com/openbmc/docs/blob/master/rest-api.md
 
 Resource        ../lib/rest_client.robot
 Resource        ../lib/pdu/pdu.robot
 Resource        ../lib/utils.robot
 Resource        ../lib/openbmc_ffdc.robot
+Resource        ../lib/boot/boot_resource_master.robot
 
 
 Library         SSHLibrary
@@ -14,69 +16,87 @@
 Test Teardown   Log FFDC
 Force Tags      chassisboot  bmcreboot
 
-***test cases***
+*** Variables ***
+${HOST_SETTING}    /org/openbmc/settings/host0
 
-Set the power restore policy       Policy                 ExpectedSystemState       NextSystemState
+*** Test Cases ***
 
-                                   LEAVE_OFF              HOST_POWERED_OFF          HOST_POWERED_OFF
-                                   LEAVE_OFF              HOST_POWERED_ON           HOST_POWERED_OFF
-                                   ALWAYS_POWER_ON        HOST_POWERED_OFF          HOST_POWERED_ON
-                                   ALWAYS_POWER_ON        HOST_POWERED_ON           HOST_POWERED_ON
-                                   RESTORE_LAST_STATE     HOST_POWERED_ON           HOST_POWERED_ON
-                                   RESTORE_LAST_STATE     HOST_POWERED_OFF          HOST_POWERED_OFF
+Set the power restore policy
+    #Policy                Expected System State     Next System State
 
-    [Documentation]   This test case sets the pilicy as given under the policy attribute.
-    ...               ExpectedSystemState:-is the state where system should be before running the test case
-    ...               NextSystemState:-is After Power cycle system should reach to this state
-    ...               if the system is not at the Expected System State,This test case brings the system
-    ...               in the Expected state then do the power cycle.
+    LEAVE_OFF              HOST_POWERED_OFF          HOST_POWERED_OFF
+    LEAVE_OFF              HOST_BOOTED               HOST_POWERED_OFF
+    ALWAYS_POWER_ON        HOST_POWERED_OFF          HOST_BOOTED
+    ALWAYS_POWER_ON        HOST_BOOTED               HOST_BOOTED
+    RESTORE_LAST_STATE     HOST_BOOTED               HOST_BOOTED
+    RESTORE_LAST_STATE     HOST_POWERED_OFF          HOST_POWERED_OFF
 
-    [Template]    setRestorePolicy
+    [Documentation]   Test to validate restore policy attribute functionality.
+    ...               Policy:
+    ...                     System policy to restore on power cycle
+    ...               Expected System State:
+    ...                     State where system should be before running the
+    ...                     test case
+    ...               Next System State:
+    ...                     After power cycle, system should reach this
+    ...                     specific state
+
+    [Template]    Set Restore Policy
 
 ***keywords***
-setRestorePolicy
-    [arguments]        ${policy}     ${expectedSystemState}   ${nextSystemState}
-    ${valueDict} =   create dictionary   data=${policy}
-    Write Attribute  /org/openbmc/settings/host0    power_policy      data=${valueDict}
-    ${currentPolicy}=      Read Attribute    /org/openbmc/settings/host0    power_policy
-    Should Be Equal     ${currentPolicy}      ${policy}
-    ${currentSystemState}=      Read Attribute    /org/openbmc/settings/host0    system_state
-    log Many   "CurrentSystemState="   ${currentSystemState}
-    log Many   "ExpectedSystemState="  ${expectedSystemState}
-    log Many   "NextSystemState="      ${nextSystemState}
-    Run Keyword If   '${currentSystemState}' != '${expectedSystemState}' and '${expectedSystemState}' == 'HOST_POWERED_ON'      powerOnHost
-    Run Keyword If   '${currentSystemState}' != '${expectedSystemState}' and '${expectedSystemState}' == 'HOST_POWERED_OFF'     powerOffHost
-    log to console   "Doing power cycle"
+
+Set Restore Policy
+    [arguments]    ${policy}   ${expectedState}   ${nextState}
+
+    Set Policy Setting   ${policy}
+
+    ${currentState}=
+    ...   Read Attribute   ${HOST_SETTING}   system_state
+
+    Log   Current System State= ${currentState}
+    Log   Expected System State= ${expectedState}
+    Log   Next System State= ${nextState}
+
+    Run Keyword If
+    ...   '${currentState}' != '${expectedState}'
+    ...   Set Initial Test State   ${expectedState}
+
+    Log   "Doing power cycle"
     PDU Power Cycle
     Check If BMC is Up   5 min    10 sec
-    log to console   "Host is pingable now"
-    Sleep   100sec
-    ${afterPduSystemState}=      Read Attribute    /org/openbmc/settings/host0    system_state
-    Should be equal   ${afterPduSystemState}    ${nextSystemState}
+    Log   "BMC is Online now"
 
-powerOffHost
-    log to console    "Powering off the host"
-    @{arglist}=   Create List
-    ${args}=     Create Dictionary   data=@{arglist}
-    ${resp}=   Call Method    /org/openbmc/control/chassis0/    powerOff    data=${args}
-    should be equal as strings      ${resp.status_code}     ${HTTP_OK}
-    ${json} =   to json         ${resp.content}
-    should be equal as strings      ${json['status']}      ok
-    sleep  30sec
-    ${currentSystemState}=      Read Attribute    /org/openbmc/settings/host0    system_state
-    Should be equal   ${currentSystemState}     HOST_POWERED_OFF
-
-powerOnHost
-    log to console    "Powering on the host"
-    @{arglist}=   Create List
-    ${args}=     Create Dictionary    data=@{arglist}
-    ${resp}=   Call Method    /org/openbmc/control/chassis0/    powerOn    data=${args}
-    should be equal as strings      ${resp.status_code}     ${HTTP_OK}
-    ${json} =   to json         ${resp.content}
-    should be equal as strings      ${json['status']}      ok
-    sleep   30sec
-    ${currentSystemState}=      Read Attribute    /org/openbmc/settings/host0    system_state
-    Should be equal   ${currentSystemState}     HOST_POWERED_ON
+    Wait Until Keyword Succeeds
+    ...   5 min   10 sec   System State  ${nextState}
 
 
+Set Policy Setting
+    [Documentation]   Set the given test policy
+    [arguments]   ${policy}
+
+    ${valueDict} =     create dictionary  data=${policy}
+    Write Attribute    ${HOST_SETTING}    power_policy   data=${valueDict}
+    ${currentPolicy}=  Read Attribute     ${HOST_SETTING}   power_policy
+    Should Be Equal    ${currentPolicy}   ${policy}
+
+
+Set Initial Test State
+    [Documentation]   Poweron if ON expected, Poweroff if OFF expected
+    ...               to initial state of the test.
+    [arguments]   ${expectedState}
+
+    Run Keyword If
+    ...   '${expectedState}' == 'HOST_BOOTED'
+    ...   BMC Power On
+
+    Run Keyword If
+    ...   '${expectedState}' == 'HOST_POWERED_OFF'
+    ...   BMC Power Off
+
+
+System State
+    [arguments]     ${nextState}
+    ${afterPduSystemState}=
+    ...   Read Attribute    ${HOST_SETTING}    system_state
+    Should be equal   ${afterPduSystemState}    ${nextState}