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
18 from unittest import mock
22 from yardstick.dispatcher.influxdb import InfluxdbDispatcher
25 class InfluxdbDispatcherTestCase(unittest.TestCase):
32 "ip": "10.229.43.154",
34 "/root/yardstick/yardstick/resources/files"
40 "ipaddr": "10.229.44.134"
46 "object": "yardstick.benchmark.scenarios.networking.ping"
48 "output_filename": "/tmp/yardstick.out",
55 "target": "10.229.44.134",
61 "task_id": "ea958583-c91e-461a-af14-2a7f9d7f79e7"
66 "timestamp": "1451478117.883505",
93 self.yardstick_conf = {'yardstick': {}}
95 def test_record_result_data_no_target(self):
96 influxdb = InfluxdbDispatcher(None, self.yardstick_conf)
98 self.assertEqual(influxdb.record_result_data(self.data1), -1)
100 def test_record_result_data_no_case_name(self):
101 influxdb = InfluxdbDispatcher(None, self.yardstick_conf)
102 self.assertEqual(influxdb.record_result_data(self.data2), -1)
104 @mock.patch('yardstick.dispatcher.influxdb.requests')
105 def test_record_result_data(self, mock_requests):
106 type(mock_requests.post.return_value).status_code = 204
107 influxdb = InfluxdbDispatcher(None, self.yardstick_conf)
108 self.assertEqual(influxdb.record_result_data(self.data1), 0)
109 self.assertEqual(influxdb.record_result_data(self.data2), 0)
110 self.assertEqual(influxdb.flush_result_data(), 0)
112 def test__dict_key_flatten(self):
113 line = 'mpstat.loadavg1=0.29,rtt=1.03,mpstat.loadavg0=1.09,' \
114 'mpstat.cpu0.%idle=99.00,mpstat.cpu0.%sys=0.00'
115 # need to sort for assert to work
116 line = ",".join(sorted(line.split(',')))
117 influxdb = InfluxdbDispatcher(None, self.yardstick_conf)
118 flattened_data = influxdb._dict_key_flatten(
119 self.data3['benchmark']['data'])
121 [k + "=" + v for k, v in sorted(flattened_data.items())])
122 self.assertEqual(result, line)
124 def test__get_nano_timestamp(self):
125 influxdb = InfluxdbDispatcher(None, self.yardstick_conf)
126 results = {'benchmark': {'timestamp': '1451461248.925574'}}
127 self.assertEqual(influxdb._get_nano_timestamp(results),
128 '1451461248925574144')
130 @mock.patch('yardstick.dispatcher.influxdb.time')
131 def test__get_nano_timestamp_except(self, mock_time):
133 influxdb = InfluxdbDispatcher(None, self.yardstick_conf)
134 mock_time.time.return_value = 1451461248.925574
135 self.assertEqual(influxdb._get_nano_timestamp(results),
136 '1451461248925574144')
143 if __name__ == '__main__':