From 0cc0df1035777c1afbe7e67a78ac30172c0c067c Mon Sep 17 00:00:00 2001 From: camila Date: Wed, 15 Jun 2022 09:27:48 -0300 Subject: [PATCH 1/9] md: Modify callback to return all all matches --- deps/wazuh_testing/wazuh_testing/gcloud.py | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/deps/wazuh_testing/wazuh_testing/gcloud.py b/deps/wazuh_testing/wazuh_testing/gcloud.py index 1722848b62..29aaca4b4c 100644 --- a/deps/wazuh_testing/wazuh_testing/gcloud.py +++ b/deps/wazuh_testing/wazuh_testing/gcloud.py @@ -56,9 +56,10 @@ def detect_gcp_start(file_monitor): def callback_received_messages_number(line): - match = re.match(r'.*wm_gcp_pubsub_run\(\): INFO: - INFO - Received and acknowledged (\d+) messages', line) + received_messages_number = rf".*wm_gcp_pubsub_run\(\): INFO: - INFO - Received and acknowledged (\d+) messages" + match = re.findall(received_messages_number, line) if match: - return match.group(1) + return match return None From 5d42992621d8a98e24c695beb69412f37be68e50 Mon Sep 17 00:00:00 2001 From: camila Date: Wed, 15 Jun 2022 09:28:15 -0300 Subject: [PATCH 2/9] fix: Modify test in order to use the new callback --- .../test_functionality/test_max_messages.py | 31 +++++++------------ 1 file changed, 11 insertions(+), 20 deletions(-) diff --git a/tests/integration/test_gcloud/test_functionality/test_max_messages.py b/tests/integration/test_gcloud/test_functionality/test_max_messages.py index 3283e94915..6e99e94368 100644 --- a/tests/integration/test_gcloud/test_functionality/test_max_messages.py +++ b/tests/integration/test_gcloud/test_functionality/test_max_messages.py @@ -1,5 +1,5 @@ ''' -copyright: Copyright (C) 2015-2021, Wazuh Inc. +copyright: Copyright (C) 2015-2022, Wazuh Inc. Created by Wazuh, Inc. . @@ -180,27 +180,18 @@ def test_max_messages(get_configuration, configure_environment, reset_ossec_log, callback=callback_detect_start_fetching_logs, error_message='Did not receive expected ' '"Starting fetching of logs" event') - - if publish_messages <= max_messages: - number_pulled = wazuh_log_monitor.start(timeout=pull_messages_timeout, + + numbers_pulled = wazuh_log_monitor.start(timeout=pull_messages_timeout, callback=callback_received_messages_number, error_message='Did not receive expected ' '- INFO - Received and acknowledged x messages').result() + if publish_messages <= max_messages: # GCP might log messages from sources other than ourselves - assert int(number_pulled) >= publish_messages + for number_pulled in numbers_pulled: + if int(number_pulled) != 0: + assert int(number_pulled) >= publish_messages + assert int(number_pulled) <= max_messages else: - ntimes = int(publish_messages / max_messages) - remainder = int(publish_messages % max_messages) - - for i in range(ntimes): - number_pulled = wazuh_log_monitor.start(timeout=pull_messages_timeout, - callback=callback_received_messages_number, - error_message='Did not receive expected ' - 'Received and acknowledged x messages').result() - assert int(number_pulled) == max_messages - number_pulled = wazuh_log_monitor.start(timeout=pull_messages_timeout, - callback=callback_received_messages_number, - error_message='Did not receive expected ' - '- INFO - Received and acknowledged x messages').result() - # GCP might log messages from sources other than ourselves - assert int(number_pulled) >= remainder + for number_pulled in numbers_pulled: + if int(number_pulled) != 0: + assert int(number_pulled) <= max_messages From 1cbbd850745cdaa6a95cc6e75c084baf3a70de92 Mon Sep 17 00:00:00 2001 From: camila Date: Wed, 15 Jun 2022 09:30:53 -0300 Subject: [PATCH 3/9] fix: Fix pycodestyle --- .../test_functionality/test_max_messages.py | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/tests/integration/test_gcloud/test_functionality/test_max_messages.py b/tests/integration/test_gcloud/test_functionality/test_max_messages.py index 6e99e94368..ef67236ea3 100644 --- a/tests/integration/test_gcloud/test_functionality/test_max_messages.py +++ b/tests/integration/test_gcloud/test_functionality/test_max_messages.py @@ -109,6 +109,7 @@ # fixtures + @pytest.fixture(scope='module', params=configurations) def get_configuration(request): """Get configurations from the module.""" @@ -180,11 +181,11 @@ def test_max_messages(get_configuration, configure_environment, reset_ossec_log, callback=callback_detect_start_fetching_logs, error_message='Did not receive expected ' '"Starting fetching of logs" event') - + numbers_pulled = wazuh_log_monitor.start(timeout=pull_messages_timeout, - callback=callback_received_messages_number, - error_message='Did not receive expected ' - '- INFO - Received and acknowledged x messages').result() + callback=callback_received_messages_number, + error_message='Did not receive expected ' + '- INFO - Received and acknowledged x messages').result() if publish_messages <= max_messages: # GCP might log messages from sources other than ourselves for number_pulled in numbers_pulled: @@ -194,4 +195,4 @@ def test_max_messages(get_configuration, configure_environment, reset_ossec_log, else: for number_pulled in numbers_pulled: if int(number_pulled) != 0: - assert int(number_pulled) <= max_messages + assert int(number_pulled) <= max_messages From 87a362742354f5732eaa3fbaedfb0b9130901dbf Mon Sep 17 00:00:00 2001 From: camila Date: Wed, 15 Jun 2022 10:39:12 -0300 Subject: [PATCH 4/9] add: Add a count message --- .../test_gcloud/test_functionality/test_max_messages.py | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/tests/integration/test_gcloud/test_functionality/test_max_messages.py b/tests/integration/test_gcloud/test_functionality/test_max_messages.py index ef67236ea3..478d41de1f 100644 --- a/tests/integration/test_gcloud/test_functionality/test_max_messages.py +++ b/tests/integration/test_gcloud/test_functionality/test_max_messages.py @@ -59,6 +59,7 @@ - scan - maximum ''' +from itertools import count import os import sys @@ -82,6 +83,7 @@ pull_messages_timeout = global_parameters.default_timeout + 60 pull_on_start = 'no' max_messages = 100 +count_message = 0 logging = 'info' wazuh_log_monitor = FileMonitor(LOG_FILE_PATH) test_data_path = os.path.join(os.path.dirname(os.path.realpath(__file__)), 'data') @@ -190,8 +192,10 @@ def test_max_messages(get_configuration, configure_environment, reset_ossec_log, # GCP might log messages from sources other than ourselves for number_pulled in numbers_pulled: if int(number_pulled) != 0: - assert int(number_pulled) >= publish_messages + if (int(number_pulled) >= publish_messages): + count_message += 1 assert int(number_pulled) <= max_messages + assert count_message >=1 else: for number_pulled in numbers_pulled: if int(number_pulled) != 0: From 642aa5577fb527f72d52af45dc88a3b06eb38f53 Mon Sep 17 00:00:00 2001 From: camila Date: Wed, 15 Jun 2022 10:54:05 -0300 Subject: [PATCH 5/9] mf: Modify variable - count_message --- .../test_gcloud/test_functionality/test_max_messages.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/integration/test_gcloud/test_functionality/test_max_messages.py b/tests/integration/test_gcloud/test_functionality/test_max_messages.py index 478d41de1f..af2e3f4225 100644 --- a/tests/integration/test_gcloud/test_functionality/test_max_messages.py +++ b/tests/integration/test_gcloud/test_functionality/test_max_messages.py @@ -83,7 +83,6 @@ pull_messages_timeout = global_parameters.default_timeout + 60 pull_on_start = 'no' max_messages = 100 -count_message = 0 logging = 'info' wazuh_log_monitor = FileMonitor(LOG_FILE_PATH) test_data_path = os.path.join(os.path.dirname(os.path.realpath(__file__)), 'data') @@ -175,6 +174,7 @@ def test_max_messages(get_configuration, configure_environment, reset_ossec_log, - logs - scheduled ''' + count_message = 0 str_interval = get_configuration['sections'][0]['elements'][4]['interval']['value'] time_interval = int(''.join(filter(str.isdigit, str_interval))) From ef8b2d91d0e5e2174e67eb9e8f76c8d70793c422 Mon Sep 17 00:00:00 2001 From: camila Date: Thu, 16 Jun 2022 11:21:51 -0300 Subject: [PATCH 6/9] add: Add coment in counter --- .../test_gcloud/test_functionality/test_max_messages.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/integration/test_gcloud/test_functionality/test_max_messages.py b/tests/integration/test_gcloud/test_functionality/test_max_messages.py index af2e3f4225..2ca20e75c6 100644 --- a/tests/integration/test_gcloud/test_functionality/test_max_messages.py +++ b/tests/integration/test_gcloud/test_functionality/test_max_messages.py @@ -59,11 +59,10 @@ - scan - maximum ''' -from itertools import count import os import sys - import pytest + from wazuh_testing import global_parameters from wazuh_testing.fim import generate_params from wazuh_testing.gcloud import callback_detect_start_fetching_logs, callback_received_messages_number @@ -193,6 +192,7 @@ def test_max_messages(get_configuration, configure_environment, reset_ossec_log, for number_pulled in numbers_pulled: if int(number_pulled) != 0: if (int(number_pulled) >= publish_messages): + # A counter is used to prevent it from failing due to logs from other sources count_message += 1 assert int(number_pulled) <= max_messages assert count_message >=1 From c34dab08934686aabcdc83602480e59041fcca92 Mon Sep 17 00:00:00 2001 From: camila Date: Thu, 16 Jun 2022 11:25:02 -0300 Subject: [PATCH 7/9] add: Add pycodestyle --- .../test_gcloud/test_functionality/test_max_messages.py | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/tests/integration/test_gcloud/test_functionality/test_max_messages.py b/tests/integration/test_gcloud/test_functionality/test_max_messages.py index 2ca20e75c6..7e2e4c1b41 100644 --- a/tests/integration/test_gcloud/test_functionality/test_max_messages.py +++ b/tests/integration/test_gcloud/test_functionality/test_max_messages.py @@ -124,7 +124,8 @@ def get_configuration(request): ['- DEBUG - GCP message' for _ in range(100)], ['- DEBUG - GCP message' for _ in range(120)] ], indirect=True) -def test_max_messages(get_configuration, configure_environment, reset_ossec_log, publish_messages, daemons_handler, wait_for_gcp_start): +def test_max_messages(get_configuration, configure_environment, reset_ossec_log, publish_messages, + daemons_handler, wait_for_gcp_start): ''' description: Check if the 'gcp-pubsub' module pulls a message number less than or equal to the limit set in the 'max_messages' tag. For this purpose, the test will use a fixed limit and generate a @@ -195,7 +196,7 @@ def test_max_messages(get_configuration, configure_environment, reset_ossec_log, # A counter is used to prevent it from failing due to logs from other sources count_message += 1 assert int(number_pulled) <= max_messages - assert count_message >=1 + assert count_message >= 1 else: for number_pulled in numbers_pulled: if int(number_pulled) != 0: From 5c68493ef9540194bfea05bd023ce8717a1b5a6b Mon Sep 17 00:00:00 2001 From: camila Date: Mon, 27 Jun 2022 12:13:15 -0300 Subject: [PATCH 8/9] rm: Removed unnecessary condition --- .../test_gcloud/test_functionality/test_max_messages.py | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/tests/integration/test_gcloud/test_functionality/test_max_messages.py b/tests/integration/test_gcloud/test_functionality/test_max_messages.py index 7e2e4c1b41..d8b26d310d 100644 --- a/tests/integration/test_gcloud/test_functionality/test_max_messages.py +++ b/tests/integration/test_gcloud/test_functionality/test_max_messages.py @@ -199,5 +199,4 @@ def test_max_messages(get_configuration, configure_environment, reset_ossec_log, assert count_message >= 1 else: for number_pulled in numbers_pulled: - if int(number_pulled) != 0: - assert int(number_pulled) <= max_messages + assert int(number_pulled) <= max_messages From 4aa168fbe23cfd74064a4a1524a8951cf05a196f Mon Sep 17 00:00:00 2001 From: camila Date: Mon, 27 Jun 2022 12:25:58 -0300 Subject: [PATCH 9/9] add: Add comments to explain the code --- .../test_gcloud/test_functionality/test_max_messages.py | 3 +++ 1 file changed, 3 insertions(+) diff --git a/tests/integration/test_gcloud/test_functionality/test_max_messages.py b/tests/integration/test_gcloud/test_functionality/test_max_messages.py index d8b26d310d..b8b6d3eefc 100644 --- a/tests/integration/test_gcloud/test_functionality/test_max_messages.py +++ b/tests/integration/test_gcloud/test_functionality/test_max_messages.py @@ -188,6 +188,8 @@ def test_max_messages(get_configuration, configure_environment, reset_ossec_log, callback=callback_received_messages_number, error_message='Did not receive expected ' '- INFO - Received and acknowledged x messages').result() + # Validate that the number pulled is at least once greater or equal than the published messages + # and less than the maximum number of messages extracted allowed in each iteration if publish_messages <= max_messages: # GCP might log messages from sources other than ourselves for number_pulled in numbers_pulled: @@ -198,5 +200,6 @@ def test_max_messages(get_configuration, configure_environment, reset_ossec_log, assert int(number_pulled) <= max_messages assert count_message >= 1 else: + # Validate that the number pulled always is less than the maximum messages pulled allowed in each iteration for number_pulled in numbers_pulled: assert int(number_pulled) <= max_messages