Skip to content

Commit cf6dd78

Browse files
committed
tests: runtime: add health endpoint tests for in_http
Add tests for the new health endpoint functionality: - health_endpoint: tests enabled health check returns 200 with JSON - health_endpoint_disabled: tests disabled health check returns 400 The tests verify the health_check configuration directive works correctly and that JSON response contains expected fields.
1 parent 491b4e4 commit cf6dd78

File tree

2 files changed

+121
-1
lines changed

2 files changed

+121
-1
lines changed

plugins/in_http/http_prot.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -778,7 +778,7 @@ static int http_prot_uncompress(struct flb_http *ctx,
778778
return 0;
779779
}
780780

781-
static int process_payload(struct flb_http *ctx, struct http_conn *conn,
781+
static int process_payload(struct flb_http *ctx, struct http_conn *conn,
782782
flb_sds_t tag,
783783
struct mk_http_session *session,
784784
struct mk_http_request *request)

tests/runtime/in_http.c

Lines changed: 120 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -671,6 +671,124 @@ void flb_test_http_tag_key_with_array_input()
671671
test_http_tag_key("[{\"tag\":\"new_tag\",\"test\":\"msg\"}]");
672672
}
673673

674+
void flb_test_http_health_endpoint()
675+
{
676+
struct flb_lib_out_cb cb_data;
677+
struct test_ctx *ctx;
678+
struct flb_http_client *c;
679+
int ret;
680+
size_t b_sent;
681+
682+
clear_output_num();
683+
684+
cb_data.cb = cb_check_result_json;
685+
cb_data.data = "dummy";
686+
687+
ctx = test_ctx_create(&cb_data);
688+
if (!TEST_CHECK(ctx != NULL)) {
689+
TEST_MSG("test_ctx_create failed");
690+
exit(EXIT_FAILURE);
691+
}
692+
693+
ret = flb_input_set(ctx->flb, ctx->i_ffd,
694+
"health_check", "true",
695+
NULL);
696+
TEST_CHECK(ret == 0);
697+
698+
ret = flb_output_set(ctx->flb, ctx->o_ffd,
699+
"match", "*",
700+
"format", "json",
701+
NULL);
702+
TEST_CHECK(ret == 0);
703+
704+
/* Start the engine */
705+
ret = flb_start(ctx->flb);
706+
if (!TEST_CHECK(ret == 0)) {
707+
TEST_MSG("flb_start failed with ret=%d", ret);
708+
return;
709+
}
710+
711+
ctx->httpc = http_client_ctx_create();
712+
if (!TEST_CHECK(ctx->httpc != NULL)) {
713+
TEST_MSG("http_client_ctx_create failed");
714+
return;
715+
}
716+
717+
/* Test GET /health endpoint */
718+
c = flb_http_client(ctx->httpc->u_conn, FLB_HTTP_GET, "/health", NULL, 0,
719+
"127.0.0.1", 9880, NULL, 0);
720+
if (!TEST_CHECK(c != NULL)) {
721+
TEST_MSG("flb_http_client failed");
722+
return;
723+
}
724+
725+
ret = flb_http_do(c, &b_sent);
726+
TEST_CHECK(ret == 0);
727+
TEST_CHECK(c->resp.status == 200);
728+
729+
/* Check response contains expected JSON fields */
730+
TEST_CHECK(c->resp.payload != NULL && c->resp.payload_size > 0);
731+
TEST_CHECK(strstr(c->resp.payload, "\"status\": \"ok\"") != NULL);
732+
TEST_CHECK(strstr(c->resp.payload, "\"version\":") != NULL);
733+
TEST_CHECK(strstr(c->resp.payload, "\"git_hash\":") != NULL);
734+
TEST_CHECK(strstr(c->resp.payload, "\"timestamp\":") != NULL);
735+
736+
flb_http_client_destroy(c);
737+
flb_upstream_conn_release(ctx->httpc->u_conn);
738+
test_ctx_destroy(ctx);
739+
}
740+
741+
void flb_test_http_health_endpoint_disabled()
742+
{
743+
struct flb_lib_out_cb cb_data;
744+
struct test_ctx *ctx;
745+
struct flb_http_client *c;
746+
int ret;
747+
size_t b_sent;
748+
749+
clear_output_num();
750+
751+
cb_data.cb = cb_check_result_json;
752+
cb_data.data = "dummy";
753+
754+
ctx = test_ctx_create(&cb_data);
755+
if (!TEST_CHECK(ctx != NULL)) {
756+
TEST_MSG("test_ctx_create failed");
757+
exit(EXIT_FAILURE);
758+
}
759+
760+
ret = flb_input_set(ctx->flb, ctx->i_ffd,
761+
"health_check", "false",
762+
NULL);
763+
TEST_CHECK(ret == 0);
764+
765+
ret = flb_output_set(ctx->flb, ctx->o_ffd,
766+
"match", "*",
767+
"format", "json",
768+
NULL);
769+
TEST_CHECK(ret == 0);
770+
771+
/* Start the engine */
772+
ret = flb_start(ctx->flb);
773+
TEST_CHECK(ret == 0);
774+
775+
ctx->httpc = http_client_ctx_create();
776+
TEST_CHECK(ctx->httpc != NULL);
777+
778+
/* Test GET /health endpoint - should return 400 for invalid method */
779+
c = flb_http_client(ctx->httpc->u_conn, FLB_HTTP_GET, "/health", NULL, 0,
780+
"127.0.0.1", 9880, NULL, 0);
781+
TEST_CHECK(c != NULL);
782+
783+
ret = flb_http_do(c, &b_sent);
784+
TEST_CHECK(ret == 0);
785+
TEST_CHECK(c->resp.status == 400);
786+
787+
flb_http_client_destroy(c);
788+
flb_upstream_conn_release(ctx->httpc->u_conn);
789+
test_ctx_destroy(ctx);
790+
}
791+
674792
TEST_LIST = {
675793
{"http", flb_test_http},
676794
{"successful_response_code_200", flb_test_http_successful_response_code_200},
@@ -679,5 +797,7 @@ TEST_LIST = {
679797
{"failure_response_code_400_bad_disk_write", flb_test_http_failure_400_bad_disk_write},
680798
{"tag_key_with_map_input", flb_test_http_tag_key_with_map_input},
681799
{"tag_key_with_array_input", flb_test_http_tag_key_with_array_input},
800+
{"health_endpoint", flb_test_http_health_endpoint},
801+
{"health_endpoint_disabled", flb_test_http_health_endpoint_disabled},
682802
{NULL, NULL}
683803
};

0 commit comments

Comments
 (0)