bca94e385539870d4063c256d5c4eec99ff649e2
[yardstick.git] / yardstick / tests / unit / dispatcher / test_influxdb.py
1 #!/usr/bin/env python
2
3 ##############################################################################
4 # Copyright (c) 2015 Huawei Technologies Co.,Ltd and others.
5 #
6 # All rights reserved. This program and the accompanying materials
7 # are made available under the terms of the Apache License, Version 2.0
8 # which accompanies this distribution, and is available at
9 # http://www.apache.org/licenses/LICENSE-2.0
10 ##############################################################################
11
12 # Unittest for yardstick.dispatcher.influxdb
13
14 import mock
15 import unittest
16
17 from yardstick.dispatcher.influxdb import InfluxdbDispatcher
18 from yardstick import _init_logging
19
20
21 _init_logging()
22
23
24 class InfluxdbDispatcherTestCase(unittest.TestCase):
25
26     def setUp(self):
27         self.data1 = {
28             "runner_id": 8921,
29             "context_cfg": {
30                 "host": {
31                     "ip": "10.229.43.154",
32                     "key_filename":
33                         "/root/yardstick/yardstick/resources/files"
34                         "/yardstick_key",
35                     "name": "kvm.LF",
36                     "user": "root"
37                 },
38                 "target": {
39                     "ipaddr": "10.229.44.134"
40                 }
41             },
42             "scenario_cfg": {
43                 "runner": {
44                     "interval": 1,
45                     "object": "yardstick.benchmark.scenarios.networking.ping"
46                               ".Ping",
47                     "output_filename": "/tmp/yardstick.out",
48                     "runner_id": 8921,
49                     "duration": 10,
50                     "type": "Duration"
51                 },
52                 "host": "kvm.LF",
53                 "type": "Ping",
54                 "target": "10.229.44.134",
55                 "sla": {
56                     "action": "monitor",
57                     "max_rtt": 10
58                 },
59                 "tc": "ping",
60                 "task_id": "ea958583-c91e-461a-af14-2a7f9d7f79e7"
61             }
62         }
63         self.data2 = {
64             "benchmark": {
65                 "timestamp": "1451478117.883505",
66                 "errors": "",
67                 "data": {
68                     "rtt": 0.613
69                 },
70                 "sequence": 1
71             },
72             "runner_id": 8921
73         }
74
75         self.yardstick_conf = {'dispatcher_influxdb': {}}
76
77     @mock.patch('yardstick.dispatcher.influxdb.requests')
78     def test_record_result_data(self, mock_requests):
79         type(mock_requests.post.return_value).status_code = 204
80         influxdb = InfluxdbDispatcher(self.yardstick_conf)
81         data = {
82             'status': 1,
83             'result': {
84                 'criteria': 'PASS',
85                 'info': {
86                 },
87                 'task_id': 'b9e2bbc2-dfd8-410d-8c24-07771e9f7979',
88                 'testcases': {
89                 }
90             }
91         }
92         self.assertEqual(influxdb.flush_result_data(data), 0)
93
94     def test__get_nano_timestamp(self):
95         influxdb = InfluxdbDispatcher(self.yardstick_conf)
96         results = {'timestamp': '1451461248.925574'}
97         self.assertEqual(influxdb._get_nano_timestamp(results),
98                          '1451461248925574144')
99
100     @mock.patch('yardstick.dispatcher.influxdb.time')
101     def test__get_nano_timestamp_except(self, mock_time):
102         results = {}
103         influxdb = InfluxdbDispatcher(self.yardstick_conf)
104         mock_time.time.return_value = 1451461248.925574
105         self.assertEqual(influxdb._get_nano_timestamp(results),
106                          '1451461248925574144')
107
108
109 def main():
110     unittest.main()
111
112
113 if __name__ == '__main__':
114     main()