Skip to content

Commit

Permalink
filter: tests: Add testcase for filtering label's key-value
Browse files Browse the repository at this point in the history
Signed-off-by: Hiroshi Hatake <[email protected]>
  • Loading branch information
cosmo0920 committed May 9, 2024
1 parent f6770ef commit b0b4402
Showing 1 changed file with 147 additions and 1 deletion.
148 changes: 147 additions & 1 deletion tests/filter.c
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,9 @@ struct cmt *generate_filter_test_data()
ts = cfl_time_now();
cmt_untyped_set(u, ts, 1.3, 2, (char *[]) {"localhost", "eth0"});

ts = cfl_time_now();
cmt_untyped_set(u, ts, 1.8, 2, (char *[]) {"dev", "enp1s0"});

c = cmt_counter_create(cmt, "cmetrics", "test", "cat_counter", "second counter",
2, (char *[]) {"label1", "label2"});
TEST_CHECK(c != NULL);
Expand Down Expand Up @@ -129,6 +132,12 @@ struct cmt *generate_filter_test_data()
TEST_CHECK(h != NULL);

ts = cfl_time_now();

for (i = 0; i < sizeof(hist_observe_values)/(sizeof(double)); i++) {
val = hist_observe_values[i];
cmt_histogram_observe(h, ts, val, 1, (char *[]) {"another_hist"});
}

for (i = 0; i < sizeof(hist_observe_values)/(sizeof(double)); i++) {
val = hist_observe_values[i];
cmt_histogram_observe(h, ts, val, 1, (char *[]) {"my_label"});
Expand All @@ -151,7 +160,7 @@ struct cmt *generate_filter_test_data()
r[4] = 5;
r[5] = 6;

/* Create a gauge metric type */
/* Create a summary metric type */
s = cmt_summary_create(cmt,
"spring", "kafka_listener", "seconds", "Kafka Listener Timer",
6, q,
Expand Down Expand Up @@ -308,7 +317,144 @@ void test_filter()
cmt_destroy(cmt6);
}

void test_filter_with_label_key_value_pairs()
{
int ret;
cfl_sds_t text;
struct cmt *cmt;
struct cmt *cmt2;
struct cmt *cmt3;
struct cmt *cmt4;
struct cmt *cmt5;
struct cmt *cmt6;
char *label_key;
char *label_value;
char *tmp = NULL;

cmt = generate_filter_test_data();

text = cmt_encode_text_create(cmt);
printf("[Not filtered] ====>\n%s\n", text);

cmt_encode_text_destroy(text);

cmt2 = cmt_create();
TEST_CHECK(cmt2 != NULL);

label_key = "label3";
label_value = "tyu";

/* filter with label key-value */
ret = cmt_filter_with_label_pair(cmt2, cmt, label_key, label_value);
TEST_CHECK(ret == 0);
/* check output (fqname) */
text = cmt_encode_text_create(cmt2);
printf("[label matched with label key-value pair: \"%s\", \"%s\" ] ====>\n%s\n",
label_key, label_value, text);

tmp = strstr(text, "label3=\"tyu\"");
TEST_CHECK(tmp == NULL);
tmp = strstr(text, "label3=\"yyy\"");
TEST_CHECK(tmp != NULL);

cmt_encode_text_destroy(text);

cmt3 = cmt_create();
TEST_CHECK(cmt3 != NULL);

label_key = "label1";
label_value = "aaa";

/* filter with label key-value */
ret = cmt_filter_with_label_pair(cmt3, cmt, label_key, label_value);
TEST_CHECK(ret == 0);
/* check output (fqname) */
text = cmt_encode_text_create(cmt3);
printf("[label matched with label key-value pair: \"%s\", \"%s\" ] ====>\n%s\n",
label_key, label_value, text);

tmp = strstr(text, "label1=\"aaa\"");
TEST_CHECK(tmp == NULL);
tmp = strstr(text, "label1=\"ccc\"");
TEST_CHECK(tmp != NULL);

cmt_encode_text_destroy(text);

cmt4 = cmt_create();
TEST_CHECK(cmt4 != NULL);

label_key = "net";
label_value = "enp1s0";

/* filter with label key-value */
ret = cmt_filter_with_label_pair(cmt4, cmt, label_key, label_value);
TEST_CHECK(ret == 0);
/* check output (fqname) */
text = cmt_encode_text_create(cmt4);
printf("[label matched with label key-value pair: \"%s\", \"%s\" ] ====>\n%s\n",
label_key, label_value, text);

tmp = strstr(text, "net=\"enp1s0\"");
TEST_CHECK(tmp == NULL);
tmp = strstr(text, "net=\"eth0\"");
TEST_CHECK(tmp != NULL);

cmt_encode_text_destroy(text);

cmt5 = cmt_create();
TEST_CHECK(cmt5 != NULL);

label_key = "exception";
label_value = "none";

/* filter with label key-value */
ret = cmt_filter_with_label_pair(cmt5, cmt, label_key, label_value);
TEST_CHECK(ret == 0);
/* check output (fqname) */
text = cmt_encode_text_create(cmt5);
printf("[label matched with label key-value pair: \"%s\", \"%s\" ] ====>\n%s\n",
label_key, label_value, text);

tmp = strstr(text, "exception=\"none\"");
TEST_CHECK(tmp == NULL);
tmp = strstr(text, "net=\"eth0\"");
TEST_CHECK(tmp != NULL);

cmt_encode_text_destroy(text);

cmt6 = cmt_create();
TEST_CHECK(cmt6 != NULL);

label_key = "my_label";
label_value = "another_hist";

/* filter with label key-value */
ret = cmt_filter_with_label_pair(cmt6, cmt, label_key, label_value);
TEST_CHECK(ret == 0);
/* check output (fqname) */
text = cmt_encode_text_create(cmt6);
printf("[label matched with label key-value pair: \"%s\", \"%s\" ] ====>\n%s\n",
label_key, label_value, text);

tmp = strstr(text, "my_label=\"another_hist\"");
TEST_CHECK(tmp == NULL);
tmp = strstr(text, "my_label=\"my_label\"");
TEST_CHECK(tmp != NULL);

cmt_encode_text_destroy(text);

/* destroy contexts */
cmt_destroy(cmt);
cmt_destroy(cmt2);
cmt_destroy(cmt3);
cmt_destroy(cmt4);
cmt_destroy(cmt5);
cmt_destroy(cmt6);
}


TEST_LIST = {
{"filter", test_filter},
{"filter_with_label_pair", test_filter_with_label_key_value_pairs},
{ 0 }
};

0 comments on commit b0b4402

Please sign in to comment.