Skip to content

Commit

Permalink
configure ITR::Runner according to remote configuration
Browse files Browse the repository at this point in the history
  • Loading branch information
anmarchenko committed Mar 5, 2024
1 parent a5bc756 commit c52453e
Show file tree
Hide file tree
Showing 9 changed files with 134 additions and 13 deletions.
6 changes: 6 additions & 0 deletions lib/datadog/ci/ext/transport.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,12 @@ module Transport
DD_API_HOST_PREFIX = "api"
DD_API_SETTINGS_PATH = "/api/v2/ci/libraries/tests/services/setting"
DD_API_SETTINGS_TYPE = "ci_app_test_service_libraries_settings"
DD_API_SETTINGS_RESPONSE_DIG_KEYS = %w[data attributes].freeze
DD_API_SETTINGS_RESPONSE_ITR_ENABLED_KEY = "itr_enabled"
DD_API_SETTINGS_RESPONSE_CODE_COVERAGE_KEY = "code_coverage"
DD_API_SETTINGS_RESPONSE_TESTS_SKIPPING_KEY = "tests_skipping"
DD_API_SETTINGS_RESPONSE_REQUIRE_GIT_KEY = "require_git"
DD_API_SETTINGS_RESPONSE_DEFAULT = {DD_API_SETTINGS_RESPONSE_ITR_ENABLED_KEY => false}.freeze

CONTENT_TYPE_MESSAGEPACK = "application/msgpack"
CONTENT_TYPE_JSON = "application/json"
Expand Down
30 changes: 28 additions & 2 deletions lib/datadog/ci/itr/runner.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
# frozen_string_literal: true

require_relative "../ext/transport"

module Datadog
module CI
module ITR
Expand All @@ -11,14 +13,38 @@ def initialize(
enabled: false
)
@enabled = enabled
@test_skipping_enabled = false
@code_coverage_enabled = false
end

def configure(remote_configuration)
@enabled = convert_to_bool(
remote_configuration.fetch(Ext::Transport::DD_API_SETTINGS_RESPONSE_ITR_ENABLED_KEY, false)
)
@test_skipping_enabled = @enabled && convert_to_bool(
remote_configuration.fetch(Ext::Transport::DD_API_SETTINGS_RESPONSE_TESTS_SKIPPING_KEY, false)
)
@code_coverage_enabled = @enabled && convert_to_bool(
remote_configuration.fetch(Ext::Transport::DD_API_SETTINGS_RESPONSE_CODE_COVERAGE_KEY, false)
)
end

def enabled?
@enabled
end

def disable
@enabled = false
def skipping_tests?
@test_skipping_enabled
end

def code_coverage?
@code_coverage_enabled
end

private

def convert_to_bool(value)
value.to_s == "true"
end
end
end
Expand Down
7 changes: 4 additions & 3 deletions lib/datadog/ci/test_visibility/recorder.rb
Original file line number Diff line number Diff line change
Expand Up @@ -191,10 +191,11 @@ def itr_enabled?
private

def configure_library(test_session)
# this will change when EFD is implemented
return unless itr_enabled?
# TODO: error handling when request failed - disable ITR runner
# TODO: configure ITR runner based on response
@api_client.fetch_library_settings(test_session)

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

def skip_tracing(block = nil)
Expand Down
10 changes: 6 additions & 4 deletions lib/datadog/ci/transport/api_client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,15 @@ def ok?
end

def payload
return @json if @json
cached = @json
return cached unless cached.nil?

resp = @http_response
return default_payload if resp.nil? || !resp.ok?
return @json = default_payload if resp.nil? || !resp.ok?

begin
@json = JSON.parse(resp.payload).dig("data", "attributes")
@json = JSON.parse(resp.payload).dig(*Ext::Transport::DD_API_SETTINGS_RESPONSE_DIG_KEYS) ||
default_payload
rescue JSON::ParserError => e
Datadog.logger.error("Failed to parse settings response payload: #{e}. Payload was: #{resp.payload}")
@json = default_payload
Expand All @@ -42,7 +44,7 @@ def payload
private

