From 67c63db1b8ad262bf37beb348a154b7043d4fb02 Mon Sep 17 00:00:00 2001 From: Andrey Date: Wed, 24 Apr 2024 16:36:31 +0200 Subject: [PATCH] add #itr_unskippable! method to a Datadog::CI::Test to mark test_span as unskippable by ITR --- lib/datadog/ci/ext/test.rb | 3 ++- lib/datadog/ci/span.rb | 7 +++++++ lib/datadog/ci/test.rb | 18 ++++++++++++++++++ sig/datadog/ci/ext/test.rbs | 6 ++++-- sig/datadog/ci/span.rbs | 2 ++ sig/datadog/ci/test.rbs | 1 + spec/datadog/ci/span_spec.rb | 8 ++++++++ spec/datadog/ci/test_spec.rb | 28 ++++++++++++++++++++++++++++ 8 files changed, 70 insertions(+), 3 deletions(-) diff --git a/lib/datadog/ci/ext/test.rb b/lib/datadog/ci/ext/test.rb index 838f93c6..7d7c84a7 100644 --- a/lib/datadog/ci/ext/test.rb +++ b/lib/datadog/ci/ext/test.rb @@ -8,7 +8,6 @@ module Ext module Test CONTEXT_ORIGIN = "ciapp-test" - TAG_ARGUMENTS = "test.arguments" TAG_FRAMEWORK = "test.framework" TAG_FRAMEWORK_VERSION = "test.framework_version" TAG_NAME = "test.name" @@ -29,6 +28,8 @@ module Test TAG_ITR_TEST_SKIPPING_COUNT = "test.itr.tests_skipping.count" TAG_ITR_SKIPPED_BY_ITR = "test.skipped_by_itr" TAG_ITR_TESTS_SKIPPED = "_dd.ci.itr.tests_skipped" + TAG_ITR_UNSKIPPABLE = "test.itr.unskippable" + TAG_ITR_FORCED_RUN = "test.itr.forced_run" # Code coverage tags TAG_CODE_COVERAGE_ENABLED = "test.code_coverage.enabled" diff --git a/lib/datadog/ci/span.rb b/lib/datadog/ci/span.rb index 097fc710..f0f44fa2 100644 --- a/lib/datadog/ci/span.rb +++ b/lib/datadog/ci/span.rb @@ -102,6 +102,13 @@ def set_tag(key, value) tracer_span.set_tag(key, value) end + # Removes tag by key. + # @param [String] key the key of the tag. + # @return [void] + def clear_tag(key) + tracer_span.clear_tag(key) + end + # Sets metric value by key. # @param [String] key the key of the metric. # @param [Numeric] value the value of the metric. diff --git a/lib/datadog/ci/test.rb b/lib/datadog/ci/test.rb index c7d936a2..7c228da9 100644 --- a/lib/datadog/ci/test.rb +++ b/lib/datadog/ci/test.rb @@ -69,6 +69,24 @@ def skipped_by_itr? get_tag(Ext::Test::TAG_ITR_SKIPPED_BY_ITR) == "true" end + # Marks this test as unskippable by the intelligent test runner. + # This must be done before the test execution starts. + # + # Examples of tests that should be unskippable: + # - tests that read files from disk + # - tests that make network requests + # - tests that call external processes + # - tests that use forking or threading + # + # @return [void] + def itr_unskippable! + set_tag(Ext::Test::TAG_ITR_UNSKIPPABLE, "true") + if skipped_by_itr? + clear_tag(Ext::Test::TAG_ITR_SKIPPED_BY_ITR) + set_tag(Ext::Test::TAG_ITR_FORCED_RUN, "true") + end + end + # Sets the status of the span to "pass". # @return [void] def passed! diff --git a/sig/datadog/ci/ext/test.rbs b/sig/datadog/ci/ext/test.rbs index 354694c5..b6c39bd2 100644 --- a/sig/datadog/ci/ext/test.rbs +++ b/sig/datadog/ci/ext/test.rbs @@ -4,8 +4,6 @@ module Datadog module Test CONTEXT_ORIGIN: "ciapp-test" - TAG_ARGUMENTS: "test.arguments" - TAG_FRAMEWORK: "test.framework" TAG_FRAMEWORK_VERSION: "test.framework_version" @@ -42,6 +40,10 @@ module Datadog TAG_ITR_TESTS_SKIPPED: "_dd.ci.itr.tests_skipped" + TAG_ITR_UNSKIPPABLE: "test.itr.unskippable" + + TAG_ITR_FORCED_RUN: "test.itr.forced_run" + TAG_CODE_COVERAGE_ENABLED: "test.code_coverage.enabled" TAG_TEST_SESSION_ID: "_test.session_id" diff --git a/sig/datadog/ci/span.rbs b/sig/datadog/ci/span.rbs index 6be70c73..94aaccfe 100644 --- a/sig/datadog/ci/span.rbs +++ b/sig/datadog/ci/span.rbs @@ -31,6 +31,8 @@ module Datadog def set_tag: (String key, untyped? value) -> void + def clear_tag: (String key) -> void + def set_metric: (String key, untyped value) -> void def set_tags: (Hash[untyped, untyped] tags) -> void diff --git a/sig/datadog/ci/test.rbs b/sig/datadog/ci/test.rbs index 00f9c925..62f7b534 100644 --- a/sig/datadog/ci/test.rbs +++ b/sig/datadog/ci/test.rbs @@ -8,6 +8,7 @@ module Datadog def test_module_id: () -> String? def test_session_id: () -> String? def skipped_by_itr?: () -> bool + def itr_unskippable!: () -> void def source_file: () -> String? def parameters: () -> String? diff --git a/spec/datadog/ci/span_spec.rb b/spec/datadog/ci/span_spec.rb index c352f2f3..0dd53baf 100644 --- a/spec/datadog/ci/span_spec.rb +++ b/spec/datadog/ci/span_spec.rb @@ -157,6 +157,14 @@ end end + describe "#clear_tag" do + it "clears the tag" do + expect(tracer_span).to receive(:clear_tag).with("foo") + + span.clear_tag("foo") + end + end + describe "#set_tags" do it "sets the tags" do expect(tracer_span).to receive(:set_tags).with({"foo" => "bar", "baz" => "qux"}) diff --git a/spec/datadog/ci/test_spec.rb b/spec/datadog/ci/test_spec.rb index 417686f0..3372c0f2 100644 --- a/spec/datadog/ci/test_spec.rb +++ b/spec/datadog/ci/test_spec.rb @@ -119,6 +119,34 @@ end end + describe "#itr_unskippable!" do + context "when test is not skipped by ITR" do + before do + allow(ci_test).to receive(:skipped_by_itr?).and_return(false) + end + + it "sets unskippable tag" do + expect(tracer_span).to receive(:set_tag).with(Datadog::CI::Ext::Test::TAG_ITR_UNSKIPPABLE, "true") + + ci_test.itr_unskippable! + end + end + + context "when test is skipped by ITR" do + before do + allow(ci_test).to receive(:skipped_by_itr?).and_return(true) + end + + it "sets unskippable tag, removes skipped by ITR tag, and sets forced run tag" do + expect(tracer_span).to receive(:set_tag).with(Datadog::CI::Ext::Test::TAG_ITR_UNSKIPPABLE, "true") + expect(tracer_span).to receive(:clear_tag).with(Datadog::CI::Ext::Test::TAG_ITR_SKIPPED_BY_ITR) + expect(tracer_span).to receive(:set_tag).with(Datadog::CI::Ext::Test::TAG_ITR_FORCED_RUN, "true") + + ci_test.itr_unskippable! + end + end + end + describe "#set_parameters" do let(:parameters) { {"foo" => "bar", "baz" => "qux"} }