def test_endtoend(self): """We can get JSON status of collectors from StatusServer.""" collectors = { "a": tcollector.Collector("mycollector", 5, "a.py"), "b": tcollector.Collector("second", 3, "b.py"), } server = tcollector.StatusServer("127.0.0.1", 32025, collectors) # runs in background until test suite exits :( but it works. thread = threading.Thread(target=server.serve_forever) thread.setDaemon(True) thread.start() r = tcollector.urlopen("http://127.0.0.1:32025").read() self.assertEqual(json.loads(r), [c.to_json() for c in collectors.values()])
def test_json(self): """A collector can be serialized to JSON.""" collector = tcollector.Collector("myname", 17, "myname.py", mtime=23, lastspawn=15) collector.nextkill += 8 collector.killstate += 2 collector.lines_sent += 10 collector.lines_received += 65 collector.lines_invalid += 7 self.assertEqual( collector.to_json(), { "name": "myname", "mtime": 23, "lastspawn": 15, "killstate": 2, "nextkill": 8, "lines_sent": 10, "lines_received": 65, "lines_invalid": 7, "last_datapoint": collector.last_datapoint, "dead": False })
def test_prefix_added(self): """Namespace prefix gets added to metrics as they are read.""" thread = tcollector.ReaderThread(1, 10, True, "my.namespace.") collector = tcollector.Collector("c", 1, "c") line = "mymetric 123 12 a=b" thread.process_line(collector, line) self.assertEqual(thread.readerq.get(), "my.namespace." + line) self.assertEqual(collector.lines_received, 1) self.assertEqual(collector.lines_invalid, 0)
def test_bad_float(self): """Values that aren't ints/floats aren't sent to OpenTSDB. This can happen if a specific collector is buggy. """ thread = tcollector.ReaderThread(1, 10, True) collector = tcollector.Collector("c", 1, "c") for line in ["xxx", "mymetric 123 True a=b"]: thread.process_line(collector, line) self.assertEqual(thread.readerq.qsize(), 0) self.assertEqual(collector.lines_received, 2) self.assertEqual(collector.lines_invalid, 2)
def test_ok_lines(self): """Good lines are passed on to OpenTSDB.""" thread = tcollector.ReaderThread(1, 10, True) collector = tcollector.Collector("c", 1, "c") for line in [ "mymetric 123.24 12 a=b", "mymetric 124 12.7 a=b", "mymetric 125 12.7" ]: thread.process_line(collector, line) self.assertEqual(thread.readerq.qsize(), 1, line) self.assertEqual(thread.readerq.get(), line) self.assertEqual(collector.lines_received, 3) self.assertEqual(collector.lines_invalid, 0)
def test_bool_true_converted_int(self): """Values that aren't ints/floats aren't sent to OpenTSDB. This can happen if a specific collector is buggy. """ thread = tcollector.ReaderThread(1, 10, True) # pylint:disable=no-member collector = tcollector.Collector("c", 1, "c") # pylint:disable=no-member line = "mymetric 123 True a=b" expected = "mymetric 123 1 a=b" thread.process_line(collector, line) self.assertEqual(thread.readerq.qsize(), 1, line) self.assertEqual(thread.readerq.get(), line) self.assertEqual(collector.lines_received, 1) self.assertEqual(collector.lines_invalid, 0)