Test power reading via IPMI raw.

Resolves openbmc/openbmc-test-automation#1248

Change-Id: I468e4e62b1ed6ae19f3ec3b0ba4162c7798bbd4d
Signed-off-by: Rahul Maheshwari <rahulmaheshwari@in.ibm.com>
diff --git a/data/ipmi_raw_cmd_table.py b/data/ipmi_raw_cmd_table.py
index 48c135c..57805f6 100644
--- a/data/ipmi_raw_cmd_table.py
+++ b/data/ipmi_raw_cmd_table.py
@@ -35,5 +35,15 @@
             "none",
             "Enabled nibble position 6th LSB e.g. 0x1",
         ],
+    },
+    'power_reading':
+    {
+        'Get':
+        [
+            # raw command, expected output(s), comment
+            "0x2c 0x02 0xdc 0x01 0x01 0x00",
+            "dc d5 00 d5 00 d5 00 d5 00 00 00 00 00 00 00 00 00 00",
+            "Byte position 2nd LSB e.g. d5 Instantaneous power readings",
+        ],
     }
 }
diff --git a/tests/ipmi/test_general_ipmi.robot b/tests/ipmi/test_general_ipmi.robot
index 695cf35..821eaba 100644
--- a/tests/ipmi/test_general_ipmi.robot
+++ b/tests/ipmi/test_general_ipmi.robot
@@ -6,10 +6,12 @@
 Resource            ../lib/boot_utils.robot
 Library             ../../lib/ipmi_utils.py
 Resource            ../../lib/bmc_network_utils.robot
+Variables           ../data/ipmi_raw_cmd_table.py
 
 Suite Setup         Suite Setup Execution
 Test Teardown       FFDC On Test Case Fail
 
+
 *** Variables ***
 
 ${new_mc_id}=  HOST
@@ -293,6 +295,56 @@
     ...  msg=Power reading above allowed threshold ${allowed_power_diff}.
 
 
+Test Power Reading Via IPMI Raw Command
+    [Documentation]  Test power reading via IPMI raw command and verify
+    ...  using REST.
+    [Tags]  Test_Power_Reading_Via_IPMI_Raw_Command
+
+    # Response data structure of power reading command output via IPMI.
+    # 1        Completion Code. Refer to section 8, DCMI Completion Codes.
+    # 2        Group Extension Identification = DCh
+    # 3:4      Current Power in watts
+
+    ${ipmi_raw_output}=  Run IPMI Standard Command
+    ...  raw ${IPMI_RAW_CMD['power_reading']['Get'][0]}
+
+    @{raw_output_list}=  Split String  ${ipmi_raw_output}  ${SPACE}
+
+    # On successful execution of raw IPMI power reading command, completion
+    # code does not come in output. So current power value will start from 2
+    # byte instead of 3.
+
+    ${power_reading_ipmi_raw_3_item}=  Get From List  ${raw_output_list}  2
+    ${power_reading_ipmi_raw_3_item}=
+    ...  Convert To Integer  0x${power_reading_ipmi_raw_3_item}
+
+    ${power_reading_rest}=  Read Attribute
+    ...  ${SENSORS_URI}power/total_power  Value
+
+    # Example of power reading via REST
+    #  "CriticalAlarmHigh": 0,
+    #  "CriticalAlarmLow": 0,
+    #  "CriticalHigh": 3100000000,
+    #  "CriticalLow": 0,
+    #  "Scale": -6,
+    #  "Unit": "xyz.openbmc_project.Sensor.Value.Unit.Watts",
+    #  "Value": 228000000,
+    #  "WarningAlarmHigh": 0,
+    #  "WarningAlarmLow": 0,
+    #  "WarningHigh": 3050000000,
+    #  "WarningLow": 0
+
+    # Get power value based on scale i.e. Value * (10 power Scale Value)
+    # e.g. from above case 228000000 * (10 power -6) = 228000000/1000000
+
+    ${power_reading_rest}=  Evaluate  ${power_reading_rest}/1000000
+    ${ipmi_rest_power_diff}=
+    ...  Evaluate  abs(${power_reading_rest} - ${power_reading_ipmi_raw_3_item})
+
+    Should Be True  ${ipmi_rest_power_diff} <= ${allowed_power_diff}
+    ...  msg=Power Reading above allowed threshold ${allowed_power_diff}.
+
+
 Test Baseboard Temperature Via IPMI
     [Documentation]  Test baseboard temperature via IPMI and verify using REST.
     [Tags]  Test_Baseboard_Temperature_Via_IPMI