Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 19 additions & 5 deletions plugins/in_udp/udp_conn.c
Original file line number Diff line number Diff line change
Expand Up @@ -264,6 +264,7 @@ static ssize_t parse_payload_none(struct udp_conn *conn)
char *buf;
char *s;
char *separator;
char *source_address;
struct flb_in_udp_config *ctx;

ctx = conn->ctx;
Expand All @@ -282,17 +283,30 @@ static ssize_t parse_payload_none(struct udp_conn *conn)
break;
}
else if (len > 0) {
source_address = NULL;
ret = flb_log_event_encoder_begin_record(ctx->log_encoder);

if (ret == FLB_EVENT_ENCODER_SUCCESS) {
ret = flb_log_event_encoder_set_current_timestamp(ctx->log_encoder);
}

if (ctx->source_address_key != NULL) {
source_address = flb_connection_get_remote_address(conn->connection);
}
if (ret == FLB_EVENT_ENCODER_SUCCESS) {
ret = flb_log_event_encoder_append_body_values(
ctx->log_encoder,
FLB_LOG_EVENT_CSTRING_VALUE("log"),
FLB_LOG_EVENT_STRING_VALUE(buf, len));
if (source_address != NULL) {
ret = flb_log_event_encoder_append_body_values(
ctx->log_encoder,
FLB_LOG_EVENT_CSTRING_VALUE("log"),
FLB_LOG_EVENT_STRING_VALUE(buf, len),
FLB_LOG_EVENT_CSTRING_VALUE(ctx->source_address_key),
FLB_LOG_EVENT_CSTRING_VALUE(source_address));
}
else {
ret = flb_log_event_encoder_append_body_values(
ctx->log_encoder,
FLB_LOG_EVENT_CSTRING_VALUE("log"),
FLB_LOG_EVENT_STRING_VALUE(buf, len));
}
}

if (ret == FLB_EVENT_ENCODER_SUCCESS) {
Expand Down
67 changes: 67 additions & 0 deletions tests/runtime/in_udp.c
Original file line number Diff line number Diff line change
Expand Up @@ -431,10 +431,77 @@ void flb_test_format_none_separator()
test_ctx_destroy(ctx);
}

void flb_test_format_none_with_source_address()
{
struct flb_lib_out_cb cb_data;
struct test_ctx *ctx;
struct sockaddr_in addr;
flb_sockfd_t fd;
int ret;
int num;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Using meaningful variable names is preferred to these generic names because even though this is a simple test case neglecting to do so leads to using these types of names in more complex (and meaningful) pieces of code which means someone who reads the code later on might have a harder time understanding it or even miss the point and end up introducing a bug.

ssize_t w_size;

char *buf = "message\n";
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please declare this variable as a const char *

size_t size = strlen(buf);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please separate the definition of this variable from the assignment of it.


clear_output_num();

cb_data.cb = cb_check_result_json;
cb_data.data = "\"log\":\"message\",\"source_host\":\"udp://";

ctx = test_ctx_create(&cb_data);
if (!TEST_CHECK(ctx != NULL)) {
TEST_MSG("test_ctx_create failed");
exit(EXIT_FAILURE);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I could be wrong but I think failed test cases shouldn't exit but instead just return

}

ret = flb_output_set(ctx->flb, ctx->o_ffd,
"match", "*",
"format", "json",
NULL);
TEST_CHECK(ret == 0);

ret = flb_input_set(ctx->flb, ctx->i_ffd,
"format", "none",
"source_address_key", "source_host",
NULL);
TEST_CHECK(ret == 0);

/* Start the engine */
ret = flb_start(ctx->flb);
TEST_CHECK(ret == 0);

/* use default host/port */
fd = init_udp(NULL, -1, &addr);
if (!TEST_CHECK(fd >= 0)) {
exit(EXIT_FAILURE);
}

w_size = sendto(fd, buf, size, 0, (const struct sockaddr *)&addr, sizeof(addr));
if (!TEST_CHECK(w_size == size)) {
TEST_MSG("failed to send, errno=%d", errno);
flb_socket_close(fd);
test_ctx_destroy(ctx);
exit(EXIT_FAILURE);
}

/* waiting to flush */
flb_time_msleep(1500);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As a general rule, basing non timing related tests on timing is problematic, particularly when CI runs on nodes that are somewhat loaded as it tends to create false positives.


num = get_output_num();
if (!TEST_CHECK(num > 0)) {
TEST_MSG("no outputs");
}

flb_socket_close(fd);
test_ctx_destroy(ctx);
}

TEST_LIST = {
{"udp", flb_test_udp},
{"udp_with_source_address", flb_test_udp_with_source_address},
{"format_none", flb_test_format_none},
{"format_none_separator", flb_test_format_none_separator},
{"format_none_with_source_address", flb_test_format_none_with_source_address},
{NULL, NULL}
};
Loading