Skip to content

Commit

Permalink
Changes after latest review by Snaow
Browse files Browse the repository at this point in the history
  • Loading branch information
yanazaeva committed Dec 30, 2021
1 parent f86d345 commit 1f9deb5
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 37 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
# Copyright (C) 2015-2021, Wazuh Inc.
# 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 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)
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,4 @@ wazuh-worker2:
wazuh-master:
- regex: '.*Worker wazuh-worker2.*Integrity check.*Finished in.*'
path: "/var/ossec/logs/cluster.log"
timeout: 60
timeout: 60
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand All @@ -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)
Expand All @@ -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']
Expand All @@ -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}'

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

0 comments on commit 1f9deb5

Please sign in to comment.