|  | @@ 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 |  |         """ | 
                                                                                
                                |  | @@ 81-103 (lines=23) @@ | 
                                                            
                                    | 78 |  |             'cpu_1_cpu,host=26f2fc918f50,type_instance=interrupt value=0 1436372292'] | 
                                                            
                                    | 79 |  |         self.assertEqual(self.encoder.encode(msg), expected) | 
                                                            
                                    | 80 |  |  | 
                                                            
                                    | 81 |  |     def test_multiple_fields(self): | 
                                                            
                                    | 82 |  |         """ | 
                                                            
                                    | 83 |  |         Test supporting multiple fields in a sample | 
                                                            
                                    | 84 |  |         [{"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"}] | 
                                                            
                                    | 85 |  |         """ | 
                                                            
                                    | 86 |  |         msg = b""" | 
                                                            
                                    | 87 |  |         [{"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"}] | 
                                                            
                                    | 88 |  |         """ | 
                                                            
                                    | 89 |  |  | 
                                                            
                                    | 90 |  |         encoded_messages = self.encoder.encode(msg) | 
                                                            
                                    | 91 |  |  | 
                                                            
                                    | 92 |  |         self.assertEqual(len(encoded_messages), 1) | 
                                                            
                                    | 93 |  |  | 
                                                            
                                    | 94 |  |         encoded_message = encoded_messages[0] | 
                                                            
                                    | 95 |  |  | 
                                                            
                                    | 96 |  |         expected = '^sys_usage_1_percent,host=26f2fc918f50 cpu_usage=(\d\.\d+),mem_usage=(\d\.\d+) 1436372292$' | 
                                                            
                                    | 97 |  |         result = re.match(expected, encoded_message) | 
                                                            
                                    | 98 |  |  | 
                                                            
                                    | 99 |  |         # Due to floating point precision there might be a tiny difference between the expected and the actual value. | 
                                                            
                                    | 100 |  |         self.assertIsNotNone(result, "Unexpected message format") | 
                                                            
                                    | 101 |  |         self.assertEqual(len(result.groups()), 2) | 
                                                            
                                    | 102 |  |         self.assertAlmostEqual(float(result.group(1)), 0.2) | 
                                                            
                                    | 103 |  |         self.assertAlmostEqual(float(result.group(2)), 0.3) | 
                                                            
                                    | 104 |  |  | 
                                                            
                                    | 105 |  |  | 
                                                            
                                    | 106 |  | """ |