b1d454340f68fb2ab22dd491319debbe6dcf8ac5
[bottlenecks.git] / utils / infra_setup / heat_template / vstf_heat_template / vstf_collector.py
1 ##############################################################################\r
2 # Copyright (c) 2015 Huawei Technologies Co.,Ltd. and others\r
3 #\r
4 # All rights reserved. This program and the accompanying materials\r
5 # are made available under the terms of the Apache License, Version 2.0\r
6 # which accompanies this distribution, and is available at\r
7 # http://www.apache.org/licenses/LICENSE-2.0\r
8 ##############################################################################\r
9 import os\r
10 import argparse\r
11 import json\r
12 import requests\r
13 import logging\r
14 __author__ = "qwyang0126@gmail.com"\r
15 __date__ = "2016-01-11"\r
16 LOG = logging.getLogger(__name__)\r
17 \r
18 \r
19 class Uploader(object):\r
20     def __init__(self, conf):\r
21         self.headers = {'Content-type': 'application/json'}\r
22         self.timeout = 5\r
23         self.result = {\r
24             "project_name": "bottlenecks",\r
25             "description": "bottlenecks test cases result"}\r
26 \r
27         with open(conf) as stream:\r
28             dashboard_conf = json.load(stream)\r
29         self.result['pod_name'] = dashboard_conf['pod_name']\r
30         self.result['installer'] = dashboard_conf['installer']\r
31         self.result['version'] = dashboard_conf['version']\r
32         self.target = dashboard_conf['target']\r
33 \r
34     def upload_result(self, case_name, raw_data):\r
35         if self.target == '':\r
36             LOG.error('No target was set, so no data will be posted.')\r
37             return\r
38         self.result["case_name"] = case_name\r
39         self.result["details"] = raw_data\r
40         try:\r
41             LOG.debug('Result to be uploaded:\n %s' % json.dumps(self.result, indent=4))\r
42             res = requests.post(self.target,\r
43                                 data=json.dumps(self.result),\r
44                                 headers=self.headers,\r
45                                 timeout=self.timeout)\r
46             print('Test result posting finished with status code %d.' % res.status_code)\r
47         except Exception as err:\r
48             LOG.error('Failed to record result data: %s', err)\r
49 \r
50 \r
51 if __name__ == "__main__":\r
52     logging.basicConfig(level=logging.DEBUG)\r
53     parser = argparse.ArgumentParser()\r
54     parser.add_argument('--config', required=True, help="basic config file for uploader, json format.")\r
55     parser.add_argument('--dir', required=True, help="result files for test cases")\r
56     args = parser.parse_args()\r
57     realpath = os.path.realpath(args.dir)\r
58     for filename in os.listdir(args.dir):\r
59         filepath = os.path.join(realpath, filename)\r
60         LOG.debug("uploading test result from file:%s", filepath)\r
61         with open(filepath) as stream:\r
62             result = eval(stream.read())\r
63             Uploader(args.config).upload_result(filename.lower().replace('-', ''), result)\r