Skip to content

Commit

Permalink
Merge pull request #23204 from wazuh/fix/22891-ait-env
Browse files Browse the repository at this point in the history
AIT environment improvements
  • Loading branch information
Selutario committed May 8, 2024
2 parents b8e0a02 + 920c410 commit e259a7c
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 94 deletions.
112 changes: 51 additions & 61 deletions api/test/integration/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -80,90 +80,78 @@ def pytest_tavern_beta_before_every_test_run(test_dict, variables):
variables["test_login_token"] = get_token_login_api()


def build_and_up(env_mode: str, interval: int = 10, interval_build_env: int = 10,
build: bool = True) -> dict:
def build_and_up(env_mode: str, interval: int = 10, build: bool = True):
"""Build all Docker environments needed for the current test.
Parameters
----------
env_mode : str
Indicates the environment to be used in the process.
interval : int
Time interval between every healthcheck.
interval_build_env : int
Time interval between every docker environment healthcheck.
Time interval between every build.
build : bool
Flag to indicate if images need to be built.
Returns
-------
dict
Dict with healthchecks parameters.
"""
os.chdir(env_path)
values = {
'interval': interval,
'max_retries': 90,
'retries': 0
}
values_build_env = {
'interval': interval_build_env,
'max_retries': 3,
'retries': 0
}
# Get current branch or tag
with open('../../../../.git/HEAD', 'r') as f:
# Get current branch or tag
with open('../../../.git/HEAD', 'r') as f:
ref = f.readline().strip()

if ref.startswith("ref:"):
current_branch = ref.split("refs/heads/")[1]
else:
current_branch = ref.split("/")[-1]

os.makedirs(test_logs_path, exist_ok=True)

if build:
# Ping the current branch tarball used to build the manager image.
response = requests.get(f"https://github.com/wazuh/wazuh/tarball/{current_branch}")
if response.status_code == 404:
pytest.fail("Current branch tarball doesn't exist")
elif not response.ok:
pytest.fail(f"Couldn't obtain branch tarball: {response.reason}")

os.chdir(env_path)
max_retries = 3
retries = 0

