Add support for Python 3
[yardstick.git] / 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 from __future__ import absolute_import
15 import unittest
16
17 try:
18     from unittest import mock
19 except ImportError:
20     import mock
21
22 from yardstick.dispatcher.influxdb import InfluxdbDispatcher
23
24
25 class InfluxdbDispatcherTestCase(unittest.TestCase):
26
27     def setUp(self):
28         self.data1 = {
29             "runner_id": 8921,
30             "context_cfg": {
31                 "host": {
32                     "ip": "10.229.43.154",
33                     "key_filename":
34                         "/root/yardstick/yardstick/resources/files"
35                         "/yardstick_key",
36                     "name": "kvm.LF",
37                     "user": "root"
38                 },
39                 "target": {
40                     "ipaddr": "10.229.44.134"
41                 }
42             },
43             "scenario_cfg": {
44                 "runner": {
45                     "interval": 1,
46                     "object": "yardstick.benchmark.scenarios.networking.ping"
47                               ".Ping",
48                     "output_filename": "/tmp/yardstick.out",
49                     "runner_id": 8921,
50                     "duration": 10,
51                     "type": "Duration"
52                 },
53                 "host": "kvm.LF",
54                 "type": "Ping",
55                 "target": "10.229.44.134",
56                 "sla": {
57                     "action": "monitor",
58                     "max_rtt": 10
59                 },
60                 "tc": "ping",
61                 "task_id": "ea958583-c91e-461a-af14-2a7f9d7f79e7"
62             }
63         }
64         self.data2 = {
65             "benchmark": {
66                 "timestamp": "1451478117.883505",
67                 "errors": "",
68                 "data": {
69                     "rtt": 0.613
70                 },
71                 "sequence": 1
72             },
73             "runner_id": 8921
74         }
75         self.data3 = {
76             "benchmark": {
77                 "data": {
78                     "mpstat": {
79                         "cpu0": {
80                             "%sys": "0.00",
81                             "%idle": "99.00"
82                         },
83                         "loadavg": [
84                             "1.09",
85                             "0.29"
86                         ]
87                     },
88                     "rtt": "1.03"
89                 }
90             }
91         }
92
93     def test_record_result_data_no_target(self):
94         influxdb = InfluxdbDispatcher(None)
95         influxdb.target = ''
96         self.assertEqual(influxdb.record_result_data(self.data1), -1)
97
98     def test_record_result_data_no_case_name(self):
99         influxdb = InfluxdbDispatcher(None)
100         self.assertEqual(influxdb.record_result_data(self.data2), -1)
101
102     @mock.patch('yardstick.dispatcher.influxdb.requests')
103     def test_record_result_data(self, mock_requests):
104         type(mock_requests.post.return_value).status_code = 204
105         influxdb = InfluxdbDispatcher(None)
106         self.assertEqual(influxdb.record_result_data(self.data1), 0)
107         self.assertEqual(influxdb.record_result_data(self.data2), 0)
108         self.assertEqual(influxdb.flush_result_data(), 0)
109
110     def test__dict_key_flatten(self):
111         line = 'mpstat.loadavg1=0.29,rtt=1.03,mpstat.loadavg0=1.09,' \
112                'mpstat.cpu0.%idle=99.00,mpstat.cpu0.%sys=0.00'
113         # need to sort for assert to work
114         line = ",".join(sorted(line.split(',')))
115         influxdb = InfluxdbDispatcher(None)
116         flattened_data = influxdb._dict_key_flatten(
117             self.data3['benchmark']['data'])
118         result = ",".join(
119             [k + "=" + v for k, v in sorted(flattened_data.items())])
120         self.assertEqual(result, line)
121
122     def test__get_nano_timestamp(self):
123         influxdb = InfluxdbDispatcher(None)
124         results = {'benchmark': {'timestamp': '1451461248.925574'}}
125         self.assertEqual(influxdb._get_nano_timestamp(results),
126                          '1451461248925574144')
127
128     @mock.patch('yardstick.dispatcher.influxdb.time')
129     def test__get_nano_timestamp_except(self, mock_time):
130         results = {}
131         influxdb = InfluxdbDispatcher(None)
132         mock_time.time.return_value = 1451461248.925574
133         self.assertEqual(influxdb._get_nano_timestamp(results),
134                          '1451461248925574144')
135
136
137 def main():
138     unittest.main()
139
140
141 if __name__ == '__main__':
142     main()