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

DTT2 - Testing Module. Replace the use of assert statements with explicit exceptions in the helper function. #5294

Open
mhamra opened this issue Apr 25, 2024 · 0 comments

Comments

@mhamra
Copy link
Contributor

mhamra commented Apr 25, 2024

Target version Related issue Related PR/dev branch
4.9.0 4495-dtt1-release

The testing module raises exceptions using assert statements in helper functions. While using assert statements in tests is okay, checking and raising explicit exceptions in the library is better.

For example, in this method, I suggest raising a custom exception.

def register_agent(inventory_path, manager_path):
with open(manager_path, 'r') as yaml_file:
manager_path = yaml.safe_load(yaml_file)
host = manager_path.get('ansible_host')
internal_ip = HostInformation.get_internal_ip_from_aws_dns(host) if 'amazonaws' in host else host
commands = [
f"sed -i 's/<address>MANAGER_IP<\/address>/<address>{internal_ip}<\/address>/g' {WAZUH_CONF}",
"systemctl restart wazuh-agent"
]
Executor.execute_commands(inventory_path, commands)
assert internal_ip in Executor.execute_command(inventory_path, f'cat {WAZUH_CONF}'), logger.error(f'Error configuring the Manager IP ({internal_ip}) in: {HostInformation.get_os_name_and_version_from_inventory(inventory_path)} agent')

Creating custom testing module exceptions is a better solution to handle known error conditions. The pytest tests will receive these exceptions, making the test fail.

@rauldpm rauldpm changed the title DTT1-Testing Module. Replace the use of assert statements with explicit exceptions in the helper function. DTT2 - Testing Module. Replace the use of assert statements with explicit exceptions in the helper function. Apr 26, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant