Skip to content

Commit

Permalink
set ITR tags with number of skipped tests when session ends
Browse files Browse the repository at this point in the history
  • Loading branch information
anmarchenko committed Apr 17, 2024
1 parent 2044803 commit a0f8989
Show file tree
Hide file tree
Showing 11 changed files with 89 additions and 8 deletions.
2 changes: 2 additions & 0 deletions lib/datadog/ci/ext/test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,9 @@ module Test
# ITR tags
TAG_ITR_TEST_SKIPPING_ENABLED = "test.itr.tests_skipping.enabled"
TAG_ITR_TEST_SKIPPING_TYPE = "test.itr.tests_skipping.type"
TAG_ITR_TEST_SKIPPING_COUNT = "test.itr.tests_skipping.count"
TAG_ITR_SKIPPED_BY_ITR = "test.itr.skipped_by_itr"
TAG_ITR_TESTS_SKIPPED = "_dd.ci.itr.tests_skipped"

# Code coverage tags
TAG_CODE_COVERAGE_ENABLED = "test.code_coverage.enabled"
Expand Down
7 changes: 7 additions & 0 deletions lib/datadog/ci/itr/runner.rb
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,13 @@ def count_skipped_test(test)
end
end

def write_test_session_tags(test_session)
return if !enabled?

test_session.set_tag(Ext::Test::TAG_ITR_TESTS_SKIPPED, @skipped_tests_count.positive?.to_s)
test_session.set_tag(Ext::Test::TAG_ITR_TEST_SKIPPING_COUNT, @skipped_tests_count)
end

def shutdown!
@coverage_writer&.stop
end
Expand Down
4 changes: 2 additions & 2 deletions lib/datadog/ci/test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,9 @@ def name
# Finishes the current test.
# @return [void]
def finish
super

recorder.deactivate_test

super
end

# Running test suite that this test is part of (if any).
Expand Down
4 changes: 2 additions & 2 deletions lib/datadog/ci/test_module.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,9 @@ class TestModule < ConcurrentSpan
# Finishes this test module.
# @return [void]
def finish
super

recorder.deactivate_test_module

super
end
end
end
Expand Down
4 changes: 2 additions & 2 deletions lib/datadog/ci/test_session.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,9 @@ class TestSession < ConcurrentSpan
# Finishes the current test session.
# @return [void]
def finish
super

recorder.deactivate_test_session

super
end

# Return the test session's name which is equal to test command used
Expand Down
4 changes: 2 additions & 2 deletions lib/datadog/ci/test_suite.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,9 @@ def finish
# we try to derive test suite status from execution stats if no status was set explicitly
set_status_from_stats! if undefined?

super

recorder.deactivate_test_suite(name)

super
end
end

Expand Down
7 changes: 7 additions & 0 deletions lib/datadog/ci/test_visibility/recorder.rb
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,9 @@ def deactivate_test
end

def deactivate_test_session
test_session = active_test_session
on_test_session_finished(test_session) if test_session

@global_context.deactivate_test_session!
end

Expand Down Expand Up @@ -405,6 +408,10 @@ def on_test_started(test)
@itr.mark_if_skippable(test)
@itr.start_coverage(test)
end

def on_test_session_finished(test_session)
@itr.write_test_session_tags(test_session)
end
end
end
end
Expand Down
4 changes: 4 additions & 0 deletions sig/datadog/ci/ext/test.rbs
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,12 @@ module Datadog

TAG_ITR_TEST_SKIPPING_TYPE: "test.itr.tests_skipping.type"

TAG_ITR_TEST_SKIPPING_COUNT: "test.itr.tests_skipping.count"

TAG_ITR_SKIPPED_BY_ITR: "test.itr.skipped_by_itr"

TAG_ITR_TESTS_SKIPPED: "_dd.ci.itr.tests_skipped"

TAG_CODE_COVERAGE_ENABLED: "test.code_coverage.enabled"

TAG_TEST_SESSION_ID: "_test.session_id"
Expand Down
2 changes: 2 additions & 0 deletions sig/datadog/ci/itr/runner.rbs
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ module Datadog

def count_skipped_test: (Datadog::CI::Test test) -> void

def write_test_session_tags: (Datadog::CI::TestSession test_session) -> void

def shutdown!: () -> void

private
Expand Down
2 changes: 2 additions & 0 deletions sig/datadog/ci/test_visibility/recorder.rbs
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,8 @@ module Datadog
def on_test_finished: (Datadog::CI::Test test) -> void

def on_test_started: (Datadog::CI::Test test) -> void

def on_test_session_finished: (Datadog::CI::TestSession test_session) -> void
end
end
end
Expand Down
57 changes: 57 additions & 0 deletions spec/datadog/ci/itr/runner_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -337,4 +337,61 @@
end
end
end

describe "#write_test_session_tags" do
let(:test_session_span) do
Datadog::CI::TestSession.new(
Datadog::Tracing::SpanOperation.new("test_session")
)
end

before do
runner.count_skipped_test(test_span)
end

subject { runner.write_test_session_tags(test_session_span) }

let(:test_span) do
Datadog::CI::Test.new(
Datadog::Tracing::SpanOperation.new("test", tags: {"test.status" => "pass"})
)
end

context "when ITR is enabled" do
context "when tests were not skipped" do
it "submits 0 skipped tests" do
subject

expect(test_session_span.get_tag(Datadog::CI::Ext::Test::TAG_ITR_TESTS_SKIPPED)).to eq("false")
expect(test_session_span.get_tag(Datadog::CI::Ext::Test::TAG_ITR_TEST_SKIPPING_COUNT)).to eq(0)
end
end

context "when tests were skipped" do
let(:test_span) do
Datadog::CI::Test.new(
Datadog::Tracing::SpanOperation.new("test", tags: {"test.status" => "skip", "test.itr.skipped_by_itr" => "true"})
)
end

it "submits number of skipped tests" do
subject

expect(test_session_span.get_tag(Datadog::CI::Ext::Test::TAG_ITR_TESTS_SKIPPED)).to eq("true")
expect(test_session_span.get_tag(Datadog::CI::Ext::Test::TAG_ITR_TEST_SKIPPING_COUNT)).to eq(1)
end
end
end

context "when ITR is disabled" do
let(:itr_enabled) { false }

it "does not add ITR tags to the session" do
subject

expect(test_session_span.get_tag(Datadog::CI::Ext::Test::TAG_ITR_TESTS_SKIPPED)).to be_nil
expect(test_session_span.get_tag(Datadog::CI::Ext::Test::TAG_ITR_TEST_SKIPPING_COUNT)).to be_nil
end
end
end
end

0 comments on commit a0f8989

Please sign in to comment.