Power control metrics check for power consumed watts

Resolve    openbmc/openbmc-test-automation#1611
Resolve    openbmc/openbmc-test-automation#1594

Signed-off-by: Marissa Garza <garzam@us.ibm.com>
Change-Id: Ibfa5d6cfa8c1148006ca4b0273640ef34754ef44
diff --git a/lib/bmc_redfish_utils.robot b/lib/bmc_redfish_utils.robot
index ff24182..b950f5e 100644
--- a/lib/bmc_redfish_utils.robot
+++ b/lib/bmc_redfish_utils.robot
@@ -100,3 +100,40 @@
     ${num_valid_frus}=  Get length  ${fru_records}
 
     [Return]  ${num_valid_frus}
+
+
+Verify Valid Records
+    [Documentation]  Verify all records retrieved with the given arguments are valid.
+    [Arguments]  ${record_type}  ${redfish_uri}  ${reading_type}
+
+    # Description of Argument(s):
+    # record_type    The sensor record type (e.g. "PowerSupplies")
+    # redfish_uri    The power supply URI (e.g. /redfish/v1/Chassis/chassis/Power)
+    # reading_type   The power watt readings (e.g. "PowerInputWatts")
+
+    # A valid record will have "State" key "Enabled" and "Health" key "OK".
+    ${records}=  Redfish.Get Attribute  ${redfish_uri}  ${record_type}
+
+    Rprint Vars  records
+
+    # Example output:
+    # records:
+    #   [0]:
+    #     [@odata.id]:                 /redfish/v1/Chassis/chassis/Power#/PowerControl/0
+    #     [@odata.type]:               #Power.v1_0_0.PowerControl
+    #     [MemberId]:                  0
+    #     [Name]:                      Chassis Power Control
+    #     [PowerConsumedWatts]:        264.0
+    #     [PowerLimit]:
+    #       [LimitInWatts]:            None
+    #     [PowerMetrics]:
+    #       [AverageConsumedWatts]:    325
+    #       [IntervalInMin]:           3
+    #       [MaxConsumedWatts]:        538
+    #     [Status]:
+    #       [Health]:                  OK
+    #       [State]:                   Enabled
+
+    ${invalid_records}=  Filter Struct  ${records}
+    ...  [('Health', '^OK$'), ('State', '^Enabled$'), ('${reading_type}', '')]  regex=1  invert=1
+    Valid Length  invalid_records  max_length=0
diff --git a/redfish/systems/test_power_control_metrics.robot b/redfish/systems/test_power_control_metrics.robot
new file mode 100755
index 0000000..e922ac4
--- /dev/null
+++ b/redfish/systems/test_power_control_metrics.robot
@@ -0,0 +1,64 @@
+*** Settings ***
+Documentation       Get the system power supply voltage readings.
+
+Resource            ../../lib/bmc_redfish_resource.robot
+Resource            ../../lib/bmc_redfish_utils.robot
+Resource            ../../lib/openbmc_ffdc.robot
+Library             ../../lib/gen_robot_valid.py
+
+Suite Setup         Suite Setup Execution
+Suite Teardown      Suite Teardown Execution
+Test Setup          Printn
+Test Teardown       Test Teardown Execution
+
+
+*** Test Cases ***
+
+Verify Power Control Consumed Watts
+    [Documentation]  Verify there are no invalid power control consumed watt records.
+    [Tags]  Verify_Power_Control_Consumed_Watts
+    [Template]  Verify Power Metric Records
+
+    # record_type   redfish_uri                   reading_type
+    PowerControl    ${REDFISH_CHASSIS_POWER_URI}  PowerConsumedWatts
+
+
+*** Keywords ***
+
+Verify Power Metric Records
+    [Documentation]  Verify the power metric records.
+    [Arguments]  ${record_type}  ${redfish_uri}  ${reading_type}
+
+    # Description of Arguments(s):
+    # record_type    The sensor record type (e.g. "PowerControl")
+    # redfish_uri    The power supply URI (e.g. /redfish/v1/Chassis/chassis/Power)
+    # reading_type   The power metric readings (e.g. "PowerConsumedWatts")
+
+    Verify Valid Records  ${record_type}  ${redfish_uri}  ${reading_type}
+
+    ${records}=  Redfish.Get Attribute  ${redfish_uri}  ${record_type}
+
+    ${invalid_records}=  Evaluate
+    ...  [x for x in ${records} if not x['${reading_type}'] <= x['PowerMetrics']['MaxConsumedWatts']]
+
+    Valid Length  invalid_records  max_length=0
+
+
+Suite Teardown Execution
+    [Documentation]  Do the post suite teardown.
+
+    Redfish.Logout
+
+
+Suite Setup Execution
+    [Documentation]  Do test case setup tasks.
+
+    Printn
+    Redfish Power On  stack_mode=skip
+    Redfish.Login
+
+
+Test Teardown Execution
+    [Documentation]  Do the post test teardown.
+
+    FFDC On Test Case Fail
diff --git a/redfish/systems/test_powersupply_readings.robot b/redfish/systems/test_powersupply_readings.robot
index 8744205..21910da 100755
--- a/redfish/systems/test_powersupply_readings.robot
+++ b/redfish/systems/test_powersupply_readings.robot
@@ -1,5 +1,5 @@
 *** Settings ***
-Documentation       Get the systems' power supply voltage readings.
+Documentation       Get the system power supply voltage readings.
 
 Resource            ../../lib/bmc_redfish_resource.robot
 Resource            ../../lib/bmc_redfish_utils.robot
@@ -57,42 +57,12 @@
 
     Verify Valid Records  ${record_type}  ${redfish_uri}  ${reading_type}
 
-    ${records}=  Redfish.Get Attribute
-    ...  ${redfish_uri}  ${record_type}
+    ${records}=  Redfish.Get Attribute  ${redfish_uri}  ${record_type}
 
     ${invalid_records}=  Evaluate
     ...  [x for x in ${records} if not x['LowerThresholdNonCritical'] <= x['${reading_type}'] <= x['UpperThresholdNonCritical']]
 
-    ${num_invalid_records}=  Get Length  ${invalid_records}
-    Run Keyword If  ${num_invalid_records} > ${0}
-    ...  Rprint Vars  num_invalid_records  invalid_records
-    Valid Value  num_invalid_records  valid_values=[0]
-
-
-Verify Valid Records
-    [Documentation]  Verify all the valid records.
-    [Arguments]  ${record_type}  ${redfish_uri}  ${reading_type}
-
-    # Description of Arguments(s):
-    # record_type    The sensor record type (e.g. "PowerSupplies")
-    # redfish_uri    The power supply URI (e.g. /redfish/v1/Chassis/chassis/Power)
-    # reading_type   The power watt readings (e.g. "PowerInputWatts")
-
-    # A valid record will have "State" key "Enabled" and "Health" key "OK".
-    ${records}=  Redfish.Get Attribute
-    ...  ${redfish_uri}  ${record_type}
-
-    ${num_records}=  Get Length  ${records}
-    Rprint Vars  num_records  records
-
-    ${invalid_records}=  Filter Struct  ${records}
-    ...  [('Health', '^OK$'), ('State', '^Enabled$'), ('${reading_type}', '')]  regex=1  invert=1
-    ${num_invalid_records}=  Get Length  ${invalid_records}
-
-    Run Keyword If  ${num_invalid_records} > ${0}
-    ...  Rprint Vars  num_invalid_records  invalid_records
-    Valid Value  num_invalid_records  valid_values=[0]
-
+    Valid Length  invalid_records  max_length=0
 
 Suite Teardown Execution
     [Documentation]  Do the post suite teardown.