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

Add multigroups tests cases for test_assign_groups_guess #3979

Merged
merged 14 commits into from
Mar 7, 2023
Merged
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ Release report: TBD

### Added

- Add multigroups tests cases for `test_assign_groups_guess` ([#3979](https://github.com/wazuh/wazuh-qa/pull/3979)) \- (Tests)
- Fix test_agent_groups system test ([#3955](https://github.com/wazuh/wazuh-qa/pull/3964)) \- (Tests)
- Add new group_hash case and update the `without condition` case output in `wazuh_db/sync_agent_groups_get` ([#3959](https://github.com/wazuh/wazuh-qa/pull/3959)) \- (Tests)
- Add markers for each system test environment ([#3961](https://github.com/wazuh/wazuh-qa/pull/3961)) \- (Framework + Tests)
Expand Down
18 changes: 17 additions & 1 deletion deps/wazuh_testing/wazuh_testing/tools/system.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
import testinfra
import yaml

from wazuh_testing.tools import WAZUH_CONF, WAZUH_API_CONF, API_LOG_FILE_PATH
from wazuh_testing.tools import WAZUH_CONF, WAZUH_API_CONF, API_LOG_FILE_PATH, WAZUH_LOCAL_INTERNAL_OPTIONS
from wazuh_testing.tools.configuration import set_section_wazuh_conf


Expand Down Expand Up @@ -322,6 +322,22 @@ def get_stats(self, host: str, path: str):
"""
return self.get_host(host).ansible("stat", f"path={path}")

def configure_local_internal_options(self, local_internal_options: dict):
"""Add internal options in local_internal_options.conf

Args:
local_internal_options (dict): dictionary with hosts and internal options.
"""
for target_host in local_internal_options:
internal_options_data = []
backup_local_internal_options = self.get_file_content(target_host, WAZUH_LOCAL_INTERNAL_OPTIONS)
for internal_options in local_internal_options[target_host]:
internal_options_data.append(f"{internal_options['name']}={internal_options['value']}\n")
replace = backup_local_internal_options
for internal_option in internal_options_data:
replace = replace + internal_option
self.modify_file_content(target_host, WAZUH_LOCAL_INTERNAL_OPTIONS, replace)


def clean_environment(host_manager, target_files):
"""Clears a series of files on target hosts managed by a host manager
Expand Down
23 changes: 12 additions & 11 deletions tests/system/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,16 +46,21 @@ def clean_cluster_logs(hosts_list, host_manager):
host_manager.clear_file_without_recreate(host=host, file_path=CLUSTER_LOGS_PATH)


def remove_cluster_agents(wazuh_master, agents_list, host_manager):
def remove_cluster_agents(wazuh_master, agents_list, host_manager, agents_id=None):
# Removes a list of agents from the cluster using manage_agents
agent_id = get_agent_id(host_manager)
for agent in agents_list:
host_manager.control_service(host=agent, service='wazuh', state="stopped")
host_manager.control_service(host=agent, service='wazuh', state='stopped')
host_manager.clear_file(agent, file_path=os.path.join(WAZUH_PATH, 'etc', 'client.keys'))
while (agent_id != ''):
host_manager.get_host(wazuh_master).ansible("command", f'{WAZUH_PATH}/bin/manage_agents -r {agent_id}',
check=False)
agent_id = get_agent_id(host_manager)
if agents_id is None:
id = get_agent_id(host_manager)
while id != '':
host_manager.get_host(wazuh_master).ansible('command', f"{WAZUH_PATH}/bin/manage_agents -r {id}",
check=False)
id = get_agent_id(host_manager)
else:
for id in agents_id:
host_manager.get_host(wazuh_master).ansible('command', f"{WAZUH_PATH}/bin/manage_agents -r {id}",
check=False)


def get_agents_in_cluster(host, host_manager):
Expand All @@ -73,11 +78,7 @@ def create_new_agent_group(host, group_name, host_manager):
host_manager.run_command(host, f"/var/ossec/bin/agent_groups -q -a -g {group_name}")


# Create new group and assing agent
def assign_agent_to_new_group(host, id_group, id_agent, host_manager):
# Create group
host_manager.run_command(host, f"/var/ossec/bin/agent_groups -q -a -g {id_group}")

# Add agent to a group
host_manager.run_command(host, f"/var/ossec/bin/agent_groups -q -a -i {id_agent} -g {id_group}")

Expand Down
37 changes: 20 additions & 17 deletions tests/system/provisioning/enrollment_cluster/destroy.yml
Original file line number Diff line number Diff line change
@@ -1,19 +1,22 @@
---
- hosts: localhost
tasks:
- docker_container:
name: wazuh-master
state: absent
force_kill: yes
- docker_container:
name: wazuh-worker1
state: absent
force_kill: yes
- docker_container:
name: wazuh-worker2
state: absent
force_kill: yes
- docker_container:
name: wazuh-agent1
state: absent
force_kill: yes
- docker_container:
name: wazuh-master
state: absent
force_kill: true
- docker_container:
name: wazuh-worker1
state: absent
force_kill: true
- docker_container:
name: wazuh-worker2
state: absent
force_kill: true
- docker_container:
name: wazuh-agent1
state: absent
force_kill: true
- docker_container:
name: wazuh-agent2
state: absent
force_kill: true
6 changes: 6 additions & 0 deletions tests/system/provisioning/enrollment_cluster/inventory.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@ all:
wazuh-agent1:
ansible_connection: docker
ansible_python_interpreter: python
wazuh-agent2:
ansible_connection: docker
ansible_python_interpreter: python
managers:
hosts:
wazuh-master:
Expand Down Expand Up @@ -41,3 +44,6 @@ agents:
wazuh-agent1:
ansible_connection: docker
ansible_python_interpreter: python
wazuh-agent2:
ansible_connection: docker
ansible_python_interpreter: python
22 changes: 22 additions & 0 deletions tests/system/provisioning/enrollment_cluster/playbook.yml
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,18 @@
networks:
- name: "{{ docker_network }}"

- name: Create our container (Agent2)
hosts: localhost
vars_files:
- ./vars/configurations.yaml
tasks:
- docker_container:
name: "{{ agent2_hostname }}"
image: "{{ image }}"
hostname: "{{ agent2_hostname }}"
networks:
- name: "{{ docker_network }}"

- name: Wazuh Master
hosts: wazuh-master
vars:
Expand Down Expand Up @@ -85,3 +97,13 @@
- ./vars/configurations.yaml
roles:
- name: roles/agent-role

- name: Wazuh Agent2
hosts: wazuh-agent2
vars:
manager_hostname: wazuh-master
agent_hostname: "{{ agent2_hostname }}"
vars_files:
- ./vars/configurations.yaml
roles:
- name: roles/agent-role
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ worker1_hostname: wazuh-worker1
worker2_hostname: wazuh-worker2

agent1_hostname: wazuh-agent1
agent2_hostname: wazuh-agent2

docker_network: cluster_net

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@
from system.test_cluster.test_agent_groups.common import register_agent
from system import (AGENT_NO_GROUPS, AGENT_STATUS_ACTIVE, AGENT_STATUS_DISCONNECTED, check_agent_groups,
check_agent_status, restart_cluster, check_keys_file, assign_agent_to_new_group,
delete_group_of_agents, ERR_MSG_CLIENT_KEYS_IN_MASTER_NOT_FOUND)
delete_group_of_agents, ERR_MSG_CLIENT_KEYS_IN_MASTER_NOT_FOUND, create_new_agent_group)
from wazuh_testing.modules import WAZUH_SERVICE_PREFIX, WAZUH_SERVICES_STOPPED
from wazuh_testing.tools.system import HostManager

Expand Down Expand Up @@ -116,6 +116,9 @@ def test_assign_agent_to_a_group(agent_target, initial_status, clean_environment
check_agent_status(agent_id, agent_name, agent_ip, AGENT_STATUS_DISCONNECTED, host_manager, test_infra_managers)

try:
# Create new group
create_new_agent_group(test_infra_managers[0], test_group, host_manager)

# Add agent to a new group
assign_agent_to_new_group('wazuh-master', test_group, agent_id, host_manager)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,8 @@
import pytest

from system.test_cluster.test_agent_groups.common import register_agent
from system import (check_agent_groups, check_agent_status, check_keys_file, delete_group_of_agents,
assign_agent_to_new_group, AGENT_NO_GROUPS, AGENT_STATUS_NEVER_CONNECTED)
from system import (check_agent_groups, check_agent_status, check_keys_file, create_new_agent_group,
delete_group_of_agents, assign_agent_to_new_group, AGENT_NO_GROUPS, AGENT_STATUS_NEVER_CONNECTED)
from wazuh_testing.tools.system import HostManager


Expand Down Expand Up @@ -105,6 +105,9 @@ def test_assign_agent_to_a_group_by_tool(agent_target, clean_environment):
check_agent_groups(agent_id, AGENT_NO_GROUPS, test_infra_managers, host_manager)

try:
# Create group
create_new_agent_group(test_infra_managers[0], group_id, host_manager)

# Add group to agent1
assign_agent_to_new_group(test_infra_managers[0], group_id, agent_id, host_manager)

Expand Down
Loading