Skip to content

Commit

Permalink
Merge pull request #166 from DataDog/anmarchenko/custom_configuration…
Browse files Browse the repository at this point in the history
…_tags

[CIVIS-9333] support custom configuration tags supplied from the environment
  • Loading branch information
anmarchenko authored Apr 25, 2024
2 parents f0592d1 + a1f0eb7 commit f042dc4
Show file tree
Hide file tree
Showing 12 changed files with 103 additions and 11 deletions.
7 changes: 6 additions & 1 deletion lib/datadog/ci/configuration/components.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
)
Expand Down
7 changes: 6 additions & 1 deletion lib/datadog/ci/itr/runner.rb
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,15 @@ class Runner

def initialize(
dd_env:,
config_tags: {},
api: nil,
coverage_writer: nil,
enabled: false
)
@enabled = enabled
@api = api
@dd_env = dd_env
@config_tags = config_tags || {}

@test_skipping_enabled = false
@code_coverage_enabled = false
Expand Down Expand Up @@ -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

Expand Down
6 changes: 4 additions & 2 deletions lib/datadog/ci/itr/skippable.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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
}
}
}
Expand Down
6 changes: 4 additions & 2 deletions lib/datadog/ci/transport/remote_settings_api.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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
}
}
}
Expand Down
12 changes: 12 additions & 0 deletions lib/datadog/ci/utils/test_run.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
3 changes: 2 additions & 1 deletion sig/datadog/ci/itr/runner.rbs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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

Expand Down
3 changes: 2 additions & 1 deletion sig/datadog/ci/itr/skippable.rbs
Original file line number Diff line number Diff line change
Expand Up @@ -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?
Expand All @@ -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

Expand Down
3 changes: 2 additions & 1 deletion sig/datadog/ci/transport/remote_settings_api.rbs
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
2 changes: 2 additions & 0 deletions sig/datadog/ci/utils/test_run.rbs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
22 changes: 21 additions & 1 deletion spec/datadog/ci/itr/skippable_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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" }
Expand Down Expand Up @@ -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
19 changes: 18 additions & 1 deletion spec/datadog/ci/transport/remote_settings_api_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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" }
Expand Down Expand Up @@ -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
24 changes: 24 additions & 0 deletions spec/datadog/ci/utils/test_run_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit f042dc4

Please sign in to comment.