with open(docker_log_path, mode='w') as f_docker:
while values_build_env['retries'] < values_build_env['max_retries']:
while retries < max_retries:
if build:
current_process = subprocess.Popen(["docker", "compose", "--profile", env_mode,
"build", "--build-arg", f"WAZUH_BRANCH={current_branch}",
"--build-arg", f"ENV_MODE={env_mode}",
"--no-cache"],
build_process = subprocess.Popen(["docker", "compose", "--profile", env_mode,
"build", "--build-arg", f"WAZUH_BRANCH={current_branch}",
"--build-arg", f"ENV_MODE={env_mode}", "--no-cache"],
stdout=f_docker, stderr=subprocess.STDOUT, universal_newlines=True)
current_process.wait()
current_process = subprocess.Popen(
build_process.wait()
up_process = subprocess.Popen(
["docker", "compose", "--profile", env_mode, "up", "-d"],
env=dict(os.environ, ENV_MODE=env_mode),
stdout=f_docker, stderr=subprocess.STDOUT, universal_newlines=True)
current_process.wait()
up_process.wait()

if current_process.returncode == 0:
time.sleep(values_build_env['interval'])
if up_process.returncode == 0:
break
else:
time.sleep(values_build_env['interval'])
values_build_env['retries'] += 1
os.chdir(current_path)

time.sleep(interval)
retries += 1

return values
os.chdir(current_path)


def down_env():
def down_env(env_mode: str):
"""Stop and remove all Docker containers."""
os.chdir(env_path)
with open(docker_log_path, mode='a') as f_docker:
current_process = subprocess.Popen(["docker", "compose", "down", "-t0" ], stdout=f_docker,
stderr=subprocess.STDOUT, universal_newlines=True)
current_process = subprocess.Popen(["docker", "compose", "--profile", env_mode, "down"],
stdout=f_docker, stderr=subprocess.STDOUT, universal_newlines=True)
current_process.wait()
os.chdir(current_path)


def check_health(interval: int = 10, node_type: str = 'manager', agents: list = None,
def check_health(node_type: str = 'manager', agents: list = None,
only_check_master_health: bool = False):
"""Check the Wazuh nodes health.
Parameters
----------
interval : int
Time interval between every healthcheck.
node_type : str
Can be agent, manager or nginx-lb.
agents : list
Expand All @@ -177,7 +165,6 @@ def check_health(interval: int = 10, node_type: str = 'manager', agents: list =
bool
True if all healthchecks passed, False otherwise.
"""
time.sleep(interval)
if node_type == 'manager':
nodes_to_check = ['master'] if only_check_master_health else env_cluster_nodes
for node in nodes_to_check:
Expand Down Expand Up @@ -368,7 +355,7 @@ def api_test(request: _pytest.fixtures.SubRequest):
Object that contains information about the current test
"""

def clean_up_env():
def clean_up_env(env_mode: str):
"""Clean temporary folder, save environment logs and status; and stop and remove all Docker containers."""
clean_tmp_folder()
if request.session.testsfailed > 0:
Expand All @@ -377,14 +364,15 @@ def clean_up_env():
# Get the environment current status
global environment_status
environment_status = get_health()
down_env()
down_env(env_mode)

# Get the value of the mark indicating the test mode. This value will vary between 'cluster' or 'standalone'
mode = request.node.config.getoption("-m")
env_mode = standalone_env_mode if mode == 'standalone' else cluster_env_mode
os.makedirs(test_logs_path, exist_ok=True)

# Add clean_up_env as fixture finalizer
request.addfinalizer(clean_up_env)
request.addfinalizer(lambda: clean_up_env(env_mode))

test_filename = request.node.config.args[0].split('_')
if 'rbac' in test_filename:
Expand All @@ -403,27 +391,29 @@ def clean_up_env():
enable_white_mode()

general_procedure(module)
values = build_and_up(interval=10, build=request.config.getoption('--nobuild'), env_mode=env_mode)
build_and_up(build=request.config.getoption('--nobuild'), env_mode=env_mode)

max_retries = 30
retries = 0

while retries < max_retries:
managers_health = check_health(only_check_master_health=env_mode == standalone_env_mode)
agents_health = check_health(node_type='agent', agents=list(range(1, 9)))
nginx_health = check_health(node_type='nginx-lb')

while values['retries'] < values['max_retries']:
managers_health = check_health(interval=values['interval'],
only_check_master_health=env_mode == standalone_env_mode)
agents_health = check_health(interval=values['interval'], node_type='agent', agents=list(range(1, 9)))
nginx_health = check_health(interval=values['interval'], node_type='nginx-lb')
# Check if entrypoint was successful
try:
error_message = subprocess.check_output(["docker", "exec", "-t",
"env-wazuh-master-1", "sh", "-c",
error_message = subprocess.check_output(["docker", "exec", "-t", "env-wazuh-master-1", "sh", "-c",
"cat /entrypoint_error"]).decode().strip()
pytest.fail(error_message)
except subprocess.CalledProcessError:
pass

if managers_health and agents_health and nginx_health:
time.sleep(values['interval'])
return
else:
values['retries'] += 1

retries += 1
time.sleep(10)


def get_health():
Expand Down
42 changes: 11 additions & 31 deletions api/test/integration/env/docker-compose.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
version: '3.7'

services:
wazuh-master:
profiles:
Expand All @@ -25,9 +23,6 @@ services:
wazuh-worker1:
profiles:
- cluster
build:
context: .
dockerfile: base/manager/manager.Dockerfile
image: integration_test_wazuh-manager
hostname: wazuh-worker1
volumes:
Expand All @@ -38,13 +33,12 @@ services:
- wazuh-master
- worker1
- worker
depends_on:
- wazuh-master

wazuh-worker2:
profiles:
- cluster
build:
context: .
dockerfile: base/manager/manager.Dockerfile
image: integration_test_wazuh-manager
hostname: wazuh-worker2
volumes:
Expand All @@ -55,6 +49,8 @@ services:
- wazuh-master
- worker2
- worker
depends_on:
- wazuh-master

wazuh-agent1:
profiles:
Expand All @@ -79,9 +75,6 @@ services:
profiles:
- standalone
- cluster
build:
context: .
dockerfile: base/agent/new.Dockerfile
image: integration_test_wazuh-agent
hostname: wazuh-agent2
volumes:
Expand All @@ -99,9 +92,6 @@ services:
profiles:
- standalone
- cluster
build:
context: .
dockerfile: base/agent/new.Dockerfile
image: integration_test_wazuh-agent
hostname: wazuh-agent3
volumes:
Expand All @@ -112,16 +102,13 @@ services:
- nginx-lb
- wazuh-agent3
depends_on:
- wazuh-agent2
- wazuh-agent1
- nginx-lb

wazuh-agent4:
profiles:
- standalone
- cluster
build:
context: .
dockerfile: base/agent/new.Dockerfile
image: integration_test_wazuh-agent
hostname: wazuh-agent4
volumes:
Expand All @@ -132,7 +119,7 @@ services:
- nginx-lb
- wazuh-agent4
depends_on:
- wazuh-agent3
- wazuh-agent1
- nginx-lb

wazuh-agent5:
Expand All @@ -153,16 +140,12 @@ services:
- wazuh-agent5
- agent_old
depends_on:
- wazuh-agent4
- nginx-lb

wazuh-agent6:
profiles:
- standalone
- cluster
build:
context: .
dockerfile: base/agent/old.Dockerfile
image: integration_test_wazuh-agent_old
hostname: wazuh-agent6
volumes:
Expand All @@ -181,9 +164,6 @@ services:
profiles:
- standalone
- cluster
build:
context: .
dockerfile: base/agent/old.Dockerfile
image: integration_test_wazuh-agent_old
hostname: wazuh-agent7
volumes:
Expand All @@ -195,16 +175,13 @@ services:
- wazuh-agent7
- agent_old
depends_on:
- wazuh-agent6
- wazuh-agent5
- nginx-lb

wazuh-agent8:
profiles:
- standalone
- cluster
build:
context: .
dockerfile: base/agent/old.Dockerfile
image: integration_test_wazuh-agent_old
hostname: wazuh-agent8
volumes:
Expand All @@ -216,7 +193,7 @@ services:
- wazuh-agent8
- agent_old
depends_on:
- wazuh-agent7
- wazuh-agent5
- nginx-lb

nginx-lb:
Expand All @@ -226,6 +203,9 @@ services:
build:
context: ./base/nginx-lb
image: integration_test_nginx-lb
restart: always
entrypoint:
- /scripts/entrypoint.sh
- ${ENV_MODE}
depends_on:
- wazuh-master
5 changes: 3 additions & 2 deletions api/test/integration/test_agent_DELETE_endpoints.tavern.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -815,7 +815,7 @@ stages:
delay_after: !float "{global_db_delay}"

# DELETE /agents
- name: Delete all agents (006,007,008)
- name: Delete all agents
request:
verify: False
url: "{protocol:s}://{host:s}:{port:d}/agents"
Expand All @@ -835,4 +835,5 @@ stages:
- "006"
- "007"
- "008"
total_affected_items: 3
- "013"
total_affected_items: 4

0 comments on commit e259a7c

Please sign in to comment.