From 1608dfcb32c55a7b39c01a35fc51d56e58da59ba Mon Sep 17 00:00:00 2001 From: Antonio Date: Thu, 1 Aug 2024 09:42:01 +0200 Subject: [PATCH] fix(#5616): Adding port waits --- .../modules/testing/tests/helpers/agent.py | 51 +++++++++++++------ .../testing/tests/helpers/dashboard.py | 1 + .../modules/testing/tests/helpers/indexer.py | 1 + .../modules/testing/tests/helpers/manager.py | 3 ++ 4 files changed, 41 insertions(+), 15 deletions(-) diff --git a/deployability/modules/testing/tests/helpers/agent.py b/deployability/modules/testing/tests/helpers/agent.py index 5d1c2d3fb0..671303cadc 100644 --- a/deployability/modules/testing/tests/helpers/agent.py +++ b/deployability/modules/testing/tests/helpers/agent.py @@ -6,6 +6,7 @@ import re import requests import yaml +import time from modules.testing.utils import logger from .constants import WAZUH_CONF, WAZUH_ROOT, WAZUH_WINDOWS_CONF, WAZUH_MACOS_CONF @@ -444,26 +445,46 @@ def is_agent_port_open(inventory_path): str: Os name. """ os_type = HostInformation.get_os_type(inventory_path) + time.sleep(5) if os_type == 'linux': - result = ConnectionManager.execute_commands(inventory_path, 'ss -t -a -n | grep ":1514" | grep ESTAB') - if result.get('success'): - return 'ESTAB' in result.get('output') - else: - return False + for attempt in range(3): + result = ConnectionManager.execute_commands( + inventory_path, + 'ss -t -a -n | grep ":1514" | grep ESTAB' + ) + if result.get('success'): + if 'ESTAB' in result.get('output'): + return True + if attempt < 2: + time.sleep(5) + return False elif os_type == 'windows': - result = ConnectionManager.execute_commands(inventory_path, 'netstat -ano | Select-String -Pattern "TCP" | Select-String -Pattern "ESTABLISHED" | Select-String -Pattern ":1514"') - if result.get('success'): - return 'ESTABLISHED' in result.get('output') - else: - return False + for attempt in range(3): + result = ConnectionManager.execute_commands( + inventory_path, + 'netstat -ano | Select-String -Pattern "TCP" | Select-String -Pattern "ESTABLISHED" | Select-String -Pattern ":1514"' + ) + if result.get('success'): + if 'ESTABLISHED' in result.get('output'): + return True + if attempt < 2: + time.sleep(5) + return False elif os_type == 'macos': - result = ConnectionManager.execute_commands(inventory_path, 'netstat -an | grep ".1514 " | grep ESTABLISHED') - if result.get('success'): - return 'ESTABLISHED' in result.get('output') - else: - return False + for attempt in range(3): + result = ConnectionManager.execute_commands( + inventory_path, + 'netstat -an | grep ".1514 " | grep ESTABLISHED' + ) + if result.get('success'): + if 'ESTABLISHED' in result.get('output'): + return True + if attempt < 2: + time.sleep(5) + return False + def get_agents_information(wazuh_api: WazuhAPI) -> list: """ diff --git a/deployability/modules/testing/tests/helpers/dashboard.py b/deployability/modules/testing/tests/helpers/dashboard.py index 0348f49829..b737b1e205 100644 --- a/deployability/modules/testing/tests/helpers/dashboard.py +++ b/deployability/modules/testing/tests/helpers/dashboard.py @@ -90,6 +90,7 @@ def is_dashboard_port_open(inventory_path, wait=10, cycles=50): Returns: str: OS name. """ + time.sleep(5) wait_cycles = 0 while wait_cycles < cycles: ports = ConnectionManager.execute_commands(inventory_path, 'ss -t -a -n | grep ":443"').get('output') or "" diff --git a/deployability/modules/testing/tests/helpers/indexer.py b/deployability/modules/testing/tests/helpers/indexer.py index a790b9236d..42d518ebb2 100644 --- a/deployability/modules/testing/tests/helpers/indexer.py +++ b/deployability/modules/testing/tests/helpers/indexer.py @@ -95,6 +95,7 @@ def is_indexer_port_open(inventory_path, wait=10, cycles=50) -> bool: Returns: str: OS name. """ + time.sleep(5) wait_cycles = 0 while wait_cycles < cycles: ports = ConnectionManager.execute_commands(inventory_path, 'ss -t -a -n | grep ":9200"').get('output') or "" diff --git a/deployability/modules/testing/tests/helpers/manager.py b/deployability/modules/testing/tests/helpers/manager.py index b14b189f43..f180f07199 100644 --- a/deployability/modules/testing/tests/helpers/manager.py +++ b/deployability/modules/testing/tests/helpers/manager.py @@ -241,6 +241,7 @@ def is_wazuh_api_port_open(inventory_path, wait=10, cycles=50) -> bool: Returns: bool: True if port is opened. """ + time.sleep(5) wait_cycles = 0 while wait_cycles < cycles: ports = ConnectionManager.execute_commands(inventory_path, 'ss -t -a -n | grep ":443"').get('output') or "" @@ -266,6 +267,7 @@ def is_wazuh_agent_port_open(inventory_path, wait=10, cycles=50) -> bool: Returns: bool: True if port is opened. """ + time.sleep(5) wait_cycles = 0 while wait_cycles < cycles: ports = ConnectionManager.execute_commands(inventory_path, 'ss -t -a -n | grep ":1514"').get('output') or "" @@ -291,6 +293,7 @@ def is_wazuh_agent_enrollment_port_open(inventory_path, wait=10, cycles=50) -> b Returns: bool: True if port is opened. """ + time.sleep(5) wait_cycles = 0 while wait_cycles < cycles: ports = ConnectionManager.execute_commands(inventory_path, 'ss -t -a -n | grep ":443"').get('output') or ""