Skip to content

Commit

Permalink
Add negative testing to regular DT test suite
Browse files Browse the repository at this point in the history
  • Loading branch information
TimPansino committed Jul 13, 2023
1 parent fbdbdc0 commit fadd1ad
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 12 deletions.
2 changes: 1 addition & 1 deletion newrelic/api/transaction.py
Original file line number Diff line number Diff line change
Expand Up @@ -1291,7 +1291,7 @@ def accept_distributed_trace_headers(self, headers, transport_type="HTTP"):
# Remove trusted new relic header if available and parse
if payload:
try:
tracestate_data = NrTraceState.decode(payload, tk)
tracestate_data = NrTraceState.decode(payload, trusted_account_key)
except:
tracestate_data = None
if tracestate_data:
Expand Down
61 changes: 60 additions & 1 deletion tests/agent_features/test_distributed_tracing.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@

import pytest
import webtest
from testing_support.fixtures import override_application_settings, validate_attributes
from testing_support.fixtures import override_application_settings, override_generic_settings, validate_attributes
from testing_support.validators.validate_error_event_attributes import (
validate_error_event_attributes,
)
Expand All @@ -30,6 +30,7 @@

from newrelic.api.application import application_instance
from newrelic.api.background_task import BackgroundTask, background_task
from newrelic.api.external_trace import ExternalTrace
from newrelic.api.time_trace import current_trace
from newrelic.api.transaction import (
accept_distributed_trace_headers,
Expand Down Expand Up @@ -361,3 +362,61 @@ def test_current_span_id_inside_transaction():
def test_current_span_id_outside_transaction():
span_id = current_span_id()
assert span_id is None


@pytest.mark.parametrize("trusted_account_key", ('1', None), ids=("tk_set", "tk_unset"))
def test_outbound_dt_payload_generation(trusted_account_key):
@override_application_settings({
'distributed_tracing.enabled': True,
'account_id': '1',
'trusted_account_key': trusted_account_key,
'primary_application_id': '1',
})
@background_task(name='test_outbound_dt_payload_generation')
def _test_outbound_dt_payload_generation():
transaction = current_transaction()
payload = ExternalTrace.generate_request_headers(transaction)
if trusted_account_key:
assert payload
# Ensure trusted account key present as vendor
assert dict(payload)["tracestate"].startswith("1@nr=")
else:
assert not payload

_test_outbound_dt_payload_generation()


@pytest.mark.parametrize("trusted_account_key", ('1', None), ids=("tk_set", "tk_unset"))
def test_inbound_dt_payload_acceptance(trusted_account_key):
@override_application_settings({
'distributed_tracing.enabled': True,
'account_id': '1',
'trusted_account_key': trusted_account_key,
'primary_application_id': '1',
})
@background_task(name='_test_inbound_dt_payload_acceptance')
def _test_inbound_dt_payload_acceptance():
transaction = current_transaction()

payload = {
'v': [0, 1],
'd': {
'ty': 'Mobile',
'ac': '1',
'tk': '1',
'ap': '2827902',
'pa': '5e5733a911cfbc73',
'id': '7d3efb1b173fecfa',
'tr': 'd6b4ba0c3a712ca',
'ti': 1518469636035,
'tx': '8703ff3d88eefe9d',
}
}

result = transaction.accept_distributed_trace_payload(payload)
if trusted_account_key:
assert result
else:
assert not result

_test_inbound_dt_payload_acceptance()
20 changes: 10 additions & 10 deletions tests/agent_features/test_serverless_mode.py
Original file line number Diff line number Diff line change
Expand Up @@ -94,8 +94,8 @@ def _test_cat_headers():
_test_cat_headers()


@pytest.mark.parametrize("trusted_account_key", ('1', None))
def test_dt_outbound(serverless_application, trusted_account_key):
@pytest.mark.parametrize("trusted_account_key", ('1', None), ids=("tk_set", "tk_unset"))
def test_outbound_dt_payload_generation(serverless_application, trusted_account_key):
@override_generic_settings(serverless_application.settings, {
'distributed_tracing.enabled': True,
'account_id': '1',
Expand All @@ -104,19 +104,19 @@ def test_dt_outbound(serverless_application, trusted_account_key):
})
@background_task(
application=serverless_application,
name='test_dt_outbound')
def _test_dt_outbound():
name='test_outbound_dt_payload_generation')
def _test_outbound_dt_payload_generation():
transaction = current_transaction()
payload = ExternalTrace.generate_request_headers(transaction)
assert payload
# Ensure trusted account key or account ID present as vendor
assert dict(payload)["tracestate"].startswith("1@nr=")

_test_dt_outbound()
_test_outbound_dt_payload_generation()


@pytest.mark.parametrize("trusted_account_key", ('1', None))
def test_dt_inbound(serverless_application, trusted_account_key):
@pytest.mark.parametrize("trusted_account_key", ('1', None), ids=("tk_set", "tk_unset"))
def test_inbound_dt_payload_acceptance(serverless_application, trusted_account_key):
@override_generic_settings(serverless_application.settings, {
'distributed_tracing.enabled': True,
'account_id': '1',
Expand All @@ -125,8 +125,8 @@ def test_dt_inbound(serverless_application, trusted_account_key):
})
@background_task(
application=serverless_application,
name='test_dt_inbound')
def _test_dt_inbound():
name='test_inbound_dt_payload_acceptance')
def _test_inbound_dt_payload_acceptance():
transaction = current_transaction()

payload = {
Expand All @@ -147,7 +147,7 @@ def _test_dt_inbound():
result = transaction.accept_distributed_trace_payload(payload)
assert result

_test_dt_inbound()
_test_inbound_dt_payload_acceptance()


@pytest.mark.parametrize('arn_set', (True, False))
Expand Down

0 comments on commit fadd1ad

Please sign in to comment.