| 1 |  |  | import unittest | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  | from kafka_influxdb.encoder import collectd_json_encoder | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | import re | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  | class TestCollectdJsonEncoder(unittest.TestCase): | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  |     def setUp(self): | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  |         self.encoder = collectd_json_encoder.Encoder() | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 9 |  |  |  | 
            
                                                                        
                            
            
                                                                    
                                                                                                        
            
            
                | 10 |  | View Code Duplication |     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 |  |  |         """ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  |         Test encoding of messages in collectd json format | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  |         See https://github.com/mre/kafka-influxdb/issues/6 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  |         :return: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  |         """ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  |         msg = b""" | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  |         [{"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"}] | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  |         [{"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"}] | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  |         [{"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"}] | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  |         [{"values":[0],"dstypes":["gauge"],"dsnames":["value"],"time":1444745145.824,"interval":10.000,"host":"myhost","plugin":"cpu","plugin_instance":"0","type":"percent","type_instance":"interrupt"}] | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  |         [{"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"}] | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  |             """ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  |         expected = [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  |             ('cpu_1_percent,host=xx.example.internal,type_instance=system value=(.*) 1444745144', 0.6), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  |             ('cpu_1_percent,host=example.com,type_instance=user value=(.*) 1444745144', 0.7), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  |             ('cpu_0_percent,host=myhost,type_instance=nice value=(.*) 1444745144', 37.7), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  |             ('cpu_0_percent,host=myhost,type_instance=interrupt value=(.*) 1444745145', 0), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  |             ('memory_percent,host=myhost,type_instance=slab_recl value=(.*) 1444745136', 1.1) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  |         ] | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  |         encoded_messages = self.encoder.encode(msg) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  |         # We've encoded exactly one message | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  |         self.assertEqual(len(encoded_messages), 5) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  |         for encoded_message, expected in zip(encoded_messages, expected): | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  |             expected_message, expected_value = expected | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  |             result = re.match(expected_message, encoded_message) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  |             self.assertIsNotNone(result, "Unexpected message format") | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  |             self.assertEqual(len(result.groups()), 1) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  |             self.assertAlmostEqual(float(result.group(1)), expected_value) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  |     def test_invalid_messages(self): | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  |         invalid_messages = [b'', b'\n', b'bla', b'foo\nbar\nbaz'] | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  |         for msg in invalid_messages: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  |             self.assertEqual(self.encoder.encode(msg), []) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  |     def test_documentation_examples(self): | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  |         msg = b""" | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 |  |  |         [{"values":[0],"dstypes":["derive"],"dsnames":["value"],"time":1436372292.412,"interval":10.000,"host":"26f2fc918f50","plugin":"cpu","plugin_instance":"1","type":"cpu","type_instance":"interrupt"}] | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 |  |  |             """ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 |  |  |         expected = [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 |  |  |             'cpu_1_cpu,host=26f2fc918f50,type_instance=interrupt value=0 1436372292'] | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 |  |  |         self.assertEqual(self.encoder.encode(msg), expected) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 |  |  |  | 
            
                                                                                                            
                            
            
                                                                    
                                                                                                        
            
            
                | 81 |  | View Code Duplication |     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 |  |  | """ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 107 |  |  |     [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 108 |  |  |        { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 109 |  |  |          "values":  [1901474177], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 110 |  |  |          "dstypes":  ["counter"], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 111 |  |  |          "dsnames":    ["value"], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 112 |  |  |          "time":      1280959128, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 113 |  |  |          "interval":          10, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 114 |  |  |          "host":            "leeloo.octo.it", | 
            
                                                                                                            
                            
            
                                    
            
            
                | 115 |  |  |          "plugin":          "cpu", | 
            
                                                                                                            
                            
            
                                    
            
            
                | 116 |  |  |          "plugin_instance": "0", | 
            
                                                                                                            
                            
            
                                    
            
            
                | 117 |  |  |          "type":            "cpu", | 
            
                                                                                                            
                            
            
                                    
            
            
                | 118 |  |  |          "type_instance":   "idle" | 
            
                                                                                                            
                            
            
                                    
            
            
                | 119 |  |  |        } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 120 |  |  |     ] | 
            
                                                                                                            
                            
            
                                    
            
            
                | 121 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 122 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 123 |  |  | # See https://collectd.org/wiki/index.php/JSON | 
            
                                                                                                            
                            
            
                                    
            
            
                | 124 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 125 |  |  | [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 126 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 127 |  |  |         "values":  [1901474177], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 128 |  |  |         "dstypes":  ["counter"], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 129 |  |  |         "dsnames":    ["value"], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 130 |  |  |         "time":      1280959128, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 131 |  |  |         "interval":          10, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 132 |  |  |         "host":            "leeloo.octo.it", | 
            
                                                                                                            
                            
            
                                    
            
            
                | 133 |  |  |         "plugin":          "cpu", | 
            
                                                                                                            
                            
            
                                    
            
            
                | 134 |  |  |         "plugin_instance": "0", | 
            
                                                                                                            
                            
            
                                    
            
            
                | 135 |  |  |         "type":            "cpu", | 
            
                                                                                                            
                            
            
                                    
            
            
                | 136 |  |  |         "type_instance":   "idle" | 
            
                                                                                                            
                            
            
                                    
            
            
                | 137 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 138 |  |  | ] | 
            
                                                                                                            
                            
            
                                    
            
            
                | 139 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 140 |  |  | # See https://github.com/mjuenema/collectd-write_json | 
            
                                                                                                            
                            
            
                                    
            
            
                | 141 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 142 |  |  | [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 143 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 144 |  |  |         "dsnames": ['shorttem', 'midterm', 'longterm'], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 145 |  |  |         "dstypes": ['gauge', 'gauge', 'gauge'], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 146 |  |  |         "host": "localhost", | 
            
                                                                                                            
                            
            
                                    
            
            
                | 147 |  |  |         "interval": 5.0, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 148 |  |  |         "plugin": "load", | 
            
                                                                                                            
                            
            
                                    
            
            
                | 149 |  |  |         "plugin_instance": "", | 
            
                                                                                                            
                            
            
                                    
            
            
                | 150 |  |  |         "time": 1432086959.8153536, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 151 |  |  |         "type": "load", | 
            
                                                                                                            
                            
            
                                    
            
            
                | 152 |  |  |         "type_instance": "", | 
            
                                                                                                            
                            
            
                                    
            
            
                | 153 |  |  |         "values": [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 154 |  |  |             0.0, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 155 |  |  |             0.01, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 156 |  |  |             0.050000000000000003 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 157 |  |  |         ] | 
            
                                                                                                            
                            
            
                                    
            
            
                | 158 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 159 |  |  | ] | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 160 |  |  | """ | 
            
                                                        
            
                                    
            
            
                | 161 |  |  |  |