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",
55 "timestamp": "1451478117.883505",
82 def test_record_result_data_no_target(self):
83 influxdb = InfluxdbDispatcher(None)
85 self.assertEqual(influxdb.record_result_data(self.data1), -1)
87 def test_record_result_data_no_case_name(self):
88 influxdb = InfluxdbDispatcher(None)
89 self.assertEqual(influxdb.record_result_data(self.data2), -1)
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)
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)
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')
111 @mock.patch('yardstick.dispatcher.influxdb.time')
112 def test__get_nano_timestamp_except(self, mock_time):
114 influxdb = InfluxdbDispatcher(None)
115 mock_time.time.return_value = 1451461248.925574
116 self.assertEqual(influxdb._get_nano_timestamp(results), '1451461248925574144')
121 if __name__ == '__main__':