def default_payload
{"itr_enabled" => false}
Ext::Transport::DD_API_SETTINGS_RESPONSE_DEFAULT
end
end

Expand Down
12 changes: 12 additions & 0 deletions sig/datadog/ci/ext/transport.rbs
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,18 @@ module Datadog

DD_API_SETTINGS_TYPE: "ci_app_test_service_libraries_settings"

DD_API_SETTINGS_RESPONSE_DIG_KEYS: Array[String]

DD_API_SETTINGS_RESPONSE_ITR_ENABLED_KEY: "itr_enabled"

DD_API_SETTINGS_RESPONSE_CODE_COVERAGE_KEY: "code_coverage"

DD_API_SETTINGS_RESPONSE_TESTS_SKIPPING_KEY: "tests_skipping"

DD_API_SETTINGS_RESPONSE_REQUIRE_GIT_KEY: "require_git"

DD_API_SETTINGS_RESPONSE_DEFAULT: Hash[String, untyped]

CONTENT_TYPE_MESSAGEPACK: "application/msgpack"

CONTENT_TYPE_JSON: "application/json"
Expand Down
12 changes: 11 additions & 1 deletion sig/datadog/ci/itr/runner.rbs
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,22 @@ module Datadog
module ITR
class Runner
@enabled: bool
@test_skipping_enabled: bool
@code_coverage_enabled: bool

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

def configure: (Hash[String, untyped] remote_configuration) -> void

def enabled?: () -> bool

def disable: () -> void
def skipping_tests?: () -> bool

def code_coverage: () -> bool

private

def convert_to_bool: (untyped value) -> bool
end
end
end
Expand Down
2 changes: 2 additions & 0 deletions sig/datadog/ci/transport/api_client.rbs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ module Datadog

def ok?: () -> bool

def payload: () -> Hash[String, untyped]

private

def default_payload: () -> Hash[String, untyped]
Expand Down
46 changes: 43 additions & 3 deletions spec/datadog/ci/itr/runner_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,49 @@

subject(:runner) { described_class.new(enabled: itr_enabled) }

describe "#disable" do
it "disables the runner" do
expect { runner.disable }.to change { runner.enabled? }.from(true).to(false)
describe "#configure" do
before do
runner.configure(remote_configuration)
end

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

it "configures the runner" do
expect(runner.enabled?).to be false
expect(runner.skipping_tests?).to be false
expect(runner.code_coverage?).to be false
end
end

context "when remote configuration call returned correct response" do
let(:remote_configuration) { {"itr_enabled" => true, "code_coverage" => true, "tests_skipping" => false} }

it "configures the runner" do
expect(runner.enabled?).to be true
expect(runner.skipping_tests?).to be false
expect(runner.code_coverage?).to be true
end
end

context "when remote configuration call returned correct response with strings instead of bools" do
let(:remote_configuration) { {"itr_enabled" => "true", "code_coverage" => "true", "tests_skipping" => "false"} }

it "configures the runner" do
expect(runner.enabled?).to be true
expect(runner.skipping_tests?).to be false
expect(runner.code_coverage?).to be true
end
end

context "when remote configuration call returns empty hash" do
let(:remote_configuration) { {} }

it "configures the runner" do
expect(runner.enabled?).to be false
expect(runner.skipping_tests?).to be false
expect(runner.code_coverage?).to be false
end
end
end
end
22 changes: 22 additions & 0 deletions spec/datadog/ci/transport/api_client_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,28 @@
expect(response.payload).to eq("itr_enabled" => false)
end
end

context "when response is OK but JSON has different format" do
let(:http_response) do
double(
"http_response",
ok?: true,
payload: {
"attributes" => {
"code_coverage" => true,
"tests_skipping" => false,
"itr_enabled" => true,
"require_git" => false
}
}.to_json
)
end

it "parses the response" do
expect(response.ok?).to be true
expect(response.payload).to eq("itr_enabled" => false)
end
end
end

context "when there is no api" do
Expand Down

0 comments on commit c52453e

Please sign in to comment.