-
Notifications
You must be signed in to change notification settings - Fork 32
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Change all sleeps for monitorings and check wdb socket instead of glo…
…bal.db
- Loading branch information
Showing
5 changed files
with
93 additions
and
42 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
16 changes: 12 additions & 4 deletions
16
tests/system/test_cluster/test_agent_files_deletion/data/messages.yml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,19 @@ | ||
--- | ||
wazuh-master: | ||
- regex: ".*Agent key generated for agent.*" | ||
path: "/var/ossec/logs/ossec.log" | ||
wazuh-worker2: | ||
- regex: '.*Received 1 shared files to update from .*' | ||
path: "/var/ossec/logs/cluster.log" | ||
timeout: 60 | ||
- regex: '.*Worker wazuh-worker2.*Integrity sync.*Finished in.*' | ||
path: "/var/ossec/logs/cluster.log" | ||
timeout: 60 | ||
- regex: '.*Worker wazuh-worker1.*Integrity check.*Finished in.*' | ||
- regex: '.*Worker wazuh-worker2.*Integrity sync.*Starting.*' | ||
path: "/var/ossec/logs/cluster.log" | ||
timeout: 60 | ||
- regex: '.*Worker wazuh-worker2.*Integrity sync.*Finished in.*' | ||
path: "/var/ossec/logs/cluster.log" | ||
timeout: 60 | ||
|
||
wazuh-master: | ||
- regex: '.*Worker wazuh-worker2.*Integrity check.*Finished in.*' | ||
path: "/var/ossec/logs/cluster.log" | ||
timeout: 60 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,10 @@ | ||
# Copyright (C) 2015-2021, Wazuh Inc. | ||
# Created by Wazuh, Inc. <[email protected]>. | ||
# This program is free software; you can redistribute it and/or modify it under the terms of GPLv2 | ||
|
||
import datetime | ||
import os | ||
from os.path import join, dirname, abspath | ||
from time import sleep | ||
from time import time | ||
|
||
import pytest | ||
from wazuh_testing.tools import WAZUH_PATH, WAZUH_LOGS_PATH | ||
|
@@ -20,15 +20,12 @@ | |
managers_hosts = [master_host, worker_host] | ||
inventory_path = join(dirname(dirname(dirname(abspath(__file__)))), 'provisioning', 'basic_cluster', 'inventory.yml') | ||
host_manager = HostManager(inventory_path) | ||
time_to_sync = 60 | ||
|
||
# Each file should exist in all hosts specified in 'hosts'. | ||
files = [{'path': join(WAZUH_PATH, 'queue', 'rids', '{id}'), 'hosts': managers_hosts}, | ||
{'path': join(WAZUH_PATH, 'queue', 'agent-groups', '{id}'), 'hosts': managers_hosts}, | ||
{'path': join(WAZUH_PATH, 'queue', 'diff', '{name}'), 'hosts': [worker_host]}, | ||
{'path': join(WAZUH_PATH, 'queue', 'db', '{id}.db'), 'hosts': [worker_host]}] | ||
db_queries = ["select * from agent where id={id}", | ||
"select * from belongs where id_agent={id}"] | ||
|
||
|
||
@pytest.fixture(scope='function') | ||
|
@@ -38,16 +35,24 @@ def register_agent(): | |
host_manager.get_host(agent_host).ansible('command', f'service wazuh-agent restart', check=False) | ||
|
||
# Wait until the agent is reconnected | ||
timeout = time() + 180 | ||
while True: | ||
if int(host_manager.run_shell('wazuh-worker2', | ||
f'{WAZUH_PATH}/bin/cluster_control -a | ' | ||
f'grep active | wc -l')) == 4 and int(host_manager.run_shell('wazuh-master', | ||
f'{WAZUH_PATH}/bin/cluster_control -a | ' | ||
f'grep active | wc -l')) == 4 or time() > timeout: | ||
break | ||
HostMonitor(inventory_path=inventory_path, messages_path=messages_path, tmp_path=tmp_path).run() | ||
sleep(time_to_sync) | ||
|
||
|
||
def test_agent_files_deletion(register_agent): | ||
"""Check that when an agent is deleted, all its related files in managers are also removed.""" | ||
# Clean ossec.log and cluster.log | ||
host_manager.clear_file(host='wazuh-master', file_path=os.path.join(WAZUH_LOGS_PATH, 'ossec.log')) | ||
host_manager.clear_file(host='wazuh-master', file_path=os.path.join(WAZUH_LOGS_PATH, 'cluster.log')) | ||
host_manager.control_service(host='wazuh-master', service='wazuh', state="restarted") | ||
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") | ||
|
||
# Get the current ID and name of the agent that is reporting to worker_host. | ||
master_token = host_manager.get_api_token(master_host) | ||
|
@@ -70,22 +75,18 @@ def test_agent_files_deletion(register_agent): | |
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 exists in global.db | ||
# Check that agent information is in the wdb socket | ||
for host in managers_hosts: | ||
for query in db_queries: | ||
result = host_manager.run_command( | ||
host, | ||
f'sqlite3 {join(WAZUH_PATH, "queue", "db", "global.db")} "{query.format(id=agent_id, name=agent_name)}"' | ||
) | ||
assert result, f'This db query should have returned something in {host}, but it did not: ' \ | ||
f'{query.format(id=agent_id, name=agent_name)}' | ||
result = host_manager.run_command(host, f"/var/ossec/framework/python/bin/python3.9 /send_msg.py {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}' | ||
|
||
# Wait until information is synced to all workers | ||
sleep(time_to_sync) | ||
HostMonitor(inventory_path=inventory_path, messages_path=messages_path, tmp_path=tmp_path).run() | ||
|
||
# Check that agent-related files where removed from each node. | ||
for file in files: | ||
|
@@ -96,12 +97,8 @@ def test_agent_files_deletion(register_agent): | |
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 does not exist anymore in global.db | ||
# Check that agent information is not in the wdb socket | ||
for host in managers_hosts: | ||
for query in db_queries: | ||
result = host_manager.run_command( | ||
host, | ||
f'sqlite3 {join(WAZUH_PATH, "queue", "db", "global.db")} "{query.format(id=agent_id, name=agent_name)}"' | ||
) | ||
assert not result, f'This db query should have not returned anything in {host}, but it did: ' \ | ||
f'{query.format(id=agent_id, name=agent_name)} -> {result}' | ||
result = host_manager.run_command(host, | ||
f"/var/ossec/framework/python/bin/python3.9 /send_msg.py {agent_id}") | ||
assert not result, f'This db query should have not returned anything in {host}, but it did: {result}' |