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