diff options
author | Yujun Zhang <zhang.yujunz@zte.com.cn> | 2017-04-26 10:14:59 +0800 |
---|---|---|
committer | Yujun Zhang <zhang.yujunz@zte.com.cn> | 2017-04-26 11:00:18 +0800 |
commit | 1403c94ef114912c5eef3700e123b137b030c2c7 (patch) | |
tree | 80b42128c74d9a06b3c9907bd4e1cab0ebb7647d | |
parent | 0c10841189cee0eab2dcd0a18ab946bb3ae617a0 (diff) |
Add support for result aggregation
Change-Id: I678b765f3f430cb6a5d130d94960273b8eea85e7
Signed-off-by: Yujun Zhang <zhang.yujunz@zte.com.cn>
-rw-r--r-- | qtip/ansible_library/plugins/action/aggregate.py | 22 | ||||
-rw-r--r-- | qtip/util/export_to.py | 5 | ||||
-rw-r--r-- | resources/ansible_roles/qtip/tasks/aggregate.yml | 5 | ||||
-rw-r--r-- | tests/data/results/expected.json | 7 | ||||
-rw-r--r-- | tests/data/results/host1/qpi.json | 3 | ||||
-rw-r--r-- | tests/data/results/host2/qpi.json | 3 | ||||
-rw-r--r-- | tests/unit/ansible_library/plugins/action/aggregate_test.py | 24 |
7 files changed, 62 insertions, 7 deletions
diff --git a/qtip/ansible_library/plugins/action/aggregate.py b/qtip/ansible_library/plugins/action/aggregate.py index 6e280419..f1451e06 100644 --- a/qtip/ansible_library/plugins/action/aggregate.py +++ b/qtip/ansible_library/plugins/action/aggregate.py @@ -9,10 +9,14 @@ # http://www.apache.org/licenses/LICENSE-2.0 ############################################################################## +import json from numpy import mean +import os from ansible.plugins.action import ActionBase +from qtip.util.export_to import export_to_file + class ActionModule(ActionBase): def run(self, tmp=None, task_vars=None): @@ -25,12 +29,22 @@ class ActionModule(ActionBase): if result.get('skipped', False): return result - return aggregate(self._task.args.get('group'), task_vars) + basepath = self._task.args.get('basepath') + + return aggregate( + hosts=task_vars['groups'][self._task.args.get('group')], + basepath=basepath, + src=self._task.args.get('src'), + dest=os.path.join(basepath, self._task.args.get('dest')) + ) # aggregate QPI results -def aggregate(group, task_vars): - qpi_results = [task_vars['hostvars'][host]['qpi_result'] for host in task_vars['groups'][group]] +@export_to_file +def aggregate(hosts, basepath, src): + host_results = [{'host': host, 'result': json.load(open(os.path.join(basepath, host, src)))} for host in hosts] + score = int(mean([r['result']['score'] for r in host_results])) return { - 'score': int(mean([r['score'] for r in qpi_results])) + 'score': score, + 'host_results': host_results } diff --git a/qtip/util/export_to.py b/qtip/util/export_to.py index 17adae7c..4d054c66 100644 --- a/qtip/util/export_to.py +++ b/qtip/util/export_to.py @@ -11,8 +11,9 @@ import json def export_to_file(func): - def func_wrapper(spec, metrics, dest=None): - content = func(spec, metrics) + def func_wrapper(*args, **kwargs): + dest = kwargs.pop('dest', None) + content = func(*args, **kwargs) if dest is not None: with open(dest, 'w+') as f: f.write(json.dumps(content, indent=2)) diff --git a/resources/ansible_roles/qtip/tasks/aggregate.yml b/resources/ansible_roles/qtip/tasks/aggregate.yml index 659874fe..9ecdc700 100644 --- a/resources/ansible_roles/qtip/tasks/aggregate.yml +++ b/resources/ansible_roles/qtip/tasks/aggregate.yml @@ -9,7 +9,10 @@ --- -- name: aggregate results from all tested nodes +- name: aggregating results from all tested nodes aggregate: group: compute + basepath: "{{ qtip_results }}/current" + src: "compute.json" + dest: "{{ pod_name }}-qpi.json" register: pod_result diff --git a/tests/data/results/expected.json b/tests/data/results/expected.json new file mode 100644 index 00000000..a495d999 --- /dev/null +++ b/tests/data/results/expected.json @@ -0,0 +1,7 @@ +{ + "score": 150, + "host_results": [ + {"host": "host1", "result": {"score": 100}}, + {"host": "host2", "result": {"score": 200}} + ] +} diff --git a/tests/data/results/host1/qpi.json b/tests/data/results/host1/qpi.json new file mode 100644 index 00000000..ff8cbf80 --- /dev/null +++ b/tests/data/results/host1/qpi.json @@ -0,0 +1,3 @@ +{ + "score": 100 +}
\ No newline at end of file diff --git a/tests/data/results/host2/qpi.json b/tests/data/results/host2/qpi.json new file mode 100644 index 00000000..1b848ba5 --- /dev/null +++ b/tests/data/results/host2/qpi.json @@ -0,0 +1,3 @@ +{ + "score": 200 +}
\ No newline at end of file diff --git a/tests/unit/ansible_library/plugins/action/aggregate_test.py b/tests/unit/ansible_library/plugins/action/aggregate_test.py new file mode 100644 index 00000000..71706e5b --- /dev/null +++ b/tests/unit/ansible_library/plugins/action/aggregate_test.py @@ -0,0 +1,24 @@ +############################################################################## +# Copyright (c) 2017 ZTE Corp and others. +# +# All rights reserved. This program and the accompanying materials +# are made available under the terms of the Apache License, Version 2.0 +# which accompanies this distribution, and is available at +# http://www.apache.org/licenses/LICENSE-2.0 +############################################################################## + +import json +import os + +from qtip.ansible_library.plugins.action.aggregate import aggregate + + +def test_aggregate(data_root): + hosts = ['host1', 'host2'] + pod_results = aggregate( + hosts=hosts, + basepath=os.path.join(data_root, 'results'), + src='qpi.json' + ) + expected = json.load(open(os.path.join(data_root, 'results', 'expected.json'))) + assert pod_results == expected |