1 # Copyright 2013 IBM Corp
4 # Licensed under the Apache License, Version 2.0 (the "License"); you may
5 # not use this file except in compliance with the License. You may obtain
6 # a copy of the License at
8 # http://www.apache.org/licenses/LICENSE-2.0
10 # Unless required by applicable law or agreed to in writing, software
11 # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12 # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13 # License for the specific language governing permissions and limitations
16 # yardstick comment: this is a modified copy of
17 # ceilometer/ceilometer/dispatcher/http.py
19 from __future__ import absolute_import
24 from oslo_serialization import jsonutils
26 from oslo_config import cfg
28 from yardstick.dispatcher.base import Base as DispatchBase
30 LOG = logging.getLogger(__name__)
33 http_dispatcher_opts = [
35 default='http://127.0.0.1:8000/results',
36 help='The target where the http request will be sent. '
37 'If this is not set, no data will be posted. For '
38 'example: target = http://hostname:1234/path'),
41 help='The max time in seconds to wait for a request to '
45 CONF.register_opts(http_dispatcher_opts, group="dispatcher_http")
48 class HttpDispatcher(DispatchBase):
49 """Dispatcher class for posting data into a http target.
52 __dispatcher_type__ = "Http"
54 def __init__(self, conf, config):
55 super(HttpDispatcher, self).__init__(conf)
56 self.headers = {'Content-type': 'application/json'}
57 self.timeout = CONF.dispatcher_http.timeout
58 self.target = CONF.dispatcher_http.target
61 "project_name": "yardstick",
62 "description": "yardstick test cases result",
63 "pod_name": os.environ.get('NODE_NAME', 'unknown'),
64 "installer": os.environ.get('INSTALLER_TYPE', 'unknown'),
65 "version": os.environ.get('YARDSTICK_VERSION', 'unknown')
68 def record_result_data(self, data):
69 self.raw_result.append(data)
71 def flush_result_data(self):
73 # if the target was not set, do not do anything
74 LOG.error('Dispatcher target was not set, no data will'
78 self.result["details"] = self.raw_result
81 for v in self.raw_result:
82 if isinstance(v, dict) and "scenario_cfg" in v:
83 case_name = v["scenario_cfg"]["tc"]
86 LOG.error('Test result : %s',
87 jsonutils.dump_as_bytes(self.result))
88 LOG.error('The case_name cannot be found, no data will be posted.')
91 self.result["case_name"] = case_name
94 LOG.debug('Test result : %s',
95 jsonutils.dump_as_bytes(self.result))
96 res = requests.post(self.target,
97 data=jsonutils.dump_as_bytes(self.result),
100 LOG.debug('Test result posting finished with status code'
101 ' %d.' % res.status_code)
102 except Exception as err:
103 LOG.exception('Failed to record result data: %s',