Yardstick output format unified
[yardstick.git] / yardstick / dispatcher / http.py
1 # Copyright 2013 IBM Corp
2 # All Rights Reserved.
3 #
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
7 #
8 #         http://www.apache.org/licenses/LICENSE-2.0
9 #
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
14 #    under the License.
15
16 # yardstick comment: this is a modified copy of
17 # ceilometer/ceilometer/dispatcher/http.py
18
19 from __future__ import absolute_import
20
21 import logging
22 import os
23 from datetime import datetime
24
25 from oslo_serialization import jsonutils
26 import requests
27
28 from yardstick.dispatcher.base import Base as DispatchBase
29
30 LOG = logging.getLogger(__name__)
31
32
33 class HttpDispatcher(DispatchBase):
34     """Dispatcher class for posting data into a http target.
35     """
36
37     __dispatcher_type__ = "Http"
38
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')
45
46     def flush_result_data(self, data):
47         if self.target == '':
48             # if the target was not set, do not do anything
49             LOG.error('Dispatcher target was not set, no data will'
50                       'be posted.')
51             return
52
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']
58
59         for case, data in testcases.items():
60             self._upload_case_result(case, data)
61
62     def _upload_case_result(self, case, data):
63         try:
64             scenario_data = data.get('tc_data', [])[0]
65         except IndexError:
66             current_time = datetime.now()
67         else:
68             timestamp = float(scenario_data.get('timestamp', 0.0))
69             current_time = datetime.fromtimestamp(timestamp)
70
71         result = {
72             "project_name": "yardstick",
73             "case_name": case,
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": "",
84             "details": ""
85         }
86
87         try:
88             LOG.debug('Test result : %s', result)
89             res = requests.post(self.target,
90                                 data=jsonutils.dump_as_bytes(result),
91                                 headers=self.headers,
92                                 timeout=self.timeout)
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)