Fan control dump utility
Changes:
- Add code to dump and get attributes from
fan config JSON file generated dump.
- Add python function to walk the nested dictionary
to find the matched key.
Tested:
- Test on sandbox with sample test code calling it.
Robot code example:
${holds_data}= Get Fan Attribute Value target_holds
Should Be Empty ${holds_data}
${locks_data}= Get Fan Attribute Value target_locks
Should Be Empty ${locks_data}
Change-Id: Idae5f1d06331cd20f2d689a910efcea9251544a2
Signed-off-by: George Keishing <gkeishin@in.ibm.com>
diff --git a/lib/open_power_utils.robot b/lib/open_power_utils.robot
index 343436b..08474ea 100644
--- a/lib/open_power_utils.robot
+++ b/lib/open_power_utils.robot
@@ -4,6 +4,7 @@
Variables ../data/variables.py
Resource ../lib/utils.robot
Resource ../lib/connection_client.robot
+Library utilities.py
*** Variables ***
${functional_cpu_count} ${0}
@@ -401,3 +402,45 @@
Rprint Vars json_sensor_data
Fail Runtime generated Dbus sensors does not match
+
+Dump Fan Control JSON
+ [Documentation] Execute fan control on BMC to dump config with 'fanctl dump',
+ ... which makes it write a /tmp/fan_control_dump.json file.
+
+ ${output} ${stderr} ${rc} = BMC Execute Command test -f /usr/bin/fanctl
+ ... print_err=1 ignore_err=1
+ Return From Keyword If ${rc} == 1 fanctl application doesn't exist.
+
+ # This command will force a fan_control_dump.json file in temp path and
+ # takes few seconds to complete..
+ BMC Execute Command fanctl dump
+ Sleep 10s
+
+ # Check for the generated file and return the file data as JSON and fails if
+ # it doesn't find file generated.
+ ${cmd}= Catenate test -f /tmp/fan_control_dump.json; cat /tmp/fan_control_dump.json
+ ${json_string} ${stderr} ${rc} = BMC Execute Command ${cmd}
+ ... print_out=1 print_err=1 ignore_err=1
+
+ Should Be True ${rc} == 0 msg=No Fan control config JSON file is generated.
+ ${fan_json}= Evaluate json.loads('''${json_string}''') json
+
+ [return] ${fan_json}
+
+
+Get Fan Attribute Value
+ [Documentation] Return the specified value of the matched search key in
+ ... nested dictionary data.
+ [Arguments] ${key_value}
+
+ # Description of Argument(s):
+ # key_value User input attribute value in the dictionary.
+
+ ${fan_dict}= Dump Fan Control JSON
+
+ # Python module: get_value_from_nested_dict(key,dict)
+ ${value_list}= utilities.Get Value From Nested Dict ${key_value} ${fan_dict}
+
+ Should Not Be Empty ${value_list} msg=${key_value} key attribute not found.
+
+ [Return] ${value_list[0]}
diff --git a/lib/utilities.py b/lib/utilities.py
index 96421e8..403164b 100755
--- a/lib/utilities.py
+++ b/lib/utilities.py
@@ -307,3 +307,20 @@
for item in data_list:
prefix_string += prefix + item + " "
return prefix_string.strip()
+
+
+def get_value_from_nested_dict(key, nested_dict):
+ r"""
+ Returns the key value from the nested dictionary.
+
+ key Key value of the dictionary to look up.
+ nested_dict Dictionary data.
+ """
+ result = []
+ for k, v in nested_dict.items():
+ if k == key:
+ result.append(v)
+ elif isinstance(v, dict) and k != key:
+ result += get_value_from_nested_dict(key, v)
+
+ return result