Add vstf_collector.py for upload tu case results. 51/6251/1
authorYiting.Li <liyiting@huawei.com>
Mon, 11 Jan 2016 12:43:17 +0000 (20:43 +0800)
committerJun Li <matthew.lijun@huawei.com>
Tue, 12 Jan 2016 03:25:07 +0000 (03:25 +0000)
JIRA: BOTTLENECK-29

Signed-off-by: qwyang0126 <qwyang0126@gmail.com>
Change-Id: I238da7d9f208b3bba61001583e493a5362f5bc33
(cherry picked from commit 770a36af6a4b8681b4566cc970056c5645f6f6cf)

utils/infra_setup/heat_template/vstf_heat_template/dashboard.json [new file with mode: 0755]
utils/infra_setup/heat_template/vstf_heat_template/vstf_collector.py [new file with mode: 0755]
utils/infra_setup/heat_template/vstf_heat_template/vstf_test.sh

diff --git a/utils/infra_setup/heat_template/vstf_heat_template/dashboard.json b/utils/infra_setup/heat_template/vstf_heat_template/dashboard.json
new file mode 100755 (executable)
index 0000000..df6f64c
--- /dev/null
@@ -0,0 +1,6 @@
+{\r
+  "pod_name": "unknown-pod",\r
+  "installer": "fuel",\r
+  "version": "unknown",\r
+  "target": "http://213.77.62.197/results"\r
+}
\ No newline at end of file
diff --git a/utils/infra_setup/heat_template/vstf_heat_template/vstf_collector.py b/utils/infra_setup/heat_template/vstf_heat_template/vstf_collector.py
new file mode 100755 (executable)
index 0000000..b1d4543
--- /dev/null
@@ -0,0 +1,63 @@
+##############################################################################\r
+# Copyright (c) 2015 Huawei Technologies Co.,Ltd. and others\r
+#\r
+# All rights reserved. This program and the accompanying materials\r
+# are made available under the terms of the Apache License, Version 2.0\r
+# which accompanies this distribution, and is available at\r
+# http://www.apache.org/licenses/LICENSE-2.0\r
+##############################################################################\r
+import os\r
+import argparse\r
+import json\r
+import requests\r
+import logging\r
+__author__ = "qwyang0126@gmail.com"\r
+__date__ = "2016-01-11"\r
+LOG = logging.getLogger(__name__)\r
+\r
+\r
+class Uploader(object):\r
+    def __init__(self, conf):\r
+        self.headers = {'Content-type': 'application/json'}\r
+        self.timeout = 5\r
+        self.result = {\r
+            "project_name": "bottlenecks",\r
+            "description": "bottlenecks test cases result"}\r
+\r
+        with open(conf) as stream:\r
+            dashboard_conf = json.load(stream)\r
+        self.result['pod_name'] = dashboard_conf['pod_name']\r
+        self.result['installer'] = dashboard_conf['installer']\r
+        self.result['version'] = dashboard_conf['version']\r
+        self.target = dashboard_conf['target']\r
+\r
+    def upload_result(self, case_name, raw_data):\r
+        if self.target == '':\r
+            LOG.error('No target was set, so no data will be posted.')\r
+            return\r
+        self.result["case_name"] = case_name\r
+        self.result["details"] = raw_data\r
+        try:\r
+            LOG.debug('Result to be uploaded:\n %s' % json.dumps(self.result, indent=4))\r
+            res = requests.post(self.target,\r
+                                data=json.dumps(self.result),\r
+                                headers=self.headers,\r
+                                timeout=self.timeout)\r
+            print('Test result posting finished with status code %d.' % res.status_code)\r
+        except Exception as err:\r
+            LOG.error('Failed to record result data: %s', err)\r
+\r
+\r
+if __name__ == "__main__":\r
+    logging.basicConfig(level=logging.DEBUG)\r
+    parser = argparse.ArgumentParser()\r
+    parser.add_argument('--config', required=True, help="basic config file for uploader, json format.")\r
+    parser.add_argument('--dir', required=True, help="result files for test cases")\r
+    args = parser.parse_args()\r
+    realpath = os.path.realpath(args.dir)\r
+    for filename in os.listdir(args.dir):\r
+        filepath = os.path.join(realpath, filename)\r
+        LOG.debug("uploading test result from file:%s", filepath)\r
+        with open(filepath) as stream:\r
+            result = eval(stream.read())\r
+            Uploader(args.config).upload_result(filename.lower().replace('-', ''), result)\r
index 051f285..d162e4c 100644 (file)
@@ -75,11 +75,16 @@ function fn_result(){
     return 0
 }
 
+function fn_upload(){
+    python vstf_collector.py --config dashboard.json --dir result
+}
+
 function main(){
     fn_parser_ipaddress
     fn_vstf_test_config
     fn_testing_scenario
     fn_result
+    fn_upload
     return 0
 }