Skip to content

Commit

Permalink
connect Test to TestSession if present
Browse files Browse the repository at this point in the history
  • Loading branch information
anmarchenko committed Nov 23, 2023
1 parent 56d0c20 commit 0cae355
Show file tree
Hide file tree
Showing 7 changed files with 31 additions and 6 deletions.
7 changes: 4 additions & 3 deletions lib/datadog/ci.rb
Original file line number Diff line number Diff line change
Expand Up @@ -59,9 +59,10 @@ def active_test_session
end

# Return a {Datadog::CI::Test ci_test} that will trace a test called `test_name`.
# Raises an error if a test is already active. If there is an active test session,
# the new test will be connected to the session. The test will inherit service name and tags from
# the running test session if none provided.
# Raises an error if a test is already active.
# If there is an active test session, the new test will be connected to the session.
# The test will inherit service name and tags from the running test session if not provided
# in parameters.
#
# You could trace your test using a <tt>do-block</tt> like:
#
Expand Down
3 changes: 3 additions & 0 deletions lib/datadog/ci/ext/test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@ module Test
TAG_TRAITS = "test.traits"
TAG_TYPE = "test.type"

# those tags are special and they are used to conrrelate tests with the test sessions, suites, and modules
TAG_TEST_SESSION_ID = "_test.session_id"

# tags that can be inherited from the test session
INHERITABLE_TAGS = [TAG_FRAMEWORK, TAG_FRAMEWORK_VERSION, TAG_TYPE].freeze

Expand Down
1 change: 1 addition & 0 deletions lib/datadog/ci/recorder.rb
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ def trace_test(test_name, service_name: nil, operation_name: "test", tags: {}, &
}

tags[Ext::Test::TAG_NAME] = test_name
tags[Ext::Test::TAG_TEST_SESSION_ID] = test_session.id if test_session

if block
Datadog::Tracing.trace(operation_name, **span_options) do |tracer_span, trace|
Expand Down
5 changes: 5 additions & 0 deletions lib/datadog/ci/span.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,11 @@ def initialize(tracer_span)
@tracer_span = tracer_span
end

# @return [Integer] the ID of the span.
def id
tracer_span.id
end

# @return [String] the name of the span.
def name
tracer_span.name
Expand Down
2 changes: 2 additions & 0 deletions sig/datadog/ci/ext/test.rbs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ module Datadog

TAG_TYPE: String

TAG_TEST_SESSION_ID: String

INHERITABLE_TAGS: Array[String]

TAG_OS_ARCHITECTURE: String
Expand Down
2 changes: 2 additions & 0 deletions sig/datadog/ci/span.rbs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ module Datadog

def initialize: (Datadog::Tracing::SpanOperation tracer_span) -> void

def id: () -> Integer

def name: () -> String

def service: () -> String
Expand Down
17 changes: 14 additions & 3 deletions spec/datadog/ci/recorder_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -120,18 +120,27 @@
end

context "when test session is active" do
let(:test_session_id) { 42 }
let(:inheritable_tags) { {"test.framework" => "my framework"} }

let(:test_session_service) { "my-test-service" }
let(:test_session) do
instance_double(Datadog::CI::TestSession, service: test_session_service, inheritable_tags: inheritable_tags)
instance_double(
Datadog::CI::TestSession,
service: test_session_service,
inheritable_tags: inheritable_tags,
id: test_session_id
)
end

before do
allow(recorder).to receive(:active_test_session).and_return(test_session)
end

context "when service name and tags are not given" do
let(:expected_tags) { {"test.framework" => "my framework", "test.name" => test_name} }
let(:expected_tags) do
{"test.framework" => "my framework", "test.name" => test_name, "_test.session_id" => test_session_id}
end

subject(:trace) do
recorder.trace_test(
Expand Down Expand Up @@ -165,7 +174,9 @@

context "when service name and tags are given" do
let(:tags) { {"test.framework" => "special test framework"} }
let(:expected_tags) { {"test.framework" => "special test framework", "test.name" => test_name} }
let(:expected_tags) do
{"test.framework" => "special test framework", "test.name" => test_name, "_test.session_id" => test_session_id}
end

subject(:trace) do
recorder.trace_test(
Expand Down

0 comments on commit 0cae355

Please sign in to comment.