Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Full Yellow Local - Fix WazuhDB tests self-configuration in 4.2 #1643

Merged
merged 7 commits into from
Aug 17, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 28 additions & 0 deletions deps/wazuh_testing/wazuh_testing/tools/wazuh_manager.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import os
import subprocess
from wazuh_testing.tools import WAZUH_PATH
from wazuh_testing.wazuh_db import query_wdb


def list_agents_ids():
wazuhdb_result = query_wdb('global get-all-agents last_id -1')
list_agents = [agent['id'] for agent in wazuhdb_result if not (0 == agent.get('id'))]

return list_agents


def remove_agents(agents_id, remove_type='wazuhdb'):
if remove_type not in ['wazuhdb', 'manage_agents']:
raise ValueError("Invalid type of agent removal: %s" % remove_type)

if agents_id:
for agent_id in agents_id:
if remove_type == 'manage_agents':
subprocess.call([f"{WAZUH_PATH}/bin/manage_agents", "-r", f"{agent_id}"], stdout=open(os.devnull, "w"),
stderr=subprocess.STDOUT)
elif remove_type == 'wazuhdb':
result = query_wdb(f"global delete-agent {str(agent_id)}")


def remove_all_agents(remove_type):
remove_agents(list_agents_ids(), remove_type)
21 changes: 16 additions & 5 deletions tests/integration/test_wazuh_db/test_wazuh_db.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
import pytest
import yaml
from wazuh_testing.tools import WAZUH_PATH
from wazuh_testing.tools.services import control_service
from wazuh_testing.tools.wazuh_manager import remove_all_agents

# Marks

Expand Down Expand Up @@ -56,7 +58,16 @@ def regex_match(regex, string):
return re.match(regex, string)


Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
# Fixtures

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This comment follows the file standard, however, maybe they are not very useful. It should be necessary to standardize all tests structure and commentaries with the rest of the team in the future.

# Tests
@pytest.fixture(scope="module")
def clean_registered_agents():
remove_all_agents('wazuhdb')
time.sleep(5)


@pytest.fixture(scope="module")
def restart_wazuh():
control_service('restart')


@pytest.fixture(scope="function")
def pre_insert_agents():
Expand All @@ -82,7 +93,7 @@ def pre_insert_agents():
for module_data, module_name in module_tests
for case in module_data]
)
def test_wazuh_db_messages(configure_sockets_environment, connect_to_sockets_module, test_case: list):
def test_wazuh_db_messages(restart_wazuh, clean_registered_agents, configure_sockets_environment, connect_to_sockets_module, test_case):
"""Check that every input message in wazuh-db socket generates the adequate output to wazuh-db socket

Parameters
Expand All @@ -106,19 +117,19 @@ def test_wazuh_db_messages(configure_sockets_environment, connect_to_sockets_mod
.format(index + 1, stage['stage'], expected, response)


def test_wazuh_db_create_agent(configure_sockets_environment, connect_to_sockets_module):
def test_wazuh_db_create_agent(restart_wazuh, clean_registered_agents, configure_sockets_environment, connect_to_sockets_module):
"""Check that Wazuh DB creates the agent database when a query with a new agent ID is sent"""
test = {"name": "Create agent",
"description": "Wazuh DB creates automatically the agent's database the first time a query with a new agent"
" ID reaches it. Once the database is created, the query is processed as expected.",
"test_case": [{"input": "agent 999 syscheck integrity_check_left",
"output": "err Invalid FIM query syntax, near 'integrity_check_left'",
"stage": "Syscheck - Agent does not exits yet"}]}
test_wazuh_db_messages(configure_sockets_environment, connect_to_sockets_module, test['test_case'])
test_wazuh_db_messages(clean_registered_agents, restart_wazuh, configure_sockets_environment, connect_to_sockets_module, test['test_case'])
assert os.path.exists(os.path.join(WAZUH_PATH, 'queue', 'db', "999.db"))


def test_wazuh_db_chunks(configure_sockets_environment, connect_to_sockets_module, pre_insert_agents):
def test_wazuh_db_chunks(restart_wazuh, clean_registered_agents, configure_sockets_environment, connect_to_sockets_module, pre_insert_agents):
"""Check that commands by chunks work properly when agents amount exceed the response maximum size"""

def send_chunk_command(command):
Expand Down