Skip to content

Commit 033fb9f

Browse files
Jorge Niedbalskiedsiper
Jorge Niedbalski
authored andcommitted
tests: internal: conditionals: add unit tests for GTE and LTE operators
Add comprehensive test coverage for the new GTE and LTE operators including: - Numeric boundary handling - Full test coverage for both operators - Edge cases and comparisons Signed-off-by: Jorge Niedbalski <jorge.niedbalski@chronosphere.io>
1 parent 6efc4d8 commit 033fb9f

File tree

1 file changed

+274
-0
lines changed

1 file changed

+274
-0
lines changed

tests/internal/conditionals.c

+274
Original file line numberDiff line numberDiff line change
@@ -1524,6 +1524,277 @@ void test_condition_not_regex_border_cases()
15241524
destroy_test_record(record_data);
15251525
}
15261526

1527+
void test_condition_gte_lte()
1528+
{
1529+
struct test_record *record_data;
1530+
struct flb_condition *cond;
1531+
int result;
1532+
double val;
1533+
1534+
/* Test GTE with equal CPU value */
1535+
record_data = create_test_record_numeric("cpu_usage", 80.0);
1536+
TEST_CHECK(record_data != NULL);
1537+
1538+
cond = flb_condition_create(FLB_COND_OP_AND);
1539+
TEST_CHECK(cond != NULL);
1540+
1541+
val = 80.0;
1542+
TEST_CHECK(flb_condition_add_rule(cond, "$cpu_usage", FLB_RULE_OP_GTE,
1543+
&val, 0, RECORD_CONTEXT_BODY) == FLB_TRUE);
1544+
1545+
result = flb_condition_evaluate(cond, &record_data->chunk);
1546+
TEST_CHECK(result == FLB_TRUE);
1547+
1548+
flb_condition_destroy(cond);
1549+
destroy_test_record(record_data);
1550+
1551+
/* Test GTE with higher CPU usage */
1552+
record_data = create_test_record_numeric("cpu_usage", 95.0);
1553+
TEST_CHECK(record_data != NULL);
1554+
1555+
cond = flb_condition_create(FLB_COND_OP_AND);
1556+
TEST_CHECK(cond != NULL);
1557+
1558+
val = 80.0;
1559+
TEST_CHECK(flb_condition_add_rule(cond, "$cpu_usage", FLB_RULE_OP_GTE,
1560+
&val, 0, RECORD_CONTEXT_BODY) == FLB_TRUE);
1561+
1562+
result = flb_condition_evaluate(cond, &record_data->chunk);
1563+
TEST_CHECK(result == FLB_TRUE);
1564+
1565+
flb_condition_destroy(cond);
1566+
destroy_test_record(record_data);
1567+
1568+
/* Test GTE with lower CPU usage */
1569+
record_data = create_test_record_numeric("cpu_usage", 75.0);
1570+
TEST_CHECK(record_data != NULL);
1571+
1572+
cond = flb_condition_create(FLB_COND_OP_AND);
1573+
TEST_CHECK(cond != NULL);
1574+
1575+
val = 80.0;
1576+
TEST_CHECK(flb_condition_add_rule(cond, "$cpu_usage", FLB_RULE_OP_GTE,
1577+
&val, 0, RECORD_CONTEXT_BODY) == FLB_TRUE);
1578+
1579+
result = flb_condition_evaluate(cond, &record_data->chunk);
1580+
TEST_CHECK(result == FLB_FALSE);
1581+
1582+
flb_condition_destroy(cond);
1583+
destroy_test_record(record_data);
1584+
1585+
/* Test LTE with equal memory value */
1586+
record_data = create_test_record_numeric("memory_usage", 2048.0);
1587+
TEST_CHECK(record_data != NULL);
1588+
1589+
cond = flb_condition_create(FLB_COND_OP_AND);
1590+
TEST_CHECK(cond != NULL);
1591+
1592+
val = 2048.0;
1593+
TEST_CHECK(flb_condition_add_rule(cond, "$memory_usage", FLB_RULE_OP_LTE,
1594+
&val, 0, RECORD_CONTEXT_BODY) == FLB_TRUE);
1595+
1596+
result = flb_condition_evaluate(cond, &record_data->chunk);
1597+
TEST_CHECK(result == FLB_TRUE);
1598+
1599+
flb_condition_destroy(cond);
1600+
destroy_test_record(record_data);
1601+
1602+
/* Test LTE with lower memory usage */
1603+
record_data = create_test_record_numeric("memory_usage", 1024.0);
1604+
TEST_CHECK(record_data != NULL);
1605+
1606+
cond = flb_condition_create(FLB_COND_OP_AND);
1607+
TEST_CHECK(cond != NULL);
1608+
1609+
val = 2048.0;
1610+
TEST_CHECK(flb_condition_add_rule(cond, "$memory_usage", FLB_RULE_OP_LTE,
1611+
&val, 0, RECORD_CONTEXT_BODY) == FLB_TRUE);
1612+
1613+
result = flb_condition_evaluate(cond, &record_data->chunk);
1614+
TEST_CHECK(result == FLB_TRUE);
1615+
1616+
flb_condition_destroy(cond);
1617+
destroy_test_record(record_data);
1618+
1619+
/* Test LTE with higher memory usage */
1620+
record_data = create_test_record_numeric("memory_usage", 3072.0);
1621+
TEST_CHECK(record_data != NULL);
1622+
1623+
cond = flb_condition_create(FLB_COND_OP_AND);
1624+
TEST_CHECK(cond != NULL);
1625+
1626+
val = 2048.0;
1627+
TEST_CHECK(flb_condition_add_rule(cond, "$memory_usage", FLB_RULE_OP_LTE,
1628+
&val, 0, RECORD_CONTEXT_BODY) == FLB_TRUE);
1629+
1630+
result = flb_condition_evaluate(cond, &record_data->chunk);
1631+
TEST_CHECK(result == FLB_FALSE);
1632+
1633+
flb_condition_destroy(cond);
1634+
destroy_test_record(record_data);
1635+
}
1636+
1637+
void test_condition_gte_lte_multiple()
1638+
{
1639+
struct test_record *record_data;
1640+
struct flb_condition *cond;
1641+
int result;
1642+
double val1, val2;
1643+
1644+
/* Test multiple GTE/LTE with AND (pod ready condition) */
1645+
record_data = create_test_record_numeric("ready_replicas", 3.0);
1646+
TEST_CHECK(record_data != NULL);
1647+
1648+
cond = flb_condition_create(FLB_COND_OP_AND);
1649+
TEST_CHECK(cond != NULL);
1650+
1651+
val1 = 2.0; /* minimum replicas */
1652+
val2 = 5.0; /* maximum replicas */
1653+
TEST_CHECK(flb_condition_add_rule(cond, "$ready_replicas", FLB_RULE_OP_GTE,
1654+
&val1, 0, RECORD_CONTEXT_BODY) == FLB_TRUE);
1655+
TEST_CHECK(flb_condition_add_rule(cond, "$ready_replicas", FLB_RULE_OP_LTE,
1656+
&val2, 0, RECORD_CONTEXT_BODY) == FLB_TRUE);
1657+
1658+
result = flb_condition_evaluate(cond, &record_data->chunk);
1659+
TEST_CHECK(result == FLB_TRUE); /* 3 replicas is within [2, 5] */
1660+
1661+
flb_condition_destroy(cond);
1662+
destroy_test_record(record_data);
1663+
1664+
/* Test GTE/LTE with OR (resource request check) */
1665+
record_data = create_test_record_numeric("requested_cpu", 1.5);
1666+
TEST_CHECK(record_data != NULL);
1667+
1668+
cond = flb_condition_create(FLB_COND_OP_OR);
1669+
TEST_CHECK(cond != NULL);
1670+
1671+
val1 = 2.0; /* CPU threshold */
1672+
val2 = 1.0; /* Minimum CPU */
1673+
TEST_CHECK(flb_condition_add_rule(cond, "$requested_cpu", FLB_RULE_OP_GTE,
1674+
&val1, 0, RECORD_CONTEXT_BODY) == FLB_TRUE);
1675+
TEST_CHECK(flb_condition_add_rule(cond, "$requested_cpu", FLB_RULE_OP_LTE,
1676+
&val2, 0, RECORD_CONTEXT_BODY) == FLB_TRUE);
1677+
1678+
result = flb_condition_evaluate(cond, &record_data->chunk);
1679+
TEST_CHECK(result == FLB_FALSE); /* 1.5 CPU is neither >= 2.0 nor <= 1.0 */
1680+
1681+
flb_condition_destroy(cond);
1682+
destroy_test_record(record_data);
1683+
1684+
/* Test combined with other operators */
1685+
record_data = create_test_record_with_meta("restart_count", "5",
1686+
"namespace", "production");
1687+
TEST_CHECK(record_data != NULL);
1688+
1689+
cond = flb_condition_create(FLB_COND_OP_AND);
1690+
TEST_CHECK(cond != NULL);
1691+
1692+
val1 = 3.0; /* restart threshold */
1693+
TEST_CHECK(flb_condition_add_rule(cond, "$restart_count", FLB_RULE_OP_GTE,
1694+
&val1, 0, RECORD_CONTEXT_BODY) == FLB_TRUE);
1695+
TEST_CHECK(flb_condition_add_rule(cond, "$namespace", FLB_RULE_OP_EQ,
1696+
"production", 0, RECORD_CONTEXT_METADATA) == FLB_TRUE);
1697+
1698+
result = flb_condition_evaluate(cond, &record_data->chunk);
1699+
TEST_CHECK(result == FLB_TRUE);
1700+
1701+
flb_condition_destroy(cond);
1702+
destroy_test_record(record_data);
1703+
}
1704+
1705+
void test_condition_gte_lte_border_cases()
1706+
{
1707+
struct test_record *record_data;
1708+
struct flb_condition *cond;
1709+
int result;
1710+
double val;
1711+
1712+
/* Test with non-numeric string */
1713+
record_data = create_test_record("pod_status", "CrashLoopBackOff");
1714+
TEST_CHECK(record_data != NULL);
1715+
1716+
cond = flb_condition_create(FLB_COND_OP_AND);
1717+
TEST_CHECK(cond != NULL);
1718+
1719+
val = 1.0;
1720+
TEST_CHECK(flb_condition_add_rule(cond, "$pod_status", FLB_RULE_OP_GTE,
1721+
&val, 0, RECORD_CONTEXT_BODY) == FLB_TRUE);
1722+
1723+
result = flb_condition_evaluate(cond, &record_data->chunk);
1724+
TEST_CHECK(result == FLB_FALSE);
1725+
1726+
flb_condition_destroy(cond);
1727+
destroy_test_record(record_data);
1728+
1729+
/* Test with very small resource difference */
1730+
record_data = create_test_record_numeric("cpu_limit", 1.0000001);
1731+
TEST_CHECK(record_data != NULL);
1732+
1733+
cond = flb_condition_create(FLB_COND_OP_AND);
1734+
TEST_CHECK(cond != NULL);
1735+
1736+
val = 1.0;
1737+
TEST_CHECK(flb_condition_add_rule(cond, "$cpu_limit", FLB_RULE_OP_GTE,
1738+
&val, 0, RECORD_CONTEXT_BODY) == FLB_TRUE);
1739+
1740+
result = flb_condition_evaluate(cond, &record_data->chunk);
1741+
TEST_CHECK(result == FLB_TRUE);
1742+
1743+
flb_condition_destroy(cond);
1744+
destroy_test_record(record_data);
1745+
1746+
/* Test with negative resource request (invalid but should handle) */
1747+
record_data = create_test_record_numeric("memory_request", -128.0);
1748+
TEST_CHECK(record_data != NULL);
1749+
1750+
cond = flb_condition_create(FLB_COND_OP_AND);
1751+
TEST_CHECK(cond != NULL);
1752+
1753+
val = -256.0;
1754+
TEST_CHECK(flb_condition_add_rule(cond, "$memory_request", FLB_RULE_OP_GTE,
1755+
&val, 0, RECORD_CONTEXT_BODY) == FLB_TRUE);
1756+
1757+
result = flb_condition_evaluate(cond, &record_data->chunk);
1758+
TEST_CHECK(result == FLB_TRUE);
1759+
1760+
flb_condition_destroy(cond);
1761+
destroy_test_record(record_data);
1762+
1763+
/* Test with zero replicas */
1764+
record_data = create_test_record_numeric("current_replicas", 0.0);
1765+
TEST_CHECK(record_data != NULL);
1766+
1767+
cond = flb_condition_create(FLB_COND_OP_AND);
1768+
TEST_CHECK(cond != NULL);
1769+
1770+
val = 0.0;
1771+
TEST_CHECK(flb_condition_add_rule(cond, "$current_replicas", FLB_RULE_OP_GTE,
1772+
&val, 0, RECORD_CONTEXT_BODY) == FLB_TRUE);
1773+
1774+
result = flb_condition_evaluate(cond, &record_data->chunk);
1775+
TEST_CHECK(result == FLB_TRUE);
1776+
1777+
flb_condition_destroy(cond);
1778+
destroy_test_record(record_data);
1779+
1780+
/* Test scaling down to zero */
1781+
record_data = create_test_record_numeric("desired_replicas", -0.0);
1782+
TEST_CHECK(record_data != NULL);
1783+
1784+
cond = flb_condition_create(FLB_COND_OP_AND);
1785+
TEST_CHECK(cond != NULL);
1786+
1787+
val = 0.0;
1788+
TEST_CHECK(flb_condition_add_rule(cond, "$desired_replicas", FLB_RULE_OP_LTE,
1789+
&val, 0, RECORD_CONTEXT_BODY) == FLB_TRUE);
1790+
1791+
result = flb_condition_evaluate(cond, &record_data->chunk);
1792+
TEST_CHECK(result == FLB_TRUE);
1793+
1794+
flb_condition_destroy(cond);
1795+
destroy_test_record(record_data);
1796+
}
1797+
15271798
TEST_LIST = {
15281799
{"equals", test_condition_equals},
15291800
{"not_equals", test_condition_not_equals},
@@ -1542,5 +1813,8 @@ TEST_LIST = {
15421813
{"metadata", test_condition_metadata},
15431814
{"missing_values", test_condition_missing_values},
15441815
{"border_cases", test_condition_border_cases},
1816+
{"gte_lte", test_condition_gte_lte},
1817+
{"gte_lte_multiple", test_condition_gte_lte_multiple},
1818+
{"gte_lte_border_cases", test_condition_gte_lte_border_cases},
15451819
{NULL, NULL}
15461820
};

0 commit comments

Comments
 (0)