From 1f9deb5fcb24854a56082b355da2a7fec0a87d68 Mon Sep 17 00:00:00 2001 From: Yana Date: Thu, 30 Dec 2021 11:42:22 +0100 Subject: [PATCH] Changes after latest review by Snaow --- .../roles/master-role/tasks/main.yml | 12 --------- .../roles/worker-role/tasks/main.yml | 12 --------- .../data/get_wdb_agent.py | 10 ++++++++ .../data/messages.yml | 2 +- .../test_agent_files_deletion.py | 25 ++++++++++--------- 5 files changed, 24 insertions(+), 37 deletions(-) create mode 100644 tests/system/test_cluster/test_agent_files_deletion/data/get_wdb_agent.py diff --git a/tests/system/provisioning/basic_cluster/roles/master-role/tasks/main.yml b/tests/system/provisioning/basic_cluster/roles/master-role/tasks/main.yml index 7372a7d5b9..efa2a7fb0e 100644 --- a/tests/system/provisioning/basic_cluster/roles/master-role/tasks/main.yml +++ b/tests/system/provisioning/basic_cluster/roles/master-role/tasks/main.yml @@ -111,15 +111,3 @@ - name: "Install necessary dependencies" command: /var/ossec/framework/python/bin/python3.9 -m pip install lockfile filetype certifi testinfra - -- name: "Create wdb get agent script" - copy: - dest: "/get_wdb_agent.py" - content: | - import sys - sys.path.append('/wazuh-qa/deps/wazuh_testing') - from wazuh_testing import wazuh_db - - result = wazuh_db.query_wdb(sys.argv[1]) - if result: - print(result) diff --git a/tests/system/provisioning/basic_cluster/roles/worker-role/tasks/main.yml b/tests/system/provisioning/basic_cluster/roles/worker-role/tasks/main.yml index 23851dcf70..eeeb212096 100644 --- a/tests/system/provisioning/basic_cluster/roles/worker-role/tasks/main.yml +++ b/tests/system/provisioning/basic_cluster/roles/worker-role/tasks/main.yml @@ -99,15 +99,3 @@ - name: "Install necessary dependencies" command: /var/ossec/framework/python/bin/python3.9 -m pip install lockfile filetype certifi testinfra - -- name: "Create wdb get agent script" - copy: - dest: "/get_wdb_agent.py" - content: | - import sys - sys.path.append('/wazuh-qa/deps/wazuh_testing') - from wazuh_testing import wazuh_db - - result = wazuh_db.query_wdb(sys.argv[1]) - if result: - print(result) diff --git a/tests/system/test_cluster/test_agent_files_deletion/data/get_wdb_agent.py b/tests/system/test_cluster/test_agent_files_deletion/data/get_wdb_agent.py new file mode 100644 index 0000000000..6dad75ee23 --- /dev/null +++ b/tests/system/test_cluster/test_agent_files_deletion/data/get_wdb_agent.py @@ -0,0 +1,10 @@ +# Copyright (C) 2015-2021, Wazuh Inc. +# Created by Wazuh, Inc. . +# This program is free software; you can redistribute it and/or modify it under the terms of GPLv2 +import sys +sys.path.append('/wazuh-qa/deps/wazuh_testing') +from wazuh_testing import wazuh_db + +result = wazuh_db.query_wdb(sys.argv[1]) +if result: + print(result) diff --git a/tests/system/test_cluster/test_agent_files_deletion/data/messages.yml b/tests/system/test_cluster/test_agent_files_deletion/data/messages.yml index 2d71693704..fa699c3a3e 100644 --- a/tests/system/test_cluster/test_agent_files_deletion/data/messages.yml +++ b/tests/system/test_cluster/test_agent_files_deletion/data/messages.yml @@ -16,4 +16,4 @@ wazuh-worker2: wazuh-master: - regex: '.*Worker wazuh-worker2.*Integrity check.*Finished in.*' path: "/var/ossec/logs/cluster.log" - timeout: 60 \ No newline at end of file + timeout: 60 diff --git a/tests/system/test_cluster/test_agent_files_deletion/test_agent_files_deletion.py b/tests/system/test_cluster/test_agent_files_deletion/test_agent_files_deletion.py index 14ffda6928..b2bf4ea17f 100644 --- a/tests/system/test_cluster/test_agent_files_deletion/test_agent_files_deletion.py +++ b/tests/system/test_cluster/test_agent_files_deletion/test_agent_files_deletion.py @@ -15,6 +15,7 @@ agent_host = 'wazuh-agent3' local_path = os.path.dirname(os.path.abspath(__file__)) messages_path = os.path.join(local_path, 'data/messages.yml') +script_path = '/wazuh-qa/tests/system/test_cluster/test_agent_files_deletion/data' tmp_path = os.path.join(local_path, 'tmp') managers_hosts = [master_host, worker_host] inventory_path = join(dirname(dirname(dirname(abspath(__file__)))), 'provisioning', 'basic_cluster', 'inventory.yml') @@ -32,15 +33,14 @@ 'global sql select * from belongs where id_agent={id}'] -def agent_healthcheck(): +def agent_healthcheck(master_token): """Check if the agent is active and reporting.""" timeout = time() + time_to_agent_reconnect - command = f'{WAZUH_PATH}/bin/cluster_control -a | grep active | wc -l' while True: - if int(host_manager.run_shell('wazuh-worker2', - command)) == 4 and int(host_manager.run_shell('wazuh-master', - command)) == 4 or time() > timeout: + response = host_manager.make_api_call(host=master_host, method='GET', token=master_token, + endpoint=f'/agents?status=active') + if int(response["json"]["data"]["total_affected_items"]) == 4 or time() > timeout: break sleep(5) sleep(time_to_sync) @@ -54,14 +54,14 @@ def test_agent_files_deletion(): host_manager.clear_file(host=hosts, file_path=os.path.join(WAZUH_LOGS_PATH, 'cluster.log')) host_manager.control_service(host=hosts, service='wazuh', state="restarted") - # Check if the agent is connected and reporting - agent_healthcheck() - # Get the current ID and name of the agent that is reporting to worker_host. master_token = host_manager.get_api_token(master_host) response = host_manager.make_api_call(host=master_host, method='GET', token=master_token, endpoint=f'/agents?select=id,name&q=manager={worker_host}') + # Check if the agent is connected and reporting + agent_healthcheck(master_token) + assert response['status'] == 200, f'Failed when trying to obtain agent ID: {response}' try: agent_id = response['json']['data']['affected_items'][0]['id'] @@ -81,9 +81,9 @@ def test_agent_files_deletion(): # Check that agent information is in the wdb socket for host in managers_hosts: for query in queries: - print(query.format(id=agent_id)) result = host_manager.run_command(host, - f"/var/ossec/framework/python/bin/python3.9 /get_wdb_agent.py " + f"{WAZUH_PATH}/framework/python/bin/python3.9 " + f"{script_path}/get_wdb_agent.py " f"'{query.format(id=agent_id)}'") assert result, f'This db query should have returned something in {host}, but it did not: {result}' @@ -109,9 +109,10 @@ def test_agent_files_deletion(): for host in managers_hosts: for query in queries: result = host_manager.run_command(host, - f"/var/ossec/framework/python/bin/python3.9 /get_wdb_agent.py " + f"{WAZUH_PATH}/framework/python/bin/python3.9 " + f"{script_path}/get_wdb_agent.py " f"'{query.format(id=agent_id)}'") assert not result, f'This db query should have not returned anything in {host}, but it did: {result}' host_manager.get_host(agent_host).ansible('command', f'service wazuh-agent restart', check=False) - agent_healthcheck() + agent_healthcheck(master_token)