Skip to content

Commit

Permalink
in_statsd: tests: Add metrics On test cases
Browse files Browse the repository at this point in the history
Signed-off-by: Hiroshi Hatake <[email protected]>
  • Loading branch information
cosmo0920 authored and edsiper committed Aug 14, 2024
1 parent fed5553 commit f4ce7dd
Showing 1 changed file with 78 additions and 8 deletions.
86 changes: 78 additions & 8 deletions tests/runtime/in_statsd.c
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,7 @@ static int init_udp(char *in_host, int in_port, struct sockaddr_in *addr)
return fd;
}

static int test_normal(char *payload, struct str_list *expected)
static int test_normal(char *payload, struct str_list *expected, int use_metrics)
{
struct flb_lib_out_cb cb_data;
struct test_ctx *ctx;
Expand Down Expand Up @@ -202,6 +202,13 @@ static int test_normal(char *payload, struct str_list *expected)
exit(EXIT_FAILURE);
}

if (use_metrics == FLB_TRUE) {
ret = flb_input_set(ctx->flb, ctx->i_ffd,
"metrics", "On",
NULL);
TEST_CHECK(ret == 0);
}

ret = flb_output_set(ctx->flb, ctx->o_ffd,
"format", "json",
NULL);
Expand Down Expand Up @@ -249,7 +256,7 @@ void flb_test_statsd_count()
char *buf = "gorets:1|c";
int ret;

ret = test_normal(buf, &expected);
ret = test_normal(buf, &expected, FLB_FALSE);
if (!TEST_CHECK(ret == 0)) {
TEST_MSG("test failed");
exit(EXIT_FAILURE);
Expand All @@ -259,7 +266,7 @@ void flb_test_statsd_count()

void flb_test_statsd_sample()
{
char *expected_strs[] = {"\"bucket\":\"gorets\"", "\"type\":\"counter\"",
char *expected_strs[] = {"\"bucket\":\"gorets\"", "\"type\":\"counter\"",
"\"value\":1", "\"sample_rate\":0.1"};
struct str_list expected = {
.size = sizeof(expected_strs)/sizeof(char*),
Expand All @@ -269,7 +276,7 @@ void flb_test_statsd_sample()
char *buf = "gorets:1|c|@0.1";
int ret;

ret = test_normal(buf, &expected);
ret = test_normal(buf, &expected, FLB_FALSE);
if (!TEST_CHECK(ret == 0)) {
TEST_MSG("test failed");
exit(EXIT_FAILURE);
Expand All @@ -288,7 +295,7 @@ void flb_test_statsd_gauge()
char *buf = "gaugor:333|g";
int ret;

ret = test_normal(buf, &expected);
ret = test_normal(buf, &expected, FLB_FALSE);
if (!TEST_CHECK(ret == 0)) {
TEST_MSG("test failed");
exit(EXIT_FAILURE);
Expand All @@ -297,7 +304,7 @@ void flb_test_statsd_gauge()

void flb_test_statsd_set()
{
char *expected_strs[] = {"\"bucket\":\"uniques\"", "\"type\":\"set\"",
char *expected_strs[] = {"\"bucket\":\"uniques\"", "\"type\":\"set\"",
"\"value\":\"765\""};
struct str_list expected = {
.size = sizeof(expected_strs)/sizeof(char*),
Expand All @@ -307,18 +314,81 @@ void flb_test_statsd_set()
char *buf = "uniques:765|s";
int ret;

ret = test_normal(buf, &expected);
ret = test_normal(buf, &expected, FLB_FALSE);
if (!TEST_CHECK(ret == 0)) {
TEST_MSG("test failed");
exit(EXIT_FAILURE);
}
}

#ifdef FLB_HAVE_METRICS
void flb_test_statsd_metrics_gauge()
{
char *expected_strs[] = {"\"name\":\"gorets\"", "\"desc\":\"-\"", "\"type\":1"};
struct str_list expected = {
.size = sizeof(expected_strs)/sizeof(char*),
.lists = &expected_strs[0],
};

char *buf = "gorets:1|g";
int ret;

ret = test_normal(buf, &expected, FLB_TRUE);
if (!TEST_CHECK(ret == 0)) {
TEST_MSG("test failed");
exit(EXIT_FAILURE);
}

}

void flb_test_statsd_metrics_counter()
{
char *expected_strs[] = {"\"name\":\"gorets\"", "\"desc\":\"-\"", "\"type\":0"};
struct str_list expected = {
.size = sizeof(expected_strs)/sizeof(char*),
.lists = &expected_strs[0],
};

char *buf = "gorets:1|c";
int ret;

ret = test_normal(buf, &expected, FLB_TRUE);
if (!TEST_CHECK(ret == 0)) {
TEST_MSG("test failed");
exit(EXIT_FAILURE);
}

}

void flb_test_statsd_metrics_untyped()
{
char *expected_strs[] = {"\"name\":\"gorets\"", "\"desc\":\"-\"", "\"type\":4"};
struct str_list expected = {
.size = sizeof(expected_strs)/sizeof(char*),
.lists = &expected_strs[0],
};

char *buf = "gorets:1|s";
int ret;

ret = test_normal(buf, &expected, FLB_TRUE);
if (!TEST_CHECK(ret == 0)) {
TEST_MSG("test failed");
exit(EXIT_FAILURE);
}

}
#endif

TEST_LIST = {
{"count", flb_test_statsd_count},
{"sample", flb_test_statsd_sample},
{"gauge", flb_test_statsd_gauge},
{"set", flb_test_statsd_set},
#ifdef FLB_HAVE_METRICS
{"metrics_gauge", flb_test_statsd_metrics_gauge},
{"metrics_counter", flb_test_statsd_metrics_counter},
{"metrics_untyped", flb_test_statsd_metrics_untyped},
#endif
{NULL, NULL}
};

0 comments on commit f4ce7dd

Please sign in to comment.