add k8s util functions for compatibility of upstream changes
[bottlenecks.git] / utils / dashboard / uploader.py
1 ##############################################################################
2 # Copyright (c) 2015 Huawei Technologies Co.,Ltd. and others
3 #
4 # All rights reserved. This program and the accompanying materials
5 # are made available under the terms of the Apache License, Version 2.0
6 # which accompanies this distribution, and is available at
7 # http://www.apache.org/licenses/LICENSE-2.0
8 ##############################################################################
9
10 import sys
11 import json
12 import requests
13 import yaml
14
15
16 class Uploader(object):
17
18     def __init__(self, conf):
19         self.headers = {'Content-type': 'application/json'}
20         self.timeout = 5
21         self.result = {
22             "project_name": "bottlenecks",
23             "description": "bottlenecks test cases result"}
24
25         with open(conf) as stream:
26             dashboard_conf = yaml.load(stream)
27         self.result['pod_name'] = dashboard_conf['pod_name']
28         self.result['installer'] = dashboard_conf['installer']
29         self.result['version'] = dashboard_conf['version']
30         self.target = dashboard_conf['target']
31
32     def upload_result(self, case_name, raw_data):
33         if self.target == '':
34             print('No target was set, so no data will be posted.')
35             return
36         self.result["case_name"] = case_name
37         self.result["details"] = raw_data
38
39         try:
40             print('Result to be uploaded:\n %s' % json.dumps(self.result))
41             res = requests.post(self.target,
42                                 data=json.dumps(self.result),
43                                 headers=self.headers,
44                                 timeout=self.timeout)
45             print(
46                 'Test result posting finished with status code %d.' %
47                 res.status_code)
48         except Exception as err:
49             print ('Failed to record result data: %s', err)
50
51
52 def _test():
53
54     # data = '{"details": [{"client": 200, "throughput": 20},
55     # {"client": 300, "throughput": 20}], "case_name": "rubbos"}'
56     if len(sys.argv) < 2:
57         print ("no argumens input!!")
58         exit(1)
59
60     with open(sys.argv[1], 'r') as stream:
61         data = json.load(stream)
62         Uploader().upload_result(data)
63
64
65 if __name__ == "__main__":
66     _test()