Skip to content

Commit

Permalink
add ITR tags to TestSession object when configuring ITR
Browse files Browse the repository at this point in the history
  • Loading branch information
anmarchenko committed Mar 5, 2024
1 parent 04be43d commit fb76b51
Show file tree
Hide file tree
Showing 10 changed files with 522 additions and 398 deletions.
9 changes: 9 additions & 0 deletions lib/datadog/ci/ext/test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,13 @@ module Test
TAG_CODEOWNERS = "test.codeowners"
TAG_PARAMETERS = "test.parameters"

# ITR tags
TAG_ITR_TEST_SKIPPING_ENABLED = "test.itr.tests_skipping.enabled"
TAG_ITR_TEST_SKIPPING_TYPE = "test.itr.tests_skipping.type"

# Code coverage tags
TAG_CODE_COVERAGE_ENABLED = "test.code_coverage.enabled"

# those tags are special and used to correlate tests with the test sessions, suites, and modules
# they are transient and not sent to the backend
TAG_TEST_SESSION_ID = "_test.session_id"
Expand All @@ -43,6 +50,8 @@ module Test
TAG_SPAN_KIND = "span.kind"
SPAN_KIND_TEST = "test"

ITR_TEST_SKIPPING_MODE = "test"

# test status as recognized by Datadog
module Status
PASS = "pass"
Expand Down
11 changes: 10 additions & 1 deletion lib/datadog/ci/itr/runner.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
# frozen_string_literal: true

require_relative "../ext/test"
require_relative "../ext/transport"

