3989f58898f45a6f88e543f4a50c0ac717a90f2e
[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             }
52         }
53         self.data2 = {
54             "benchmark": {
55                 "timestamp": "1451478117.883505",
56                 "errors": "",
57                 "data": {
58                     "rtt": 0.613
59                 },
60                 "sequence": 1
61             },
62             "runner_id": 8921
63         }
64         self.data3 ={
65             "benchmark": {
66                 "data": {
67                     "mpstat": {
68                         "cpu0": {
69                             "%sys": "0.00",
70                             "%idle": "99.00"
71                         },
72                         "loadavg": [
73                             "1.09",
74                             "0.29"
75                         ]
76                     },
77                     "rtt": "1.03"
78                 }
79             }
80         }
81
82     def test_record_result_data_no_target(self):
83         influxdb = InfluxdbDispatcher(None)
84         influxdb.target = ''
85         self.assertEqual(influxdb.record_result_data(self.data1), -1)
86
87     def test_record_result_data_no_case_name(self):
88         influxdb = InfluxdbDispatcher(None)
89         self.assertEqual(influxdb.record_result_data(self.data2), -1)
90
91     @mock.patch('yardstick.dispatcher.influxdb.requests')
92     def test_record_result_data(self, mock_requests):
93         type(mock_requests.post.return_value).status_code = 204
94         influxdb = InfluxdbDispatcher(None)
95         self.assertEqual(influxdb.record_result_data(self.data1), 0)
96         self.assertEqual(influxdb.record_result_data(self.data2), 0)
97         self.assertEqual(influxdb.flush_result_data(), 0)
98
99     def test__dict_key_flatten(self):
100         line = 'mpstat.loadavg1=0.29,rtt=1.03,mpstat.loadavg0=1.09,mpstat.cpu0.%idle=99.00,mpstat.cpu0.%sys=0.00'
101         influxdb = InfluxdbDispatcher(None)
102         flattened_data = influxdb._dict_key_flatten(self.data3['benchmark']['data'])
103         result = ",".join([k+"="+v for k, v in flattened_data.items()])
104         self.assertEqual(result, line)
105
106     def test__get_nano_timestamp(self):
107         influxdb = InfluxdbDispatcher(None)
108         results = {'benchmark': {'timestamp': '1451461248.925574'}}
109         self.assertEqual(influxdb._get_nano_timestamp(results), '1451461248925574144')
110
111     @mock.patch('yardstick.dispatcher.influxdb.time')
112     def test__get_nano_timestamp_except(self, mock_time):
113         results = {}
114         influxdb = InfluxdbDispatcher(None)
115         mock_time.time.return_value = 1451461248.925574
116         self.assertEqual(influxdb._get_nano_timestamp(results), '1451461248925574144')
117
118 def main():
119     unittest.main()
120
121 if __name__ == '__main__':
122     main()