Use result_collection_api to store test result
[yardstick.git] / yardstick / dispatcher / http.py
1 ##############################################################################
2 # Copyright (c) 2015 Huawei Technologies Co.,Ltd and others.
3 #
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 ##############################################################################
9
10 import json
11 import logging
12 import requests
13
14 from oslo_config import cfg
15
16 from yardstick.dispatcher.base import Base as DispatchBase
17
18 LOG = logging.getLogger(__name__)
19
20 CONF = cfg.CONF
21 http_dispatcher_opts = [
22     cfg.StrOpt('target',
23                default='http://127.0.0.1:8000/results',
24                help='The target where the http request will be sent. '
25                     'If this is not set, no data will be posted. For '
26                     'example: target = http://hostname:1234/path'),
27     cfg.IntOpt('timeout',
28                default=5,
29                help='The max time in seconds to wait for a request to '
30                     'timeout.'),
31 ]
32
33 CONF.register_opts(http_dispatcher_opts, group="dispatcher_http")
34
35
36 class HttpDispatcher(DispatchBase):
37     """Dispatcher class for posting data into a http target.
38     """
39
40     __dispatcher_type__ = "Http"
41
42     def __init__(self, conf):
43         super(HttpDispatcher, self).__init__(conf)
44         self.headers = {'Content-type': 'application/json'}
45         self.timeout = CONF.dispatcher_http.timeout
46         self.target = CONF.dispatcher_http.target
47         self.raw_result = []
48         # TODO set pod_name, installer, version based on pod info
49         self.result = {
50             "project_name": "yardstick",
51             "description": "yardstick test cases result",
52             "pod_name": "opnfv-jump-2",
53             "installer": "compass",
54             "version": "Brahmaputra-dev"
55         }
56
57     def record_result_data(self, data):
58         self.raw_result.append(data)
59
60     def flush_result_data(self):
61         if self.target == '':
62             # if the target was not set, do not do anything
63             LOG.error('Dispatcher target was not set, no data will'
64                       'be posted.')
65             return
66
67         self.result["details"] = self.raw_result
68
69         case_name = ""
70         for v in self.raw_result:
71             if isinstance(v, dict) and "scenario_cfg" in v:
72                 case_name = v["scenario_cfg"]["type"]
73                 break
74         if case_name == "":
75             LOG.error('Test result : %s' % json.dumps(self.result))
76             LOG.error('The case_name cannot be found, no data will be posted.')
77             return
78
79         self.result["case_name"] = case_name
80
81         try:
82             LOG.debug('Test result : %s' % json.dumps(self.result))
83             res = requests.post(self.target,
84                                 data=json.dumps(self.result),
85                                 headers=self.headers,
86                                 timeout=self.timeout)
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',
91                           err)