|
@@ 10-33 (lines=24) @@
|
| 7 |
|
def setUp(self): |
| 8 |
|
self.encoder = collectd_json_encoder.Encoder() |
| 9 |
|
|
| 10 |
|
def test_encode(self): |
| 11 |
|
""" |
| 12 |
|
Test encoding of messages in collectd json format |
| 13 |
|
See https://github.com/mre/kafka-influxdb/issues/6 |
| 14 |
|
:return: |
| 15 |
|
""" |
| 16 |
|
msg = b""" |
| 17 |
|
[{"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"}] |
| 18 |
|
""" |
| 19 |
|
|
| 20 |
|
encoded_messages = self.encoder.encode(msg) |
| 21 |
|
|
| 22 |
|
# We've encoded exactly one message |
| 23 |
|
self.assertEqual(len(encoded_messages), 1) |
| 24 |
|
|
| 25 |
|
encoded_message = encoded_messages[0] |
| 26 |
|
|
| 27 |
|
expected = '^cpu_1_percent,host=xx\.example\.internal,type_instance=system value=(\d\.\d+) 1444745144$' |
| 28 |
|
result = re.match(expected, encoded_message) |
| 29 |
|
|
| 30 |
|
# Due to floating point precision there might be a tiny difference between the expected and the actual value. |
| 31 |
|
self.assertIsNotNone(result, "Unexpected message format") |
| 32 |
|
self.assertEqual(len(result.groups()), 1) |
| 33 |
|
self.assertAlmostEqual(float(result.group(1)), 0.6) |
| 34 |
|
|
| 35 |
|
def test_multiple_measurements(self): |
| 36 |
|
""" |
|
@@ 80-102 (lines=23) @@
|
| 77 |
|
expected = ['cpu_1_cpu,host=26f2fc918f50,type_instance=interrupt value=0 1436372292'] |
| 78 |
|
self.assertEqual(self.encoder.encode(msg), expected) |
| 79 |
|
|
| 80 |
|
def test_multiple_fields(self): |
| 81 |
|
""" |
| 82 |
|
Test supporting multiple fields in a sample |
| 83 |
|
[{"values":[0.2, 0.3],"dstypes":["derive"],"dsnames":["cpu_usage", "mem_usage"],"time":1436372292.412,"interval":10.000,"host":"26f2fc918f50","plugin":"sys_usage","plugin_instance":"1","type":"percent"}] |
| 84 |
|
""" |
| 85 |
|
msg = b""" |
| 86 |
|
[{"values":[0.2, 0.3],"dstypes":["derive"],"dsnames":["cpu_usage", "mem_usage"],"time":1436372292.412,"interval":10.000,"host":"26f2fc918f50","plugin":"sys_usage","plugin_instance":"1","type":"percent"}] |
| 87 |
|
""" |
| 88 |
|
|
| 89 |
|
encoded_messages = self.encoder.encode(msg) |
| 90 |
|
|
| 91 |
|
self.assertEqual(len(encoded_messages), 1) |
| 92 |
|
|
| 93 |
|
encoded_message = encoded_messages[0] |
| 94 |
|
|
| 95 |
|
expected = '^sys_usage_1_percent,host=26f2fc918f50 cpu_usage=(\d\.\d+),mem_usage=(\d\.\d+) 1436372292$' |
| 96 |
|
result = re.match(expected, encoded_message) |
| 97 |
|
|
| 98 |
|
# Due to floating point precision there might be a tiny difference between the expected and the actual value. |
| 99 |
|
self.assertIsNotNone(result, "Unexpected message format") |
| 100 |
|
self.assertEqual(len(result.groups()), 2) |
| 101 |
|
self.assertAlmostEqual(float(result.group(1)), 0.2) |
| 102 |
|
self.assertAlmostEqual(float(result.group(2)), 0.3) |
| 103 |
|
|
| 104 |
|
|
| 105 |
|
""" |