From 9bfc4414850acc05ef94869ba6c14fff36d15a87 Mon Sep 17 00:00:00 2001 From: Ofek Lev Date: Thu, 15 Sep 2022 16:46:24 -0400 Subject: [PATCH] Refactor tooling for getting the current env name (#12939) --- cilium/tests/conftest.py | 4 ++-- datadog_checks_dev/datadog_checks/dev/kind.py | 7 ++----- .../datadog_checks/dev/tooling/commands/env/start.py | 6 +++--- datadog_checks_dev/datadog_checks/dev/tooling/testing.py | 2 +- datadog_checks_dev/datadog_checks/dev/utils.py | 4 ++-- foundationdb/tests/common.py | 4 ++-- foundationdb/tests/conftest.py | 4 ++-- foundationdb/tests/test_foundationdb.py | 8 ++++---- snmp/tests/common.py | 3 ++- snmp/tests/conftest.py | 4 ++-- teradata/tests/common.py | 4 ++-- teradata/tests/test_e2e.py | 8 ++++---- windows_performance_counters/tests/test_e2e.py | 4 ++-- 13 files changed, 30 insertions(+), 32 deletions(-) diff --git a/cilium/tests/conftest.py b/cilium/tests/conftest.py index c0c73199fe8a5..f2a9715a11aa5 100644 --- a/cilium/tests/conftest.py +++ b/cilium/tests/conftest.py @@ -11,7 +11,7 @@ from datadog_checks.dev import run_command from datadog_checks.dev.kind import kind_run from datadog_checks.dev.kube_port_forward import port_forward -from datadog_checks.dev.utils import get_tox_env +from datadog_checks.dev.utils import get_active_env from .common import CILIUM_VERSION @@ -34,7 +34,7 @@ IMAGE_NAME = "quay.io/cilium/cilium:v{}".format(CILIUM_VERSION) PORTS = [AGENT_PORT, OPERATOR_PORT] -CLUSTER_NAME = 'cluster-{}-{}'.format('cilium', get_tox_env()) +CLUSTER_NAME = 'cluster-{}-{}'.format('cilium', get_active_env()) def setup_cilium(): diff --git a/datadog_checks_dev/datadog_checks/dev/kind.py b/datadog_checks_dev/datadog_checks/dev/kind.py index 5bfca2b222de7..eab304acf6b3b 100644 --- a/datadog_checks_dev/datadog_checks/dev/kind.py +++ b/datadog_checks_dev/datadog_checks/dev/kind.py @@ -10,7 +10,7 @@ from .fs import create_file, file_exists, path_join from .structures import EnvVars, LazyFunction, TempDir from .subprocess import run_command -from .utils import get_current_check_name, get_tox_env, get_hatch_env +from .utils import get_active_env, get_current_check_name if PY3: from shutil import which @@ -43,10 +43,7 @@ def kind_run(sleep=None, endpoints=None, conditions=None, env_vars=None, wrapper check_name = get_current_check_name(depth=2) # Replace undercores as kubeadm doesn't accept them check_name = check_name.replace("_", "-") - try: - cluster_name = 'cluster-{}-{}'.format(check_name, get_hatch_env()) - except KeyError: - cluster_name = 'cluster-{}-{}'.format(check_name, get_tox_env()) + cluster_name = 'cluster-{}-{}'.format(check_name, get_active_env()) with TempDir(cluster_name) as temp_dir: kubeconfig_path = path_join(temp_dir, 'config') diff --git a/datadog_checks_dev/datadog_checks/dev/tooling/commands/env/start.py b/datadog_checks_dev/datadog_checks/dev/tooling/commands/env/start.py index 35f2bb3465ff7..995b2848dcb9f 100644 --- a/datadog_checks_dev/datadog_checks/dev/tooling/commands/env/start.py +++ b/datadog_checks_dev/datadog_checks/dev/tooling/commands/env/start.py @@ -14,7 +14,7 @@ from ...e2e import E2E_SUPPORTED_TYPES, derive_interface, start_environment, stop_environment from ...e2e.agent import DEFAULT_PYTHON_VERSION, DEFAULT_SAMPLING_COLLECTION_INTERVAL from ...git import get_current_branch -from ...testing import complete_envs, get_available_envs, get_tox_env_python_version +from ...testing import complete_envs, get_active_env_python_version, get_available_envs from ...utils import complete_testable_checks, is_testable_check from ..console import CONTEXT_SETTINGS, abort, echo_failure, echo_info, echo_success, echo_waiting, echo_warning @@ -319,9 +319,9 @@ def _check_env(check, env): def _get_python_version(env, python): - env_python_version = get_tox_env_python_version(env) + env_python_version = get_active_env_python_version(env) if not python: - # Make the tox environment Python specifier influence the Agent + # Make the environment Python specifier influence the Agent python = env_python_version or DEFAULT_PYTHON_VERSION elif env_python_version and env_python_version != int(python): echo_warning( diff --git a/datadog_checks_dev/datadog_checks/dev/tooling/testing.py b/datadog_checks_dev/datadog_checks/dev/tooling/testing.py index bd0ea424684e5..d3dc44ae4ed2b 100644 --- a/datadog_checks_dev/datadog_checks/dev/tooling/testing.py +++ b/datadog_checks_dev/datadog_checks/dev/tooling/testing.py @@ -605,7 +605,7 @@ def get_changed_directories(include_uncommitted=True): return {line.split('/')[0] for line in changed_files} -def get_tox_env_python_version(env): +def get_active_env_python_version(env): match = re.match(PYTHON_MAJOR_PATTERN, env) if match: return int(match.group(1)) diff --git a/datadog_checks_dev/datadog_checks/dev/utils.py b/datadog_checks_dev/datadog_checks/dev/utils.py index d9ad7e7d51222..be41397470dbf 100644 --- a/datadog_checks_dev/datadog_checks/dev/utils.py +++ b/datadog_checks_dev/datadog_checks/dev/utils.py @@ -26,8 +26,8 @@ GH_ANNOTATION_LEVELS = ['warning', 'error'] -def get_tox_env(): - return os.environ['TOX_ENV_NAME'] +def get_active_env(): + return os.environ.get('TOX_ENV_NAME') or os.environ['HATCH_ENV_ACTIVE'] def get_hatch_env(): return os.environ['HATCH_ENV_ACTIVE'] diff --git a/foundationdb/tests/common.py b/foundationdb/tests/common.py index df9cddf56df3e..2c426137ed295 100644 --- a/foundationdb/tests/common.py +++ b/foundationdb/tests/common.py @@ -4,12 +4,12 @@ import os from datadog_checks.dev import get_here -from datadog_checks.dev.utils import get_tox_env +from datadog_checks.dev.utils import get_active_env dirname = os.path.dirname(__file__) HERE = get_here() -TOX_ENV = get_tox_env() +ACTIVE_ENV = get_active_env() CLUSTER_FILE = os.path.join(dirname, 'fdb.cluster') TLS_CLUSTER_FILE = os.path.join(dirname, 'fdb-tls.cluster') diff --git a/foundationdb/tests/conftest.py b/foundationdb/tests/conftest.py index f48aeb7125f2b..b087893e3646b 100644 --- a/foundationdb/tests/conftest.py +++ b/foundationdb/tests/conftest.py @@ -9,12 +9,12 @@ from datadog_checks.dev import WaitFor, docker_run, run_command -from .common import E2E_CONFIG, E2E_METADATA, E2E_TLS_CONFIG, HERE, INSTANCE, TLS_INSTANCE, TOX_ENV +from .common import ACTIVE_ENV, E2E_CONFIG, E2E_METADATA, E2E_TLS_CONFIG, HERE, INSTANCE, TLS_INSTANCE @pytest.fixture(scope='session') def dd_environment(): - if TOX_ENV == 'py38-tls': + if ACTIVE_ENV == 'py38-tls': compose_file = os.path.join(HERE, 'docker', 'docker-compose-tls.yaml') with docker_run(compose_file=compose_file, conditions=[WaitFor(create_tls_database)]): yield E2E_TLS_CONFIG, E2E_METADATA diff --git a/foundationdb/tests/test_foundationdb.py b/foundationdb/tests/test_foundationdb.py index 158e719712f52..e21b999caa8ce 100644 --- a/foundationdb/tests/test_foundationdb.py +++ b/foundationdb/tests/test_foundationdb.py @@ -12,7 +12,7 @@ from datadog_checks.dev.utils import get_metadata_metrics from datadog_checks.foundationdb import FoundationdbCheck -from .common import METRICS, TOX_ENV +from .common import ACTIVE_ENV, METRICS current_dir = dir_path = os.path.dirname(os.path.realpath(__file__)) + '/' @@ -38,7 +38,7 @@ def test_full(aggregator, instance): aggregator.assert_service_check("foundationdb.can_connect", AgentCheck.OK) -@pytest.mark.skipif(TOX_ENV == 'py38-tls', reason="Non-TLS FoundationDB cluster only.") +@pytest.mark.skipif(ACTIVE_ENV == 'py38-tls', reason="Non-TLS FoundationDB cluster only.") @pytest.mark.usefixtures("dd_environment") def test_integ(aggregator, instance): # type: (AggregatorStub, Dict[str, Any]) -> None @@ -52,7 +52,7 @@ def test_integ(aggregator, instance): aggregator.assert_service_check("foundationdb.can_connect", AgentCheck.OK) -@pytest.mark.skipif(TOX_ENV == 'py38-tls', reason="Non-TLS FoundationDB cluster only.") +@pytest.mark.skipif(ACTIVE_ENV == 'py38-tls', reason="Non-TLS FoundationDB cluster only.") @pytest.mark.usefixtures("dd_environment") def test_custom_metrics(aggregator, instance): # type: (AggregatorStub, Dict[str, Any]) -> None @@ -77,7 +77,7 @@ def test_custom_metrics(aggregator, instance): del instance['custom_queries'] -@pytest.mark.skipif(TOX_ENV != 'py38-tls', reason="TLS FoundationDB cluster only.") +@pytest.mark.skipif(ACTIVE_ENV != 'py38-tls', reason="TLS FoundationDB cluster only.") @pytest.mark.usefixtures("dd_environment") def test_tls_integ(aggregator, tls_instance): # type: (AggregatorStub, Dict[str, Any]) -> None diff --git a/snmp/tests/common.py b/snmp/tests/common.py index 7410e7fd838b9..54e4bb84fadc6 100644 --- a/snmp/tests/common.py +++ b/snmp/tests/common.py @@ -16,6 +16,7 @@ from datadog_checks.base.stubs.aggregator import AggregatorStub from datadog_checks.base.utils.common import get_docker_hostname, to_native_string from datadog_checks.dev.docker import get_container_ip +from datadog_checks.dev.utils import get_active_env from datadog_checks.snmp import SnmpCheck log = logging.getLogger(__name__) @@ -25,7 +26,7 @@ HERE = os.path.dirname(os.path.abspath(__file__)) COMPOSE_DIR = os.path.join(HERE, 'compose') SNMP_LISTENER_ENV = os.environ['SNMP_LISTENER_ENV'] -TOX_ENV_NAME = os.environ['TOX_ENV_NAME'] +ACTIVE_ENV_NAME = get_active_env() AUTH_PROTOCOLS = {'MD5': 'usmHMACMD5AuthProtocol', 'SHA': 'usmHMACSHAAuthProtocol'} PRIV_PROTOCOLS = {'DES': 'usmDESPrivProtocol', 'AES': 'usmAesCfb128Protocol'} diff --git a/snmp/tests/conftest.py b/snmp/tests/conftest.py index 49ec755052145..332668f644216 100644 --- a/snmp/tests/conftest.py +++ b/snmp/tests/conftest.py @@ -15,11 +15,11 @@ from datadog_checks.dev.docker import get_container_ip from .common import ( + ACTIVE_ENV_NAME, COMPOSE_DIR, PORT, SNMP_CONTAINER_NAME, SNMP_LISTENER_ENV, - TOX_ENV_NAME, generate_container_instance_config, ) @@ -74,7 +74,7 @@ def autodiscovery_ready(): def _autodiscovery_ready(): result = run_command( - ['docker', 'exec', 'dd_snmp_{}'.format(TOX_ENV_NAME), 'agent', 'configcheck'], capture=True, check=True + ['docker', 'exec', 'dd_snmp_{}'.format(ACTIVE_ENV_NAME), 'agent', 'configcheck'], capture=True, check=True ) autodiscovery_checks = [] diff --git a/teradata/tests/common.py b/teradata/tests/common.py index e3e500c266798..2ac0ef5db130d 100644 --- a/teradata/tests/common.py +++ b/teradata/tests/common.py @@ -7,11 +7,11 @@ from datadog_checks.dev import get_here from datadog_checks.dev.ci import running_on_ci -from datadog_checks.dev.utils import get_tox_env +from datadog_checks.dev.utils import get_active_env HERE = get_here() CHECK_NAME = 'teradata' -TOX_ENV = get_tox_env() +ACTIVE_ENV = get_active_env() ON_CI = running_on_ci() TERADATA_SERVER = os.environ.get('TERADATA_SERVER') diff --git a/teradata/tests/test_e2e.py b/teradata/tests/test_e2e.py index 2f0077041b431..5b677a5f2035f 100644 --- a/teradata/tests/test_e2e.py +++ b/teradata/tests/test_e2e.py @@ -6,6 +6,7 @@ from datadog_checks.base.constants import ServiceCheck from .common import ( + ACTIVE_ENV, DEFAULT_METRICS, E2E_EXCLUDE_METRICS, EXPECTED_TAGS, @@ -15,15 +16,14 @@ SERVICE_CHECK_QUERY, TABLE_DISK_METRICS, TERADATA_SERVER, - TOX_ENV, ) skip_on_ci = pytest.mark.skipif( - ON_CI and TOX_ENV != 'py38-sandbox', reason='Do not run E2E test on sandbox environment' + ON_CI and ACTIVE_ENV != 'py38-sandbox', reason='Do not run E2E test on sandbox environment' ) -@pytest.mark.skipif(TOX_ENV == 'py38-sandbox', reason='Test only available for py38 environment') +@pytest.mark.skipif(ACTIVE_ENV == 'py38-sandbox', reason='Test only available for py38 environment') @pytest.mark.e2e def test_e2e(dd_agent_check, aggregator, instance): with pytest.raises(Exception, match="Hostname lookup failed"): @@ -32,7 +32,7 @@ def test_e2e(dd_agent_check, aggregator, instance): aggregator.assert_service_check(SERVICE_CHECK_QUERY, count=0) -@pytest.mark.skipif(TOX_ENV != 'py38-sandbox', reason='Test only available for py38 sandbox environment') +@pytest.mark.skipif(ACTIVE_ENV != 'py38-sandbox', reason='Test only available for py38 sandbox environment') @pytest.mark.e2e def test_e2e_sandbox(dd_agent_check, aggregator, instance): global_tags = ['teradata_port:1025', 'teradata_server:{}'.format(TERADATA_SERVER)] diff --git a/windows_performance_counters/tests/test_e2e.py b/windows_performance_counters/tests/test_e2e.py index 26435bf97065f..71df3ee7bf060 100644 --- a/windows_performance_counters/tests/test_e2e.py +++ b/windows_performance_counters/tests/test_e2e.py @@ -1,12 +1,12 @@ # (C) Datadog, Inc. 2021-present # All rights reserved # Licensed under a 3-clause BSD style license (see LICENSE) -import os import subprocess import pytest from datadog_checks.base.constants import ServiceCheck +from datadog_checks.dev.utils import get_active_env from .common import INSTANCE @@ -16,7 +16,7 @@ def test(dd_agent_check): aggregator = dd_agent_check(INSTANCE, rate=True) - container_name = f'dd_windows_performance_counters_{os.environ["TOX_ENV_NAME"]}' + container_name = f'dd_windows_performance_counters_{get_active_env()}' python_path = r'C:\Program Files\Datadog\Datadog Agent\embedded3\python.exe' num_threads = subprocess.check_output( ['docker', 'exec', container_name, python_path, '-c', 'import os;print(os.cpu_count())'],