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 ##############################################################################
15 from oslo_config import cfg
17 from utils.dispatcher.base import Base as DispatchBase
19 LOG = logging.getLogger(__name__)
22 http_dispatcher_opts = [
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'),
30 help='The max time in seconds to wait for a request to '
34 CONF.register_opts(http_dispatcher_opts, group="dispatcher_http")
37 class HttpDispatcher(DispatchBase):
38 """Dispatcher class for posting data into a http target.
41 __dispatcher_type__ = "Http"
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
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')
57 def record_result_data(self, data):
58 self.raw_result.append(data)
60 def flush_result_data(self):
62 # if the target was not set, do not do anything
63 LOG.error('Dispatcher target was not set, no data will'
67 self.result["details"] = self.raw_result
70 for v in self.raw_result:
71 if isinstance(v, dict) and "scenario_cfg" in v:
72 case_name = v["scenario_cfg"]["type"]
75 LOG.error('Test result : %s' % json.dumps(self.result))
76 LOG.error('The case_name cannot be found, no data will be posted.')
79 self.result["case_name"] = case_name
82 LOG.debug('Test result : %s' % json.dumps(self.result))
83 res = requests.post(self.target,
84 data=json.dumps(self.result),
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',