bugfix for vstf stack name
[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 class Uploader(object):\r
18     def __init__(self, conf):\r
19         self.headers = {'Content-type': 'application/json'}\r
20         self.timeout = 5\r
21         self.result = {\r
22             "project_name": "bottlenecks",\r
23             "description": "bottlenecks test cases result"}\r
24 \r
25         with open(conf) as stream:\r
26             dashboard_conf = json.load(stream)\r
27         self.result['pod_name'] = dashboard_conf['pod_name']\r
28         self.result['installer'] = dashboard_conf['installer']\r
29         self.result['version'] = dashboard_conf['version']\r
30         self.target = dashboard_conf['target']\r
31 \r
32     def upload_result(self, case_name, raw_data):\r
33         if self.target == '':\r
34             LOG.error('No target was set, so no data will be posted.')\r
35             return\r
36         self.result["case_name"] = case_name\r
37         self.result["details"] = raw_data\r
38         try:\r
39             LOG.debug('Result to be uploaded:\n %s' % json.dumps(self.result, indent=4))\r
40             res = requests.post(self.target,\r
41                                 data=json.dumps(self.result),\r
42                                 headers=self.headers,\r
43                                 timeout=self.timeout)\r
44             print('Test result posting finished with status code %d.' % res.status_code)\r
45         except Exception as err:\r
46             LOG.error('Failed to record result data: %s', err)\r
47 \r
48 \r
49 if __name__ == "__main__":\r
50     logging.basicConfig(level=logging.DEBUG)\r
51     parser = argparse.ArgumentParser()\r
52     parser.add_argument('--config', required=True, help="basic config file for uploader, json format.")\r
53     parser.add_argument('--dir', required=True, help="result files for test cases")\r
54     args = parser.parse_args()\r
55     realpath = os.path.realpath(args.dir)\r
56     for filename in os.listdir(args.dir):\r
57         filepath = os.path.join(realpath, filename)\r
58         LOG.debug("uploading test result from file:%s", filepath)\r
59         with open(filepath) as stream:\r
60             result = eval(stream.read())\r
61             Uploader(args.config).upload_result(filename.lower().replace('-', ''), result)\r