module Datadog
Expand All @@ -17,7 +18,7 @@ def initialize(
@code_coverage_enabled = false
end

def configure(remote_configuration)
def configure(remote_configuration, test_session)
@enabled = convert_to_bool(
remote_configuration.fetch(Ext::Transport::DD_API_SETTINGS_RESPONSE_ITR_ENABLED_KEY, false)
)
Expand All @@ -27,6 +28,14 @@ def configure(remote_configuration)
@code_coverage_enabled = @enabled && convert_to_bool(
remote_configuration.fetch(Ext::Transport::DD_API_SETTINGS_RESPONSE_CODE_COVERAGE_KEY, false)
)

test_session.set_tag(Ext::Test::TAG_ITR_TEST_SKIPPING_ENABLED, @test_skipping_enabled)
# currently we set this tag when ITR requires collecting code coverage
# this will change as soon as we implement total code coverage support in this library
test_session.set_tag(Ext::Test::TAG_CODE_COVERAGE_ENABLED, @code_coverage_enabled)

# we skip tests, not suites
test_session.set_tag(Ext::Test::TAG_ITR_TEST_SKIPPING_TYPE, Ext::Test::ITR_TEST_SKIPPING_MODE)
end

def enabled?
Expand Down
8 changes: 8 additions & 0 deletions lib/datadog/ci/test_session.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,14 @@ def name
get_tag(Ext::Test::TAG_COMMAND)
end

def skipping_tests?
get_tag(Ext::Test::TAG_ITR_TEST_SKIPPING_ENABLED) == "true"
end

def code_coverage?
get_tag(Ext::Test::TAG_CODE_COVERAGE_ENABLED) == "true"
end

# Return the test session tags that could be inherited by sub-spans
# @return [Hash] the tags to be inherited by sub-spans.
def inheritable_tags
Expand Down
2 changes: 1 addition & 1 deletion lib/datadog/ci/test_visibility/recorder.rb
Original file line number Diff line number Diff line change
Expand Up @@ -194,7 +194,7 @@ def configure_library(test_session)
return unless itr_enabled?

remote_configuration = @remote_settings_api.fetch_library_settings(test_session)
@itr.configure(remote_configuration.payload)
@itr.configure(remote_configuration.payload, test_session)
end

def skip_tracing(block = nil)
Expand Down
8 changes: 8 additions & 0 deletions sig/datadog/ci/ext/test.rbs
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,12 @@ module Datadog

TAG_PARAMETERS: "test.parameters"

TAG_ITR_TEST_SKIPPING_ENABLED: "test.itr.tests_skipping.enabled"

TAG_ITR_TEST_SKIPPING_TYPE: "test.itr.tests_skipping.type"

TAG_CODE_COVERAGE_ENABLED: "test.code_coverage.enabled"

TAG_TEST_SESSION_ID: "_test.session_id"

TAG_TEST_MODULE_ID: "_test.module_id"
Expand All @@ -54,6 +60,8 @@ module Datadog

SPAN_KIND_TEST: "test"

ITR_TEST_SKIPPING_MODE: "test"

module Status
PASS: "pass"

Expand Down
2 changes: 1 addition & 1 deletion sig/datadog/ci/itr/runner.rbs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ module Datadog

def initialize: (?enabled: bool) -> void

def configure: (Hash[String, untyped] remote_configuration) -> void
def configure: (Hash[String, untyped] remote_configuration, Datadog::CI::TestSession test_session) -> void

def enabled?: () -> bool

Expand Down
15 changes: 13 additions & 2 deletions spec/datadog/ci/itr/runner_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,17 @@
subject(:runner) { described_class.new(enabled: itr_enabled) }

describe "#configure" do
let(:tracer_span) { Datadog::Tracing::SpanOperation.new("session") }
let(:test_session) { Datadog::CI::TestSession.new(tracer_span) }

before do
runner.configure(remote_configuration)
runner.configure(remote_configuration, test_session)
end

context "when remote configuration call failed" do
let(:remote_configuration) { {"itr_enabled" => false} }

it "configures the runner" do
it "configures the runner and test session" do
expect(runner.enabled?).to be false
expect(runner.skipping_tests?).to be false
expect(runner.code_coverage?).to be false
Expand All @@ -30,6 +33,14 @@
expect(runner.skipping_tests?).to be false
expect(runner.code_coverage?).to be true
end

it "sets test session tags" do
expect(test_session.skipping_tests?).to be false
expect(test_session.code_coverage?).to be true
expect(test_session.get_tag(Datadog::CI::Ext::Test::TAG_ITR_TEST_SKIPPING_TYPE)).to eq(
Datadog::CI::Ext::Test::ITR_TEST_SKIPPING_MODE
)
end
end

context "when remote configuration call returned correct response with strings instead of bools" do
Expand Down
53 changes: 44 additions & 9 deletions spec/datadog/ci/test_session_spec.rb
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
# frozen_string_literal: true

RSpec.describe Datadog::CI::TestSession do
let(:tracer_span) { instance_double(Datadog::Tracing::SpanOperation, finish: true) }
let(:tracer_span) { Datadog::Tracing::SpanOperation.new("session") }
let(:recorder) { spy("recorder") }

before { allow_any_instance_of(described_class).to receive(:recorder).and_return(recorder) }
subject(:ci_test_session) { described_class.new(tracer_span) }

describe "#finish" do
subject(:ci_test_session) { described_class.new(tracer_span) }

it "deactivates the test session" do
ci_test_session.finish

Expand All @@ -19,11 +18,9 @@
describe "#inheritable_tags" do
subject(:inheritable_tags) { ci_test_session.inheritable_tags }

let(:ci_test_session) { described_class.new(tracer_span) }

before do
Datadog::CI::Ext::Test::INHERITABLE_TAGS.each do |tag|
allow(tracer_span).to receive(:get_tag).with(tag).and_return("value for #{tag}")
tracer_span.set_tag(tag, "value for #{tag}")
end
end

Expand All @@ -39,12 +36,50 @@
describe "#name" do
subject(:name) { ci_test_session.name }

let(:ci_test_session) { described_class.new(tracer_span) }

before do
allow(tracer_span).to receive(:get_tag).with(Datadog::CI::Ext::Test::TAG_COMMAND).and_return("test command")
tracer_span.set_tag(Datadog::CI::Ext::Test::TAG_COMMAND, "test command")
end

it { is_expected.to eq("test command") }
end

describe "#skipping_tests?" do
subject(:skipping_tests?) { ci_test_session.skipping_tests? }

context "when not set" do
it { is_expected.to be false }
end

context "when true" do
before { tracer_span.set_tag(Datadog::CI::Ext::Test::TAG_ITR_TEST_SKIPPING_ENABLED, true) }

it { is_expected.to be true }
end

context "when false" do
before { tracer_span.set_tag(Datadog::CI::Ext::Test::TAG_ITR_TEST_SKIPPING_ENABLED, false) }

it { is_expected.to be false }
end
end

describe "#code_coverage?" do
subject(:code_coverage?) { ci_test_session.code_coverage? }

context "when not set" do
it { is_expected.to be false }
end

context "when true" do
before { tracer_span.set_tag(Datadog::CI::Ext::Test::TAG_CODE_COVERAGE_ENABLED, true) }

it { is_expected.to be true }
end

context "when false" do
before { tracer_span.set_tag(Datadog::CI::Ext::Test::TAG_CODE_COVERAGE_ENABLED, false) }

it { is_expected.to be false }
end
end
end
Loading

0 comments on commit fb76b51

Please sign in to comment.