From a3bc932740b796ffd8a3dd721c73bdcb32843412 Mon Sep 17 00:00:00 2001 From: Hiroshi Hatake Date: Fri, 19 Jul 2024 20:44:19 +0900 Subject: [PATCH] decoding: decode_statsd: tests: Add a test case Signed-off-by: Hiroshi Hatake --- tests/data/statsd_payload.txt | 13 +++++++++++++ tests/decoding.c | 32 ++++++++++++++++++++++++++++++++ 2 files changed, 45 insertions(+) create mode 100644 tests/data/statsd_payload.txt diff --git a/tests/data/statsd_payload.txt b/tests/data/statsd_payload.txt new file mode 100644 index 0000000..d71221d --- /dev/null +++ b/tests/data/statsd_payload.txt @@ -0,0 +1,13 @@ +statsdTestMetric011:5000|g|#mykey:myvalue,mykey2:othervalue +statsdTestMetric012:400|s|@0.125|#mykey:myvalue +statsdTestMetric013:+500|g|#mykey:myvalue +statsdTestMetric014:-400|g|#mykey:myvalue +statsdTestMetric015:+2|g|#mykey:myvalue +statsdTestMetric016:-1|g|@0.1|#mykey:myvalue +statsdTestMetric021:365|g|#mykey:myvalue +statsdTestMetric022:+300|c|#mykey:myvalue +statsdTestMetric023:-200|s|#mykey:myvalue +statsdTestMetric024:200|g|#mykey:myvalue +expohisto:1|ms|#mykey:myvalue +expohisto:0|ms|#mykey:myvalue +expohisto:-1|ms|#mykey:myvalue diff --git a/tests/decoding.c b/tests/decoding.c index 358ca53..6c70811 100644 --- a/tests/decoding.c +++ b/tests/decoding.c @@ -28,6 +28,7 @@ #include #include #include +#include #include "cmt_tests.h" @@ -197,9 +198,40 @@ void test_prometheus_remote_write() cfl_sds_destroy(payload); } +void test_statsd() +{ + int ret; + struct cmt *decoded_context; + cfl_sds_t payload = read_file(CMT_TESTS_DATA_PATH "/statsd_payload.txt"); + size_t len = 0; + cfl_sds_t text = NULL; + int flags = 0; + + /* For strtok_r, fill the last byte as \0. */ + len = cfl_sds_len(payload); + cfl_sds_set_len(payload, len + 1); + payload[len] = '\0'; + + cmt_initialize(); + + flags |= CMT_DECODE_STATSD_GAUGE_OBSERVER; + + ret = cmt_decode_statsd_create(&decoded_context, payload, cfl_sds_len(payload), flags); + TEST_CHECK(ret == CMT_DECODE_PROMETHEUS_REMOTE_WRITE_SUCCESS); + text = cmt_encode_prometheus_create(decoded_context, CMT_FALSE); + + printf("%s\n", text); + cmt_encode_prometheus_destroy(text); + + cmt_decode_statsd_destroy(decoded_context); + + cfl_sds_destroy(payload); +} + TEST_LIST = { {"opentelemetry", test_opentelemetry}, {"prometheus_remote_write", test_prometheus_remote_write}, + {"statsd", test_statsd}, { 0 } };