Skip to content

Commit

Permalink
fixed ci_visibility
Browse files Browse the repository at this point in the history
  • Loading branch information
romainkomorndatadog committed Sep 23, 2024
1 parent ceea872 commit 6df23bd
Show file tree
Hide file tree
Showing 24 changed files with 496 additions and 573 deletions.
14 changes: 9 additions & 5 deletions ddtrace/ext/test_visibility/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,20 @@
from ddtrace.internal.utils.formats import asbool


# Default test visibility settings
config._add(
"test_visibility",
dict(
def _get_default_test_visibility_contrib_config():
return dict(
_default_service="default_test_visibility_service",
itr_skipping_level=ITR_SKIPPING_LEVEL.SUITE
if asbool(os.getenv("_DD_CIVISIBILITY_ITR_SUITE_MODE"))
else ITR_SKIPPING_LEVEL.TEST,
_itr_skipping_ignore_parameters=False,
),
)


# Default test visibility settings
config._add(
"test_visibility",
_get_default_test_visibility_contrib_config(),
)


Expand Down
7 changes: 5 additions & 2 deletions ddtrace/internal/ci_visibility/_api_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -466,7 +466,7 @@ def fetch_skippable_items(
record_itr_skippable_request_error(error_type)


class AgentlessTestVisibilityClient(_TestVisibilityAPIClientBase):
class AgentlessTestVisibilityAPIClient(_TestVisibilityAPIClientBase):
_requests_mode = REQUESTS_MODE.AGENTLESS_EVENTS

def __init__(
Expand All @@ -481,6 +481,9 @@ def __init__(
dd_env: t.Optional[str] = None,
timeout: float = DEFAULT_TIMEOUT,
):
if not api_key:
raise ValueError("API key is required for AgentlessTestVisibilityAPIClient")

_dd_site = dd_site if dd_site is not None else AGENTLESS_DEFAULT_SITE
base_url = agentless_url if agentless_url is not None else "https://api." + _dd_site

Expand All @@ -497,7 +500,7 @@ def _redact_headers(self) -> t.Dict[str, str]:
return headers


class EVPProxyTestVisibilityClient(_TestVisibilityAPIClientBase):
class EVPProxyTestVisibilityAPIClient(_TestVisibilityAPIClientBase):
_requests_mode = REQUESTS_MODE.EVP_PROXY_EVENTS

def __init__(
Expand Down
8 changes: 7 additions & 1 deletion ddtrace/internal/ci_visibility/api/_module.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,11 @@
from ddtrace.internal.ci_visibility.api._base import TestVisibilityParentItem
from ddtrace.internal.ci_visibility.api._base import TestVisibilitySessionSettings
from ddtrace.internal.ci_visibility.api._suite import TestVisibilitySuite
from ddtrace.internal.ci_visibility.constants import ITR_SKIPPING_LEVEL
from ddtrace.internal.ci_visibility.constants import MODULE_ID
from ddtrace.internal.ci_visibility.constants import MODULE_TYPE
from ddtrace.internal.ci_visibility.constants import SUITE
from ddtrace.internal.ci_visibility.constants import TEST
from ddtrace.internal.ci_visibility.telemetry.constants import EVENT_TYPES
from ddtrace.internal.ci_visibility.telemetry.events import record_event_created
from ddtrace.internal.ci_visibility.telemetry.events import record_event_finished
Expand Down Expand Up @@ -64,7 +67,10 @@ def _set_itr_tags(self, itr_enabled: bool):

self.set_tag(test.ITR_TEST_SKIPPING_ENABLED, self._session_settings.itr_test_skipping_enabled)
if itr_enabled:
self.set_tag(test.ITR_TEST_SKIPPING_TYPE, self._session_settings.itr_test_skipping_level)
self.set_tag(
test.ITR_TEST_SKIPPING_TYPE,
TEST if self._session_settings.itr_test_skipping_level == ITR_SKIPPING_LEVEL.TEST else SUITE,
)
self.set_tag(test.ITR_DD_CI_ITR_TESTS_SKIPPED, self._itr_skipped_count > 0)

def _telemetry_record_event_created(self):
Expand Down
11 changes: 6 additions & 5 deletions ddtrace/internal/ci_visibility/recorder.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,8 @@
from ddtrace.internal import core
from ddtrace.internal import telemetry
from ddtrace.internal.agent import get_connection
from ddtrace.internal.ci_visibility._api_client import AgentlessTestVisibilityClient
from ddtrace.internal.ci_visibility._api_client import EVPProxyTestVisibilityClient
from ddtrace.internal.ci_visibility._api_client import AgentlessTestVisibilityAPIClient
from ddtrace.internal.ci_visibility._api_client import EVPProxyTestVisibilityAPIClient
from ddtrace.internal.ci_visibility._api_client import ITRData
from ddtrace.internal.ci_visibility._api_client import TestVisibilityAPISettings
from ddtrace.internal.ci_visibility._api_client import _TestVisibilityAPIClientBase
Expand Down Expand Up @@ -185,7 +185,8 @@ def __init__(self, tracer=None, config=None, service=None):
if not isinstance(ddconfig.test_visibility.itr_skipping_level, ITR_SKIPPING_LEVEL):
log.warning(
"itr_skipping_level should be of type %s but is of type %s, defaulting to %s",
ddconfig.test_visibility.itr_skipping_level,
ITR_SKIPPING_LEVEL,
type(ddconfig.test_visibility.itr_skipping_level),
ITR_SKIPPING_LEVEL.TEST.name,
)
self._itr_skipping_level = ITR_SKIPPING_LEVEL.TEST
Expand Down Expand Up @@ -225,7 +226,7 @@ def __init__(self, tracer=None, config=None, service=None):
)
requests_mode_str = "agentless"
self._requests_mode = REQUESTS_MODE.AGENTLESS_EVENTS
self._api_client = AgentlessTestVisibilityClient(
self._api_client = AgentlessTestVisibilityAPIClient(
self._itr_skipping_level,
self._git_data,
self._configurations,
Expand All @@ -238,7 +239,7 @@ def __init__(self, tracer=None, config=None, service=None):
elif self._agent_evp_proxy_is_available():
self._requests_mode = REQUESTS_MODE.EVP_PROXY_EVENTS
requests_mode_str = "EVP Proxy"
self._api_client = EVPProxyTestVisibilityClient(
self._api_client = EVPProxyTestVisibilityAPIClient(
self._itr_skipping_level,
self._git_data,
self._configurations,
Expand Down
4 changes: 0 additions & 4 deletions ddtrace/internal/test_visibility/coverage_lines.py
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,3 @@ def from_bytearray(cls, lines: bytearray) -> "CoverageLines":
coverage = cls()
coverage._lines = lines
return coverage

@classmethod
def from_b64_string(cls, b64_string: str) -> "CoverageLines":
return cls.from_bytearray(bytearray(b64_string, "utf-8"))
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,12 @@

import ddtrace
from ddtrace.ext.test_visibility import api as ext_api
from ddtrace.internal.ci_visibility.constants import ITR_SKIPPING_LEVEL
from ddtrace.internal.test_visibility import api


def main():
ddtrace.config.test_visibility.itr_skipping_level = "suite"
ddtrace.config.test_visibility.itr_skipping_level = ITR_SKIPPING_LEVEL.SUITE

ext_api.enable_test_visibility()

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@

import ddtrace
from ddtrace.ext.test_visibility import api as ext_api
from ddtrace.internal.ci_visibility.constants import ITR_SKIPPING_LEVEL
from ddtrace.internal.test_visibility import api
from ddtrace.internal.test_visibility.coverage_lines import CoverageLines

Expand All @@ -36,7 +37,7 @@ def _make_excinfo():


def main():
ddtrace.config.test_visibility.itr_skipping_level = "suite"
ddtrace.config.test_visibility.itr_skipping_level = ITR_SKIPPING_LEVEL.SUITE

ext_api.enable_test_visibility()

Expand Down
42 changes: 22 additions & 20 deletions tests/ci_visibility/api/test_api_fake_runners.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@

import pytest

from ddtrace.internal.ci_visibility.recorder import _CIVisibilitySettings
from ddtrace.internal.ci_visibility._api_client import TestVisibilityAPISettings
from ddtrace.internal.ci_visibility.constants import ITR_SKIPPING_LEVEL
from tests import utils
from tests.ci_visibility.util import _get_default_ci_env_vars
from tests.ci_visibility.util import _get_default_civisibility_ddconfig
Expand Down Expand Up @@ -62,8 +63,8 @@ def test_manual_api_fake_runner_all_pass(self):
),
replace_os_env=True,
), mock.patch(
"ddtrace.internal.ci_visibility.recorder.CIVisibility._check_settings_api",
return_value=_CIVisibilitySettings(False, False, False, False),
"ddtrace.internal.ci_visibility._api_client._TestVisibilityAPIClientBase.fetch_settings",
return_value=TestVisibilityAPISettings(False, False, False, False),
), mock.patch(
"ddtrace.internal.ci_visibility.recorder.ddconfig", _get_default_civisibility_ddconfig()
):
Expand All @@ -88,8 +89,8 @@ def test_manual_api_fake_runner_all_fail(self):
replace_os_env=True,
):
with mock.patch(
"ddtrace.internal.ci_visibility.recorder.CIVisibility._check_settings_api",
return_value=_CIVisibilitySettings(False, False, False, False),
"ddtrace.internal.ci_visibility._api_client._TestVisibilityAPIClientBase.fetch_settings",
return_value=TestVisibilityAPISettings(False, False, False, False),
), mock.patch("ddtrace.internal.ci_visibility.recorder.ddconfig", _get_default_civisibility_ddconfig()):
subprocess.run(["python", "fake_runner_all_fail.py"])

Expand All @@ -111,8 +112,8 @@ def test_manual_api_fake_runner_all_skip(self):
),
replace_os_env=True,
), mock.patch(
"ddtrace.internal.ci_visibility.recorder.CIVisibility._check_settings_api",
return_value=_CIVisibilitySettings(False, False, False, False),
"ddtrace.internal.ci_visibility._api_client._TestVisibilityAPIClientBase.fetch_settings",
return_value=TestVisibilityAPISettings(False, False, False, False),
), mock.patch(
"ddtrace.internal.ci_visibility.recorder.ddconfig", _get_default_civisibility_ddconfig()
):
Expand All @@ -136,8 +137,8 @@ def test_manual_api_fake_runner_all_itr_skip_test_level(self):
),
replace_os_env=True,
), mock.patch(
"ddtrace.internal.ci_visibility.recorder.CIVisibility._check_settings_api",
return_value=_CIVisibilitySettings(False, False, False, False),
"ddtrace.internal.ci_visibility._api_client._TestVisibilityAPIClientBase.fetch_settings",
return_value=TestVisibilityAPISettings(False, False, False, False),
), mock.patch(
"ddtrace.internal.ci_visibility.recorder.ddconfig", _get_default_civisibility_ddconfig()
):
Expand All @@ -161,10 +162,11 @@ def test_manual_api_fake_runner_all_itr_skip_suite_level(self):
),
replace_os_env=True,
), mock.patch(
"ddtrace.internal.ci_visibility.recorder.CIVisibility._check_settings_api",
return_value=_CIVisibilitySettings(False, False, False, False),
"ddtrace.internal.ci_visibility._api_client._TestVisibilityAPIClientBase.fetch_settings",
return_value=TestVisibilityAPISettings(False, False, False, False),
), mock.patch(
"ddtrace.internal.ci_visibility.recorder.ddconfig", _get_default_civisibility_ddconfig()
"ddtrace.internal.ci_visibility.recorder.ddconfig",
_get_default_civisibility_ddconfig(itr_skipping_level=ITR_SKIPPING_LEVEL.SUITE),
):
subprocess.run(["python", "fake_runner_all_itr_skip_suite_level.py"])

