From 3aa5a25a6f5b5223944e36d1975d4e4306efa4b2 Mon Sep 17 00:00:00 2001 From: camila Date: Thu, 24 Feb 2022 13:20:45 -0300 Subject: [PATCH 01/11] Add: add cases to check sync_status --- .../data/sync_agent_groups_get.yaml | 15 ++++ .../test_sync_agent_groups_get.py | 70 +++++++++++++++++++ 2 files changed, 85 insertions(+) create mode 100644 tests/integration/test_wazuh_db/test_sync_agent_groups_get/data/sync_agent_groups_get.yaml create mode 100644 tests/integration/test_wazuh_db/test_sync_agent_groups_get/test_sync_agent_groups_get.py diff --git a/tests/integration/test_wazuh_db/test_sync_agent_groups_get/data/sync_agent_groups_get.yaml b/tests/integration/test_wazuh_db/test_sync_agent_groups_get/data/sync_agent_groups_get.yaml new file mode 100644 index 0000000000..f3a83aa1ea --- /dev/null +++ b/tests/integration/test_wazuh_db/test_sync_agent_groups_get/data/sync_agent_groups_get.yaml @@ -0,0 +1,15 @@ +--- +- + name: "Test sync_status with response" + test_case: + - + input: 'global sync-agent-groups-get {"condition":"sync_status"}' + output: '[{"data":[{"id":1,"groups":["Test_group1"]},{"id":2,"groups":["Test_group2"]}]}]' + +- + name: "Test sync_status without response" + test_case: + - + pre_input: 'global sql UPDATE agent SET group_sync_status="synced"' + input: 'global sync-agent-groups-get {"condition":"sync_status"}' + output: "[{'data': []}]" diff --git a/tests/integration/test_wazuh_db/test_sync_agent_groups_get/test_sync_agent_groups_get.py b/tests/integration/test_wazuh_db/test_sync_agent_groups_get/test_sync_agent_groups_get.py new file mode 100644 index 0000000000..9a8fbbdf20 --- /dev/null +++ b/tests/integration/test_wazuh_db/test_sync_agent_groups_get/test_sync_agent_groups_get.py @@ -0,0 +1,70 @@ + +import os +import time +import pytest +import yaml +from wazuh_testing.tools import WAZUH_PATH +from wazuh_testing.wazuh_db import query_wdb +from wazuh_testing.tools.services import delete_dbs + +# Marks +pytestmark = [pytest.mark.linux, pytest.mark.tier(level=0), pytest.mark.server] + +# Configurations +test_data_path = os.path.join(os.path.dirname(os.path.realpath(__file__)), 'data') +messages_file = os.path.join(test_data_path, 'sync_agent_groups_get.yaml') +module_tests = [] +with open(messages_file) as f: + module_tests.append((yaml.safe_load(f), messages_file.split('_')[0])) + +log_monitor_paths = [] +wdb_path = os.path.join(os.path.join(WAZUH_PATH, 'queue', 'db', 'wdb')) +receiver_sockets_params = [(wdb_path, 'AF_UNIX', 'TCP')] +monitored_sockets_params = [('wazuh-db', None, True)] +receiver_sockets= None # Set in the fixtures +agents = ['agent1', 'agent2'] + + +#Fixtures +@pytest.fixture(scope='module') +def remove_database(request): + yield + delete_dbs() + + +@pytest.fixture(scope='module') +def pre_insert_agents(): + for i in range(len(agents)): + id = i + 1 + name = 'Agent-test' + str(id) + date = time.time() + command = f'global insert-agent {{"id":{id},"name":"{name}","date_add":{date}}}' + results = query_wdb(command) + assert results == 'ok' + + command = f'global set-agent-groups {{"mode":"append","sync_status":"syncreq","source":"remote","data":[{{"id":{id},"groups":["Test_group{id}"]}}]}}' + results = query_wdb(command) + assert results == 'ok' + + +# Tests +@pytest.mark.parametrize('test_case', + [case['test_case'] for module_data in module_tests for case in module_data[0]], + ids=[f"{module_name}: {case['name']}" + for module_data, module_name in module_tests + for case in module_data] + ) +def test_set_agent_groups(remove_database, configure_sockets_environment, connect_to_sockets_module, test_case, pre_insert_agents): + + case_data = test_case[0] + output = case_data["output"] + + if 'pre_input' in case_data: + query_wdb(case_data['pre_input']) + + response = query_wdb(case_data["input"]) + + # validate response + assert str(response) == output + + \ No newline at end of file From f701b42944c7d88365f91e1ff3bada0945bf8366 Mon Sep 17 00:00:00 2001 From: camila Date: Fri, 25 Feb 2022 13:30:17 -0300 Subject: [PATCH 02/11] Add: add new test cases to check sync_agent_groups --- deps/wazuh_testing/wazuh_testing/wazuh_db.py | 41 ++++- .../data/sync_agent_groups_get.yaml | 156 +++++++++++++++++- .../test_sync_agent_groups_get.py | 115 ++++++++++--- 3 files changed, 284 insertions(+), 28 deletions(-) diff --git a/deps/wazuh_testing/wazuh_testing/wazuh_db.py b/deps/wazuh_testing/wazuh_testing/wazuh_db.py index 8cc4846d03..551ebca571 100644 --- a/deps/wazuh_testing/wazuh_testing/wazuh_db.py +++ b/deps/wazuh_testing/wazuh_testing/wazuh_db.py @@ -1,4 +1,4 @@ -# Copyright (C) 2015-2021, Wazuh Inc. +# Copyright (C) 2015-2022, Wazuh Inc. # Created by Wazuh, Inc. . # This program is free software; you can redistribute it and/or modify it under the terms of GPLv2 import functools @@ -6,11 +6,13 @@ import logging import socket import sqlite3 +import time from wazuh_testing.tools import GLOBAL_DB_PATH, WAZUH_DB_SOCKET_PATH from wazuh_testing.tools.monitoring import wazuh_pack, wazuh_unpack from wazuh_testing.tools.services import control_service + def callback_wazuhdb_response(item): if isinstance(item, tuple): data, response = item @@ -167,6 +169,28 @@ def clean_agents_from_db(): raise Exception('Unable to clean agents') +def clean_groups_from_db(): + """ + Clean groups from DB + """ + command = 'global sql DELETE FROM "group"' + try: + query_wdb(command) + except Exception: + raise Exception('Unable to clean groups') + + +def clean_belongs(): + """ + Clean belong DB + """ + command = 'global sql DELETE FROM belongs' + try: + query_wdb(command) + except Exception: + raise Exception('Unable to clean belongs') + + def insert_agent_in_db(id=1, name='TestAgent', ip='any', registration_time=0, connection_status=0, disconnection_time=0): """ @@ -180,3 +204,18 @@ def insert_agent_in_db(id=1, name='TestAgent', ip='any', registration_time=0, co query_wdb(update_command) except Exception: raise Exception(f"Unable to add agent {id}") + + +def insert_agent_into_group(amount): + for i in range(amount): + id = i + 1 + name = 'Agent-test' + str(id) + date = time.time() + command = f'global insert-agent {{"id":{id},"name":"{name}","date_add":{date}}}' + results = query_wdb(command) + assert results == 'ok' + + command = f'''global set-agent-groups {{"mode":"append","sync_status":"syncreq", + "source":"remote","data":[{{"id":{id},"groups":["Test_group{id}"]}}]}}''' + results = query_wdb(command) + assert results == 'ok' diff --git a/tests/integration/test_wazuh_db/test_sync_agent_groups_get/data/sync_agent_groups_get.yaml b/tests/integration/test_wazuh_db/test_sync_agent_groups_get/data/sync_agent_groups_get.yaml index f3a83aa1ea..69356be682 100644 --- a/tests/integration/test_wazuh_db/test_sync_agent_groups_get/data/sync_agent_groups_get.yaml +++ b/tests/integration/test_wazuh_db/test_sync_agent_groups_get/data/sync_agent_groups_get.yaml @@ -4,7 +4,7 @@ test_case: - input: 'global sync-agent-groups-get {"condition":"sync_status"}' - output: '[{"data":[{"id":1,"groups":["Test_group1"]},{"id":2,"groups":["Test_group2"]}]}]' + output: "[{'data': [{'id': 1, 'groups': ['Test_group1']}, {'id': 2, 'groups': ['Test_group2']}]}]" - name: "Test sync_status without response" @@ -13,3 +13,157 @@ pre_input: 'global sql UPDATE agent SET group_sync_status="synced"' input: 'global sync-agent-groups-get {"condition":"sync_status"}' output: "[{'data': []}]" + +- + name: "Test 'all' condition when agent groups are in 'sync_req'" + test_case: + - + input: 'global sync-agent-groups-get {"condition":"all"}' + output: "[{'data': [{'id': 1, 'groups': ['Test_group1']}, {'id': 2, 'groups': ['Test_group2']}]}]" + +- + name: "Test 'all' condition when agent groups are in 'synced'" + test_case: + - + pre_input: 'global sql UPDATE agent SET group_sync_status="synced"' + input: 'global sync-agent-groups-get {"condition":"all"}' + output: "[{'data': [{'id': 1, 'groups': ['Test_group1']}, {'id': 2, 'groups': ['Test_group2']}]}]" + +- + name: "Test 'sync_status' condition when one agent groups are in 'synced'" + test_case: + - + pre_input: 'global sql UPDATE agent SET group_sync_status="synced" WHERE id = 2' + input: 'global sync-agent-groups-get {"condition":"sync_status"}' + output: "[{'data': [{'id': 1, 'groups': ['Test_group1']}]}]" + +- + name: "Test 'all' condition when one agent groups are in 'synced'" + test_case: + - + pre_input: 'global sql UPDATE agent SET group_sync_status="synced" WHERE id = 2' + input: 'global sync-agent-groups-get {"condition":"all"}' + output: "[{'data': [{'id': 1, 'groups': ['Test_group1']}, {'id': 2, 'groups': ['Test_group2']}]}]" + +- + name: "Test with and invalid filter in condition" + test_case: + - + input: 'global sync-agent-groups-get {"condition":"testinvalid"}' + output: 'err Could not obtain a response from wdb_global_sync_agent_groups_get' + +- + name: "Test without condition" + test_case: + - + input: 'global sync-agent-groups-get {"last_id":0}' + output: 'err Invalid JSON data, missing required fields' + +- + name: "Test set_synced in True" + test_case: + - + input: 'global sync-agent-groups-get {"condition":"sync_status", "set_synced":true}' + output: "[{'data': [{'id': 1, 'groups': ['Test_group1']}, {'id': 2, 'groups': ['Test_group2']}]}]" + new_status: "synced" + agent_id: "[1,2]" + +- + name: "Test set_synced with invalidad value" + test_case: + - + input: 'global sync-agent-groups-get {"condition":"sync_status", "set_synced":false}' + output: "[{'data': [{'id': 1, 'groups': ['Test_group1']}, {'id': 2, 'groups': ['Test_group2']}]}]" + new_status: "syncreq" + agent_id: "[1,2]" + +- + name: "Test set_synced with invalidad value" + test_case: + - + input: 'global sync-agent-groups-get {"condition":"sync_status", "set_synced":"set"}' + output: "[{'data': [{'id': 1, 'groups': ['Test_group1']}, {'id': 2, 'groups': ['Test_group2']}]}]" + new_status: "syncreq" + agent_id: "[1,2]" + +- + name: "Test get_global_hash in true" + test_case: + - + pre_input: 'global sql UPDATE agent SET group_hash = "DUMMY"' + input: 'global sync-agent-groups-get {"last_id":0, "condition":"sync_status", "get_global_hash":true}' + output: "[{'data': [{'id': 1, 'groups': ['Test_group1']}, {'id': 2, 'groups': ['Test_group2']}], 'hash': '49087946dd7a587ae30ae89cbc8084cad2cb0bfd'}]" + +- + name: "Test get_global_hash in false" + test_case: + - + pre_input: 'global sql UPDATE agent SET group_hash = "DUMMY"' + input: 'global sync-agent-groups-get {"last_id":0, "condition":"sync_status", "get_global_hash":false}' + output: "[{'data': [{'id': 1, 'groups': ['Test_group1']}, {'id': 2, 'groups': ['Test_group2']}]}]" + +- + name: "Test get_global_hash with invalid value" + test_case: + - + pre_input: 'global sql UPDATE agent SET group_hash = "DUMMY"' + input: 'global sync-agent-groups-get {"last_id":0, "condition":"sync_status", "get_global_hash":"set"}' + output: "[{'data': [{'id': 1, 'groups': ['Test_group1']}, {'id': 2, 'groups': ['Test_group2']}]}]" + +- + name: "Test 'agent_registration_delta' in and sync_status" + test_case: + - + input: 'global sync-agent-groups-get {"condition":"sync_status", "agent_registration_delta":0}' + output: "[{'data': [{'id': 1, 'groups': ['Test_group1']}, {'id': 2, 'groups': ['Test_group2']}]}]" + +- + name: "Test 'agent_registration_delta' with delta in 10000" + test_case: + - + input: 'global sync-agent-groups-get {"condition":"sync_status", "agent_registration_delta":10000}' + output: "[{'data': []}]" + +- + name: "Test 'agent_registration_delta' with delta in 10000" + test_case: + - + pre_input: 'global insert-agent {"id":4,"name":"Agent-test4","date_add":10}' + input: 'global sync-agent-groups-get {"condition":"sync_status", "agent_registration_delta":10000}' + output: "[{'data': [{'id': 4, 'groups': ['default']}]}]" + +- + name: "Test 'agent_registration_delta' in 0 and all condition " + test_case: + - pre_input: 'global insert-agent {"id":4,"name":"Agent-test4","date_add":10}' + input: 'global sync-agent-groups-get {"condition":"all", "agent_registration_delta":0}' + output: "[{'data': [{'id': 1, 'groups': ['Test_group1']}, {'id': 2, 'groups': ['Test_group2']}]}]" + +- + name: "Test 'agent_registration_delta' in 0 and all condition " + test_case: + - pre_input: 'global insert-agent {"id":4,"name":"Agent-test4","date_add":10}' + input: 'global sync-agent-groups-get {"condition":"all", "agent_registration_delta":10000}' + output: "[{'data': [{'id': 4, 'groups': ['default']}]}]" + +- + name: "Test last_id - by default" + test_case: + - + input: 'global sync-agent-groups-get {"last_id":0, "condition":"sync_status"}' + output: "[{'data': [{'id': 1, 'groups': ['Test_group1']}, {'id': 2, 'groups': ['Test_group2']}]}]" + +- + name: "Test last_id - obtain from second group" + test_case: + - + input: 'global sync-agent-groups-get {"last_id":1, "condition":"sync_status"}' + output: "[{'data': [{'id': 2, 'groups': ['Test_group2']}]}]" + +- + name: "Test last_id - with not exist id" + test_case: + - + pre_input: 'global sql UPDATE agent SET group_hash = "DUMMY"' + input: 'global sync-agent-groups-get {"last_id":3, "condition":"sync_status"}' + output: "[{'data': []}]" diff --git a/tests/integration/test_wazuh_db/test_sync_agent_groups_get/test_sync_agent_groups_get.py b/tests/integration/test_wazuh_db/test_sync_agent_groups_get/test_sync_agent_groups_get.py index 9a8fbbdf20..8ecbb44fb4 100644 --- a/tests/integration/test_wazuh_db/test_sync_agent_groups_get/test_sync_agent_groups_get.py +++ b/tests/integration/test_wazuh_db/test_sync_agent_groups_get/test_sync_agent_groups_get.py @@ -1,11 +1,54 @@ - +''' +copyright: Copyright (C) 2015-2022, Wazuh Inc. + Created by Wazuh, Inc. . + This program is free software; you can redistribute it and/or modify it under the terms of GPLv2 +type: integration +brief: Wazuh-db is the daemon in charge of the databases with all the Wazuh persistent information, exposing a socket + to receive requests and provide information. The Wazuh core uses list-based databases to store information + related to agent keys, and FIM/Rootcheck event data. + This test checks the usage of the sync-agent-groups-get command used to allow the cluster getting the + information to be synchronized.. +tier: 0 +modules: + - wazuh_db +components: + - manager +daemons: + - wazuh-db +os_platform: + - linux +os_version: + - Arch Linux + - Amazon Linux 2 + - Amazon Linux 1 + - CentOS 8 + - CentOS 7 + - CentOS 6 + - Ubuntu Focal + - Ubuntu Bionic + - Ubuntu Xenial + - Ubuntu Trusty + - Debian Buster + - Debian Stretch + - Debian Jessie + - Debian Wheezy + - Red Hat 8 + - Red Hat 7 + - Red Hat 6 +references: + - https://documentation.wazuh.com/current/user-manual/reference/daemons/wazuh-db.html +tags: + - wazuh_db +''' import os import time import pytest import yaml +import json from wazuh_testing.tools import WAZUH_PATH -from wazuh_testing.wazuh_db import query_wdb -from wazuh_testing.tools.services import delete_dbs +from wazuh_testing.wazuh_db import (query_wdb, insert_agent_into_group, clean_agents_from_db, + clean_groups_from_db, clean_belongs) + # Marks pytestmark = [pytest.mark.linux, pytest.mark.tier(level=0), pytest.mark.server] @@ -21,30 +64,19 @@ wdb_path = os.path.join(os.path.join(WAZUH_PATH, 'queue', 'db', 'wdb')) receiver_sockets_params = [(wdb_path, 'AF_UNIX', 'TCP')] monitored_sockets_params = [('wazuh-db', None, True)] -receiver_sockets= None # Set in the fixtures +receiver_sockets = None # Set in the fixtures agents = ['agent1', 'agent2'] -#Fixtures -@pytest.fixture(scope='module') -def remove_database(request): - yield - delete_dbs() - - -@pytest.fixture(scope='module') +# Fixtures +@pytest.fixture(scope='function') def pre_insert_agents(): - for i in range(len(agents)): - id = i + 1 - name = 'Agent-test' + str(id) - date = time.time() - command = f'global insert-agent {{"id":{id},"name":"{name}","date_add":{date}}}' - results = query_wdb(command) - assert results == 'ok' + insert_agent_into_group(2) - command = f'global set-agent-groups {{"mode":"append","sync_status":"syncreq","source":"remote","data":[{{"id":{id},"groups":["Test_group{id}"]}}]}}' - results = query_wdb(command) - assert results == 'ok' + yield + clean_agents_from_db() + clean_groups_from_db() + clean_belongs() # Tests @@ -54,17 +86,48 @@ def pre_insert_agents(): for module_data, module_name in module_tests for case in module_data] ) -def test_set_agent_groups(remove_database, configure_sockets_environment, connect_to_sockets_module, test_case, pre_insert_agents): - +def test_sync_agent_groups(configure_sockets_environment, connect_to_sockets_module, test_case, pre_insert_agents): + ''' + description: Check that commands about sync_aget_groups_get works properly. + wazuh_min_version: 4.4.0 + parameters: + - configure_sockets_environment: + type: fixture + brief: Configure environment for sockets and MITM. + - connect_to_sockets_module: + type: fixture + brief: Module scope version of 'connect_to_sockets' fixture. + - test_case: + type: fixture + brief: List of test_case stages (dicts with input, output and agent_id and expected_groups keys). + - pre_insert_agents: + type: fixture + brief: fixture in charge of insert agents and groups into DB. + assertions: + - Verify that the socket response matches the expected output. + input_description: + - Test cases are defined in the sync_agent_groups_get.yaml file. + expected_output: + - an array with all the agents that match with the search criteria + tags: + - wazuh_db + - wdb_socket + ''' case_data = test_case[0] output = case_data["output"] if 'pre_input' in case_data: query_wdb(case_data['pre_input']) + time.sleep(1) response = query_wdb(case_data["input"]) - + # validate response assert str(response) == output - \ No newline at end of file + # validate if the status of the group has change + if "new_status" in case_data: + agent_id = json.loads(case_data["agent_id"]) + for id in agent_id: + response = query_wdb(f'global get-agent-info {id}') + assert case_data["new_status"] == response[0]['group_sync_status'] From 3268e62a8c7590b34865187c847b8ac079b4a677 Mon Sep 17 00:00:00 2001 From: camila Date: Sat, 26 Feb 2022 00:53:06 -0300 Subject: [PATCH 03/11] Add: add invalid test cases --- .../data/sync_agent_groups_get.yaml | 34 +++++++++---------- 1 file changed, 17 insertions(+), 17 deletions(-) diff --git a/tests/integration/test_wazuh_db/test_sync_agent_groups_get/data/sync_agent_groups_get.yaml b/tests/integration/test_wazuh_db/test_sync_agent_groups_get/data/sync_agent_groups_get.yaml index 69356be682..913b97f9c5 100644 --- a/tests/integration/test_wazuh_db/test_sync_agent_groups_get/data/sync_agent_groups_get.yaml +++ b/tests/integration/test_wazuh_db/test_sync_agent_groups_get/data/sync_agent_groups_get.yaml @@ -111,38 +111,32 @@ output: "[{'data': [{'id': 1, 'groups': ['Test_group1']}, {'id': 2, 'groups': ['Test_group2']}]}]" - - name: "Test 'agent_registration_delta' in and sync_status" + name: "Test 'agent_registration_delta' in 0 and sync_status" test_case: - input: 'global sync-agent-groups-get {"condition":"sync_status", "agent_registration_delta":0}' output: "[{'data': [{'id': 1, 'groups': ['Test_group1']}, {'id': 2, 'groups': ['Test_group2']}]}]" - - name: "Test 'agent_registration_delta' with delta in 10000" + name: "Test 'agent_registration_delta' in 0 and all condition " test_case: - - input: 'global sync-agent-groups-get {"condition":"sync_status", "agent_registration_delta":10000}' - output: "[{'data': []}]" + input: 'global sync-agent-groups-get {"condition":"all", "agent_registration_delta":0}' + output: "[{'data': [{'id': 1, 'groups': ['Test_group1']}, {'id': 2, 'groups': ['Test_group2']}]}]" - - name: "Test 'agent_registration_delta' with delta in 10000" + name: "Test 'agent_registration_delta' with delta in 10000 and sync_status" test_case: - - pre_input: 'global insert-agent {"id":4,"name":"Agent-test4","date_add":10}' + pre_input: 'global insert-agent {"id":4,"name":"Agent-test4","date_add":1545753642}' input: 'global sync-agent-groups-get {"condition":"sync_status", "agent_registration_delta":10000}' output: "[{'data': [{'id': 4, 'groups': ['default']}]}]" - - name: "Test 'agent_registration_delta' in 0 and all condition " - test_case: - - pre_input: 'global insert-agent {"id":4,"name":"Agent-test4","date_add":10}' - input: 'global sync-agent-groups-get {"condition":"all", "agent_registration_delta":0}' - output: "[{'data': [{'id': 1, 'groups': ['Test_group1']}, {'id': 2, 'groups': ['Test_group2']}]}]" - -- - name: "Test 'agent_registration_delta' in 0 and all condition " + name: "Test 'agent_registration_delta' with delta in 10000 and all" test_case: - - pre_input: 'global insert-agent {"id":4,"name":"Agent-test4","date_add":10}' + - + pre_input: 'global insert-agent {"id":4,"name":"Agent-test4","date_add":1545753642}' input: 'global sync-agent-groups-get {"condition":"all", "agent_registration_delta":10000}' output: "[{'data': [{'id': 4, 'groups': ['default']}]}]" @@ -164,6 +158,12 @@ name: "Test last_id - with not exist id" test_case: - - pre_input: 'global sql UPDATE agent SET group_hash = "DUMMY"' - input: 'global sync-agent-groups-get {"last_id":3, "condition":"sync_status"}' + input: 'global sync-agent-groups-get {"last_id":50, "condition":"sync_status"}' output: "[{'data': []}]" + +- + name: "Test last_id - with negative value" + test_case: + - + input: 'global sync-agent-groups-get {"last_id":-3, "condition":"sync_status"}' + output: "[{'data': [{'id': 1, 'groups': ['Test_group1']}, {'id': 2, 'groups': ['Test_group2']}]}]" \ No newline at end of file From 19e96b29094e0e60da2d23d0ca8d17d291e17672 Mon Sep 17 00:00:00 2001 From: camila Date: Wed, 2 Mar 2022 15:34:11 -0300 Subject: [PATCH 04/11] Rf: Refactor test to accept more than one preinput --- .../data/sync_agent_groups_get.yaml | 32 ++++++++++--------- .../test_sync_agent_groups_get.py | 6 ++-- 2 files changed, 21 insertions(+), 17 deletions(-) diff --git a/tests/integration/test_wazuh_db/test_sync_agent_groups_get/data/sync_agent_groups_get.yaml b/tests/integration/test_wazuh_db/test_sync_agent_groups_get/data/sync_agent_groups_get.yaml index 913b97f9c5..bbef48a54a 100644 --- a/tests/integration/test_wazuh_db/test_sync_agent_groups_get/data/sync_agent_groups_get.yaml +++ b/tests/integration/test_wazuh_db/test_sync_agent_groups_get/data/sync_agent_groups_get.yaml @@ -10,7 +10,7 @@ name: "Test sync_status without response" test_case: - - pre_input: 'global sql UPDATE agent SET group_sync_status="synced"' + pre_input: ['global sql UPDATE agent SET group_sync_status="synced"'] input: 'global sync-agent-groups-get {"condition":"sync_status"}' output: "[{'data': []}]" @@ -25,7 +25,7 @@ name: "Test 'all' condition when agent groups are in 'synced'" test_case: - - pre_input: 'global sql UPDATE agent SET group_sync_status="synced"' + pre_input: ['global sql UPDATE agent SET group_sync_status="synced"'] input: 'global sync-agent-groups-get {"condition":"all"}' output: "[{'data': [{'id': 1, 'groups': ['Test_group1']}, {'id': 2, 'groups': ['Test_group2']}]}]" @@ -33,7 +33,7 @@ name: "Test 'sync_status' condition when one agent groups are in 'synced'" test_case: - - pre_input: 'global sql UPDATE agent SET group_sync_status="synced" WHERE id = 2' + pre_input: ['global sql UPDATE agent SET group_sync_status="synced" WHERE id = 2'] input: 'global sync-agent-groups-get {"condition":"sync_status"}' output: "[{'data': [{'id': 1, 'groups': ['Test_group1']}]}]" @@ -41,7 +41,7 @@ name: "Test 'all' condition when one agent groups are in 'synced'" test_case: - - pre_input: 'global sql UPDATE agent SET group_sync_status="synced" WHERE id = 2' + pre_input: ['global sql UPDATE agent SET group_sync_status="synced" WHERE id = 2'] input: 'global sync-agent-groups-get {"condition":"all"}' output: "[{'data': [{'id': 1, 'groups': ['Test_group1']}, {'id': 2, 'groups': ['Test_group2']}]}]" @@ -90,7 +90,7 @@ name: "Test get_global_hash in true" test_case: - - pre_input: 'global sql UPDATE agent SET group_hash = "DUMMY"' + pre_input: ['global sql UPDATE agent SET group_hash = "DUMMY"'] input: 'global sync-agent-groups-get {"last_id":0, "condition":"sync_status", "get_global_hash":true}' output: "[{'data': [{'id': 1, 'groups': ['Test_group1']}, {'id': 2, 'groups': ['Test_group2']}], 'hash': '49087946dd7a587ae30ae89cbc8084cad2cb0bfd'}]" @@ -98,7 +98,7 @@ name: "Test get_global_hash in false" test_case: - - pre_input: 'global sql UPDATE agent SET group_hash = "DUMMY"' + pre_input: ['global sql UPDATE agent SET group_hash = "DUMMY"'] input: 'global sync-agent-groups-get {"last_id":0, "condition":"sync_status", "get_global_hash":false}' output: "[{'data': [{'id': 1, 'groups': ['Test_group1']}, {'id': 2, 'groups': ['Test_group2']}]}]" @@ -106,7 +106,7 @@ name: "Test get_global_hash with invalid value" test_case: - - pre_input: 'global sql UPDATE agent SET group_hash = "DUMMY"' + pre_input: ['global sql UPDATE agent SET group_hash = "DUMMY"'] input: 'global sync-agent-groups-get {"last_id":0, "condition":"sync_status", "get_global_hash":"set"}' output: "[{'data': [{'id': 1, 'groups': ['Test_group1']}, {'id': 2, 'groups': ['Test_group2']}]}]" @@ -127,18 +127,20 @@ - name: "Test 'agent_registration_delta' with delta in 10000 and sync_status" test_case: - - - pre_input: 'global insert-agent {"id":4,"name":"Agent-test4","date_add":1545753642}' + - + pre_input: ['global insert-agent {"id":5,"name":"Agent-test5","date_add":1545753642}', 'global set-agent-groups {"mode":"append","sync_status":"syncreq", + "source":"remote","data":[{"id":5,"groups":["Test_group5"]}]}'] input: 'global sync-agent-groups-get {"condition":"sync_status", "agent_registration_delta":10000}' - output: "[{'data': [{'id': 4, 'groups': ['default']}]}]" + output: "[{'data': [{'id': 5, 'groups': ['Test_group5']}]}]" - name: "Test 'agent_registration_delta' with delta in 10000 and all" test_case: - - pre_input: 'global insert-agent {"id":4,"name":"Agent-test4","date_add":1545753642}' + pre_input: ['global insert-agent {"id":6,"name":"Agent-test6","date_add":1545753642}', 'global set-agent-groups {"mode":"append","sync_status":"syncreq", + "source":"remote","data":[{"id":6,"groups":["Test_group6"]}]}'] input: 'global sync-agent-groups-get {"condition":"all", "agent_registration_delta":10000}' - output: "[{'data': [{'id': 4, 'groups': ['default']}]}]" + output: "[{'data': [{'id': 6, 'groups': ['Test_group6']}]}]" - name: "Test last_id - by default" @@ -158,12 +160,12 @@ name: "Test last_id - with not exist id" test_case: - - input: 'global sync-agent-groups-get {"last_id":50, "condition":"sync_status"}' + input: 'global sync-agent-groups-get {"last_id":3, "condition":"sync_status"}' output: "[{'data': []}]" - name: "Test last_id - with negative value" test_case: - - input: 'global sync-agent-groups-get {"last_id":-3, "condition":"sync_status"}' - output: "[{'data': [{'id': 1, 'groups': ['Test_group1']}, {'id': 2, 'groups': ['Test_group2']}]}]" \ No newline at end of file + input: 'global sync-agent-groups-get {"last_id":50, "condition":"sync_status"}' + output: "[{'data': []}]" diff --git a/tests/integration/test_wazuh_db/test_sync_agent_groups_get/test_sync_agent_groups_get.py b/tests/integration/test_wazuh_db/test_sync_agent_groups_get/test_sync_agent_groups_get.py index 8ecbb44fb4..cb84b3752d 100644 --- a/tests/integration/test_wazuh_db/test_sync_agent_groups_get/test_sync_agent_groups_get.py +++ b/tests/integration/test_wazuh_db/test_sync_agent_groups_get/test_sync_agent_groups_get.py @@ -117,8 +117,10 @@ def test_sync_agent_groups(configure_sockets_environment, connect_to_sockets_mod output = case_data["output"] if 'pre_input' in case_data: - query_wdb(case_data['pre_input']) - + for command in case_data['pre_input']: + query_wdb(command) + results = query_wdb(command) + time.sleep(1) response = query_wdb(case_data["input"]) From 899d96bc00176a0353033d9e9baa55b5dce34a98 Mon Sep 17 00:00:00 2001 From: camila Date: Wed, 2 Mar 2022 16:13:34 -0300 Subject: [PATCH 05/11] rf: Applied new structure --- CHANGELOG.md | 1 + .../data => data/global}/sync_agent_groups_get.yaml | 0 .../test_sync_agent_groups_get.py | 2 +- 3 files changed, 2 insertions(+), 1 deletion(-) rename tests/integration/test_wazuh_db/{test_sync_agent_groups_get/data => data/global}/sync_agent_groups_get.yaml (100%) rename tests/integration/test_wazuh_db/{test_sync_agent_groups_get => }/test_sync_agent_groups_get.py (97%) diff --git a/CHANGELOG.md b/CHANGELOG.md index 2444ef14ff..c74877710a 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -9,6 +9,7 @@ Release report: TBD ### Added +- Test `sync-agens-groups-get` WDB command ([#26026](https://github.com/wazuh/wazuh-qa/pull/2626) - Test `get-groups-integrity` WDB command ([#2607](https://github.com/wazuh/wazuh-qa/pull/2607)) - Test `set-agent-groups` WDB command ([#2602](https://github.com/wazuh/wazuh-qa/pull/2602)) - Add test fim with file currently open ([#2300](https://github.com/wazuh/wazuh-qa/pull/2300)) diff --git a/tests/integration/test_wazuh_db/test_sync_agent_groups_get/data/sync_agent_groups_get.yaml b/tests/integration/test_wazuh_db/data/global/sync_agent_groups_get.yaml similarity index 100% rename from tests/integration/test_wazuh_db/test_sync_agent_groups_get/data/sync_agent_groups_get.yaml rename to tests/integration/test_wazuh_db/data/global/sync_agent_groups_get.yaml diff --git a/tests/integration/test_wazuh_db/test_sync_agent_groups_get/test_sync_agent_groups_get.py b/tests/integration/test_wazuh_db/test_sync_agent_groups_get.py similarity index 97% rename from tests/integration/test_wazuh_db/test_sync_agent_groups_get/test_sync_agent_groups_get.py rename to tests/integration/test_wazuh_db/test_sync_agent_groups_get.py index cb84b3752d..32d6fd1af6 100644 --- a/tests/integration/test_wazuh_db/test_sync_agent_groups_get/test_sync_agent_groups_get.py +++ b/tests/integration/test_wazuh_db/test_sync_agent_groups_get.py @@ -55,7 +55,7 @@ # Configurations test_data_path = os.path.join(os.path.dirname(os.path.realpath(__file__)), 'data') -messages_file = os.path.join(test_data_path, 'sync_agent_groups_get.yaml') +messages_file = os.path.join(os.path.join(test_data_path, 'global'), 'sync_agent_groups_get.yaml') module_tests = [] with open(messages_file) as f: module_tests.append((yaml.safe_load(f), messages_file.split('_')[0])) From bd02bce662d4dccf3d76a245e2a5cab3741db710 Mon Sep 17 00:00:00 2001 From: camila Date: Wed, 2 Mar 2022 16:14:57 -0300 Subject: [PATCH 06/11] fix typo --- CHANGELOG.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index c74877710a..90ef42e490 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -9,7 +9,7 @@ Release report: TBD ### Added -- Test `sync-agens-groups-get` WDB command ([#26026](https://github.com/wazuh/wazuh-qa/pull/2626) +- Test `sync-agens-groups-get` WDB command ([#2626](https://github.com/wazuh/wazuh-qa/pull/2626) - Test `get-groups-integrity` WDB command ([#2607](https://github.com/wazuh/wazuh-qa/pull/2607)) - Test `set-agent-groups` WDB command ([#2602](https://github.com/wazuh/wazuh-qa/pull/2602)) - Add test fim with file currently open ([#2300](https://github.com/wazuh/wazuh-qa/pull/2300)) From bbbb7e51fd2c5805da833cf2e63a0f18b79e75c5 Mon Sep 17 00:00:00 2001 From: camila Date: Fri, 4 Mar 2022 10:47:47 -0300 Subject: [PATCH 07/11] Rf: Apply changes in comments --- deps/wazuh_testing/wazuh_testing/wazuh_db.py | 13 ++++++------ .../test_sync_agent_groups_get.py | 21 ++++++++++--------- 2 files changed, 18 insertions(+), 16 deletions(-) diff --git a/deps/wazuh_testing/wazuh_testing/wazuh_db.py b/deps/wazuh_testing/wazuh_testing/wazuh_db.py index 82a0cfede5..b25671d78b 100644 --- a/deps/wazuh_testing/wazuh_testing/wazuh_db.py +++ b/deps/wazuh_testing/wazuh_testing/wazuh_db.py @@ -171,24 +171,24 @@ def clean_agents_from_db(): def clean_groups_from_db(): """ - Clean groups from DB + Clean groups table from global.db """ command = 'global sql DELETE FROM "group"' try: query_wdb(command) except Exception: - raise Exception('Unable to clean groups') + raise Exception('Unable to clean groups table.') def clean_belongs(): """ - Clean belong DB + Clean belong table from global.db """ command = 'global sql DELETE FROM belongs' try: query_wdb(command) except Exception: - raise Exception('Unable to clean belongs') + raise Exception('Unable to clean belongs table.') def insert_agent_in_db(id=1, name='TestAgent', ip='any', registration_time=0, connection_status="never_connected", @@ -206,8 +206,9 @@ def insert_agent_in_db(id=1, name='TestAgent', ip='any', registration_time=0, co raise Exception(f"Unable to add agent {id}") -def insert_agent_into_group(amount): - for i in range(amount): +# Insert agents into DB and assign them into a group +def insert_agent_into_group(total_agents): + for i in range(total_agents): id = i + 1 name = 'Agent-test' + str(id) date = time.time() diff --git a/tests/integration/test_wazuh_db/test_sync_agent_groups_get.py b/tests/integration/test_wazuh_db/test_sync_agent_groups_get.py index 32d6fd1af6..310197219a 100644 --- a/tests/integration/test_wazuh_db/test_sync_agent_groups_get.py +++ b/tests/integration/test_wazuh_db/test_sync_agent_groups_get.py @@ -48,7 +48,7 @@ from wazuh_testing.tools import WAZUH_PATH from wazuh_testing.wazuh_db import (query_wdb, insert_agent_into_group, clean_agents_from_db, clean_groups_from_db, clean_belongs) - +from wazuh_testing.tools.file import get_list_of_content_yml # Marks pytestmark = [pytest.mark.linux, pytest.mark.tier(level=0), pytest.mark.server] @@ -56,21 +56,20 @@ # Configurations test_data_path = os.path.join(os.path.dirname(os.path.realpath(__file__)), 'data') messages_file = os.path.join(os.path.join(test_data_path, 'global'), 'sync_agent_groups_get.yaml') -module_tests = [] -with open(messages_file) as f: - module_tests.append((yaml.safe_load(f), messages_file.split('_')[0])) +module_tests = get_list_of_content_yml(messages_file, ".split('_')[0]") log_monitor_paths = [] wdb_path = os.path.join(os.path.join(WAZUH_PATH, 'queue', 'db', 'wdb')) receiver_sockets_params = [(wdb_path, 'AF_UNIX', 'TCP')] monitored_sockets_params = [('wazuh-db', None, True)] receiver_sockets = None # Set in the fixtures -agents = ['agent1', 'agent2'] # Fixtures + +# Insert agents into DB and assign them into a group @pytest.fixture(scope='function') -def pre_insert_agents(): +def pre_insert_agents_into_group(): insert_agent_into_group(2) yield @@ -86,7 +85,7 @@ def pre_insert_agents(): for module_data, module_name in module_tests for case in module_data] ) -def test_sync_agent_groups(configure_sockets_environment, connect_to_sockets_module, test_case, pre_insert_agents): +def test_sync_agent_groups(configure_sockets_environment, connect_to_sockets_module, test_case, pre_insert_agents_into_group): ''' description: Check that commands about sync_aget_groups_get works properly. wazuh_min_version: 4.4.0 @@ -100,7 +99,7 @@ def test_sync_agent_groups(configure_sockets_environment, connect_to_sockets_mod - test_case: type: fixture brief: List of test_case stages (dicts with input, output and agent_id and expected_groups keys). - - pre_insert_agents: + - pre_insert_agents_into_group: type: fixture brief: fixture in charge of insert agents and groups into DB. assertions: @@ -113,9 +112,11 @@ def test_sync_agent_groups(configure_sockets_environment, connect_to_sockets_mod - wazuh_db - wdb_socket ''' + # Set each case case_data = test_case[0] output = case_data["output"] + # Check if it requires any special configuration if 'pre_input' in case_data: for command in case_data['pre_input']: query_wdb(command) @@ -124,10 +125,10 @@ def test_sync_agent_groups(configure_sockets_environment, connect_to_sockets_mod time.sleep(1) response = query_wdb(case_data["input"]) - # validate response + # Validate response assert str(response) == output - # validate if the status of the group has change + # Validate if the status of the group has change if "new_status" in case_data: agent_id = json.loads(case_data["agent_id"]) for id in agent_id: From 7cf071bc621fe62508f0ca6bf2f2a69810217571 Mon Sep 17 00:00:00 2001 From: camila Date: Fri, 4 Mar 2022 11:54:29 -0300 Subject: [PATCH 08/11] Rf: Modify test cases --- .../data/global/sync_agent_groups_get.yaml | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/tests/integration/test_wazuh_db/data/global/sync_agent_groups_get.yaml b/tests/integration/test_wazuh_db/data/global/sync_agent_groups_get.yaml index bbef48a54a..f5fd694d12 100644 --- a/tests/integration/test_wazuh_db/data/global/sync_agent_groups_get.yaml +++ b/tests/integration/test_wazuh_db/data/global/sync_agent_groups_get.yaml @@ -57,7 +57,7 @@ test_case: - input: 'global sync-agent-groups-get {"last_id":0}' - output: 'err Invalid JSON data, missing required fields' + output: "err Invalid JSON data, missing required 'condition' field" - name: "Test set_synced in True" @@ -69,7 +69,7 @@ agent_id: "[1,2]" - - name: "Test set_synced with invalidad value" + name: "Test set_synced with invalid value - false" test_case: - input: 'global sync-agent-groups-get {"condition":"sync_status", "set_synced":false}' @@ -78,11 +78,11 @@ agent_id: "[1,2]" - - name: "Test set_synced with invalidad value" + name: "Test set_synced with invalid value - String" test_case: - input: 'global sync-agent-groups-get {"condition":"sync_status", "set_synced":"set"}' - output: "[{'data': [{'id': 1, 'groups': ['Test_group1']}, {'id': 2, 'groups': ['Test_group2']}]}]" + output: "err Invalid JSON data, invalid alternative fields data type" new_status: "syncreq" agent_id: "[1,2]" @@ -108,7 +108,7 @@ - pre_input: ['global sql UPDATE agent SET group_hash = "DUMMY"'] input: 'global sync-agent-groups-get {"last_id":0, "condition":"sync_status", "get_global_hash":"set"}' - output: "[{'data': [{'id': 1, 'groups': ['Test_group1']}, {'id': 2, 'groups': ['Test_group2']}]}]" + output: "err Invalid JSON data, invalid alternative fields data type" - name: "Test 'agent_registration_delta' in 0 and sync_status" @@ -167,5 +167,5 @@ name: "Test last_id - with negative value" test_case: - - input: 'global sync-agent-groups-get {"last_id":50, "condition":"sync_status"}' - output: "[{'data': []}]" + input: 'global sync-agent-groups-get {"last_id":-3, "condition":"sync_status"}' + output: "[{'data': [{'id': 1, 'groups': ['Test_group1']}, {'id': 2, 'groups': ['Test_group2']}]}]" From 2c5809c6489a8f09191b84a5d45dff0b3ac89131 Mon Sep 17 00:00:00 2001 From: camila Date: Mon, 7 Mar 2022 08:24:28 -0300 Subject: [PATCH 09/11] Add: add new blank lines --- tests/integration/test_wazuh_db/test_sync_agent_groups_get.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/tests/integration/test_wazuh_db/test_sync_agent_groups_get.py b/tests/integration/test_wazuh_db/test_sync_agent_groups_get.py index 310197219a..edc4dbedca 100644 --- a/tests/integration/test_wazuh_db/test_sync_agent_groups_get.py +++ b/tests/integration/test_wazuh_db/test_sync_agent_groups_get.py @@ -41,10 +41,11 @@ - wazuh_db ''' import os + import time import pytest -import yaml import json + from wazuh_testing.tools import WAZUH_PATH from wazuh_testing.wazuh_db import (query_wdb, insert_agent_into_group, clean_agents_from_db, clean_groups_from_db, clean_belongs) From aa3490433086795891eb1e22b27d58912006f9c5 Mon Sep 17 00:00:00 2001 From: camila Date: Mon, 7 Mar 2022 08:30:14 -0300 Subject: [PATCH 10/11] Rf: changes in pytest marks --- tests/integration/test_wazuh_db/test_sync_agent_groups_get.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/integration/test_wazuh_db/test_sync_agent_groups_get.py b/tests/integration/test_wazuh_db/test_sync_agent_groups_get.py index edc4dbedca..bbf66ab3cd 100644 --- a/tests/integration/test_wazuh_db/test_sync_agent_groups_get.py +++ b/tests/integration/test_wazuh_db/test_sync_agent_groups_get.py @@ -50,9 +50,10 @@ from wazuh_testing.wazuh_db import (query_wdb, insert_agent_into_group, clean_agents_from_db, clean_groups_from_db, clean_belongs) from wazuh_testing.tools.file import get_list_of_content_yml +from wazuh_testing.modules import TIER0, SERVER, LINUX # Marks -pytestmark = [pytest.mark.linux, pytest.mark.tier(level=0), pytest.mark.server] +pytestmark = [LINUX, TIER0, SERVER] # Configurations test_data_path = os.path.join(os.path.dirname(os.path.realpath(__file__)), 'data') @@ -121,7 +122,6 @@ def test_sync_agent_groups(configure_sockets_environment, connect_to_sockets_mod if 'pre_input' in case_data: for command in case_data['pre_input']: query_wdb(command) - results = query_wdb(command) time.sleep(1) response = query_wdb(case_data["input"]) From bfcc353f8fbdfcfb949a4e8ab25eddcf0a82cd59 Mon Sep 17 00:00:00 2001 From: camila Date: Mon, 7 Mar 2022 08:32:07 -0300 Subject: [PATCH 11/11] rf: Changes to addapt pip8 style --- .../integration/test_wazuh_db/test_sync_agent_groups_get.py | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/tests/integration/test_wazuh_db/test_sync_agent_groups_get.py b/tests/integration/test_wazuh_db/test_sync_agent_groups_get.py index bbf66ab3cd..fb206e9164 100644 --- a/tests/integration/test_wazuh_db/test_sync_agent_groups_get.py +++ b/tests/integration/test_wazuh_db/test_sync_agent_groups_get.py @@ -87,7 +87,8 @@ def pre_insert_agents_into_group(): for module_data, module_name in module_tests for case in module_data] ) -def test_sync_agent_groups(configure_sockets_environment, connect_to_sockets_module, test_case, pre_insert_agents_into_group): +def test_sync_agent_groups(configure_sockets_environment, connect_to_sockets_module, + test_case, pre_insert_agents_into_group): ''' description: Check that commands about sync_aget_groups_get works properly. wazuh_min_version: 4.4.0 @@ -122,7 +123,7 @@ def test_sync_agent_groups(configure_sockets_environment, connect_to_sockets_mod if 'pre_input' in case_data: for command in case_data['pre_input']: query_wdb(command) - + time.sleep(1) response = query_wdb(case_data["input"])