Skip to content

Commit

Permalink
Changes after Julia's review
Browse files Browse the repository at this point in the history
  • Loading branch information
yanazaeva committed Jan 5, 2022
1 parent 95a2b6b commit 3c6e20c
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 36 deletions.
2 changes: 1 addition & 1 deletion tests/system/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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"}'
```
Expand Down
24 changes: 12 additions & 12 deletions tests/system/provisioning/basic_cluster/playbook.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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"
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
# Created by Wazuh, Inc. <info@wazuh.com>.
# 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

Expand All @@ -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

Expand All @@ -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)


Expand All @@ -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)
Expand All @@ -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']
Expand All @@ -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()
Expand All @@ -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)

0 comments on commit 3c6e20c

Please sign in to comment.