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
17 from yardstick.dispatcher.influxdb import InfluxdbDispatcher
19 class InfluxdbDispatcherTestCase(unittest.TestCase):
26 "ip": "10.229.43.154",
27 "key_filename": "/root/yardstick/yardstick/resources/files/yardstick_key",
32 "ipaddr": "10.229.44.134"
38 "object": "yardstick.benchmark.scenarios.networking.ping.Ping",
39 "output_filename": "/tmp/yardstick.out",
46 "target": "10.229.44.134",
52 "task_id": "ea958583-c91e-461a-af14-2a7f9d7f79e7"
57 "timestamp": "1451478117.883505",
84 def test_record_result_data_no_target(self):
85 influxdb = InfluxdbDispatcher(None)
87 self.assertEqual(influxdb.record_result_data(self.data1), -1)
89 def test_record_result_data_no_case_name(self):
90 influxdb = InfluxdbDispatcher(None)
91 self.assertEqual(influxdb.record_result_data(self.data2), -1)
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)
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)
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')
113 @mock.patch('yardstick.dispatcher.influxdb.time')
114 def test__get_nano_timestamp_except(self, mock_time):
116 influxdb = InfluxdbDispatcher(None)
117 mock_time.time.return_value = 1451461248.925574
118 self.assertEqual(influxdb._get_nano_timestamp(results), '1451461248925574144')
123 if __name__ == '__main__':