Skip to content

Commit

Permalink
Remove AnsibleUtil and AnsibleConstants
Browse files Browse the repository at this point in the history
  • Loading branch information
jakubrak committed Oct 22, 2024
1 parent 69f82dd commit 270aca1
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 33 deletions.
6 changes: 0 additions & 6 deletions roles/oneagent/tests/ansible/util.py

This file was deleted.

6 changes: 0 additions & 6 deletions roles/oneagent/tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
from pathlib import Path

import ansible.constants as AnsibleConstants
import ansible.util as AnsibleUtil

from command.platform_command_wrapper import PlatformCommandWrapper
from ansible.config import AnsibleConfig
Expand All @@ -26,7 +25,6 @@
USER_KEY = "user"
PASS_KEY = "password"

UTIL_KEY = "util"
RUNNER_KEY = "runner"
WRAPPER_KEY = "wrapper"
CONSTANTS_KEY = "constants"
Expand Down Expand Up @@ -188,7 +186,6 @@ def pytest_generate_tests(metafunc) -> None:
wrapper = PlatformCommandWrapper(user, password)
configurator = AnsibleConfig(user, password, platforms)
runner = AnsibleRunner(user, password)
util = AnsibleUtil
constants = AnsibleConstants

if CONFIGURATOR_KEY in metafunc.fixturenames:
Expand All @@ -197,9 +194,6 @@ def pytest_generate_tests(metafunc) -> None:
if RUNNER_KEY in metafunc.fixturenames:
metafunc.parametrize(RUNNER_KEY, [runner])

if UTIL_KEY in metafunc.fixturenames:
metafunc.parametrize(UTIL_KEY, [util])

if CONSTANTS_KEY in metafunc.fixturenames:
metafunc.parametrize(CONSTANTS_KEY, [constants])

Expand Down
47 changes: 26 additions & 21 deletions roles/oneagent/tests/test_resilience.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@

import pytest

from ansible.constants import TEST_SIGNATURE_FILE
from ansible.constants import TEST_SIGNATURE_FILE, ERROR_MESSAGES_FILE, FAILED_DEPLOYMENT_EXIT_CODE, VARIABLE_PREFIX
from util.common_utils import read_yaml_file
from util.test_data_types import DeploymentResult
from util.test_helpers import run_deployment, set_installer_download_params, enable_for_system_family
from util.constants.common_constants import InstallerVersion
Expand All @@ -21,19 +22,23 @@
VERSION_LOWER_THAN_INSTALLED_KEY = "version_lower_than_installed"


def _parse_error_messages_file() -> dict[str, str]:
return read_yaml_file(ERROR_MESSAGES_FILE)


def _prepare_test_data(constants, data: dict[str, str]) -> dict[str, str]:
parsed_data = {}
for key, value in data.items():
key = key.strip().removeprefix(constants.VARIABLE_PREFIX)
key = key.strip().removeprefix(VARIABLE_PREFIX)
value = value.strip().strip('"')
value = value.replace("(", "\\(").replace(")", "\\)")
parsed_data[key] = re.sub("%\\w", ".*", value)
return parsed_data


@pytest.fixture
def _error_messages(util, constants) -> dict[str, str]:
return _prepare_test_data(constants, util.parse_error_messages_file())
def _error_messages(constants) -> dict[str, str]:
return _prepare_test_data(constants, _parse_error_messages_file())


