From d6861f4f62b409fd24f6b2381061fbb6dc686051 Mon Sep 17 00:00:00 2001 From: Vladimir Domnich Date: Mon, 8 Aug 2022 19:51:28 +0300 Subject: [PATCH] Refactor env properties Encapculate reading/writing environment.properties in helper. Signed-off-by: Vladimir Domnich --- pytest_tests/helpers/env_properties.py | 40 +++++++++++++++++++ pytest_tests/testsuites/conftest.py | 13 +++--- .../testsuites/services/test_binaries.py | 40 +++++++------------ 3 files changed, 59 insertions(+), 34 deletions(-) create mode 100644 pytest_tests/helpers/env_properties.py diff --git a/pytest_tests/helpers/env_properties.py b/pytest_tests/helpers/env_properties.py new file mode 100644 index 0000000..a961546 --- /dev/null +++ b/pytest_tests/helpers/env_properties.py @@ -0,0 +1,40 @@ +import logging +import re + +import allure +from pytest import Config + +logger = logging.getLogger('NeoLogger') + + +@allure.step('Read environment.properties') +def read_env_properties(config: Config) -> dict: + environment_dir = config.getoption('--alluredir') + if not environment_dir: + return None + + file_path = f'{environment_dir}/environment.properties' + with open(file_path, 'r') as file: + raw_content = file.read() + + env_properties = {} + for line in raw_content.split('\n'): + m = re.match('(.*?)=(.*)', line) + if not m: + logger.warning(f'Could not parse env property from {line}') + continue + key, value = m.group(1), m.group(2) + env_properties[key] = value + return env_properties + + +@allure.step('Update data in environment.properties') +def save_env_properties(config: Config, env_data: dict) -> None: + environment_dir = config.getoption('--alluredir') + if not environment_dir: + return None + + file_path = f'{environment_dir}/environment.properties' + with open(file_path, 'a+') as env_file: + for env, env_value in env_data.items(): + env_file.write(f'{env}={env_value}\n') diff --git a/pytest_tests/testsuites/conftest.py b/pytest_tests/testsuites/conftest.py index 1e36f88..75a845d 100644 --- a/pytest_tests/testsuites/conftest.py +++ b/pytest_tests/testsuites/conftest.py @@ -11,6 +11,7 @@ import wallet from cli_helpers import _cmd_run from common import (ASSETS_DIR, FREE_STORAGE, INFRASTRUCTURE_TYPE, MAINNET_WALLET_PATH, NEOFS_NETMAP_DICT) +from env_properties import save_env_properties from payment_neogo import neofs_deposit, transfer_mainnet_gas from python_keywords.node_management import node_healthcheck from service_helper import get_storage_service_helper @@ -37,23 +38,19 @@ def cloud_infrastructure_check(): def check_binary_versions(request): # Collect versions of local binaries binaries = ['neo-go', 'neofs-cli', 'neofs-authmate'] - env_out = _get_binaries_version_local(binaries) + local_binaries = _get_binaries_version_local(binaries) # Collect versions of remote binaries helper = get_storage_service_helper() remote_binaries = helper.get_binaries_version() - env_out = {**env_out, **remote_binaries} + all_binaries = {**local_binaries, **remote_binaries} # Get version of aws binary out = _cmd_run('aws --version') out_lines = out.split("\n") - env_out["AWS"] = out_lines[0] if out_lines else 'Unknown' + all_binaries["AWS"] = out_lines[0] if out_lines else 'Unknown' - environment_dir = request.config.getoption('--alluredir') - if environment_dir: - with open(f'{environment_dir}/environment.properties', 'w') as out_file: - for env, env_value in env_out.items(): - out_file.write(f'{env}={env_value}\n') + save_env_properties(request.config, all_binaries) def _get_binaries_version_local(binaries: list) -> dict: diff --git a/pytest_tests/testsuites/services/test_binaries.py b/pytest_tests/testsuites/services/test_binaries.py index d474158..895e8ef 100644 --- a/pytest_tests/testsuites/services/test_binaries.py +++ b/pytest_tests/testsuites/services/test_binaries.py @@ -7,6 +7,7 @@ import pytest import requests from common import BIN_VERSIONS_FILE +from env_properties import read_env_properties, save_env_properties from service_helper import get_storage_service_helper logger = logging.getLogger('NeoLogger') @@ -21,34 +22,28 @@ def test_binaries_versions(request): if not BIN_VERSIONS_FILE: pytest.skip('File with binaries and versions was not provided') - failed_versions = {} - environment_dir = request.config.getoption('--alluredir') - env_data = None - data_for_env = {} - binaries_to_check = download_versions_info(BIN_VERSIONS_FILE) - with allure.step('Get binaries versions from servers'): helper = get_storage_service_helper() got_versions = helper.get_binaries_version(binaries=list(binaries_to_check.keys())) - if environment_dir: - with open(f'{environment_dir}/environment.properties', 'r') as env_file: - env_data = env_file.read() + env_properties = read_env_properties(request.config) # compare versions from servers and file + failed_versions = {} + additional_env_properties = {} for binary, version in binaries_to_check.items(): - if binary not in got_versions: - failed_versions[binary] = 'Can not find binary' - if got_versions[binary] != version: - failed_versions[binary] = f'Expected version {version}, found version {got_versions[binary]}' + actual_version = got_versions.get(binary) + if actual_version != version: + failed_versions[binary] = f'Expected version {version}, found version {actual_version}' - # if something missed in environment.properties file, let's add - if env_data and binary not in env_data: - data_for_env[binary] = got_versions[binary] + # If some binary was not listed in the env properties file, let's add it + # so that we have full information about versions in allure report + if env_properties and binary not in env_properties: + additional_env_properties[binary] = actual_version - if environment_dir and data_for_env: - add_to_environment_properties(f'{environment_dir}/environment.properties', data_for_env) + if env_properties and additional_env_properties: + save_env_properties(request.config, additional_env_properties) # create clear beautiful error with aggregation info if failed_versions: @@ -56,7 +51,7 @@ def test_binaries_versions(request): raise AssertionError(f'Found binaries with unexpected versions:\n{msg}') -@allure.step('Download info from {url}') +@allure.step('Download versions info from {url}') def download_versions_info(url: str) -> dict: binaries_to_version = {} @@ -77,10 +72,3 @@ def download_versions_info(url: str) -> dict: binaries_to_version[bin_name] = bin_version return binaries_to_version - - -@allure.step('Update data in environment.properties') -def add_to_environment_properties(file_path: str, env_data: dict): - with open(file_path, 'a+') as env_file: - for env, env_value in env_data.items(): - env_file.write(f'{env}={env_value}\n')