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