Skip to content

Commit

Permalink
Refactor tooling for getting the current env name (#12939)
Browse files Browse the repository at this point in the history
  • Loading branch information
ofek authored and steveny91 committed Oct 27, 2022
1 parent f4f7b0b commit 9bfc441
Show file tree
Hide file tree
Showing 13 changed files with 30 additions and 32 deletions.
4 changes: 2 additions & 2 deletions cilium/tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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():
Expand Down
7 changes: 2 additions & 5 deletions datadog_checks_dev/datadog_checks/dev/kind.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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')
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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(
Expand Down
2 changes: 1 addition & 1 deletion datadog_checks_dev/datadog_checks/dev/tooling/testing.py
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down
4 changes: 2 additions & 2 deletions datadog_checks_dev/datadog_checks/dev/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -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']
Expand Down
4 changes: 2 additions & 2 deletions foundationdb/tests/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand Down
4 changes: 2 additions & 2 deletions foundationdb/tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
8 changes: 4 additions & 4 deletions foundationdb/tests/test_foundationdb.py
Original file line number Diff line number Diff line change
Expand Up @@ -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__)) + '/'

Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
Expand Down
3 changes: 2 additions & 1 deletion snmp/tests/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -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__)
Expand All @@ -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'}
Expand Down
4 changes: 2 additions & 2 deletions snmp/tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
)

Expand Down Expand Up @@ -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 = []
Expand Down
4 changes: 2 additions & 2 deletions teradata/tests/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand Down
8 changes: 4 additions & 4 deletions teradata/tests/test_e2e.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
from datadog_checks.base.constants import ServiceCheck

from .common import (
ACTIVE_ENV,
DEFAULT_METRICS,
E2E_EXCLUDE_METRICS,
EXPECTED_TAGS,
Expand All @@ -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"):
Expand All @@ -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)]
Expand Down
4 changes: 2 additions & 2 deletions windows_performance_counters/tests/test_e2e.py
Original file line number Diff line number Diff line change
@@ -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

Expand All @@ -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())'],
Expand Down

0 comments on commit 9bfc441

Please sign in to comment.