From e1ec6105ab118a9e226c86ac1f3e0bfefc54ca20 Mon Sep 17 00:00:00 2001 From: Balazs Scheidler Date: Mon, 8 Apr 2024 20:53:56 +0200 Subject: [PATCH] filterx: add light tests for filterx isset/unset functions Signed-off-by: Balazs Scheidler --- .../functional_tests/filterx/test_filterx.py | 168 ++++++++++++++++++ 1 file changed, 168 insertions(+) diff --git a/tests/light/functional_tests/filterx/test_filterx.py b/tests/light/functional_tests/filterx/test_filterx.py index 4369f45ac22..95347fb0a52 100644 --- a/tests/light/functional_tests/filterx/test_filterx.py +++ b/tests/light/functional_tests/filterx/test_filterx.py @@ -762,3 +762,171 @@ def test_if_condition_non_matching_expression(config, syslog_ng): assert file_true.get_stats()["processed"] == 1 assert "processed" not in file_false.get_stats() assert file_true.read_log() == "default\n" + + +def test_isset_existing_value_not_in_scope_yet(config, syslog_ng): + (file_true, file_false) = create_config( + config, """ + isset($MSG); +""", + ) + syslog_ng.start(config) + + assert file_true.get_stats()["processed"] == 1 + assert "processed" not in file_false.get_stats() + +def test_isset_existing_value_already_in_scope(config, syslog_ng): + (file_true, file_false) = create_config( + config, """ + $MSG; + isset($MSG); +""", + ) + syslog_ng.start(config) + + assert file_true.get_stats()["processed"] == 1 + assert "processed" not in file_false.get_stats() + + +def test_isset_inexisting_value_not_in_scope_yet(config, syslog_ng): + (file_true, file_false) = create_config( + config, """ + not isset($almafa); +""", + ) + syslog_ng.start(config) + + assert file_true.get_stats()["processed"] == 1 + assert "processed" not in file_false.get_stats() + +def test_isset_inexisting_value_already_in_scope(config, syslog_ng): + (file_true, file_false) = create_config( + config, """ + $almafa = "x"; + unset($almafa); + not isset($almafa); +""", + ) + syslog_ng.start(config) + + assert file_true.get_stats()["processed"] == 1 + assert "processed" not in file_false.get_stats() + + +def test_isset(config, syslog_ng): + (file_true, file_false) = create_config( + config, """ + $MSG = json(); + $MSG.inner_key = "foo"; + + isset(${values.int}); + not isset($almafa); + isset($MSG["inner_key"]); + not isset($MSG["almafa"]); +""", + ) + syslog_ng.start(config) + + assert file_true.get_stats()["processed"] == 1 + assert "processed" not in file_false.get_stats() + + +def test_unset_value_not_in_scope_yet(config, syslog_ng): + (file_true, file_false) = create_config( + config, """ + unset($MSG); +""", + ) + syslog_ng.start(config) + + assert file_true.get_stats()["processed"] == 1 + assert "processed" not in file_false.get_stats() + assert file_true.read_log() == "\n" + + +def test_unset_value_already_in_scope(config, syslog_ng): + (file_true, file_false) = create_config( + config, """ + $MSG; + unset($MSG); +""", + ) + syslog_ng.start(config) + + assert file_true.get_stats()["processed"] == 1 + assert "processed" not in file_false.get_stats() + assert file_true.read_log() == "\n" + + +def test_unset_existing_key(config, syslog_ng): + (file_true, file_false) = create_config( + config, """ + $MSG = json(); + $MSG["foo"] = "bar"; + unset($MSG["foo"]); + not isset($MSG["foo"]); +""", + ) + syslog_ng.start(config) + + assert file_true.get_stats()["processed"] == 1 + assert "processed" not in file_false.get_stats() + assert file_true.read_log() == "{}\n" + + +def test_unset_inexisting_key(config, syslog_ng): + (file_true, file_false) = create_config( + config, """ + $MSG = json(); + unset($MSG["foo"]); + not isset($MSG["foo"]); +""", + ) + syslog_ng.start(config) + + assert file_true.get_stats()["processed"] == 1 + assert "processed" not in file_false.get_stats() + assert file_true.read_log() == "{}\n" + + +def test_setting_an_unset_key_will_contain_the_right_value(config, syslog_ng): + (file_true, file_false) = create_config( + config, """ + $MSG = json(); + $MSG["foo"] = "first"; + unset($MSG["foo"]); + not isset($MSG["foo"]); + $MSG["foo"] = "second"; +""", + ) + syslog_ng.start(config) + + assert file_true.get_stats()["processed"] == 1 + assert "processed" not in file_false.get_stats() + assert file_true.read_log() == '{"foo":"second"}\n' + + + +def test_unset(config, syslog_ng): + (file_true, file_false) = create_config( + config, """ + $MSG = json(); + $MSG["inner_key"] = "foo"; + + unset(${values.int}); + unset($almafa); + unset($MSG["inner_key"]); + unset($MSG["almafa"]); + + not isset(${values.int}); + not isset($almafa); + not isset($MSG["inner_key"]); + not isset($MSG["almafa"]); + unset($MSG); +""", + ) + syslog_ng.start(config) + + assert file_true.get_stats()["processed"] == 1 + assert "processed" not in file_false.get_stats() + assert file_true.read_log() == "\n"