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