Merge "Change PTL informatin in INFO"
[bottlenecks.git] / utils / dispatcher / http.py
1 ##############################################################################
2 # Copyright (c) 2015 Huawei Technologies Co.,Ltd and others.
3 # liangqi1@huawei.com matthew.lijun@huawei.com
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 os
11 import json
12 import logging
13 import requests
14
15 from oslo_config import cfg
16
17 from utils.dispatcher.base import Base as DispatchBase
18
19 LOG = logging.getLogger(__name__)
20
21 CONF = cfg.CONF
22 http_dispatcher_opts = [
23     cfg.StrOpt('target',
24                default='http://127.0.0.1:8000/results',
25                help='The target where the http request will be sent. '
26                     'If this is not set, no data will be posted. For '
27                     'example: target = http://hostname:1234/path'),
28     cfg.IntOpt('timeout',
29                default=5,
30                help='The max time in seconds to wait for a request to '
31                     'timeout.'),
32 ]
33
34 CONF.register_opts(http_dispatcher_opts, group="dispatcher_http")
35
36
37 class HttpDispatcher(DispatchBase):
38     """Dispatcher class for posting data into a http target.
39     """
40
41     __dispatcher_type__ = "Http"
42
43     def __init__(self, conf):
44         super(HttpDispatcher, self).__init__(conf)
45         self.headers = {'Content-type': 'application/json'}
46         self.timeout = CONF.dispatcher_http.timeout
47         self.target = CONF.dispatcher_http.target
48         self.raw_result = []
49         self.result = {
50             "project_name": "bottlenecks",
51             "description": "bottlenecks test cases result",
52             "pod_name": os.environ.get('NODE_NAME', 'unknown'),
53             "installer": os.environ.get('INSTALLER_TYPE', 'unknown'),
54             "version": os.environ.get('BOTTLENECKS_VERSION', 'unknown')
55         }
56
57     def record_result_data(self, data):
58         self.raw_result.append(data)
59
60     def flush_result_data(self):
61         if self.target == '':
62             # if the target was not set, do not do anything
63             LOG.error('Dispatcher target was not set, no data will'
64                       'be posted.')
65             return
66
67         self.result["details"] = self.raw_result
68
69         case_name = ""
70         for v in self.raw_result:
71             if isinstance(v, dict) and "scenario_cfg" in v:
72                 case_name = v["scenario_cfg"]["type"]
73                 break
74         if case_name == "":
75             LOG.error('Test result : %s' % json.dumps(self.result))
76             LOG.error('The case_name cannot be found, no data will be posted.')
77             return
78
79         self.result["case_name"] = case_name
80
81         try:
82             LOG.debug('Test result : %s' % json.dumps(self.result))
83             res = requests.post(self.target,
84                                 data=json.dumps(self.result),
85                                 headers=self.headers,
86                                 timeout=self.timeout)
87             LOG.debug('Test result posting finished with status code'
88                       ' %d.' % res.status_code)
89         except Exception as err:
90             LOG.exception('Failed to record result data: %s',
91                           err)