Expand All @@ -186,8 +188,8 @@ def test_manual_api_fake_runner_mix_pass(self):
),
replace_os_env=True,
), mock.patch(
"ddtrace.internal.ci_visibility.recorder.CIVisibility._check_settings_api",
return_value=_CIVisibilitySettings(False, False, False, False),
"ddtrace.internal.ci_visibility._api_client._TestVisibilityAPIClientBase.fetch_settings",
return_value=TestVisibilityAPISettings(False, False, False, False),
), mock.patch(
"ddtrace.internal.ci_visibility.recorder.ddconfig", _get_default_civisibility_ddconfig()
):
Expand All @@ -211,8 +213,8 @@ def test_manual_api_fake_runner_mix_fail(self):
),
replace_os_env=True,
), mock.patch(
"ddtrace.internal.ci_visibility.recorder.CIVisibility._check_settings_api",
return_value=_CIVisibilitySettings(False, False, False, False),
"ddtrace.internal.ci_visibility._api_client._TestVisibilityAPIClientBase.fetch_settings",
return_value=TestVisibilityAPISettings(False, False, False, False),
), mock.patch(
"ddtrace.internal.ci_visibility.recorder.ddconfig", _get_default_civisibility_ddconfig()
):
Expand All @@ -237,8 +239,8 @@ def test_manual_api_fake_runner_mix_fail_itr_test_level(self):
),
replace_os_env=True,
), mock.patch(
"ddtrace.internal.ci_visibility.recorder.CIVisibility._check_settings_api",
return_value=_CIVisibilitySettings(False, False, False, False),
"ddtrace.internal.ci_visibility._api_client._TestVisibilityAPIClientBase.fetch_settings",
return_value=TestVisibilityAPISettings(False, False, False, False),
), mock.patch(
"ddtrace.internal.ci_visibility.recorder.ddconfig", _get_default_civisibility_ddconfig()
):
Expand All @@ -263,8 +265,8 @@ def test_manual_api_fake_runner_mix_fail_itr_suite_level(self):
),
replace_os_env=True,
), mock.patch(
"ddtrace.internal.ci_visibility.recorder.CIVisibility._check_settings_api",
return_value=_CIVisibilitySettings(False, False, False, False),
"ddtrace.internal.ci_visibility._api_client._TestVisibilityAPIClientBase.fetch_settings",
return_value=TestVisibilityAPISettings(False, False, False, False),
), mock.patch(
"ddtrace.internal.ci_visibility.recorder.ddconfig", _get_default_civisibility_ddconfig()
):
Expand Down
Loading

0 comments on commit 6df23bd

Please sign in to comment.