Completed
Push — master ( 5cbcf8...d3d059 )
by Matthias
01:08
created

test_multiple_measurements()   A

Complexity

Conditions 1

Size

Total Lines 21

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
dl 0
loc 21
rs 9.3142
c 1
b 0
f 0
1
import unittest
2
from kafka_influxdb.encoder import collectd_json_encoder
3
4
5
class TestCollectdJsonEncoder(unittest.TestCase):
6
    def setUp(self):
7
        self.encoder = collectd_json_encoder.Encoder()
8
9
    def test_encode(self):
10
        """
11
        Test encoding of messages in collectd json format
12
        See https://github.com/mre/kafka-influxdb/issues/6
13
        :return:
14
        """
15
        msg = b"""
16
        [{"values":[0.6],"dstypes":["gauge"],"dsnames":["value"],"time":1444745144.824,"interval":10.000,"host":"xx.example.internal","plugin":"cpu","plugin_instance":"1","type":"percent","type_instance":"system"}]
17
            """
18
        expected = ['cpu-1_cpu-system,host=xx.example.internal value=0.6 1444745144.824']
19
        self.assertEqual(self.encoder.encode(msg), expected)
20
21
    def test_multiple_measurements(self):
22
        """
23
        Test encoding of messages in collectd json format
24
        See https://github.com/mre/kafka-influxdb/issues/6
25
        :return:
26
        """
27
        msg = b"""
28
        [{"values":[0.6],"dstypes":["gauge"],"dsnames":["value"],"time":1444745144.824,"interval":10.000,"host":"xx.example.internal","plugin":"cpu","plugin_instance":"1","type":"percent","type_instance":"system"}]
29
        [{"values":[0.7],"dstypes":["gauge"],"dsnames":["value"],"time":1444745144.824,"interval":10.000,"host":"example.com","plugin":"cpu","plugin_instance":"1","type":"percent","type_instance":"user"}]
30
        [{"values":[37.7],"dstypes":["gauge"],"dsnames":["value"],"time":1444745144.824,"interval":10.000,"host":"myhost","plugin":"cpu","plugin_instance":"0","type":"percent","type_instance":"nice"}]
31
        [{"values":[0],"dstypes":["gauge"],"dsnames":["value"],"time":1444745145.824,"interval":10.000,"host":"myhost","plugin":"cpu","plugin_instance":"0","type":"percent","type_instance":"interrupt"}]
32
        [{"values":[1.1],"dstypes":["gauge"],"dsnames":["value"],"time":1444745136.182,"interval":10.000,"host":"myhost","plugin":"memory","plugin_instance":"","type":"percent","type_instance":"slab_recl"}]
33
            """
34
        expected = [
35
            'cpu-1_cpu-system,host=xx.example.internal value=0.6 1444745144.824',
36
            'cpu-1_cpu-user,host=example.com value=0.7 1444745144.824',
37
            'cpu-0_cpu-nice,host=myhost value=37.7 1444745144.824',
38
            'cpu-0_cpu-interrupt,host=myhost value=0 1444745145.824',
39
            'memory_memory-slab_recl,host=myhost value=1.1 1444745136.182'
40
        ]
41
        self.assertEqual(self.encoder.encode(msg), expected)
42
43
    def test_invalid_messages(self):
44
        invalid_messages = [b'', b'\n', b'bla', b'foo\nbar\nbaz']
45
        for msg in invalid_messages:
46
            self.assertEqual(self.encoder.encode(msg), [])
47
48
    def test_documentation_examples(self):
49
        msg = b"""
50
        [{"values":[0],"dstypes":["derive"],"dsnames":["value"],"time":1436372292.412,"interval":10.000,"host":"26f2fc918f50","plugin":"cpu","plugin_instance":"1","type":"cpu","type_instance":"interrupt"}]
51
            """
52
        expected = ['cpu-1_cpu-interrupt,host=26f2fc918f50 value=0 1436372292.412']
53
        self.assertEqual(self.encoder.encode(msg), expected)
54
55
56
"""
57
    [
58
       {
59
         "values":  [1901474177],
60
         "dstypes":  ["counter"],
61
         "dsnames":    ["value"],
62
         "time":      1280959128,
63
         "interval":          10,
64
         "host":            "leeloo.octo.it",
65
         "plugin":          "cpu",
66
         "plugin_instance": "0",
67
         "type":            "cpu",
68
         "type_instance":   "idle"
69
       }
70
    ]
71
72
73
# See https://collectd.org/wiki/index.php/JSON
74
75
[
76
    {
77
        "values":  [1901474177],
78
        "dstypes":  ["counter"],
79
        "dsnames":    ["value"],
80
        "time":      1280959128,
81
        "interval":          10,
82
        "host":            "leeloo.octo.it",
83
        "plugin":          "cpu",
84
        "plugin_instance": "0",
85
        "type":            "cpu",
86
        "type_instance":   "idle"
87
    }
88
]
89
90
# See https://github.com/mjuenema/collectd-write_json
91
92
[
93
    {
94
        "dsnames": ['shorttem', 'midterm', 'longterm'],
95
        "dstypes": ['gauge', 'gauge', 'gauge'],
96
        "host": "localhost",
97
        "interval": 5.0,
98
        "plugin": "load",
99
        "plugin_instance": "",
100
        "time": 1432086959.8153536,
101
        "type": "load",
102
        "type_instance": "",
103
        "values": [
104
            0.0,
105
            0.01,
106
            0.050000000000000003
107
        ]
108
    }
109
]
110
"""
111