diff --git a/lib/datadog/ci/configuration/components.rb b/lib/datadog/ci/configuration/components.rb index 329f9546..4ae191fc 100644 --- a/lib/datadog/ci/configuration/components.rb +++ b/lib/datadog/ci/configuration/components.rb @@ -13,6 +13,7 @@ require_relative "../test_visibility/transport" require_relative "../transport/api/builder" require_relative "../transport/remote_settings_api" +require_relative "../utils/test_run" require_relative "../worker" module Datadog @@ -90,14 +91,18 @@ def activate_ci!(settings) settings.tracing.test_mode.writer_options = writer_options + custom_configuration_tags = Utils::TestRun.custom_configuration(settings.tags) + remote_settings_api = Transport::RemoteSettingsApi.new( api: test_visibility_api, - dd_env: settings.env + dd_env: settings.env, + config_tags: custom_configuration_tags ) itr = ITR::Runner.new( api: test_visibility_api, dd_env: settings.env, + config_tags: custom_configuration_tags, coverage_writer: coverage_writer, enabled: settings.ci.enabled && settings.ci.itr_enabled ) diff --git a/lib/datadog/ci/itr/runner.rb b/lib/datadog/ci/itr/runner.rb index abd1f68f..e7d3b01b 100644 --- a/lib/datadog/ci/itr/runner.rb +++ b/lib/datadog/ci/itr/runner.rb @@ -27,6 +27,7 @@ class Runner def initialize( dd_env:, + config_tags: {}, api: nil, coverage_writer: nil, enabled: false @@ -34,6 +35,7 @@ def initialize( @enabled = enabled @api = api @dd_env = dd_env + @config_tags = config_tags || {} @test_skipping_enabled = false @code_coverage_enabled = false @@ -191,7 +193,10 @@ def fetch_skippable_tests(test_session:, git_tree_upload_worker:) # we can only request skippable tests if git metadata is already uploaded git_tree_upload_worker.wait_until_done - skippable_response = Skippable.new(api: @api, dd_env: @dd_env).fetch_skippable_tests(test_session) + skippable_response = + Skippable.new(api: @api, dd_env: @dd_env, config_tags: @config_tags) + .fetch_skippable_tests(test_session) + @correlation_id = skippable_response.correlation_id @skippable_tests = skippable_response.tests diff --git a/lib/datadog/ci/itr/skippable.rb b/lib/datadog/ci/itr/skippable.rb index a1382ad0..c0bcbb27 100644 --- a/lib/datadog/ci/itr/skippable.rb +++ b/lib/datadog/ci/itr/skippable.rb @@ -57,9 +57,10 @@ def payload end end - def initialize(dd_env:, api: nil) + def initialize(dd_env:, api: nil, config_tags: {}) @api = api @dd_env = dd_env + @config_tags = config_tags end def fetch_skippable_tests(test_session) @@ -94,7 +95,8 @@ def payload(test_session) Ext::Test::TAG_OS_ARCHITECTURE => test_session.os_architecture, Ext::Test::TAG_OS_VERSION => test_session.os_version, Ext::Test::TAG_RUNTIME_NAME => test_session.runtime_name, - Ext::Test::TAG_RUNTIME_VERSION => test_session.runtime_version + Ext::Test::TAG_RUNTIME_VERSION => test_session.runtime_version, + "custom" => @config_tags } } } diff --git a/lib/datadog/ci/transport/remote_settings_api.rb b/lib/datadog/ci/transport/remote_settings_api.rb index 03ac76a1..e07e999f 100644 --- a/lib/datadog/ci/transport/remote_settings_api.rb +++ b/lib/datadog/ci/transport/remote_settings_api.rb @@ -51,9 +51,10 @@ def default_payload end end - def initialize(dd_env:, api: nil) + def initialize(dd_env:, api: nil, config_tags: {}) @api = api @dd_env = dd_env + @config_tags = config_tags || {} end def fetch_library_settings(test_session) @@ -90,7 +91,8 @@ def payload(test_session) Ext::Test::TAG_OS_ARCHITECTURE => test_session.os_architecture, Ext::Test::TAG_OS_VERSION => test_session.os_version, Ext::Test::TAG_RUNTIME_NAME => test_session.runtime_name, - Ext::Test::TAG_RUNTIME_VERSION => test_session.runtime_version + Ext::Test::TAG_RUNTIME_VERSION => test_session.runtime_version, + "custom" => @config_tags } } } diff --git a/lib/datadog/ci/utils/test_run.rb b/lib/datadog/ci/utils/test_run.rb index eb494182..53da3ab9 100644 --- a/lib/datadog/ci/utils/test_run.rb +++ b/lib/datadog/ci/utils/test_run.rb @@ -22,6 +22,18 @@ def self.test_parameters(arguments: {}, metadata: {}) } ) end + + def self.custom_configuration(env_tags) + return {} if env_tags.nil? + + res = {} + env_tags.each do |tag, value| + next unless tag.start_with?("test.configuration.") + + res[tag.sub("test.configuration.", "")] = value + end + res + end end end end diff --git a/sig/datadog/ci/itr/runner.rbs b/sig/datadog/ci/itr/runner.rbs index eaecd27b..f76a308d 100644 --- a/sig/datadog/ci/itr/runner.rbs +++ b/sig/datadog/ci/itr/runner.rbs @@ -13,6 +13,7 @@ module Datadog @api: Datadog::CI::Transport::Api::Base? @dd_env: String? + @config_tags: Hash[String, String] @skipped_tests_count: Integer @mutex: Thread::Mutex @@ -21,7 +22,7 @@ module Datadog attr_reader skipped_tests_count: Integer attr_reader correlation_id: String? - def initialize: (dd_env: String?, ?enabled: bool, coverage_writer: Datadog::CI::ITR::Coverage::Writer?, api: Datadog::CI::Transport::Api::Base?) -> void + def initialize: (dd_env: String?, ?enabled: bool, coverage_writer: Datadog::CI::ITR::Coverage::Writer?, api: Datadog::CI::Transport::Api::Base?, ?config_tags: Hash[String, String]?) -> void def configure: (Hash[String, untyped] remote_configuration, test_session: Datadog::CI::TestSession, git_tree_upload_worker: Datadog::CI::Worker) -> void diff --git a/sig/datadog/ci/itr/skippable.rbs b/sig/datadog/ci/itr/skippable.rbs index 15c81922..f29e893c 100644 --- a/sig/datadog/ci/itr/skippable.rbs +++ b/sig/datadog/ci/itr/skippable.rbs @@ -4,6 +4,7 @@ module Datadog class Skippable @api: Datadog::CI::Transport::Api::Base? @dd_env: String? + @config_tags: Hash[String, String] class Response @http_response: Datadog::Core::Transport::HTTP::Adapters::Net::Response? @@ -22,7 +23,7 @@ module Datadog def payload: () -> Hash[String, untyped] end - def initialize: (?api: Datadog::CI::Transport::Api::Base?, dd_env: String?) -> void + def initialize: (?api: Datadog::CI::Transport::Api::Base?, dd_env: String?, ?config_tags: Hash[String, String]) -> void def fetch_skippable_tests: (Datadog::CI::TestSession test_session) -> Response diff --git a/sig/datadog/ci/transport/remote_settings_api.rbs b/sig/datadog/ci/transport/remote_settings_api.rbs index 1c655f0e..925066bf 100644 --- a/sig/datadog/ci/transport/remote_settings_api.rbs +++ b/sig/datadog/ci/transport/remote_settings_api.rbs @@ -21,8 +21,9 @@ module Datadog @api: Datadog::CI::Transport::Api::Base? @dd_env: String? + @config_tags: Hash[String, String] - def initialize: (?api: Datadog::CI::Transport::Api::Base?, dd_env: String?) -> void + def initialize: (?api: Datadog::CI::Transport::Api::Base?, dd_env: String?, ?config_tags: Hash[String, String]?) -> void def fetch_library_settings: (Datadog::CI::TestSession test_session) -> Response diff --git a/sig/datadog/ci/utils/test_run.rbs b/sig/datadog/ci/utils/test_run.rbs index 71287469..9b78c27b 100644 --- a/sig/datadog/ci/utils/test_run.rbs +++ b/sig/datadog/ci/utils/test_run.rbs @@ -9,6 +9,8 @@ module Datadog def self.skippable_test_id: (String test_name, String? test_suite, ?String? parameters) -> String def self.test_parameters: (?arguments: Hash[untyped, untyped], ?metadata: Hash[untyped, untyped]) -> String + + def self.custom_configuration: (Hash[String, String]? env_tags) -> Hash[String, String] end end end diff --git a/spec/datadog/ci/itr/skippable_spec.rb b/spec/datadog/ci/itr/skippable_spec.rb index e778cb86..4cdf7982 100644 --- a/spec/datadog/ci/itr/skippable_spec.rb +++ b/spec/datadog/ci/itr/skippable_spec.rb @@ -5,8 +5,9 @@ RSpec.describe Datadog::CI::ITR::Skippable do let(:api) { spy("api") } let(:dd_env) { "ci" } + let(:config_tags) { {} } - subject(:client) { described_class.new(api: api, dd_env: dd_env) } + subject(:client) { described_class.new(api: api, dd_env: dd_env, config_tags: config_tags) } describe "#fetch_skippable_tests" do let(:service) { "service" } @@ -176,5 +177,24 @@ end end end + + context "when there are custom configurations" do + let(:config_tags) do + { + "tag1" => "value1" + } + end + + it "requests the skippable tests with custom configurations" do + client.fetch_skippable_tests(test_session) + + expect(api).to have_received(:api_request) do |args| + data = JSON.parse(args[:payload])["data"] + configurations = data["attributes"]["configurations"] + + expect(configurations["custom"]).to eq("tag1" => "value1") + end + end + end end end diff --git a/spec/datadog/ci/transport/remote_settings_api_spec.rb b/spec/datadog/ci/transport/remote_settings_api_spec.rb index f9c52091..b89e3ff6 100644 --- a/spec/datadog/ci/transport/remote_settings_api_spec.rb +++ b/spec/datadog/ci/transport/remote_settings_api_spec.rb @@ -5,8 +5,9 @@ RSpec.describe Datadog::CI::Transport::RemoteSettingsApi do let(:api) { spy("api") } let(:dd_env) { "ci" } + let(:config_tags) { {} } - subject(:client) { described_class.new(api: api, dd_env: dd_env) } + subject(:client) { described_class.new(api: api, dd_env: dd_env, config_tags: config_tags) } describe "#fetch_library_settings" do let(:service) { "service" } @@ -175,5 +176,21 @@ end end end + + context "with custom configuration" do + let(:config_tags) { {"key" => "value"} } + + it "requests the settings" do + client.fetch_library_settings(test_session) + + expect(api).to have_received(:api_request) do |args| + data = JSON.parse(args[:payload])["data"] + + attributes = data["attributes"] + configurations = attributes["configurations"] + expect(configurations["custom"]).to eq("key" => "value") + end + end + end end end diff --git a/spec/datadog/ci/utils/test_run_spec.rb b/spec/datadog/ci/utils/test_run_spec.rb index b2e7b93d..a6ead39e 100644 --- a/spec/datadog/ci/utils/test_run_spec.rb +++ b/spec/datadog/ci/utils/test_run_spec.rb @@ -34,4 +34,28 @@ ) end end + + describe ".custom_configuration" do + subject { described_class.custom_configuration(env_tags) } + + context "when env_tags is nil" do + let(:env_tags) { nil } + + it { is_expected.to eq({}) } + end + + context "when env_tags is not nil" do + let(:env_tags) do + { + "test.configuration.tag1" => "value1", + "test.configuration.tag2" => "value2", + "tag3" => "value3", + "test.configurations.tag4" => "value4", + "test.configuration" => "value5" + } + end + + it { is_expected.to eq({"tag1" => "value1", "tag2" => "value2"}) } + end + end end