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

Move the 'get_datetime_diff' function to 'wazuh-testing' utils module #2782

Merged
merged 2 commits into from
May 30, 2022
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
24 changes: 17 additions & 7 deletions deps/wazuh_testing/wazuh_testing/tools/utils.py
Original file line number Diff line number Diff line change
@@ -1,18 +1,17 @@
# Copyright (C) 2015-2021, Wazuh Inc.
# Copyright (C) 2015, Wazuh Inc.
# Created by Wazuh, Inc. <[email protected]>.
# This program is free software; you can redistribute it and/or modify it under the terms of GPLv2
import ipaddress
import json
import logging
import numbers
import re
import socket
import string
import numbers
from datetime import datetime
from functools import wraps
from random import randint, SystemRandom
from time import sleep
from random import randint, SystemRandom, choice
import string
import json
import socket
import ipaddress


def retry(exceptions, attempts=5, delay=1, delay_multiplier=2):
Expand Down Expand Up @@ -179,3 +178,14 @@ def format_ipv6_long(ipv6_address):
str: IPV6 long form
"""
return (ipaddress.ip_address(ipv6_address).exploded).upper()


def get_datetime_diff(phase_datetimes, date_format):
"""Calculate the difference between two datetimes.

Args:
phase_datetimes (list): List containing start and end datetimes.
juliamagan marked this conversation as resolved.
Show resolved Hide resolved
date_format (str): Expected datetime shape.
"""
return datetime.strptime(phase_datetimes[1], date_format) - datetime.strptime(phase_datetimes[0], date_format)

Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ resources:
workers:
max: 47.85
mean: 9.35
reg_cof: 0.06
reg_cof: 0.1
Copy link
Member

Choose a reason for hiding this comment

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

Why is this change needed in this PR?

Copy link
Contributor

Choose a reason for hiding this comment

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

The current thresholds are too small and strict, which causes most tests to be considered failed. It will be necessary to increase them little by little so that they only fail if an environment really has unusually high resource consumption.

FD:
master:
max: 160
Expand Down
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
# Copyright (C) 2015-2021, Wazuh Inc.
# Copyright (C) 2015, Wazuh Inc.
# Created by Wazuh, Inc. <[email protected]>.
# This program is free software; you can redistribute it and/or modify it under the terms of GPLv2

from datetime import datetime
from os import listdir
from os.path import join, dirname, realpath

import pytest
from yaml import safe_load

from wazuh_testing.tools.performance.csv_parser import ClusterCSVTasksParser, ClusterCSVResourcesParser, ClusterEnvInfo
from wazuh_testing.tools.utils import get_datetime_diff

test_data_path = join(dirname(realpath(__file__)), 'data')
configurations = {file.replace('_thresholds.yaml', ''): safe_load(open(join(test_data_path, file))) for file in
Expand All @@ -21,22 +21,12 @@
# Fixtures
@pytest.fixture()
def n_workers(pytestconfig):
return pytestconfig.getoption("n_workers")
return pytestconfig.getoption('n_workers')


@pytest.fixture()
def n_agents(pytestconfig):
return pytestconfig.getoption("n_agents")


# Functions
def get_datetime_diff(phase_datetimes):
"""Calculate the difference between two datetimes.

Args:
phase_datetimes (list): List containing start and end datetimes.
"""
return datetime.strptime(phase_datetimes[1], date_format) - datetime.strptime(phase_datetimes[0], date_format)
return pytestconfig.getoption('n_agents')


def test_cluster_performance(artifacts_path, n_workers, n_agents):
Expand Down Expand Up @@ -64,19 +54,19 @@ def test_cluster_performance(artifacts_path, n_workers, n_agents):
try:
cluster_info = ClusterEnvInfo(artifacts_path).get_all_info()
except FileNotFoundError:
pytest.fail(f'Path "{artifacts_path}" could not be found or it may not follow the proper structure.')
pytest.fail(f"Path '{artifacts_path}' could not be found or it may not follow the proper structure.")

if cluster_info.get('worker_nodes', 0) != int(n_workers):
pytest.fail(f'Information of {n_workers} workers was expected inside the artifacts folder, but '
f'{cluster_info.get("worker_nodes", 0)} were found.')
pytest.fail(f"Information of {n_workers} workers was expected inside the artifacts folder, but "
f"{cluster_info.get('worker_nodes', 0)} were found.")

# Calculate stats from data inside artifacts path.
data = {'tasks': ClusterCSVTasksParser(artifacts_path).get_stats(),
'resources': ClusterCSVResourcesParser(artifacts_path).get_stats()}

if not data['tasks'] or not data['resources']:
pytest.fail(f'Stats could not be retrieved, "{artifacts_path}" path may not exist, it is empty or it may not'
f' follow the proper structure.')
pytest.fail(f"Stats could not be retrieved, '{artifacts_path}' path may not exist, it is empty or it may not"
f" follow the proper structure.")

# Compare each stat with its threshold.
for data_name, data_stats in data.items():
Expand All @@ -92,16 +82,15 @@ def test_cluster_performance(artifacts_path, n_workers, n_agents):
'phase': phase})

try:
assert not exceeded_thresholds, f"Some thresholds were exceeded:\n- " + '\n- '.join(
assert not exceeded_thresholds, 'Some thresholds were exceeded:\n- ' + '\n- '.join(
'{stat} {column} {value} >= {threshold} ({node}, {file}, {phase})'.format(**item) for item in
exceeded_thresholds)
finally:
# Add useful information to report as stdout
try:
print('\n')
print(f'Setup phase took {get_datetime_diff(cluster_info["phases"]["setup_phase"])}s '
f'({cluster_info["phases"]["setup_phase"][0]} - {cluster_info["phases"]["setup_phase"][1]}).')
print(f'Stable phase took {get_datetime_diff(cluster_info["phases"]["stable_phase"])}s '
f'({cluster_info["phases"]["stable_phase"][0]} - {cluster_info["phases"]["stable_phase"][1]}).')
print(f"\nSetup phase took {get_datetime_diff(cluster_info['phases']['setup_phase'], date_format)}s "
f"({cluster_info['phases']['setup_phase'][0]} - {cluster_info['phases']['setup_phase'][1]}).")
print(f"Stable phase took {get_datetime_diff(cluster_info['phases']['stable_phase'], date_format)}s "
f"({cluster_info['phases']['stable_phase'][0]} - {cluster_info['phases']['stable_phase'][1]}).")
except KeyError:
print('No information available about test phases.')