3 ##############################################################################
4 # Copyright (c) 2015 Huawei Technologies Co.,Ltd and others.
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 ##############################################################################
12 # Unittest for yardstick.dispatcher.influxdb
14 from __future__ import absolute_import
19 from unittest import mock
23 from yardstick import _init_logging
26 from yardstick.dispatcher.influxdb import InfluxdbDispatcher
29 class InfluxdbDispatcherTestCase(unittest.TestCase):
36 "ip": "10.229.43.154",
38 "/root/yardstick/yardstick/resources/files"
44 "ipaddr": "10.229.44.134"
50 "object": "yardstick.benchmark.scenarios.networking.ping"
52 "output_filename": "/tmp/yardstick.out",
59 "target": "10.229.44.134",
65 "task_id": "ea958583-c91e-461a-af14-2a7f9d7f79e7"
70 "timestamp": "1451478117.883505",
97 self.yardstick_conf = {'yardstick': {}}
99 def test_record_result_data_no_target(self):
100 influxdb = InfluxdbDispatcher(None, self.yardstick_conf)
102 self.assertEqual(influxdb.record_result_data(self.data1), -1)
104 def test_record_result_data_no_case_name(self):
105 influxdb = InfluxdbDispatcher(None, self.yardstick_conf)
106 self.assertEqual(influxdb.record_result_data(self.data2), -1)
108 @mock.patch('yardstick.dispatcher.influxdb.requests')
109 def test_record_result_data(self, mock_requests):
110 type(mock_requests.post.return_value).status_code = 204
111 influxdb = InfluxdbDispatcher(None, self.yardstick_conf)
112 self.assertEqual(influxdb.record_result_data(self.data1), 0)
113 self.assertEqual(influxdb.record_result_data(self.data2), 0)
114 self.assertEqual(influxdb.flush_result_data(), 0)
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(None, self.yardstick_conf)
122 flattened_data = influxdb._dict_key_flatten(
123 self.data3['benchmark']['data'])
125 [k + "=" + v for k, v in sorted(flattened_data.items())])
126 self.assertEqual(result, line)
128 def test__get_nano_timestamp(self):
129 influxdb = InfluxdbDispatcher(None, self.yardstick_conf)
130 results = {'benchmark': {'timestamp': '1451461248.925574'}}
131 self.assertEqual(influxdb._get_nano_timestamp(results),
132 '1451461248925574144')
134 @mock.patch('yardstick.dispatcher.influxdb.time')
135 def test__get_nano_timestamp_except(self, mock_time):
137 influxdb = InfluxdbDispatcher(None, self.yardstick_conf)
138 mock_time.time.return_value = 1451461248.925574
139 self.assertEqual(influxdb._get_nano_timestamp(results),
140 '1451461248925574144')
147 if __name__ == '__main__':