monasca-agent/tests/test_statsd.py
Doug Szumski a2400fcf12 Fix parsing of StatsD metrics with Py3
When running with Py3 we compare a byte string to a unicode string
when parsing StatsD metrics. This patch adds some unit tests to
reproduce the bug and decodes the bytestring to make the existing
comparisons valid under Py3. When backporting to Train we can use
Oslo encodeutils. Clearly we could have more unit tests, but
this makes a start.

Change-Id: I6341f96f5c186428d2d829cabf618a6f84f40ce2
Story: 2007684
Task: 39796
2020-05-22 21:08:28 +01:00

50 lines
1.7 KiB
Python

from unittest import mock
import unittest
import monasca_agent.common.metrics as metrics_pkg
import monasca_agent.statsd.udp as udp
class TestStatsd(unittest.TestCase):
def testSubmitPacket(self):
mock_aggregator = mock.Mock()
srv = udp.Server(mock_aggregator, 'localhost', 1234)
test_packet = b"monasca.log.out_logs_truncated_bytes:0|g|#" \
b"{'service': 'monitoring', 'component': 'monasca-log-api'}"
srv.submit_packets(test_packet)
mock_aggregator.submit_metric.assert_called_once_with(
'monasca.log.out_logs_truncated_bytes',
0,
metrics_pkg.Gauge,
dimensions={
'service': 'monitoring',
'component': 'monasca-log-api'},
sample_rate=1)
def testSubmitPackets(self):
mock_aggregator = mock.Mock()
srv = udp.Server(mock_aggregator, 'localhost', 1234)
test_packet = b"monasca.log.out_logs_truncated_bytes:0|g|#" \
b"{'service': 'monitoring', 'component': 'monasca-log-api'}\n" \
b"application_metric:10|c|#{'service': 'workload'}"
srv.submit_packets(test_packet)
mock_aggregator.submit_metric.assert_has_calls([
mock.call(
'monasca.log.out_logs_truncated_bytes',
0,
metrics_pkg.Gauge,
dimensions={
'service': 'monitoring',
'component': 'monasca-log-api'},
sample_rate=1
),
mock.call(
'application_metric',
10,
metrics_pkg.Counter,
dimensions={
'service': 'workload'},
sample_rate=1
)
])