OCC test cases

Added:
    - Supporting utility keywords for OCC testing.
    - Verify OCC object created.
    - Verify active OCC state.

Resolves  openbmc/openbmc-test-automation#952

Change-Id: I4fe553e6c0df437de8095a274759ba86d2547ab2
Signed-off-by: George Keishing <gkeishin@in.ibm.com>
diff --git a/lib/open_power_utils.robot b/lib/open_power_utils.robot
index 262486c..327b67b 100644
--- a/lib/open_power_utils.robot
+++ b/lib/open_power_utils.robot
@@ -18,7 +18,7 @@
     #          "OccActive": 1
     # }
 
-    ${occ_list}=  Get Endpoint Paths  ${OPENBMC_POWER}  occ*
+    ${occ_list}=  Get Endpoint Paths  ${OPENPOWER_CONTROL}  occ*
 
     [Return]  ${occ_list}
 
@@ -33,3 +33,35 @@
 
     ${occ_attribute}=  Read Attribute  ${occ_object}  OccActive
     [Return]  ${occ_attribute}
+
+
+Count Object Entries
+    [Documentation]  Count the occurence number of a given object.
+    [Arguments]  ${object_base_uri_path}  ${object_name}
+
+    # Description of argument(s):
+    # object_base_uri_path    Object base path
+    #                         (e.g. "/org/open_power/control/").
+    # object_name             Object name (e.g. "occ", "cpu" etc).
+
+    ${object_list}=  Get Endpoint Paths
+    ...  ${object_base_uri_path}  ${object_name}
+    ${list_count}=  Get Length  ${object_list}
+    [Return]  ${list_count}
+
+
+Read Object Attribute
+    [Documentation]  Return object attribute data.
+    [Arguments]  ${object_base_uri_path}  ${attribute_name}
+
+    # Description of argument(s):
+    # object_base_uri_path       Object path.
+    #                   (e.g. "/org/open_power/control/occ0").
+    # attribute_name    Object attribute name.
+
+    ${resp}=  OpenBMC Get Request
+    ...  ${object_base_uri_path}/attr/${attribute_name}  quiet=${1}
+    Return From Keyword If  ${resp.status_code} != ${HTTP_OK}
+    ${content}=  To JSON  ${resp.content}
+    [Return]  ${content["data"]}
+
diff --git a/tests/test_occ_power.robot b/tests/test_occ_power.robot
new file mode 100644
index 0000000..df76bfd
--- /dev/null
+++ b/tests/test_occ_power.robot
@@ -0,0 +1,87 @@
+*** Settings ***
+Documentation   Suite to test OCC power module.
+
+Resource        ../lib/open_power_utils.robot
+Resource        ../lib/boot_utils.robot
+Resource        ../lib/state_manager.robot
+Resource        ../lib/openbmc_ffdc.robot
+
+Suite Setup     Suite Setup Execution
+Test Teardown   Test Teardown Execution
+
+*** Test Cases ***
+
+Verify OCC Object Count
+    [Documentation]  Verify that OCC and inventory entries match.
+    [Tags]  Verify_OCC_Object_Count
+
+    # Example:
+    # /org/open_power/control/enumerate
+    # {
+    #    "/org/open_power/control/host0": {},
+    #    "/org/open_power/control/occ0": {
+    #       "OccActive": 0
+    #   },
+    # "/org/open_power/control/occ1": {
+    #       "OccActive": 0
+    #   }
+    # }
+
+    # Inventory counterpart cpu's:
+    # "/xyz/openbmc_project/inventory/system/chassis/motherboard/cpu0",
+    # "/xyz/openbmc_project/inventory/system/chassis/motherboard/cpu1",
+
+    ${inventory_count}=  Count Object Entries
+    ...  ${HOST_INVENTORY_URI}system/chassis/motherboard/  cpu*
+
+    Should Be Equal  ${occ_count}  ${inventory_count}
+    ...  msg=OCC and inventory entry counts are mismatched.
+
+
+Verify OCC Active State
+    [Documentation]  Check OCC active state.
+    [Tags]  Verify_OCC_Active_State
+
+    # Example cpu_list data output:
+    #  /xyz/openbmc_project/inventory/system/chassis/motherboard/cpu0
+    #  /xyz/openbmc_project/inventory/system/chassis/motherboard/cpu1
+    ${cpu_list}=  Get Endpoint Paths
+    ...  ${HOST_INVENTORY_URI}system/chassis/motherboard/  cpu*
+
+    :FOR  ${endpoint_path}  IN  @{cpu_list}
+    \  ${is_functional}=  Read Object Attribute  ${endpoint_path}  Functional
+    \  Continue For Loop If  ${is_functional} == ${0}
+    \  ${num}=  Set Variable  ${endpoint_path[-1]}
+    \  ${occ_active}=  Get OCC Active State  ${OPENPOWER_CONTROL}occ${num}
+    \  Should Be True  ${occ_active}  msg=OCC ${num} is not active.
+
+
+*** Keywords ***
+
+Suite Setup Execution
+    [Documentation]  Do the initial test suite setup.
+    # - Power off.
+    # - Boot Host.
+
+    Smart Power Off
+    REST Power On
+    Count OCC Object Entry
+
+
+Count OCC Object Entry
+    [Documentation]  Count OCC object entry and set count.
+
+    ${object_count}=  Count Object Entries  ${OPENPOWER_CONTROL}  occ*
+    Set Suite Variable  ${occ_count]  ${object_count}
+
+
+Test Teardown Execution
+    [Documentation]  Do the post test teardown.
+    # - Capture FFDC on test failure.
+    # - Delete error logs.
+    # - Close all open SSH connections.
+
+    FFDC On Test Case Fail
+    Delete Error Logs
+    Close All Connections
+
diff --git a/tests/test_occ_powercap.robot b/tests/test_occ_powercap.robot
deleted file mode 100644
index 4ccf6d8..0000000
--- a/tests/test_occ_powercap.robot
+++ /dev/null
@@ -1,179 +0,0 @@
-*** Settings ***
-Documentation  This suite is for testing OCC: Power capping setting
-
-Resource       ../lib/rest_client.robot
-Resource       ../lib/resource.txt
-Resource       ../lib/utils.robot
-Resource       ../lib/openbmc_ffdc.robot
-Resource       ../lib/state_manager.robot
-
-Suite Setup    Check OCC Readiness
-Test Teardown  FFDC On Test Case Fail
-
-Force Tags  powercapping
-
-*** Variables ***
-
-*** Test Cases ***
-
-Get OCC status
-    [Documentation]  This testcase is to test the OCCstatus for the system
-    ...              is Enabled or not
-    [Tags]  Get_OCC_status
-    ${status}=  Get OCC status
-    Should Be Equal  ${status}  Enabled
-
-Set And Get Powercap
-    [Documentation]  This testcase is to test get/set powercap feature.
-    ...              In the testcase we are reading min, max value and then
-    ...              try set the random in that range.
-    ...              Existing Issue: https://github.com/openbmc/openbmc/issues/552
-    [Tags]  Set_And_Get_Powercap  known_issue
-
-    ${min}=  Get Minimum Powercap
-    Log  ${min}
-    ${max}=  Get Maximum Powercap
-    Log  ${max}
-    ${rand_power_cap}=  Evaluate  random.randint(${min}, ${max})  modules=random
-    Log  ${rand_power_cap}
-    ${resp}=  Set Powercap  ${rand_power_cap}
-    Should Be Equal As Strings  ${resp.status_code}  ${HTTP_OK}
-    Sleep  ${DBUS_POLL_INTERVAL}
-    ${power_cap}=  Get Powercap
-    Should Be Equal  ${power_cap}  ${rand_power_cap}
-    ${user_power_cap}=  Get User Powercap
-    Should Be Equal  ${user_power_cap}  ${rand_power_cap}
-
-Set Less Than Minimum Powercap
-    [Documentation]  Test set powercap with less than min powercap value
-    ...              Existing Issue: https://github.com/openbmc/openbmc/issues/552
-    [Tags]  Set_Less_Than_Minimum_Powercap  known_issue
-
-    ${org_power_cap}=  Get Powercap
-    ${min}=  Get Minimum Powercap
-    ${sample_invalid_pcap}=  Evaluate  ${min}-${100}
-    ${resp}=  Set Powercap  ${sample_invalid_pcap}
-    Sleep  ${DBUS_POLL_INTERVAL}
-    Should Not Be Equal As Strings  ${resp.status_code}  ${HTTP_OK}
-    ${power_cap}=  Get Powercap
-    Should Be Equal  ${org_power_cap}  ${power_cap}
-
-Set More Than Maximum Powercap
-    [Documentation]  Test set powercap with more than max powercap value
-    ...              Existing Issue: https://github.com/openbmc/openbmc/issues/552
-    [Tags]  Set_More_Than_Maximum_Powercap  known_issue
-
-    ${org_power_cap}=  Get Powercap
-    ${min}=  Get Maximum Powercap
-    ${sample_invalid_pcap}=  Evaluate  ${min}+${100}
-    ${resp}=  Set Powercap  ${sample_invalid_pcap}
-    Sleep  ${DBUS_POLL_INTERVAL}
-    Should Not Be Equal As Strings  ${resp.status_code}  ${HTTP_OK}
-    ${power_cap}=  Get Powercap
-    Should Be Equal  ${org_power_cap}  ${power_cap}
-
-Disable Powercap
-    [Documentation]  Test set powercap with 0 and make sure powercap is
-    ...              disabled by checking whether the value is set to 0
-    [Tags]  Disable_Powercap
-
-    ${resp}=  Set Powercap  ${0}
-    Should Be Equal As Strings  ${resp.status_code}  ${HTTP_OK}
-    Sleep  ${DBUS_POLL_INTERVAL}
-    ${power_cap}=  Get Powercap
-    Should Be Equal  ${power_cap}  ${0}
-    ${user_power_cap}=  Get User Powercap
-    Should Be Equal  ${user_power_cap}  ${0}
-
-Get System Power Consumption
-    [Documentation]  Get the current system power consumption and check if the
-    ...              value is greater than zero
-    [Tags]  Get_System_Power_Consumption
-
-    ${resp}=  OpenBMC Get Request  ${SENSORS_URI}powercap/system_power
-    Should Be Equal As Strings  ${resp.status_code}  ${HTTP_OK}
-    ${jsondata}=  To JSON  ${resp.content}
-    Should Be True  ${jsondata["data"]["value"]} > 0
-
-*** Keywords ***
-
-Get Minimum Powercap
-    ${resp}=  OpenBMC Get Request
-    ...  ${SENSORS_URI}powercap/min_cap
-    ${jsondata}=  To JSON  ${resp.content}
-    [Return]  ${jsondata["data"]["value"]}
-
-Get Maximum Powercap
-    ${resp}=  OpenBMC Get Request  ${SENSORS_URI}powercap/max_cap
-    ${jsondata}=  To JSON  ${resp.content}
-    [Return]  ${jsondata["data"]["value"]}
-
-Get User Powercap
-    ${resp}=  OpenBMC Get Request  ${SENSORS_URI}powercap/user_cap
-    ${jsondata}=  To JSON  ${resp.content}
-    [Return]  ${jsondata["data"]["value"]}
-
-Set Powercap
-    [Arguments]  ${powercap_value}
-    @{pcap_list}=  Create List  ${powercap_value}
-    ${data}=  Create Dictionary  data=@{pcap_list}
-    ${resp}=  OpenBMC Post Request
-    ...  ${SENSORS_URI}host/powercap/action/setValue  data=${data}
-    [Return]  ${resp}
-
-Get Powercap
-    ${resp}=  OpenBMC Get Request
-    ...  ${SENSORS_URI}host/powercap
-    ${jsondata}=  To JSON  ${resp.content}
-    [Return]  ${jsondata["data"]["value"]}
-
-Get OCC status link
-    ${resp}=  OpenBMC Get Request
-    ...  ${SENSORS_URI}host/list
-    ${jsondata}=  To JSON  ${resp.content}
-    Log  ${jsondata}
-    : FOR  ${ELEMENT}  IN  @{jsondata["data"]}
-    \  Log  ${ELEMENT}
-    \  ${found}=  Get Lines Matching Pattern  ${ELEMENT}  *host/cpu*/OccStatus
-    \  Return From Keyword If  '${found}' != ''  ${found}
-
-Get OCC status
-    ${occstatus_link}=  Get OCC status link
-    ${data}=  Create Dictionary  data=@{EMPTY}
-    ${resp}=  OpenBMC Post Request
-    ...  ${occstatus_link}/action/getValue  data=${data}
-    ${jsondata}=  To JSON  ${resp.content}
-    [Return]  ${jsondata["data"]}
-
-Get Chassis URI
-    ${resp}=  OpenBMC Get Request  ${OPENBMC_BASE_URI}control/
-    ${jsondata}=  To JSON  ${resp.content}
-    Log  ${jsondata}
-    : FOR  ${ELEMENT}  IN  @{jsondata["data"]}
-    \  Log  ${ELEMENT}
-    \  ${found}=  Get Lines Matching Pattern  ${ELEMENT}  *control/chassis*
-    \  Return From Keyword If  '${found}' != ''  ${found}
-
-
-Check OCC Readiness
-    [Documentation]  Poweron If BMC power state is off. Check the OCC powercap
-    ...              if the interface attributes are activated.
-
-    ${status}=  Run Keyword and Return Status  Is Host Off
-    Run Keyword If  '${status}' == '${True}'  Initiate Host Boot
-    Wait Until Keyword Succeeds  5min  10sec  Powercap Attributes Activated
-
-
-Powercap Attributes Activated
-    [Documentation]  Verify if the response contains the pre-define list
-
-    @{precheck}=  Create List  ${SENSORS_URI}powercap/user_cap
-    ...                        ${SENSORS_URI}powercap/system_power
-    ...                        ${SENSORS_URI}powercap/curr_cap
-    ...                        ${SENSORS_URI}powercap/max_cap
-    ...                        ${SENSORS_URI}powercap/min_cap
-
-    ${resp}=  OpenBMC Get Request  ${SENSORS_URI}powercap/
-    ${jsondata}=  To JSON  ${resp.content}
-    List Should Contain Sub List  ${jsondata["data"]}  ${precheck}
-    ...  msg=Failed to activate powercap interface attributes