Add support for result aggregation 11/33811/2
authorYujun Zhang <zhang.yujunz@zte.com.cn>
Wed, 26 Apr 2017 02:14:59 +0000 (10:14 +0800)
committerYujun Zhang <zhang.yujunz@zte.com.cn>
Wed, 26 Apr 2017 03:00:18 +0000 (11:00 +0800)
Change-Id: I678b765f3f430cb6a5d130d94960273b8eea85e7
Signed-off-by: Yujun Zhang <zhang.yujunz@zte.com.cn>
qtip/ansible_library/plugins/action/aggregate.py
qtip/util/export_to.py
resources/ansible_roles/qtip/tasks/aggregate.yml
tests/data/results/expected.json [new file with mode: 0644]
tests/data/results/host1/qpi.json [new file with mode: 0644]
tests/data/results/host2/qpi.json [new file with mode: 0644]
tests/unit/ansible_library/plugins/action/aggregate_test.py [new file with mode: 0644]

index 6e28041..f1451e0 100644 (file)
@@ -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
     }
index 17adae7..4d054c6 100644 (file)
@@ -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))
index 659874f..9ecdc70 100644 (file)
@@ -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 (file)
index 0000000..a495d99
--- /dev/null
@@ -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 (file)
index 0000000..ff8cbf8
--- /dev/null
@@ -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 (file)
index 0000000..1b848ba
--- /dev/null
@@ -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 (file)
index 0000000..71706e5
--- /dev/null
@@ -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