Merge "Change PTL informatin in INFO"
[bottlenecks.git] / testsuites / vstf / 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 \r
15 LOG = logging.getLogger(__name__)\r
16 \r
17 \r
18 class Uploader(object):\r
19 \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(\r
42                 'Result to be uploaded:\n %s' %\r
43                 json.dumps(\r
44                     self.result,\r
45                     indent=4))\r
46             res = requests.post(self.target,\r
47                                 data=json.dumps(self.result),\r
48                                 headers=self.headers,\r
49                                 timeout=self.timeout)\r
50             print(\r
51                 'Test result posting finished with status code %d.' %\r
52                 res.status_code)\r
53         except Exception as err:\r
54             LOG.error('Failed to record result data: %s', err)\r
55 \r
56 \r
57 if __name__ == "__main__":\r
58     logging.basicConfig(level=logging.DEBUG)\r
59     parser = argparse.ArgumentParser()\r
60     parser.add_argument(\r
61         '--config',\r
62         required=True,\r
63         help="basic config file for uploader, json format.")\r
64     parser.add_argument(\r
65         '--dir',\r
66         required=True,\r
67         help="result files for test cases")\r
68     args = parser.parse_args()\r
69     realpath = os.path.realpath(args.dir)\r
70     for filename in os.listdir(args.dir):\r
71         filepath = os.path.join(realpath, filename)\r
72         LOG.debug("uploading test result from file:%s", filepath)\r
73         with open(filepath) as stream:\r
74             result = eval(stream.read())\r
75             Uploader(\r
76                 args.config).upload_result(\r
77                 filename.lower().replace(\r
78                     '-',\r
79                     ''),\r
80                 result)\r