Skip to content

Commit 0923ad6

Browse files
committed
tests: internal: opentelemetry: adjust paths
Signed-off-by: Eduardo Silva <[email protected]>
1 parent 63f90ec commit 0923ad6

File tree

3 files changed

+86
-9
lines changed

3 files changed

+86
-9
lines changed

tests/internal/CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -171,7 +171,7 @@ set(UNIT_TESTS_DATA
171171
data/parser/regex.conf
172172
data/input_chunk/log/a_thousand_plus_one_bytes.log
173173
data/input_chunk/log/test_buffer_valid.log
174-
data/opentelemetry/test_cases.json
174+
data/opentelemetry/logs.json
175175
)
176176

177177
set(FLB_TESTS_DATA_PATH ${CMAKE_CURRENT_SOURCE_DIR}/)

tests/internal/README.md

Lines changed: 45 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,12 @@ The following directory contains unit tests to validate specific functions of Fl
44

55
## OpenTelemetry Test Cases
66

7-
OpenTelemetry JSON tests are described in a single file located at
8-
`data/opentelemetry/test_cases.json`. Each entry is keyed by the test name and
9-
contains the following fields:
7+
OpenTelemetry JSON tests are described in two separate files:
8+
9+
### Logs Test Cases (`data/opentelemetry/logs.json`)
10+
11+
Logs test cases validate the `flb_opentelemetry_logs_json_to_msgpack()` function.
12+
Each entry is keyed by the test name and contains the following fields:
1013

1114
```
1215
{
@@ -28,6 +31,42 @@ contains the following fields:
2831
}
2932
```
3033

31-
When `expected_error` is present the unit test checks that
32-
`flb_opentelemetry_logs_json_to_msgpack()` fails with the given error code and
33-
message.
34+
### Traces Test Cases (`data/opentelemetry/traces.json`)
35+
36+
Traces test cases validate the `flb_opentelemetry_json_traces_to_ctrace()` function.
37+
Each entry is keyed by the test name and contains the following fields:
38+
39+
```
40+
{
41+
"test_name": {
42+
"input": { ... }, # OTLP/JSON traces payload
43+
"expected": { # successful result (optional)
44+
# For successful cases, the test validates that a valid ctrace object is created
45+
}
46+
},
47+
"error_case": {
48+
"input": { ... },
49+
"expected_error": {
50+
"code": "FLB_OTEL_TRACES_ERR_*",
51+
"message": "<error text>"
52+
}
53+
}
54+
}
55+
```
56+
57+
### Error Handling
58+
59+
When `expected_error` is present, the unit tests check that the respective functions fail with the given error code and message:
60+
61+
- **Logs**: `flb_opentelemetry_logs_json_to_msgpack()` should fail with `FLB_OTEL_LOGS_ERR_*` codes
62+
- **Traces**: `flb_opentelemetry_json_traces_to_ctrace()` should fail with `FLB_OTEL_TRACES_ERR_*` codes
63+
64+
### Test Coverage
65+
66+
Both test files include comprehensive coverage for:
67+
68+
- **Valid payloads**: Successful processing of well-formed OTLP JSON
69+
- **Invalid structure**: Missing required fields, wrong data types
70+
- **Hex decoding errors**: Invalid trace_id/span_id formats, zero-length strings, odd-length hex
71+
- **Field validation**: Required vs optional fields according to OpenTelemetry specification
72+
- **Edge cases**: Empty payloads, malformed JSON, boundary conditions

tests/internal/opentelemetry.c

Lines changed: 40 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -408,6 +408,44 @@ void test_hex_to_id()
408408
TEST_CHECK(memcmp(out, expect, sizeof(expect)) == 0);
409409
}
410410

411+
void test_hex_to_id_error_cases()
412+
{
413+
unsigned char out[16];
414+
int ret;
415+
416+
/* Test zero length string */
417+
ret = flb_otel_utils_hex_to_id("", 0, out, 16);
418+
TEST_CHECK(ret == 0); /* Zero length should succeed (empty output) */
419+
420+
/* Test odd length string */
421+
ret = flb_otel_utils_hex_to_id("123", 3, out, 16);
422+
TEST_CHECK(ret == -1); /* Odd length should fail */
423+
424+
/* Test invalid hex character */
425+
ret = flb_otel_utils_hex_to_id("0000000000000000000000000000000G", 32, out, 16);
426+
TEST_CHECK(ret == -1); /* Invalid hex character should fail */
427+
428+
/* Test mixed valid/invalid hex */
429+
ret = flb_otel_utils_hex_to_id("0000000000000000000000000000000Z", 32, out, 16);
430+
TEST_CHECK(ret == -1); /* Invalid hex character should fail */
431+
432+
/* Test valid hex with wrong output size */
433+
ret = flb_otel_utils_hex_to_id("00000000000000000000000000000001", 32, out, 8);
434+
TEST_CHECK(ret == 0); /* Should succeed even with larger output buffer */
435+
436+
/* Test valid hex with correct size */
437+
ret = flb_otel_utils_hex_to_id("0000000000000001", 16, out, 8);
438+
TEST_CHECK(ret == 0); /* Should succeed */
439+
440+
/* Test valid hex with uppercase */
441+
ret = flb_otel_utils_hex_to_id("0000000000000000000000000000000A", 32, out, 16);
442+
TEST_CHECK(ret == 0); /* Should succeed with uppercase hex */
443+
444+
/* Test valid hex with lowercase */
445+
ret = flb_otel_utils_hex_to_id("0000000000000000000000000000000a", 32, out, 16);
446+
TEST_CHECK(ret == 0); /* Should succeed with lowercase hex */
447+
}
448+
411449
void test_convert_string_number_to_u64()
412450
{
413451
uint64_t val;
@@ -499,7 +537,7 @@ void test_json_payload_get_wrapped_value()
499537
msgpack_unpacked_destroy(&up);
500538
}
501539

502-
#define OTEL_TEST_CASES_PATH FLB_TESTS_DATA_PATH "/data/opentelemetry/test_cases.json"
540+
#define OTEL_TEST_CASES_PATH FLB_TESTS_DATA_PATH "/data/opentelemetry/logs.json"
503541
#define OTEL_TRACES_TEST_CASES_PATH FLB_TESTS_DATA_PATH "/data/opentelemetry/traces.json"
504542

505543
void test_opentelemetry_cases()
@@ -904,7 +942,6 @@ void test_trace_span_binary_sizes()
904942
struct flb_record_accessor *ra_span_id;
905943
struct flb_ra_value *val_trace_id;
906944
struct flb_ra_value *val_span_id;
907-
size_t len;
908945

909946
/* Test input with trace_id and span_id */
910947
input_json = "{\"resourceLogs\":[{\"scopeLogs\":[{\"logRecords\":[{\"timeUnixNano\":\"1640995200000000000\",\"traceId\":\"5B8EFFF798038103D269B633813FC60C\",\"spanId\":\"EEE19B7EC3C1B174\",\"body\":{\"stringValue\":\"test\"}}]}]}]}";
@@ -979,6 +1016,7 @@ void test_trace_span_binary_sizes()
9791016
/* Test list */
9801017
TEST_LIST = {
9811018
{ "hex_to_id", test_hex_to_id },
1019+
{ "hex_to_id_error_cases", test_hex_to_id_error_cases },
9821020
{ "convert_string_number_to_u64", test_convert_string_number_to_u64 },
9831021
{ "find_map_entry_by_key", test_find_map_entry_by_key },
9841022
{ "json_payload_get_wrapped_value", test_json_payload_get_wrapped_value },

0 commit comments

Comments
 (0)