Skip to content

Commit ad2b78f

Browse files
committed
tests: internal: add cfl record accessor root-key coverage
Signed-off-by: Eduardo Silva <eduardo@calyptia.com>
1 parent be36817 commit ad2b78f

1 file changed

Lines changed: 108 additions & 1 deletion

File tree

tests/internal/cfl_record_accessor.c

Lines changed: 108 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
#include <fluent-bit/flb_info.h>
2222
#include <fluent-bit/flb_mem.h>
2323
#include <fluent-bit/flb_error.h>
24+
#include <fluent-bit/flb_regex.h>
2425
#include <fluent-bit/flb_sds.h>
2526
#include <fluent-bit/flb_pack.h>
2627
#include <fluent-bit/flb_sds.h>
@@ -1700,6 +1701,110 @@ void cb_mixed_array_map_access()
17001701
cfl_variant_destroy(vobj);
17011702
}
17021703

1704+
void cb_translate_container_map()
1705+
{
1706+
char *fmt;
1707+
char *fmt_out;
1708+
flb_sds_t str;
1709+
struct flb_cfl_record_accessor *cra;
1710+
struct cfl_kvlist *kvlist = NULL;
1711+
struct cfl_kvlist *nested = NULL;
1712+
struct cfl_variant *vobj = NULL;
1713+
1714+
kvlist = cfl_kvlist_create();
1715+
nested = cfl_kvlist_create();
1716+
1717+
TEST_CHECK(kvlist != NULL);
1718+
TEST_CHECK(nested != NULL);
1719+
if (!kvlist || !nested) {
1720+
exit(EXIT_FAILURE);
1721+
}
1722+
1723+
cfl_kvlist_insert_string(nested, "first", "alpha");
1724+
cfl_kvlist_insert_string(nested, "second", "beta");
1725+
cfl_kvlist_insert_kvlist(kvlist, "obj", nested);
1726+
1727+
vobj = cfl_variant_create_from_kvlist(kvlist);
1728+
TEST_CHECK(vobj != NULL);
1729+
if (!vobj) {
1730+
exit(EXIT_FAILURE);
1731+
}
1732+
1733+
fmt = flb_sds_create("$obj");
1734+
fmt_out = "{\"first\":\"alpha\",\"second\":\"beta\"}";
1735+
1736+
cra = flb_cfl_ra_create(fmt, FLB_FALSE);
1737+
TEST_CHECK(cra != NULL);
1738+
if (!cra) {
1739+
exit(EXIT_FAILURE);
1740+
}
1741+
1742+
str = flb_cfl_ra_translate(cra, NULL, -1, *vobj, NULL);
1743+
TEST_CHECK(str != NULL);
1744+
if (!str) {
1745+
exit(EXIT_FAILURE);
1746+
}
1747+
1748+
TEST_CHECK(flb_sds_len(str) == strlen(fmt_out));
1749+
TEST_CHECK(memcmp(str, fmt_out, strlen(fmt_out)) == 0);
1750+
1751+
flb_sds_destroy(str);
1752+
flb_sds_destroy(fmt);
1753+
flb_cfl_ra_destroy(cra);
1754+
cfl_variant_destroy(vobj);
1755+
}
1756+
1757+
void cb_strcmp_and_regex_root_key()
1758+
{
1759+
int ret;
1760+
char *fmt;
1761+
struct flb_regex *regex;
1762+
struct flb_cfl_record_accessor *cra;
1763+
struct cfl_kvlist *kvlist = NULL;
1764+
struct cfl_variant *vobj = NULL;
1765+
1766+
kvlist = cfl_kvlist_create();
1767+
TEST_CHECK(kvlist != NULL);
1768+
if (!kvlist) {
1769+
exit(EXIT_FAILURE);
1770+
}
1771+
1772+
cfl_kvlist_insert_string(kvlist, "message", "hello world");
1773+
1774+
vobj = cfl_variant_create_from_kvlist(kvlist);
1775+
TEST_CHECK(vobj != NULL);
1776+
if (!vobj) {
1777+
exit(EXIT_FAILURE);
1778+
}
1779+
1780+
fmt = flb_sds_create("$message");
1781+
cra = flb_cfl_ra_create(fmt, FLB_FALSE);
1782+
TEST_CHECK(cra != NULL);
1783+
if (!cra) {
1784+
exit(EXIT_FAILURE);
1785+
}
1786+
1787+
ret = flb_cfl_ra_strcmp(cra, *vobj, "hello world", 11);
1788+
TEST_CHECK(ret == 0);
1789+
1790+
ret = flb_cfl_ra_strcmp(cra, *vobj, "goodbye", 7);
1791+
TEST_CHECK(ret != 0);
1792+
1793+
regex = flb_regex_create("hello");
1794+
TEST_CHECK(regex != NULL);
1795+
if (!regex) {
1796+
exit(EXIT_FAILURE);
1797+
}
1798+
1799+
ret = flb_cfl_ra_regex_match(cra, *vobj, regex, NULL);
1800+
TEST_CHECK(ret > 0);
1801+
1802+
flb_regex_destroy(regex);
1803+
flb_sds_destroy(fmt);
1804+
flb_cfl_ra_destroy(cra);
1805+
cfl_variant_destroy(vobj);
1806+
}
1807+
17031808
TEST_LIST = {
17041809
{ "keys" , cb_keys},
17051810
{ "dash_key" , cb_dash_key},
@@ -1721,5 +1826,7 @@ TEST_LIST = {
17211826
{ "direct_array_access" , cb_direct_array_access},
17221827
{ "nested_array_access" , cb_nested_array_access},
17231828
{ "mixed_array_map_access" , cb_mixed_array_map_access},
1829+
{ "translate_container_map", cb_translate_container_map},
1830+
{ "strcmp_and_regex_root_key", cb_strcmp_and_regex_root_key},
17241831
{ NULL }
1725-
};
1832+
};

0 commit comments

Comments
 (0)