From 3c6e20cf9cc609c1cd568d6022232bea91311f9c Mon Sep 17 00:00:00 2001 From: Yana Date: Wed, 5 Jan 2022 15:47:46 +0100 Subject: [PATCH] Changes after Julia's review --- tests/system/README.md | 2 +- .../provisioning/basic_cluster/playbook.yml | 24 +++++----- .../test_agent_files_deletion.py | 47 ++++++++++--------- 3 files changed, 37 insertions(+), 36 deletions(-) diff --git a/tests/system/README.md b/tests/system/README.md index f052feac68..e2766974fd 100644 --- a/tests/system/README.md +++ b/tests/system/README.md @@ -195,7 +195,7 @@ If you want to specify a particular branch of the Wazuh repository, you will nee ```shell script ansible-playbook -i inventory.yml playbook.yml --extra-vars='{"wazuh_branch":"v4.3.0-rc1"}' ``` -In the **basic cluster**, you can also specify a branch from the Wazuh QA repository. +In the **basic cluster**, you also have to specify a branch from the Wazuh QA repository. ```shell script ansible-playbook -i inventory.yml playbook.yml --extra-vars='{"wazuh_branch":"v4.3.0-rc1", "wazuh_qa_branch":"master"}' ``` diff --git a/tests/system/provisioning/basic_cluster/playbook.yml b/tests/system/provisioning/basic_cluster/playbook.yml index 4d83682f62..66d919d9b8 100644 --- a/tests/system/provisioning/basic_cluster/playbook.yml +++ b/tests/system/provisioning/basic_cluster/playbook.yml @@ -113,18 +113,6 @@ roles: - name: "roles/agent-role" -- name: Wazuh Agent3 - hosts: wazuh-agent3 - vars: - manager_hostname: wazuh-worker2 - agent_id: "{{ agent3_id }}" - agent_hostname: "{{ agent3_hostname }}" - agent_key: "{{ agent3_key }}" - vars_files: - - ./vars/configurations.yml - roles: - - name: "roles/agent-role" - - name: Wazuh Agent2 hosts: wazuh-agent2 vars: @@ -136,3 +124,15 @@ - ./vars/configurations.yml roles: - name: "roles/agent-role" + +- name: Wazuh Agent3 + hosts: wazuh-agent3 + vars: + manager_hostname: wazuh-worker2 + agent_id: "{{ agent3_id }}" + agent_hostname: "{{ agent3_hostname }}" + agent_key: "{{ agent3_key }}" + vars_files: + - ./vars/configurations.yml + roles: + - name: "roles/agent-role" 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 db4054b378..f7f3c2529c 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 @@ -2,6 +2,7 @@ # Created by Wazuh, Inc. . # This program is free software; you can redistribute it and/or modify it under the terms of GPLv2 import os +import re from os.path import join, dirname, abspath from time import time, sleep @@ -15,11 +16,13 @@ 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' +script_path = os.path.join(re.sub(r'^.*?wazuh-qa', '/wazuh-qa', local_path), 'data/get_wdb_agent.py') + 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') host_manager = HostManager(inventory_path) +while_time = 5 time_to_sync = 20 time_to_agent_reconnect = 180 @@ -39,11 +42,11 @@ def agent_healthcheck(master_token): timeout = time() + time_to_agent_reconnect while True: response = host_manager.make_api_call(host=master_host, method='GET', token=master_token, - endpoint=f'/agents?status=active') - assert response['status'] == 200, f'Failed when trying to get the active agents' - if int(response["json"]["data"]["total_affected_items"]) == 4 or time() > timeout: + endpoint='/agents?status=active') + assert response['status'] == 200, 'Failed when trying to get the active agents' + if int(response['json']['data']['total_affected_items']) == 4 or time() > timeout: break - sleep(5) + sleep(while_time) sleep(time_to_sync) @@ -53,7 +56,7 @@ def test_agent_files_deletion(): for hosts in managers_hosts: host_manager.clear_file(host=hosts, file_path=os.path.join(WAZUH_LOGS_PATH, 'ossec.log')) 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") + host_manager.control_service(host=hosts, service='wazuh', state='restarted') # Get the token master_token = host_manager.get_api_token(master_host) @@ -63,9 +66,9 @@ def test_agent_files_deletion(): # Get the current ID and name of the agent that is reporting to worker_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}') + endpoint=f"/agents?select=id,name&q=manager={worker_host}") - assert response['status'] == 200, f'Failed when trying to obtain agent ID: {response}' + assert response['status'] == 200, f"Failed when trying to obtain agent ID: {response}" try: agent_id = response['json']['data']['affected_items'][0]['id'] agent_name = response['json']['data']['affected_items'][0]['name'] @@ -76,24 +79,23 @@ def test_agent_files_deletion(): for file in files: for host in file['hosts']: result = host_manager.run_shell( - host, f'test -e {file["path"].format(id=agent_id, name=agent_name)} && echo "exists"' + host, f"test -e {file['path'].format(id=agent_id, name=agent_name)} && echo 'exists'" ) - assert result, f'This file should exist in {host} but could not be found: ' \ - f'{file["path"].format(id=agent_id, name=agent_name)}' + assert result, f"This file should exist in {host} but could not be found: " \ + f"{file['path'].format(id=agent_id, name=agent_name)}" # Check that agent information is in the wdb socket for host in managers_hosts: for query in queries: result = host_manager.run_command(host, 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}' + f"{script_path} '{query.format(id=agent_id)}'") + assert result, f"This db query should have returned something in {host}, but it did not: {result}" # Remove the agent response = host_manager.make_api_call(host=master_host, method='DELETE', token=master_token, - endpoint=f'/agents?agents_list={agent_id}&status=all&older_than=0s') - assert response['status'] == 200, f'Failed when trying to remove agent {agent_id}: {response}' + endpoint=f"/agents?agents_list={agent_id}&status=all&older_than=0s") + assert response['status'] == 200, f"Failed when trying to remove agent {agent_id}: {response}" # Wait until information is synced to all workers HostMonitor(inventory_path=inventory_path, messages_path=messages_path, tmp_path=tmp_path).run() @@ -103,19 +105,18 @@ def test_agent_files_deletion(): for file in files: for host in file['hosts']: result = host_manager.run_shell( - host, f'test -e {file["path"].format(id=agent_id, name=agent_name)} && echo "exists"' + host, f"test -e {file['path'].format(id=agent_id, name=agent_name)} && echo 'exists'" ) - assert not result, f'This file should not exist in {host} but it was found: ' \ - f'{file["path"].format(id=agent_id, name=agent_name)}' + assert not result, f"This file should not exist in {host} but it was found: " \ + f"{file['path'].format(id=agent_id, name=agent_name)}" # Check that agent information is not in the wdb socket for host in managers_hosts: for query in queries: result = host_manager.run_command(host, 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}' + f"{script_path} '{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) + host_manager.control_service(host=agent_host, service='wazuh', state='restarted') agent_healthcheck(master_token)