aboutsummaryrefslogtreecommitdiffstats
path: root/tests/unit/dispatcher/test_influxdb.py
blob: b84389e7e6add78b7f87a9d1d655d5fe06743114 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
#!/usr/bin/env python

##############################################################################
# Copyright (c) 2015 Huawei Technologies Co.,Ltd and others.
#
# All rights reserved. This program and the accompanying materials
# are made available under the terms of the Apache License, Version 2.0
# which accompanies this distribution, and is available at
# http://www.apache.org/licenses/LICENSE-2.0
##############################################################################

# Unittest for yardstick.dispatcher.influxdb

from __future__ import absolute_import
import unittest

try:
    from unittest import mock
except ImportError:
    import mock

from yardstick.dispatcher.influxdb import InfluxdbDispatcher


class InfluxdbDispatcherTestCase(unittest.TestCase):

    def setUp(self):
        self.data1 = {
            "runner_id": 8921,
            "context_cfg": {
                "host": {
                    "ip": "10.229.43.154",
                    "key_filename":
                        "/root/yardstick/yardstick/resources/files"
                        "/yardstick_key",
                    "name": "kvm.LF",
                    "user": "root"
                },
                "target": {
                    "ipaddr": "10.229.44.134"
                }
            },
            "scenario_cfg": {
                "runner": {
                    "interval": 1,
                    "object": "yardstick.benchmark.scenarios.networking.ping"
                              ".Ping",
                    "output_filename": "/tmp/yardstick.out",
                    "runner_id": 8921,
                    "duration": 10,
                    "type": "Duration"
                },
                "host": "kvm.LF",
                "type": "Ping",
                "target": "10.229.44.134",
                "sla": {
                    "action": "monitor",
                    "max_rtt": 10
                },
                "tc": "ping",
                "task_id": "ea958583-c91e-461a-af14-2a7f9d7f79e7"
            }
        }
        self.data2 = {
            "benchmark": {
                "timestamp": "1451478117.883505",
                "errors": "",
                "data": {
                    "rtt": 0.613
                },
                "sequence": 1
            },
            "runner_id": 8921
        }
        self.data3 = {
            "benchmark": {
                "data": {
                    "mpstat": {
                        "cpu0": {
                            "%sys": "0.00",
                            "%idle": "99.00"
                        },
                        "loadavg": [
                            "1.09",
                            "0.29"
                        ]
                    },
                    "rtt": "1.03"
                }
            }
        }

    def test_record_result_data_no_target(self):
        influxdb = InfluxdbDispatcher(None)
        influxdb.target = ''
        self.assertEqual(influxdb.record_result_data(self.data1), -1)

    def test_record_result_data_no_case_name(self):
        influxdb = InfluxdbDispatcher(None)
        self.assertEqual(influxdb.record_result_data(self.data2), -1)

    @mock.patch('yardstick.dispatcher.influxdb.requests')
    def test_record_result_data(self, mock_requests):
        type(mock_requests.post.return_value).status_code = 204
        influxdb = InfluxdbDispatcher(None)
        self.assertEqual(influxdb.record_result_data(self.data1), 0)
        self.assertEqual(influxdb.record_result_data(self.data2), 0)
        self.assertEqual(influxdb.flush_result_data(), 0)

    def test__dict_key_flatten(self):
        line = 'mpstat.loadavg1=0.29,rtt=1.03,mpstat.loadavg0=1.09,' \
               'mpstat.cpu0.%idle=99.00,mpstat.cpu0.%sys=0.00'
        # need to sort for assert to work
        line = ",".join(sorted(line.split(',')))
        influxdb = InfluxdbDispatcher(None)
        flattened_data = influxdb._dict_key_flatten(
            self.data3['benchmark']['data'])
        result = ",".join(
            [k + "=" + v for k, v in sorted(flattened_data.items())])
        self.assertEqual(result, line)

    def test__get_nano_timestamp(self):
        influxdb = InfluxdbDispatcher(None)
        results = {'benchmark': {'timestamp': '1451461248.925574'}}
        self.assertEqual(influxdb._get_nano_timestamp(results),
                         '1451461248925574144')

    @mock.patch('yardstick.dispatcher.influxdb.time')
    def test__get_nano_timestamp_except(self, mock_time):
        results = {}
        influxdb = InfluxdbDispatcher(None)
        mock_time.time.return_value = 1451461248.925574
        self.assertEqual(influxdb._get_nano_timestamp(results),
                         '1451461248925574144')


def main():
    unittest.main()


if __name__ == '__main__':
    main()