From 4a91567c71f525794474bbebd91bee035c59f60a Mon Sep 17 00:00:00 2001 From: Paul Date: Tue, 13 Apr 2021 17:09:03 +0200 Subject: [PATCH] Add --ddtrace flag (#9124) * Add --ddtrace option to tests * Fix rebase * Disable for py2 * Update datadog_checks_dev/datadog_checks/dev/tooling/commands/test.py Co-authored-by: Florian Veaux * Fix typo Co-authored-by: Florian Veaux --- datadog_checks_dev/datadog_checks/dev/_env.py | 7 ++++++ .../dev/tooling/commands/test.py | 23 +++++++++++++++++-- .../datadog_checks/dev/tooling/testing.py | 4 ++++ 3 files changed, 32 insertions(+), 2 deletions(-) diff --git a/datadog_checks_dev/datadog_checks/dev/_env.py b/datadog_checks_dev/datadog_checks/dev/_env.py index defc7cd6f0d0d..7ea8560ade197 100644 --- a/datadog_checks_dev/datadog_checks/dev/_env.py +++ b/datadog_checks_dev/datadog_checks/dev/_env.py @@ -7,6 +7,13 @@ from six import iteritems +DDTRACE_OPTIONS_LIST = [ + 'DD_TAGS', + 'DD_TRACE*', + 'DD_SERVICE', + 'DD_AGENT_HOST', + 'DD_ENV', +] E2E_PREFIX = 'DDEV_E2E' E2E_ENV_VAR_PREFIX = '{}_ENV_'.format(E2E_PREFIX) E2E_SET_UP = '{}_UP'.format(E2E_PREFIX) diff --git a/datadog_checks_dev/datadog_checks/dev/tooling/commands/test.py b/datadog_checks_dev/datadog_checks/dev/tooling/commands/test.py index 6bd74b0c5261e..45a11422a726c 100644 --- a/datadog_checks_dev/datadog_checks/dev/tooling/commands/test.py +++ b/datadog_checks_dev/datadog_checks/dev/tooling/commands/test.py @@ -6,11 +6,11 @@ import click -from ..._env import E2E_PARENT_PYTHON, SKIP_ENVIRONMENT +from ..._env import DDTRACE_OPTIONS_LIST, E2E_PARENT_PYTHON, SKIP_ENVIRONMENT from ...ci import get_ci_env_vars, running_on_ci from ...fs import chdir, file_exists, remove_path from ...subprocess import run_command -from ...utils import get_next +from ...utils import ON_WINDOWS, get_next from ..constants import get_root from ..dependencies import read_check_base_dependencies from ..testing import construct_pytest_options, fix_coverage_report, get_tox_envs, pytest_coverage_sources @@ -32,6 +32,7 @@ def display_envs(check_envs): @click.option('--bench', '-b', is_flag=True, help='Run only benchmarks') @click.option('--latest-metrics', is_flag=True, help='Only verify support of new metrics') @click.option('--e2e', is_flag=True, help='Run only end-to-end tests') +@click.option('--ddtrace', is_flag=True, help='Run tests using dd-trace-py') @click.option('--cov', '-c', 'coverage', is_flag=True, help='Measure code coverage') @click.option('--cov-missing', '-cm', is_flag=True, help='Show line numbers of statements that were not executed') @click.option('--junit', '-j', 'junit', is_flag=True, help='Generate junit reports') @@ -58,6 +59,7 @@ def test( bench, latest_metrics, e2e, + ddtrace, coverage, junit, cov_missing, @@ -137,6 +139,13 @@ def test( test_env_vars[E2E_PARENT_PYTHON] = sys.executable test_env_vars['TOX_TESTENV_PASSENV'] += f' {E2E_PARENT_PYTHON}' + if ddtrace: + for env in DDTRACE_OPTIONS_LIST: + test_env_vars['TOX_TESTENV_PASSENV'] += f' {env}' + # Used for CI app product + test_env_vars['TOX_TESTENV_PASSENV'] += ' TF_BUILD BUILD* SYSTEM*' + test_env_vars['DD_SERVICE'] = os.getenv('DD_SERVICE', 'ddev-integrations') + org_name = ctx.obj['org'] org = ctx.obj['orgs'].get(org_name, {}) api_key = org.get('api_key') or ctx.obj['dd_api_key'] or os.getenv('DD_API_KEY') @@ -153,6 +162,15 @@ def test( echo_debug(f"No envs found for: `{check}`") continue + ddtrace_check = ddtrace + if ddtrace and ON_WINDOWS and any('py2' in env for env in envs): + # The pytest flag --ddtrace is not available for windows-py2 env. + # Removing it so it does not fail. + echo_warning( + 'ddtrace flag is not available for windows-py2 environments ; disabling the flag for this check.' + ) + ddtrace_check = False + # This is for ensuring proper spacing between output of multiple checks' tests. # Basically this avoids printing a new line before the first check's tests. output_separator = '\n' if tests_ran else '' @@ -176,6 +194,7 @@ def test( test_filter=test_filter, pytest_args=pytest_args, e2e=e2e, + ddtrace=ddtrace_check, ) if coverage: pytest_options = pytest_options.format(pytest_coverage_sources(check)) diff --git a/datadog_checks_dev/datadog_checks/dev/tooling/testing.py b/datadog_checks_dev/datadog_checks/dev/tooling/testing.py index 6bad4ca56b43b..b63788b004dab 100644 --- a/datadog_checks_dev/datadog_checks/dev/tooling/testing.py +++ b/datadog_checks_dev/datadog_checks/dev/tooling/testing.py @@ -210,6 +210,7 @@ def construct_pytest_options( test_filter='', pytest_args='', e2e=False, + ddtrace=False, ): # Prevent no verbosity pytest_options = f'--verbosity={verbose or 1}' @@ -236,6 +237,9 @@ def construct_pytest_options( pytest_options += ' --run-latest-metrics' marker = 'latest_metrics' + if ddtrace: + pytest_options += ' --ddtrace' + if junit: test_group = 'e2e' if e2e else 'unit' pytest_options += (