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
23 from datetime import datetime
25 from oslo_serialization import jsonutils
28 from yardstick.dispatcher.base import Base as DispatchBase
30 LOG = logging.getLogger(__name__)
33 class HttpDispatcher(DispatchBase):
34 """Dispatcher class for posting data into a http target.
37 __dispatcher_type__ = "Http"
39 def __init__(self, conf):
40 super(HttpDispatcher, self).__init__(conf)
41 http_conf = conf['dispatcher_http']
42 self.headers = {'Content-type': 'application/json'}
43 self.timeout = int(http_conf.get('timeout', 5))
44 self.target = http_conf.get('target', 'http://127.0.0.1:8000/results')
46 def flush_result_data(self, data):
48 # if the target was not set, do not do anything
49 LOG.error('Dispatcher target was not set, no data will'
53 result = data['result']
54 self.info = result['info']
55 self.task_id = result['task_id']
56 self.criteria = result['criteria']
57 testcases = result['testcases']
59 for case, data in testcases.items():
60 self._upload_case_result(case, data)
62 def _upload_case_result(self, case, data):
64 scenario_data = data.get('tc_data', [])[0]
66 current_time = datetime.now()
68 timestamp = float(scenario_data.get('timestamp', 0.0))
69 current_time = datetime.fromtimestamp(timestamp)
72 "project_name": "yardstick",
74 "description": "yardstick ci scenario status",
75 "scenario": self.info.get('deploy_scenario'),
76 "version": self.info.get('version'),
77 "pod_name": self.info.get('pod_name'),
78 "installer": self.info.get('installer'),
79 "build_tag": os.environ.get('BUILD_TAG'),
80 "criteria": data.get('criteria'),
81 "start_date": current_time.strftime('%Y-%m-%d %H:%M:%S'),
82 "stop_date": current_time.strftime('%Y-%m-%d %H:%M:%S'),
83 "trust_indicator": "",
88 LOG.debug('Test result : %s', result)
89 res = requests.post(self.target,
90 data=jsonutils.dump_as_bytes(result),
93 LOG.debug('Test result posting finished with status code'
94 ' %d.' % res.status_code)
95 except Exception as err:
96 LOG.exception('Failed to record result data: %s', err)