def _check_deployment_failure(results: DeploymentResult, expected_message: str, expected_code: int) -> None:
Expand All @@ -46,7 +51,7 @@ def _check_deployment_failure(results: DeploymentResult, expected_message: str,
assert re.search(expected_message, result.stdout + result.stderr)


def test_invalid_required_parameters(_error_messages, runner, configurator, constants):
def test_invalid_required_parameters(_error_messages, runner, configurator):
logging.info("Running missing required parameters test")

logging.debug("Removing required parameter - direct download scenario")
Expand All @@ -56,35 +61,35 @@ def test_invalid_required_parameters(_error_messages, runner, configurator, cons
_check_deployment_failure(
run_deployment(runner, True),
_error_messages[MISSING_REQUIRED_PARAMETERS_KEY],
constants.FAILED_DEPLOYMENT_EXIT_CODE,
FAILED_DEPLOYMENT_EXIT_CODE,
)


def test_invalid_architecture(_error_messages, runner, configurator, constants):
def test_invalid_architecture(_error_messages, runner, configurator):
logging.info("Running invalid architecture test")

set_installer_download_params(configurator)
configurator.set_common_parameter(configurator.INSTALLER_ARCH_KEY, "unknown_arch")

_check_deployment_failure(
run_deployment(runner, True), _error_messages[UNKNOWN_ARCHITECTURE_KEY], constants.FAILED_DEPLOYMENT_EXIT_CODE
run_deployment(runner, True), _error_messages[UNKNOWN_ARCHITECTURE_KEY], FAILED_DEPLOYMENT_EXIT_CODE
)


def test_missing_local_installer(_error_messages, runner, configurator, constants):
def test_missing_local_installer(_error_messages, runner, configurator):
logging.info("Running missing local installer test")

configurator.set_common_parameter(configurator.LOCAL_INSTALLER_KEY, "non_existing_installer")

_check_deployment_failure(
run_deployment(runner, True),
_error_messages[LOCAL_INSTALLER_NOT_AVAILABLE_KEY],
constants.FAILED_DEPLOYMENT_EXIT_CODE,
FAILED_DEPLOYMENT_EXIT_CODE,
)


@enable_for_system_family(family="unix")
def test_directories_contain_spaces(_error_messages, runner, configurator, constants):
def test_directories_contain_spaces(_error_messages, runner, configurator):
logging.info("Running directories contain spaces test")

logging.debug("Space in directory path - INSTALL_PATH scenario")
Expand All @@ -95,7 +100,7 @@ def test_directories_contain_spaces(_error_messages, runner, configurator, const
_check_deployment_failure(
run_deployment(runner, True),
_error_messages[INSTALL_DIR_CONTAINS_SPACES_KEY],
constants.FAILED_DEPLOYMENT_EXIT_CODE,
FAILED_DEPLOYMENT_EXIT_CODE,
)

logging.debug("Space in directory path - download dir scenario")
Expand All @@ -106,11 +111,11 @@ def test_directories_contain_spaces(_error_messages, runner, configurator, const
_check_deployment_failure(
run_deployment(runner, True),
_error_messages[DOWNLOAD_DIR_CONTAINS_SPACES_KEY],
constants.FAILED_DEPLOYMENT_EXIT_CODE,
FAILED_DEPLOYMENT_EXIT_CODE,
)


def test_version_parameter_too_low(_error_messages, runner, configurator, constants):
def test_version_parameter_too_low(_error_messages, runner, configurator):
logging.info("Running version parameter too low test")

set_installer_download_params(configurator)
Expand All @@ -119,36 +124,36 @@ def test_version_parameter_too_low(_error_messages, runner, configurator, consta
_check_deployment_failure(
run_deployment(runner, True),
_error_messages[VERSION_PARAMETER_TOO_LOW_KEY],
constants.FAILED_DEPLOYMENT_EXIT_CODE,
FAILED_DEPLOYMENT_EXIT_CODE,
)


def test_multiple_install_path_arguments(_error_messages, runner, configurator, constants):
def test_multiple_install_path_arguments(_error_messages, runner, configurator):
logging.info("Running multiple install path arguments test")

set_installer_download_params(configurator)
configurator.set_common_parameter(configurator.INSTALLER_ARGS_KEY, ["INSTALL_PATH=/path1"])
configurator.set_common_parameter(configurator.INSTALLER_PLATFORM_ARGS_KEY, ["INSTALL_PATH=/path2"])

_check_deployment_failure(
run_deployment(runner, True), _error_messages[MULTIPLE_INSTALL_PATH_KEY], constants.FAILED_DEPLOYMENT_EXIT_CODE
run_deployment(runner, True), _error_messages[MULTIPLE_INSTALL_PATH_KEY], FAILED_DEPLOYMENT_EXIT_CODE
)


def test_failed_download(_error_messages, runner, configurator, constants):
def test_failed_download(_error_messages, runner, configurator):
logging.info("Running failed download test")

set_installer_download_params(configurator)
configurator.set_common_parameter(configurator.ENVIRONMENT_URL_KEY, "0.0.0.0")

_check_deployment_failure(
run_deployment(runner, True), _error_messages[DOWNLOAD_FAILED_KEY], constants.FAILED_DEPLOYMENT_EXIT_CODE
run_deployment(runner, True), _error_messages[DOWNLOAD_FAILED_KEY], FAILED_DEPLOYMENT_EXIT_CODE
)


# noinspection PyUnusedLocal
@enable_for_system_family(family="unix")
def test_failed_signature_verification(_error_messages, runner, configurator, constants):
def test_failed_signature_verification(_error_messages, runner, configurator):
logging.info("Running failed signature verification test")

set_installer_download_params(configurator)
Expand All @@ -158,4 +163,4 @@ def test_failed_signature_verification(_error_messages, runner, configurator, co
signature.write("break signature by writing some text")

universal_message = _error_messages.get(SIGNATURE_VERIFICATION_FAILED_KEY)
_check_deployment_failure(run_deployment(runner, True), universal_message, constants.FAILED_DEPLOYMENT_EXIT_CODE)
_check_deployment_failure(run_deployment(runner, True), universal_message, FAILED_DEPLOYMENT_EXIT_CODE)

0 comments on commit 270aca1

Please sign in to comment.