Merge "Add UT: DurationRunner worker normal operation"
[yardstick.git] / yardstick / tests / unit / dispatcher / test_influxdb.py
1 ##############################################################################
2 # Copyright (c) 2015 Huawei Technologies Co.,Ltd and others.
3 #
4 # All rights reserved. This program and the accompanying materials
5 # are made available under the terms of the Apache License, Version 2.0
6 # which accompanies this distribution, and is available at
7 # http://www.apache.org/licenses/LICENSE-2.0
8 ##############################################################################
9
10 # Unittest for yardstick.dispatcher.influxdb
11
12 import mock
13 import unittest
14
15 from yardstick.dispatcher.influxdb import InfluxdbDispatcher
16 from yardstick import _init_logging
17
18
19 _init_logging()
20
21
22 class InfluxdbDispatcherTestCase(unittest.TestCase):
23
24     def setUp(self):
25         self.data1 = {
26             "runner_id": 8921,
27             "context_cfg": {
28                 "host": {
29                     "ip": "10.229.43.154",
30                     "key_filename":
31                         "/root/yardstick/yardstick/resources/files"
32                         "/yardstick_key",
33                     "name": "kvm.LF",
34                     "user": "root"
35                 },
36                 "target": {
37                     "ipaddr": "10.229.44.134"
38                 }
39             },
40             "scenario_cfg": {
41                 "runner": {
42                     "interval": 1,
43                     "object": "yardstick.benchmark.scenarios.networking.ping"
44                               ".Ping",
45                     "output_filename": "/tmp/yardstick.out",
46                     "runner_id": 8921,
47                     "duration": 10,
48                     "type": "Duration"
49                 },
50                 "host": "kvm.LF",
51                 "type": "Ping",
52                 "target": "10.229.44.134",
53                 "sla": {
54                     "action": "monitor",
55                     "max_rtt": 10
56                 },
57                 "tc": "ping",
58                 "task_id": "ea958583-c91e-461a-af14-2a7f9d7f79e7"
59             }
60         }
61         self.data2 = {
62             "benchmark": {
63                 "timestamp": "1451478117.883505",
64                 "errors": "",
65                 "data": {
66                     "rtt": 0.613
67                 },
68                 "sequence": 1
69             },
70             "runner_id": 8921
71         }
72
73         self.yardstick_conf = {'dispatcher_influxdb': {}}
74
75     @mock.patch('yardstick.dispatcher.influxdb.requests')
76     def test_record_result_data(self, mock_requests):
77         type(mock_requests.post.return_value).status_code = 204
78         influxdb = InfluxdbDispatcher(self.yardstick_conf)
79         data = {
80             'status': 1,
81             'result': {
82                 'criteria': 'PASS',
83                 'info': {
84                 },
85                 'task_id': 'b9e2bbc2-dfd8-410d-8c24-07771e9f7979',
86                 'testcases': {
87                 }
88             }
89         }
90         self.assertEqual(influxdb.flush_result_data(data), 0)
91
92     def test__get_nano_timestamp(self):
93         influxdb = InfluxdbDispatcher(self.yardstick_conf)
94         results = {'timestamp': '1451461248.925574'}
95         self.assertEqual(influxdb._get_nano_timestamp(results),
96                          '1451461248925574144')
97
98     @mock.patch('yardstick.dispatcher.influxdb.time')
99     def test__get_nano_timestamp_except(self, mock_time):
100         results = {}
101         influxdb = InfluxdbDispatcher(self.yardstick_conf)
102         mock_time.time.return_value = 1451461248.925574
103         self.assertEqual(influxdb._get_nano_timestamp(results),
104                          '1451461248925574144')