From e6c4fe6283083a32bd52131a4ff66772940b3b5f Mon Sep 17 00:00:00 2001 From: Andrey Marchenko Date: Mon, 16 Oct 2023 17:19:04 +0200 Subject: [PATCH 01/24] first take on API concept for test visibility --- lib/datadog/ci/transport/api/base.rb | 22 +++++++ lib/datadog/ci/transport/api/builder.rb | 21 ++++++ lib/datadog/ci/transport/api/ci_intake.rb | 46 +++++++++++++ sig/datadog/ci/transport/api/base.rbs | 17 +++++ sig/datadog/ci/transport/api/builder.rbs | 11 ++++ sig/datadog/ci/transport/api/ci_intake.rbs | 23 +++++++ spec/datadog/ci/transport/api/builder_spec.rb | 66 +++++++++++++++++++ .../ci/transport/api/ci_intake_spec.rb | 40 +++++++++++ 8 files changed, 246 insertions(+) create mode 100644 lib/datadog/ci/transport/api/base.rb create mode 100644 lib/datadog/ci/transport/api/builder.rb create mode 100644 lib/datadog/ci/transport/api/ci_intake.rb create mode 100644 sig/datadog/ci/transport/api/base.rbs create mode 100644 sig/datadog/ci/transport/api/builder.rbs create mode 100644 sig/datadog/ci/transport/api/ci_intake.rbs create mode 100644 spec/datadog/ci/transport/api/builder_spec.rb create mode 100644 spec/datadog/ci/transport/api/ci_intake_spec.rb diff --git a/lib/datadog/ci/transport/api/base.rb b/lib/datadog/ci/transport/api/base.rb new file mode 100644 index 00000000..7ffc9703 --- /dev/null +++ b/lib/datadog/ci/transport/api/base.rb @@ -0,0 +1,22 @@ +# frozen_string_literal: true + +module Datadog + module CI + module Transport + module Api + class Base + def request(path:, payload:, verb: "post") + end + + private + + def headers + @headers ||= { + Ext::Transport::HEADER_CONTENT_TYPE => Ext::Transport::CONTENT_TYPE_MESSAGEPACK + } + end + end + end + end + end +end diff --git a/lib/datadog/ci/transport/api/builder.rb b/lib/datadog/ci/transport/api/builder.rb new file mode 100644 index 00000000..abed4711 --- /dev/null +++ b/lib/datadog/ci/transport/api/builder.rb @@ -0,0 +1,21 @@ +# frozen_string_literal: true + +require_relative "ci_intake" + +module Datadog + module CI + module Transport + module Api + module Builder + def self.build_ci_test_cycle_api(settings) + dd_site = settings.site || "datadoghq.com" + url = settings.ci.agentless_url || + "https://#{Ext::Transport::TEST_VISIBILITY_INTAKE_HOST_PREFIX}.#{dd_site}:443" + + CIIntake.new(api_key: settings.api_key, url: url) + end + end + end + end + end +end diff --git a/lib/datadog/ci/transport/api/ci_intake.rb b/lib/datadog/ci/transport/api/ci_intake.rb new file mode 100644 index 00000000..8175d551 --- /dev/null +++ b/lib/datadog/ci/transport/api/ci_intake.rb @@ -0,0 +1,46 @@ +# frozen_string_literal: true + +require_relative "base" + +module Datadog + module CI + module Transport + module Api + class CIIntake < Base + attr_reader :api_key, :http + + def initialize(api_key:, url:) + @api_key = api_key + + uri = URI.parse(url) + raise "Invalid agentless mode URL: #{url}" if uri.host.nil? + + @http = Datadog::CI::Transport::HTTP.new( + host: uri.host, + port: uri.port, + ssl: uri.scheme == "https" || uri.port == 443, + compress: true + ) + end + + def request(path:, payload:, verb: "post") + http.request( + path: path, + payload: payload, + method: verb, + headers: headers + ) + end + + private + + def headers + headers = super + headers[Ext::Transport::HEADER_DD_API_KEY] = api_key + headers + end + end + end + end + end +end diff --git a/sig/datadog/ci/transport/api/base.rbs b/sig/datadog/ci/transport/api/base.rbs new file mode 100644 index 00000000..32dfd6bc --- /dev/null +++ b/sig/datadog/ci/transport/api/base.rbs @@ -0,0 +1,17 @@ +module Datadog + module CI + module Transport + module Api + class Base + @headers: Hash[String, String] + + def request: (path: String, payload: String, ?verb: ::String) -> untyped + + private + + def headers: () -> Hash[String, String] + end + end + end + end +end diff --git a/sig/datadog/ci/transport/api/builder.rbs b/sig/datadog/ci/transport/api/builder.rbs new file mode 100644 index 00000000..194c12e2 --- /dev/null +++ b/sig/datadog/ci/transport/api/builder.rbs @@ -0,0 +1,11 @@ +module Datadog + module CI + module Transport + module Api + module Builder + def self.build_ci_test_cycle_api: (untyped settings) -> Datadog::CI::Transport::Api::CIIntake + end + end + end + end +end diff --git a/sig/datadog/ci/transport/api/ci_intake.rbs b/sig/datadog/ci/transport/api/ci_intake.rbs new file mode 100644 index 00000000..4fd8d5b3 --- /dev/null +++ b/sig/datadog/ci/transport/api/ci_intake.rbs @@ -0,0 +1,23 @@ +module Datadog + module CI + module Transport + module Api + class CIIntake < Base + attr_reader api_key: String + attr_reader http: Datadog::CI::Transport::HTTP + + @api_key: String + @http: Datadog::CI::Transport::HTTP + + def initialize: (api_key: String, url: String) -> void + + def request: (path: String, payload: String, ?verb: ::String) -> Datadog::CI::Transport::HTTP::Response + + private + + def headers: () -> Hash[String, String] + end + end + end + end +end diff --git a/spec/datadog/ci/transport/api/builder_spec.rb b/spec/datadog/ci/transport/api/builder_spec.rb new file mode 100644 index 00000000..2ee67c15 --- /dev/null +++ b/spec/datadog/ci/transport/api/builder_spec.rb @@ -0,0 +1,66 @@ +require_relative "../../../../../lib/datadog/ci/transport/api/builder" + +RSpec.describe Datadog::CI::Transport::Api::Builder do + let(:settings) do + # When 'datadog/ci' is required, it automatically extends Settings. + if Datadog::Core::Configuration::Settings <= Datadog::CI::Configuration::Settings + Datadog::Core::Configuration::Settings.new + else + Datadog::Core::Configuration::Settings.new.tap do |settings| + settings.extend(Datadog::CI::Configuration::Settings) + end + end + end + + describe ".build_ci_test_cycle_api" do + subject { described_class.build_ci_test_cycle_api(settings) } + + let(:agentless_url) { nil } + let(:dd_site) { nil } + let(:api_key) { "api_key" } + + before do + # Stub CI mode behavior + allow(settings.ci) + .to receive(:agentless_url) + .and_return(agentless_url) + + allow(settings) + .to receive(:site) + .and_return(dd_site) + + allow(settings) + .to receive(:api_key) + .and_return(api_key) + end + + it "creates CI Intake" do + expect(Datadog::CI::Transport::Api::CIIntake).to receive(:new).with( + api_key: "api_key", url: "https://citestcycle-intake.datadoghq.com:443" + ) + subject + end + + context "when agentless_url is provided" do + let(:agentless_url) { "http://localhost:5555" } + + it "configures transport to use intake URL from settings" do + expect(Datadog::CI::Transport::Api::CIIntake).to receive(:new).with( + api_key: "api_key", url: "http://localhost:5555" + ) + subject + end + end + + context "when dd_site is provided" do + let(:dd_site) { "datadoghq.eu" } + + it "construct intake url using provided host" do + expect(Datadog::CI::Transport::Api::CIIntake).to receive(:new).with( + api_key: "api_key", url: "https://citestcycle-intake.datadoghq.eu:443" + ) + subject + end + end + end +end diff --git a/spec/datadog/ci/transport/api/ci_intake_spec.rb b/spec/datadog/ci/transport/api/ci_intake_spec.rb new file mode 100644 index 00000000..3398bc94 --- /dev/null +++ b/spec/datadog/ci/transport/api/ci_intake_spec.rb @@ -0,0 +1,40 @@ +require_relative "../../../../../lib/datadog/ci/transport/api/ci_intake" + +RSpec.describe Datadog::CI::Transport::Api::CIIntake do + subject do + described_class.new( + api_key: api_key, + url: url + ) + end + + let(:api_key) { "api_key" } + let(:url) { "https://citestcycle-intake.datad0ghq.com:443" } + + let(:http) { double(:http) } + + before do + expect(Datadog::CI::Transport::HTTP).to receive(:new).with( + host: "citestcycle-intake.datad0ghq.com", + port: 443, + ssl: true, + compress: true + ).and_return(http) + end + + describe "#request" do + it "produces correct headers and forwards request to HTTP layer" do + expect(http).to receive(:request).with( + path: "path", + payload: "payload", + method: "post", + headers: { + "DD-API-KEY" => "api_key", + "Content-Type" => "application/msgpack" + } + ) + + subject.request(path: "path", payload: "payload") + end + end +end From c17819c9b7cf38743103482bbe77ae2e0dd117de Mon Sep 17 00:00:00 2001 From: Andrey Marchenko Date: Tue, 17 Oct 2023 08:51:22 +0200 Subject: [PATCH 02/24] TestVisibility::Transport: rename env parameter to dd_env --- lib/datadog/ci/test_visibility/transport.rb | 12 ++++++------ sig/datadog/ci/test_visibility/transport.rbs | 6 +++--- spec/datadog/ci/test_visibility/transport_spec.rb | 8 ++++---- 3 files changed, 13 insertions(+), 13 deletions(-) diff --git a/lib/datadog/ci/test_visibility/transport.rb b/lib/datadog/ci/test_visibility/transport.rb index 472280b8..7d5ddd4d 100644 --- a/lib/datadog/ci/test_visibility/transport.rb +++ b/lib/datadog/ci/test_visibility/transport.rb @@ -23,19 +23,19 @@ class Transport :api_key, :max_payload_size, :http, - :env + :dd_env def initialize( api_key:, url:, - env: nil, + dd_env: nil, serializers_factory: Datadog::CI::TestVisibility::Serializers::Factories::TestLevel, max_payload_size: DEFAULT_MAX_PAYLOAD_SIZE ) @serializers_factory = serializers_factory @api_key = api_key @max_payload_size = max_payload_size - @env = env + @dd_env = dd_env uri = URI.parse(url) @@ -141,12 +141,12 @@ def pack_events(encoded_events) packer.write_map_header(1) packer.write("*") - metadata_fields_count = env ? 4 : 3 + metadata_fields_count = dd_env ? 4 : 3 packer.write_map_header(metadata_fields_count) - if env + if dd_env packer.write("env") - packer.write(env) + packer.write(dd_env) end packer.write("runtime-id") diff --git a/sig/datadog/ci/test_visibility/transport.rbs b/sig/datadog/ci/test_visibility/transport.rbs index 774564c2..c528ba55 100644 --- a/sig/datadog/ci/test_visibility/transport.rbs +++ b/sig/datadog/ci/test_visibility/transport.rbs @@ -6,12 +6,12 @@ module Datadog attr_reader serializers_factory: singleton(Datadog::CI::TestVisibility::Serializers::Factories::TestLevel) attr_reader api_key: String - attr_reader env: String? + attr_reader dd_env: String? attr_reader http: Datadog::CI::Transport::HTTP attr_reader max_payload_size: Integer @api_key: String - @env: String? + @dd_env: String? @http: Datadog::CI::Transport::HTTP @serializers_factory: singleton(Datadog::CI::TestVisibility::Serializers::Factories::TestLevel) @max_payload_size: Integer @@ -19,7 +19,7 @@ module Datadog def initialize: ( api_key: String, url: ::String, - ?env: ::String?, + ?dd_env: ::String?, ?serializers_factory: singleton(Datadog::CI::TestVisibility::Serializers::Factories::TestLevel), ?max_payload_size: Integer ) -> void diff --git a/spec/datadog/ci/test_visibility/transport_spec.rb b/spec/datadog/ci/test_visibility/transport_spec.rb index a775da46..0c099ec5 100644 --- a/spec/datadog/ci/test_visibility/transport_spec.rb +++ b/spec/datadog/ci/test_visibility/transport_spec.rb @@ -8,7 +8,7 @@ subject do described_class.new( api_key: api_key, - env: env, + dd_env: dd_env, url: url, serializers_factory: serializers_factory, max_payload_size: max_payload_size @@ -16,7 +16,7 @@ end let(:api_key) { "api_key" } - let(:env) { nil } + let(:dd_env) { nil } let(:url) { "https://citestcycle-intake.datad0ghq.com:443" } let(:serializers_factory) { Datadog::CI::TestVisibility::Serializers::Factories::TestLevel } let(:max_payload_size) { 4 * 1024 * 1024 } @@ -62,8 +62,8 @@ end end - context "with env defined" do - let(:env) { "ci" } + context "with dd_env defined" do + let(:dd_env) { "ci" } before do produce_test_trace end From af7a4abc13cf558d2a1177c53c28f722c6442a06 Mon Sep 17 00:00:00 2001 From: Andrey Marchenko Date: Tue, 17 Oct 2023 08:51:59 +0200 Subject: [PATCH 03/24] use dd_env parameter in Components --- lib/datadog/ci/configuration/components.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/datadog/ci/configuration/components.rb b/lib/datadog/ci/configuration/components.rb index 6108b48d..c1ad80ff 100644 --- a/lib/datadog/ci/configuration/components.rb +++ b/lib/datadog/ci/configuration/components.rb @@ -69,7 +69,7 @@ def build_agentless_transport(settings) Datadog::CI::TestVisibility::Transport.new( api_key: settings.api_key, url: agentless_url, - env: settings.env + dd_env: settings.env ) end end From 86278a5af571f8296bc6c420c71687dd1ae7a0d9 Mon Sep 17 00:00:00 2001 From: Andrey Marchenko Date: Tue, 17 Oct 2023 14:05:15 +0200 Subject: [PATCH 04/24] introduce :api parameter to TestVisibility::Transport --- lib/datadog/ci/configuration/components.rb | 1 + lib/datadog/ci/test_visibility/transport.rb | 3 +++ sig/datadog/ci/test_visibility/transport.rbs | 2 ++ spec/datadog/ci/test_visibility/transport_spec.rb | 2 ++ 4 files changed, 8 insertions(+) diff --git a/lib/datadog/ci/configuration/components.rb b/lib/datadog/ci/configuration/components.rb index c1ad80ff..3e51bac2 100644 --- a/lib/datadog/ci/configuration/components.rb +++ b/lib/datadog/ci/configuration/components.rb @@ -67,6 +67,7 @@ def build_agentless_transport(settings) "https://#{Ext::Transport::TEST_VISIBILITY_INTAKE_HOST_PREFIX}.#{dd_site}:443" Datadog::CI::TestVisibility::Transport.new( + api: Transport::Api::Builder.build_ci_test_cycle_api(settings), api_key: settings.api_key, url: agentless_url, dd_env: settings.env diff --git a/lib/datadog/ci/test_visibility/transport.rb b/lib/datadog/ci/test_visibility/transport.rb index 7d5ddd4d..f3db1bd3 100644 --- a/lib/datadog/ci/test_visibility/transport.rb +++ b/lib/datadog/ci/test_visibility/transport.rb @@ -20,6 +20,7 @@ class Transport DEFAULT_MAX_PAYLOAD_SIZE = 5 * 1024 * 1024 attr_reader :serializers_factory, + :api, :api_key, :max_payload_size, :http, @@ -28,6 +29,7 @@ class Transport def initialize( api_key:, url:, + api:, dd_env: nil, serializers_factory: Datadog::CI::TestVisibility::Serializers::Factories::TestLevel, max_payload_size: DEFAULT_MAX_PAYLOAD_SIZE @@ -47,6 +49,7 @@ def initialize( ssl: uri.scheme == "https" || uri.port == 443, compress: true ) + @api = api end def send_traces(traces) diff --git a/sig/datadog/ci/test_visibility/transport.rbs b/sig/datadog/ci/test_visibility/transport.rbs index c528ba55..074c23b5 100644 --- a/sig/datadog/ci/test_visibility/transport.rbs +++ b/sig/datadog/ci/test_visibility/transport.rbs @@ -8,6 +8,7 @@ module Datadog attr_reader api_key: String attr_reader dd_env: String? attr_reader http: Datadog::CI::Transport::HTTP + attr_reader api: Datadog::CI::Transport::Api::Base attr_reader max_payload_size: Integer @api_key: String @@ -17,6 +18,7 @@ module Datadog @max_payload_size: Integer def initialize: ( + api: Datadog::CI::Transport::Api::Base, api_key: String, url: ::String, ?dd_env: ::String?, diff --git a/spec/datadog/ci/test_visibility/transport_spec.rb b/spec/datadog/ci/test_visibility/transport_spec.rb index 0c099ec5..2a92bb76 100644 --- a/spec/datadog/ci/test_visibility/transport_spec.rb +++ b/spec/datadog/ci/test_visibility/transport_spec.rb @@ -7,6 +7,7 @@ subject do described_class.new( + api: api, api_key: api_key, dd_env: dd_env, url: url, @@ -15,6 +16,7 @@ ) end + let(:api) { double(:api) } let(:api_key) { "api_key" } let(:dd_env) { nil } let(:url) { "https://citestcycle-intake.datad0ghq.com:443" } From f50907c146b30230dc8c6578ad74f1a4fc6c027a Mon Sep 17 00:00:00 2001 From: Andrey Marchenko Date: Tue, 17 Oct 2023 14:16:44 +0200 Subject: [PATCH 05/24] use Datadog::CI::Transport::Api instead of Datadog::CI::Transport::HTTP in TestVisibility::Transport --- lib/datadog/ci/test_visibility/transport.rb | 20 ++---------- sig/datadog/ci/test_visibility/transport.rbs | 2 -- .../ci/configuration/components_spec.rb | 15 +++++++-- .../ci/test_visibility/transport_spec.rb | 32 ++++++------------- 4 files changed, 23 insertions(+), 46 deletions(-) diff --git a/lib/datadog/ci/test_visibility/transport.rb b/lib/datadog/ci/test_visibility/transport.rb index f3db1bd3..d75a7011 100644 --- a/lib/datadog/ci/test_visibility/transport.rb +++ b/lib/datadog/ci/test_visibility/transport.rb @@ -23,7 +23,6 @@ class Transport :api, :api_key, :max_payload_size, - :http, :dd_env def initialize( @@ -38,17 +37,6 @@ def initialize( @api_key = api_key @max_payload_size = max_payload_size @dd_env = dd_env - - uri = URI.parse(url) - - raise "Invalid agentless mode URL: #{url}" if uri.host.nil? - - @http = Datadog::CI::Transport::HTTP.new( - host: uri.host, - port: uri.port, - ssl: uri.scheme == "https" || uri.port == 443, - compress: true - ) @api = api end @@ -85,13 +73,9 @@ def send_traces(traces) private def send_payload(encoded_payload) - http.request( + api.request( path: Datadog::CI::Ext::Transport::TEST_VISIBILITY_INTAKE_PATH, - payload: encoded_payload, - headers: { - Ext::Transport::HEADER_DD_API_KEY => api_key, - Ext::Transport::HEADER_CONTENT_TYPE => Ext::Transport::CONTENT_TYPE_MESSAGEPACK - } + payload: encoded_payload ) end diff --git a/sig/datadog/ci/test_visibility/transport.rbs b/sig/datadog/ci/test_visibility/transport.rbs index 074c23b5..f2b9a307 100644 --- a/sig/datadog/ci/test_visibility/transport.rbs +++ b/sig/datadog/ci/test_visibility/transport.rbs @@ -7,13 +7,11 @@ module Datadog attr_reader serializers_factory: singleton(Datadog::CI::TestVisibility::Serializers::Factories::TestLevel) attr_reader api_key: String attr_reader dd_env: String? - attr_reader http: Datadog::CI::Transport::HTTP attr_reader api: Datadog::CI::Transport::Api::Base attr_reader max_payload_size: Integer @api_key: String @dd_env: String? - @http: Datadog::CI::Transport::HTTP @serializers_factory: singleton(Datadog::CI::TestVisibility::Serializers::Factories::TestLevel) @max_payload_size: Integer diff --git a/spec/datadog/ci/configuration/components_spec.rb b/spec/datadog/ci/configuration/components_spec.rb index 9f5283d2..762898c1 100644 --- a/spec/datadog/ci/configuration/components_spec.rb +++ b/spec/datadog/ci/configuration/components_spec.rb @@ -121,7 +121,10 @@ expect(options[:transport]).to be_kind_of(Datadog::CI::TestVisibility::Transport) expect(options[:shutdown_timeout]).to eq(60) - http_client = options[:transport].http + api = options[:transport].api + expect(api.api_key).to eq(api_key) + + http_client = api.http expect(http_client.host).to eq("citestcycle-intake.datadoghq.com") expect(http_client.port).to eq(443) expect(http_client.ssl).to eq(true) @@ -133,7 +136,10 @@ it "configures transport to use intake URL from settings" do expect(settings.tracing.test_mode).to have_received(:writer_options=) do |options| - http_client = options[:transport].http + api = options[:transport].api + expect(api.api_key).to eq(api_key) + + http_client = api.http expect(http_client.host).to eq("localhost") expect(http_client.port).to eq(5555) expect(http_client.ssl).to eq(false) @@ -146,7 +152,10 @@ it "construct intake url using provided host" do expect(settings.tracing.test_mode).to have_received(:writer_options=) do |options| - http_client = options[:transport].http + api = options[:transport].api + expect(api.api_key).to eq(api_key) + + http_client = api.http expect(http_client.host).to eq("citestcycle-intake.eu.datadoghq.com") expect(http_client.port).to eq(443) expect(http_client.ssl).to eq(true) diff --git a/spec/datadog/ci/test_visibility/transport_spec.rb b/spec/datadog/ci/test_visibility/transport_spec.rb index 2a92bb76..e0a541a5 100644 --- a/spec/datadog/ci/test_visibility/transport_spec.rb +++ b/spec/datadog/ci/test_visibility/transport_spec.rb @@ -16,23 +16,13 @@ ) end - let(:api) { double(:api) } - let(:api_key) { "api_key" } let(:dd_env) { nil } + let(:api_key) { "api_key" } let(:url) { "https://citestcycle-intake.datad0ghq.com:443" } let(:serializers_factory) { Datadog::CI::TestVisibility::Serializers::Factories::TestLevel } let(:max_payload_size) { 4 * 1024 * 1024 } - let(:http) { spy(:http) } - - before do - expect(Datadog::CI::Transport::HTTP).to receive(:new).with( - host: "citestcycle-intake.datad0ghq.com", - port: 443, - ssl: true, - compress: true - ).and_return(http) - end + let(:api) { spy(:api) } describe "#send_traces" do context "with a single trace and a single span" do @@ -43,12 +33,8 @@ it "sends correct payload" do subject.send_traces([trace]) - expect(http).to have_received(:request) do |args| + expect(api).to have_received(:request) do |args| expect(args[:path]).to eq("/api/v2/citestcycle") - expect(args[:headers]).to eq({ - "DD-API-KEY" => "api_key", - "Content-Type" => "application/msgpack" - }) payload = MessagePack.unpack(args[:payload]) expect(payload["version"]).to eq(1) @@ -73,7 +59,7 @@ it "sends correct payload including env" do subject.send_traces([trace]) - expect(http).to have_received(:request) do |args| + expect(api).to have_received(:request) do |args| payload = MessagePack.unpack(args[:payload]) metadata = payload["metadata"]["*"] @@ -93,7 +79,7 @@ it "sends event for each of spans" do subject.send_traces(traces) - expect(http).to have_received(:request) do |args| + expect(api).to have_received(:request) do |args| payload = MessagePack.unpack(args[:payload]) events = payload["events"] expect(events.count).to eq(expected_events_count) @@ -111,7 +97,7 @@ it "filters out invalid events" do subject.send_traces(traces) - expect(http).to have_received(:request) do |args| + expect(api).to have_received(:request) do |args| payload = MessagePack.unpack(args[:payload]) events = payload["events"] @@ -131,7 +117,7 @@ it "filters out invalid events" do responses = subject.send_traces(traces) - expect(http).to have_received(:request).twice + expect(api).to have_received(:request).twice expect(responses.count).to eq(2) end end @@ -144,7 +130,7 @@ it "does not send events that are larger than max size" do subject.send_traces(traces) - expect(http).not_to have_received(:request) + expect(api).not_to have_received(:request) end end end @@ -159,7 +145,7 @@ it "does not send anything" do subject.send_traces(traces) - expect(http).not_to have_received(:request) + expect(api).not_to have_received(:request) end end end From 0cbce2e9509d7b38b079cbbfb18411eeb0332a29 Mon Sep 17 00:00:00 2001 From: Andrey Marchenko Date: Tue, 17 Oct 2023 14:18:59 +0200 Subject: [PATCH 06/24] require HTTP from CIIntake API --- lib/datadog/ci/transport/api/ci_intake.rb | 1 + 1 file changed, 1 insertion(+) diff --git a/lib/datadog/ci/transport/api/ci_intake.rb b/lib/datadog/ci/transport/api/ci_intake.rb index 8175d551..f7543b1c 100644 --- a/lib/datadog/ci/transport/api/ci_intake.rb +++ b/lib/datadog/ci/transport/api/ci_intake.rb @@ -1,6 +1,7 @@ # frozen_string_literal: true require_relative "base" +require_relative "../http" module Datadog module CI From be77dbfea635fbf06cb85df54bae0de8dc7b2ab1 Mon Sep 17 00:00:00 2001 From: Andrey Marchenko Date: Tue, 17 Oct 2023 14:27:48 +0200 Subject: [PATCH 07/24] simplify tests --- .../ci/configuration/components_spec.rb | 42 +------------------ .../ci/transport/api/ci_intake_spec.rb | 39 +++++++++++++---- 2 files changed, 33 insertions(+), 48 deletions(-) diff --git a/spec/datadog/ci/configuration/components_spec.rb b/spec/datadog/ci/configuration/components_spec.rb index 762898c1..80a71f7d 100644 --- a/spec/datadog/ci/configuration/components_spec.rb +++ b/spec/datadog/ci/configuration/components_spec.rb @@ -112,7 +112,7 @@ context "when api key is set" do let(:api_key) { "api_key" } - it "sets async for test mode and provides transport and shutdown timeout to the write" do + it "sets async for test mode and provides transport and shutdown timeout to the writer" do expect(settings.tracing.test_mode) .to have_received(:async=) .with(true) @@ -120,46 +120,6 @@ expect(settings.tracing.test_mode).to have_received(:writer_options=) do |options| expect(options[:transport]).to be_kind_of(Datadog::CI::TestVisibility::Transport) expect(options[:shutdown_timeout]).to eq(60) - - api = options[:transport].api - expect(api.api_key).to eq(api_key) - - http_client = api.http - expect(http_client.host).to eq("citestcycle-intake.datadoghq.com") - expect(http_client.port).to eq(443) - expect(http_client.ssl).to eq(true) - end - end - - context "when agentless_url is provided" do - let(:agentless_url) { "http://localhost:5555" } - - it "configures transport to use intake URL from settings" do - expect(settings.tracing.test_mode).to have_received(:writer_options=) do |options| - api = options[:transport].api - expect(api.api_key).to eq(api_key) - - http_client = api.http - expect(http_client.host).to eq("localhost") - expect(http_client.port).to eq(5555) - expect(http_client.ssl).to eq(false) - end - end - end - - context "when dd_site is provided" do - let(:dd_site) { "eu.datadoghq.com" } - - it "construct intake url using provided host" do - expect(settings.tracing.test_mode).to have_received(:writer_options=) do |options| - api = options[:transport].api - expect(api.api_key).to eq(api_key) - - http_client = api.http - expect(http_client.host).to eq("citestcycle-intake.eu.datadoghq.com") - expect(http_client.port).to eq(443) - expect(http_client.ssl).to eq(true) - end end end end diff --git a/spec/datadog/ci/transport/api/ci_intake_spec.rb b/spec/datadog/ci/transport/api/ci_intake_spec.rb index 3398bc94..bfb6ffca 100644 --- a/spec/datadog/ci/transport/api/ci_intake_spec.rb +++ b/spec/datadog/ci/transport/api/ci_intake_spec.rb @@ -13,16 +13,41 @@ let(:http) { double(:http) } - before do - expect(Datadog::CI::Transport::HTTP).to receive(:new).with( - host: "citestcycle-intake.datad0ghq.com", - port: 443, - ssl: true, - compress: true - ).and_return(http) + describe "#initialize" do + context "with https URL" do + it "creates SSL transport" do + expect(Datadog::CI::Transport::HTTP).to receive(:new).with( + host: "citestcycle-intake.datad0ghq.com", + port: 443, + ssl: true, + compress: true + ).and_return(http) + + subject + end + end + + context "with http URL" do + let(:url) { "http://localhost:5555" } + + it "creates http transport without SSL" do + expect(Datadog::CI::Transport::HTTP).to receive(:new).with( + host: "localhost", + port: 5555, + ssl: false, + compress: true + ).and_return(http) + + subject + end + end end describe "#request" do + before do + allow(Datadog::CI::Transport::HTTP).to receive(:new).and_return(http) + end + it "produces correct headers and forwards request to HTTP layer" do expect(http).to receive(:request).with( path: "path", From 8dbbf5c519bddd04cd1400902944f216ad6d60b8 Mon Sep 17 00:00:00 2001 From: Andrey Marchenko Date: Tue, 17 Oct 2023 14:44:49 +0200 Subject: [PATCH 08/24] fix CIIntake after rebase --- lib/datadog/ci/transport/api/ci_intake.rb | 2 +- sig/datadog/ci/transport/api/ci_intake.rbs | 2 +- spec/datadog/ci/transport/api/ci_intake_spec.rb | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/lib/datadog/ci/transport/api/ci_intake.rb b/lib/datadog/ci/transport/api/ci_intake.rb index f7543b1c..3bd5aa74 100644 --- a/lib/datadog/ci/transport/api/ci_intake.rb +++ b/lib/datadog/ci/transport/api/ci_intake.rb @@ -28,7 +28,7 @@ def request(path:, payload:, verb: "post") http.request( path: path, payload: payload, - method: verb, + verb: verb, headers: headers ) end diff --git a/sig/datadog/ci/transport/api/ci_intake.rbs b/sig/datadog/ci/transport/api/ci_intake.rbs index 4fd8d5b3..f934fb76 100644 --- a/sig/datadog/ci/transport/api/ci_intake.rbs +++ b/sig/datadog/ci/transport/api/ci_intake.rbs @@ -11,7 +11,7 @@ module Datadog def initialize: (api_key: String, url: String) -> void - def request: (path: String, payload: String, ?verb: ::String) -> Datadog::CI::Transport::HTTP::Response + def request: (path: String, payload: String, ?verb: ::String) -> Datadog::CI::Transport::HTTP::ResponseDecorator private diff --git a/spec/datadog/ci/transport/api/ci_intake_spec.rb b/spec/datadog/ci/transport/api/ci_intake_spec.rb index bfb6ffca..a0d7a324 100644 --- a/spec/datadog/ci/transport/api/ci_intake_spec.rb +++ b/spec/datadog/ci/transport/api/ci_intake_spec.rb @@ -52,7 +52,7 @@ expect(http).to receive(:request).with( path: "path", payload: "payload", - method: "post", + verb: "post", headers: { "DD-API-KEY" => "api_key", "Content-Type" => "application/msgpack" From 382552fb8d263af7348d0a939e91d63f9a593758 Mon Sep 17 00:00:00 2001 From: Andrey Marchenko Date: Tue, 17 Oct 2023 15:06:19 +0200 Subject: [PATCH 09/24] remove unneeded parameters from TestVisibility::Transport --- lib/datadog/ci/configuration/components.rb | 16 +++++----------- lib/datadog/ci/test_visibility/transport.rb | 4 ---- sig/datadog/ci/configuration/components.rbs | 2 +- sig/datadog/ci/test_visibility/transport.rbs | 4 ---- .../datadog/ci/test_visibility/transport_spec.rb | 4 ---- 5 files changed, 6 insertions(+), 24 deletions(-) diff --git a/lib/datadog/ci/configuration/components.rb b/lib/datadog/ci/configuration/components.rb index 3e51bac2..91cbfbc3 100644 --- a/lib/datadog/ci/configuration/components.rb +++ b/lib/datadog/ci/configuration/components.rb @@ -17,7 +17,7 @@ def initialize(settings) end def activate_ci!(settings) - agentless_transport = nil + test_visibility_transport = nil if settings.ci.agentless_mode_enabled if settings.api_key.nil? @@ -34,7 +34,7 @@ def activate_ci!(settings) settings.ci.enabled = false return else - agentless_transport = build_agentless_transport(settings) + test_visibility_transport = build_test_visibility_transport(settings) end end @@ -51,8 +51,8 @@ def activate_ci!(settings) settings.tracing.test_mode.trace_flush = settings.ci.trace_flush || CI::TestVisibility::Flush::Finished.new writer_options = settings.ci.writer_options - if agentless_transport - writer_options[:transport] = agentless_transport + if test_visibility_transport + writer_options[:transport] = test_visibility_transport writer_options[:shutdown_timeout] = 60 settings.tracing.test_mode.async = true @@ -61,15 +61,9 @@ def activate_ci!(settings) settings.tracing.test_mode.writer_options = writer_options end - def build_agentless_transport(settings) - dd_site = settings.site || "datadoghq.com" - agentless_url = settings.ci.agentless_url || - "https://#{Ext::Transport::TEST_VISIBILITY_INTAKE_HOST_PREFIX}.#{dd_site}:443" - + def build_test_visibility_transport(settings) Datadog::CI::TestVisibility::Transport.new( api: Transport::Api::Builder.build_ci_test_cycle_api(settings), - api_key: settings.api_key, - url: agentless_url, dd_env: settings.env ) end diff --git a/lib/datadog/ci/test_visibility/transport.rb b/lib/datadog/ci/test_visibility/transport.rb index d75a7011..5313333c 100644 --- a/lib/datadog/ci/test_visibility/transport.rb +++ b/lib/datadog/ci/test_visibility/transport.rb @@ -21,20 +21,16 @@ class Transport attr_reader :serializers_factory, :api, - :api_key, :max_payload_size, :dd_env def initialize( - api_key:, - url:, api:, dd_env: nil, serializers_factory: Datadog::CI::TestVisibility::Serializers::Factories::TestLevel, max_payload_size: DEFAULT_MAX_PAYLOAD_SIZE ) @serializers_factory = serializers_factory - @api_key = api_key @max_payload_size = max_payload_size @dd_env = dd_env @api = api diff --git a/sig/datadog/ci/configuration/components.rbs b/sig/datadog/ci/configuration/components.rbs index 6d7569e6..bb703a64 100644 --- a/sig/datadog/ci/configuration/components.rbs +++ b/sig/datadog/ci/configuration/components.rbs @@ -6,7 +6,7 @@ module Datadog def activate_ci!: (untyped settings) -> untyped - def build_agentless_transport: (untyped settings) -> Datadog::CI::TestVisibility::Transport + def build_test_visibility_transport: (untyped settings) -> Datadog::CI::TestVisibility::Transport end end end diff --git a/sig/datadog/ci/test_visibility/transport.rbs b/sig/datadog/ci/test_visibility/transport.rbs index f2b9a307..f94a68bf 100644 --- a/sig/datadog/ci/test_visibility/transport.rbs +++ b/sig/datadog/ci/test_visibility/transport.rbs @@ -5,20 +5,16 @@ module Datadog DEFAULT_MAX_PAYLOAD_SIZE: Integer attr_reader serializers_factory: singleton(Datadog::CI::TestVisibility::Serializers::Factories::TestLevel) - attr_reader api_key: String attr_reader dd_env: String? attr_reader api: Datadog::CI::Transport::Api::Base attr_reader max_payload_size: Integer - @api_key: String @dd_env: String? @serializers_factory: singleton(Datadog::CI::TestVisibility::Serializers::Factories::TestLevel) @max_payload_size: Integer def initialize: ( api: Datadog::CI::Transport::Api::Base, - api_key: String, - url: ::String, ?dd_env: ::String?, ?serializers_factory: singleton(Datadog::CI::TestVisibility::Serializers::Factories::TestLevel), ?max_payload_size: Integer diff --git a/spec/datadog/ci/test_visibility/transport_spec.rb b/spec/datadog/ci/test_visibility/transport_spec.rb index e0a541a5..b137a338 100644 --- a/spec/datadog/ci/test_visibility/transport_spec.rb +++ b/spec/datadog/ci/test_visibility/transport_spec.rb @@ -8,17 +8,13 @@ subject do described_class.new( api: api, - api_key: api_key, dd_env: dd_env, - url: url, serializers_factory: serializers_factory, max_payload_size: max_payload_size ) end let(:dd_env) { nil } - let(:api_key) { "api_key" } - let(:url) { "https://citestcycle-intake.datad0ghq.com:443" } let(:serializers_factory) { Datadog::CI::TestVisibility::Serializers::Factories::TestLevel } let(:max_payload_size) { 4 * 1024 * 1024 } From 96a7a560c67010d1b16a05f186fa62c8546c379c Mon Sep 17 00:00:00 2001 From: Andrey Marchenko Date: Tue, 17 Oct 2023 16:19:45 +0200 Subject: [PATCH 10/24] add EVPProxy API --- lib/datadog/ci/ext/transport.rb | 3 + lib/datadog/ci/transport/api/base.rb | 2 +- lib/datadog/ci/transport/api/evp_proxy.rb | 57 ++++++++++++ sig/datadog/ci/ext/transport.rbs | 6 ++ sig/datadog/ci/transport/api/base.rbs | 2 - sig/datadog/ci/transport/api/evp_proxy.rbs | 25 ++++++ .../ci/transport/api/evp_proxy_spec.rb | 87 +++++++++++++++++++ .../0/datadog/core/environment/container.rbs | 29 +++++++ 8 files changed, 208 insertions(+), 3 deletions(-) create mode 100644 lib/datadog/ci/transport/api/evp_proxy.rb create mode 100644 sig/datadog/ci/transport/api/evp_proxy.rbs create mode 100644 spec/datadog/ci/transport/api/evp_proxy_spec.rb create mode 100644 vendor/rbs/ddtrace/0/datadog/core/environment/container.rbs diff --git a/lib/datadog/ci/ext/transport.rb b/lib/datadog/ci/ext/transport.rb index ece118eb..1810b62e 100644 --- a/lib/datadog/ci/ext/transport.rb +++ b/lib/datadog/ci/ext/transport.rb @@ -7,7 +7,10 @@ module Transport HEADER_DD_API_KEY = "DD-API-KEY" HEADER_CONTENT_TYPE = "Content-Type" HEADER_CONTENT_ENCODING = "Content-Encoding" + HEADER_EVP_SUBDOMAIN = "X-Datadog-EVP-Subdomain" + HEADER_CONTAINER_ID = "Datadog-Container-ID" + EVP_PROXY_PATH_PREFIX = "/evp_proxy/v2" TEST_VISIBILITY_INTAKE_HOST_PREFIX = "citestcycle-intake" TEST_VISIBILITY_INTAKE_PATH = "/api/v2/citestcycle" diff --git a/lib/datadog/ci/transport/api/base.rb b/lib/datadog/ci/transport/api/base.rb index 7ffc9703..eb62a2d9 100644 --- a/lib/datadog/ci/transport/api/base.rb +++ b/lib/datadog/ci/transport/api/base.rb @@ -11,7 +11,7 @@ def request(path:, payload:, verb: "post") private def headers - @headers ||= { + { Ext::Transport::HEADER_CONTENT_TYPE => Ext::Transport::CONTENT_TYPE_MESSAGEPACK } end diff --git a/lib/datadog/ci/transport/api/evp_proxy.rb b/lib/datadog/ci/transport/api/evp_proxy.rb new file mode 100644 index 00000000..e0706e19 --- /dev/null +++ b/lib/datadog/ci/transport/api/evp_proxy.rb @@ -0,0 +1,57 @@ +# frozen_string_literal: true + +require_relative "base" +require_relative "../http" + +module Datadog + module CI + module Transport + module Api + class EVPProxy < Base + attr_reader :http + + def initialize(url:) + uri = URI.parse(url) + raise "Invalid evp proxy mode URL: #{url}" if uri.host.nil? + + @http = Datadog::CI::Transport::HTTP.new( + host: uri.host, + port: uri.port, + ssl: uri.scheme == "https" || uri.port == 443, + compress: false + ) + end + + def request(path:, payload:, verb: "post") + path = "#{Ext::Transport::EVP_PROXY_PATH_PREFIX}#{path}" + + http.request( + path: path, + payload: payload, + verb: verb, + headers: headers + ) + end + + private + + def container_id + return @container_id if defined?(@container_id) + + @container_id = Datadog::Core::Environment::Container.container_id + end + + def headers + headers = super + headers[Ext::Transport::HEADER_EVP_SUBDOMAIN] = Ext::Transport::TEST_VISIBILITY_INTAKE_HOST_PREFIX + + c_id = container_id + headers[Ext::Transport::HEADER_CONTAINER_ID] = c_id unless c_id.nil? + + headers + end + end + end + end + end +end diff --git a/sig/datadog/ci/ext/transport.rbs b/sig/datadog/ci/ext/transport.rbs index 6c40b3b6..91f19a69 100644 --- a/sig/datadog/ci/ext/transport.rbs +++ b/sig/datadog/ci/ext/transport.rbs @@ -8,6 +8,12 @@ module Datadog HEADER_CONTENT_ENCODING: "Content-Encoding" + HEADER_EVP_SUBDOMAIN: "X-Datadog-EVP-Subdomain" + + HEADER_CONTAINER_ID: "Datadog-Container-ID" + + EVP_PROXY_PATH_PREFIX: "/evp_proxy/v2" + TEST_VISIBILITY_INTAKE_HOST_PREFIX: "citestcycle-intake" TEST_VISIBILITY_INTAKE_PATH: "/api/v2/citestcycle" diff --git a/sig/datadog/ci/transport/api/base.rbs b/sig/datadog/ci/transport/api/base.rbs index 32dfd6bc..c0467e33 100644 --- a/sig/datadog/ci/transport/api/base.rbs +++ b/sig/datadog/ci/transport/api/base.rbs @@ -3,8 +3,6 @@ module Datadog module Transport module Api class Base - @headers: Hash[String, String] - def request: (path: String, payload: String, ?verb: ::String) -> untyped private diff --git a/sig/datadog/ci/transport/api/evp_proxy.rbs b/sig/datadog/ci/transport/api/evp_proxy.rbs new file mode 100644 index 00000000..0da715c7 --- /dev/null +++ b/sig/datadog/ci/transport/api/evp_proxy.rbs @@ -0,0 +1,25 @@ +module Datadog + module CI + module Transport + module Api + class EVPProxy < Base + @http: Datadog::CI::Transport::HTTP + + @container_id: String? + + attr_reader http: Datadog::CI::Transport::HTTP + + def initialize: (url: String) -> void + + def request: (path: String, payload: String, ?verb: ::String) -> Datadog::CI::Transport::HTTP::ResponseDecorator + + private + + def container_id: () -> String? + + def headers: () -> Hash[String, String] + end + end + end + end +end diff --git a/spec/datadog/ci/transport/api/evp_proxy_spec.rb b/spec/datadog/ci/transport/api/evp_proxy_spec.rb new file mode 100644 index 00000000..30134fd1 --- /dev/null +++ b/spec/datadog/ci/transport/api/evp_proxy_spec.rb @@ -0,0 +1,87 @@ +require_relative "../../../../../lib/datadog/ci/transport/api/evp_proxy" + +RSpec.describe Datadog::CI::Transport::Api::EVPProxy do + subject do + described_class.new( + url: url + ) + end + + let(:url) { "http://localhost:5555" } + + let(:http) { double(:http) } + + describe "#initialize" do + context "with https URL" do + let(:url) { "https://citestcycle-intake.datad0ghq.com:443" } + + it "creates SSL transport" do + expect(Datadog::CI::Transport::HTTP).to receive(:new).with( + host: "citestcycle-intake.datad0ghq.com", + port: 443, + ssl: true, + compress: false + ).and_return(http) + + subject + end + end + + context "with http URL" do + it "creates http transport without SSL" do + expect(Datadog::CI::Transport::HTTP).to receive(:new).with( + host: "localhost", + port: 5555, + ssl: false, + compress: false + ).and_return(http) + + subject + end + end + end + + describe "#request" do + before do + allow(Datadog::CI::Transport::HTTP).to receive(:new).and_return(http) + expect(Datadog::Core::Environment::Container).to receive(:container_id).and_return(container_id) + end + + context "without container id" do + let(:container_id) { nil } + + it "produces correct headers and forwards request to HTTP layer" do + expect(http).to receive(:request).with( + path: "/evp_proxy/v2/path", + payload: "payload", + verb: "post", + headers: { + "Content-Type" => "application/msgpack", + "X-Datadog-EVP-Subdomain" => "citestcycle-intake" + } + ) + + subject.request(path: "/path", payload: "payload") + end + end + + context "with container id" do + let(:container_id) { "container-id" } + + it "produces correct headers and forwards request to HTTP layer" do + expect(http).to receive(:request).with( + path: "/evp_proxy/v2/path", + payload: "payload", + verb: "post", + headers: { + "Content-Type" => "application/msgpack", + "X-Datadog-EVP-Subdomain" => "citestcycle-intake", + "Datadog-Container-ID" => "container-id" + } + ) + + subject.request(path: "/path", payload: "payload") + end + end + end +end diff --git a/vendor/rbs/ddtrace/0/datadog/core/environment/container.rbs b/vendor/rbs/ddtrace/0/datadog/core/environment/container.rbs new file mode 100644 index 00000000..35f02b3d --- /dev/null +++ b/vendor/rbs/ddtrace/0/datadog/core/environment/container.rbs @@ -0,0 +1,29 @@ +module Datadog + module Core + module Environment + module Container + UUID_PATTERN: untyped + + CONTAINER_PATTERN: untyped + + PLATFORM_REGEX: untyped + + POD_REGEX: untyped + + CONTAINER_REGEX: untyped + + FARGATE_14_CONTAINER_REGEX: untyped + + Descriptor: untyped + + def self?.platform: () -> untyped + + def self?.container_id: () -> untyped + + def self?.task_uid: () -> untyped + + def self?.descriptor: () -> untyped + end + end + end +end \ No newline at end of file From c4322bc68899d87086f0021fb3e708cf213ad7d9 Mon Sep 17 00:00:00 2001 From: Andrey Marchenko Date: Tue, 17 Oct 2023 16:41:16 +0200 Subject: [PATCH 11/24] EVPProxy initializer accepts parameters derived from AgentSettings --- lib/datadog/ci/transport/api/builder.rb | 10 +++++ lib/datadog/ci/transport/api/evp_proxy.rb | 12 +++-- sig/datadog/ci/transport/api/evp_proxy.rbs | 2 +- .../ci/transport/api/evp_proxy_spec.rb | 44 +++++++------------ 4 files changed, 33 insertions(+), 35 deletions(-) diff --git a/lib/datadog/ci/transport/api/builder.rb b/lib/datadog/ci/transport/api/builder.rb index abed4711..fd8126bf 100644 --- a/lib/datadog/ci/transport/api/builder.rb +++ b/lib/datadog/ci/transport/api/builder.rb @@ -1,6 +1,7 @@ # frozen_string_literal: true require_relative "ci_intake" +require_relative "evp_proxy" module Datadog module CI @@ -14,6 +15,15 @@ def self.build_ci_test_cycle_api(settings) CIIntake.new(api_key: settings.api_key, url: url) end + + def self.build_evp_proxy_api(agent_settings) + EVPProxy.new( + host: agent_settings.hostname, + port: agent_settings.port, + ssl: agent_settings.ssl, + timeout: agent_settings.timeout_seconds + ) + end end end end diff --git a/lib/datadog/ci/transport/api/evp_proxy.rb b/lib/datadog/ci/transport/api/evp_proxy.rb index e0706e19..510251c6 100644 --- a/lib/datadog/ci/transport/api/evp_proxy.rb +++ b/lib/datadog/ci/transport/api/evp_proxy.rb @@ -10,14 +10,12 @@ module Api class EVPProxy < Base attr_reader :http - def initialize(url:) - uri = URI.parse(url) - raise "Invalid evp proxy mode URL: #{url}" if uri.host.nil? - + def initialize(host:, port:, ssl:, timeout:) @http = Datadog::CI::Transport::HTTP.new( - host: uri.host, - port: uri.port, - ssl: uri.scheme == "https" || uri.port == 443, + host: host, + port: port, + ssl: ssl, + timeout: timeout, compress: false ) end diff --git a/sig/datadog/ci/transport/api/evp_proxy.rbs b/sig/datadog/ci/transport/api/evp_proxy.rbs index 0da715c7..52bf2c0b 100644 --- a/sig/datadog/ci/transport/api/evp_proxy.rbs +++ b/sig/datadog/ci/transport/api/evp_proxy.rbs @@ -9,7 +9,7 @@ module Datadog attr_reader http: Datadog::CI::Transport::HTTP - def initialize: (url: String) -> void + def initialize: (host: String, port: Integer, ssl: bool, timeout: Integer) -> void def request: (path: String, payload: String, ?verb: ::String) -> Datadog::CI::Transport::HTTP::ResponseDecorator diff --git a/spec/datadog/ci/transport/api/evp_proxy_spec.rb b/spec/datadog/ci/transport/api/evp_proxy_spec.rb index 30134fd1..2742659d 100644 --- a/spec/datadog/ci/transport/api/evp_proxy_spec.rb +++ b/spec/datadog/ci/transport/api/evp_proxy_spec.rb @@ -3,41 +3,31 @@ RSpec.describe Datadog::CI::Transport::Api::EVPProxy do subject do described_class.new( - url: url + host: host, + port: port, + ssl: ssl, + timeout: timeout ) end - let(:url) { "http://localhost:5555" } + let(:host) { "localhost" } + let(:port) { 5555 } + let(:ssl) { false } + let(:timeout) { 42 } let(:http) { double(:http) } describe "#initialize" do - context "with https URL" do - let(:url) { "https://citestcycle-intake.datad0ghq.com:443" } + it "creates HTTP transport" do + expect(Datadog::CI::Transport::HTTP).to receive(:new).with( + host: host, + port: port, + ssl: ssl, + timeout: timeout, + compress: false + ).and_return(http) - it "creates SSL transport" do - expect(Datadog::CI::Transport::HTTP).to receive(:new).with( - host: "citestcycle-intake.datad0ghq.com", - port: 443, - ssl: true, - compress: false - ).and_return(http) - - subject - end - end - - context "with http URL" do - it "creates http transport without SSL" do - expect(Datadog::CI::Transport::HTTP).to receive(:new).with( - host: "localhost", - port: 5555, - ssl: false, - compress: false - ).and_return(http) - - subject - end + subject end end From 3093c0c10e99309d2a9d3f19325de01e3f338725 Mon Sep 17 00:00:00 2001 From: Andrey Marchenko Date: Tue, 17 Oct 2023 16:51:05 +0200 Subject: [PATCH 12/24] spec for Api::Builder.build_evp_proxy_api --- spec/datadog/ci/transport/api/builder_spec.rb | 26 +++++++++++++++++++ .../ci/transport/api/evp_proxy_spec.rb | 4 +-- 2 files changed, 28 insertions(+), 2 deletions(-) diff --git a/spec/datadog/ci/transport/api/builder_spec.rb b/spec/datadog/ci/transport/api/builder_spec.rb index 2ee67c15..d0e2e64d 100644 --- a/spec/datadog/ci/transport/api/builder_spec.rb +++ b/spec/datadog/ci/transport/api/builder_spec.rb @@ -63,4 +63,30 @@ end end end + + describe ".build_evp_proxy_api" do + subject { described_class.build_evp_proxy_api(agent_settings) } + + let(:agent_settings) do + Datadog::Core::Configuration::AgentSettingsResolver::AgentSettings.new( + adapter: nil, + ssl: false, + hostname: "localhost", + port: 5555, + uds_path: nil, + timeout_seconds: 42, + deprecated_for_removal_transport_configuration_proc: nil + ) + end + + it "creates EVPProxy" do + expect(Datadog::CI::Transport::Api::EVPProxy).to receive(:new).with( + host: "localhost", + port: 5555, + ssl: false, + timeout: 42 + ) + subject + end + end end diff --git a/spec/datadog/ci/transport/api/evp_proxy_spec.rb b/spec/datadog/ci/transport/api/evp_proxy_spec.rb index 2742659d..aab6cec8 100644 --- a/spec/datadog/ci/transport/api/evp_proxy_spec.rb +++ b/spec/datadog/ci/transport/api/evp_proxy_spec.rb @@ -40,7 +40,7 @@ context "without container id" do let(:container_id) { nil } - it "produces correct headers and forwards request to HTTP layer" do + it "produces correct headers and forwards request to HTTP layer prepending path with evp_proxy" do expect(http).to receive(:request).with( path: "/evp_proxy/v2/path", payload: "payload", @@ -58,7 +58,7 @@ context "with container id" do let(:container_id) { "container-id" } - it "produces correct headers and forwards request to HTTP layer" do + it "adds an additional Datadog-Container-ID header" do expect(http).to receive(:request).with( path: "/evp_proxy/v2/path", payload: "payload", From 832868265f2c8c4a47b16401f773f804c4e8f8fb Mon Sep 17 00:00:00 2001 From: Andrey Marchenko Date: Tue, 17 Oct 2023 17:25:36 +0200 Subject: [PATCH 13/24] configuration of evp proxy in Components (untested) --- lib/datadog/ci/configuration/components.rb | 53 ++++++++++++------- sig/datadog/ci/configuration/components.rbs | 4 +- sig/datadog/ci/transport/api/builder.rbs | 1 + .../0/datadog/core/remote/negotiation.rbs | 18 +++++++ .../core/remote/transport/negotiation.rbs | 36 +++++++++++++ 5 files changed, 93 insertions(+), 19 deletions(-) create mode 100644 vendor/rbs/ddtrace/0/datadog/core/remote/negotiation.rbs create mode 100644 vendor/rbs/ddtrace/0/datadog/core/remote/transport/negotiation.rbs diff --git a/lib/datadog/ci/configuration/components.rb b/lib/datadog/ci/configuration/components.rb index 91cbfbc3..ca007ec8 100644 --- a/lib/datadog/ci/configuration/components.rb +++ b/lib/datadog/ci/configuration/components.rb @@ -18,24 +18,12 @@ def initialize(settings) def activate_ci!(settings) test_visibility_transport = nil + agent_settings = Datadog::Core::Configuration::AgentSettingsResolver.call(settings) if settings.ci.agentless_mode_enabled - if settings.api_key.nil? - # agentless mode is requested but no API key is provided - - # we cannot continue and log an error - # Tests are running without CI visibility enabled - - Datadog.logger.error( - "DATADOG CONFIGURATION - CI VISIBILITY - ATTENTION - " \ - "Agentless mode was enabled but DD_API_KEY is not set: CI visibility is disabled. " \ - "Please make sure to set valid api key in DD_API_KEY environment variable" - ) - - settings.ci.enabled = false - return - else - test_visibility_transport = build_test_visibility_transport(settings) - end + test_visibility_transport = build_agentless_transport(settings) + elsif can_use_evp_proxy?(settings, agent_settings) + test_visibility_transport = build_evp_proxy_transport(settings, agent_settings) end # Deactivate telemetry @@ -61,9 +49,38 @@ def activate_ci!(settings) settings.tracing.test_mode.writer_options = writer_options end - def build_test_visibility_transport(settings) + def can_use_evp_proxy?(settings, agent_settings) + Datadog::Core::Remote::Negotiation.new(settings, agent_settings).endpoint?( + "#{Ext::Transport::EVP_PROXY_PATH_PREFIX}/" + ) + end + + def build_agentless_transport(settings) + if settings.api_key.nil? + # agentless mode is requested but no API key is provided - + # we cannot continue and log an error + # Tests are running without CI visibility enabled + + Datadog.logger.error( + "DATADOG CONFIGURATION - CI VISIBILITY - ATTENTION - " \ + "Agentless mode was enabled but DD_API_KEY is not set: CI visibility is disabled. " \ + "Please make sure to set valid api key in DD_API_KEY environment variable" + ) + + settings.ci.enabled = false + + nil + else + Datadog::CI::TestVisibility::Transport.new( + api: Transport::Api::Builder.build_ci_test_cycle_api(settings), + dd_env: settings.env + ) + end + end + + def build_evp_proxy_transport(settings, agent_settings) Datadog::CI::TestVisibility::Transport.new( - api: Transport::Api::Builder.build_ci_test_cycle_api(settings), + api: Transport::Api::Builder.build_evp_proxy_api(agent_settings), dd_env: settings.env ) end diff --git a/sig/datadog/ci/configuration/components.rbs b/sig/datadog/ci/configuration/components.rbs index bb703a64..62304a3e 100644 --- a/sig/datadog/ci/configuration/components.rbs +++ b/sig/datadog/ci/configuration/components.rbs @@ -6,7 +6,9 @@ module Datadog def activate_ci!: (untyped settings) -> untyped - def build_test_visibility_transport: (untyped settings) -> Datadog::CI::TestVisibility::Transport + def build_agentless_transport: (untyped settings) -> Datadog::CI::TestVisibility::Transport? + def build_evp_proxy_transport: (untyped settings, untyped agent_settings) -> Datadog::CI::TestVisibility::Transport + def can_use_evp_proxy?: (untyped settings, untyped agent_settings) -> bool end end end diff --git a/sig/datadog/ci/transport/api/builder.rbs b/sig/datadog/ci/transport/api/builder.rbs index 194c12e2..2c959713 100644 --- a/sig/datadog/ci/transport/api/builder.rbs +++ b/sig/datadog/ci/transport/api/builder.rbs @@ -4,6 +4,7 @@ module Datadog module Api module Builder def self.build_ci_test_cycle_api: (untyped settings) -> Datadog::CI::Transport::Api::CIIntake + def self.build_evp_proxy_api: (untyped agent_settings) -> Datadog::CI::Transport::Api::EVPProxy end end end diff --git a/vendor/rbs/ddtrace/0/datadog/core/remote/negotiation.rbs b/vendor/rbs/ddtrace/0/datadog/core/remote/negotiation.rbs new file mode 100644 index 00000000..2beb34ac --- /dev/null +++ b/vendor/rbs/ddtrace/0/datadog/core/remote/negotiation.rbs @@ -0,0 +1,18 @@ +module Datadog + module Core + module Remote + class Negotiation + @transport_root: Datadog::Core::Remote::Transport::Negotiation::Transport + @logged: ::Hash[::Symbol, bool] + + def initialize: (Datadog::Core::Configuration::Settings _settings, Datadog::Core::Configuration::AgentSettingsResolver::AgentSettings agent_settings) -> void + + def endpoint?: (::String path) -> bool + + private + + def network_error?: (::Exception error) -> bool + end + end + end +end diff --git a/vendor/rbs/ddtrace/0/datadog/core/remote/transport/negotiation.rbs b/vendor/rbs/ddtrace/0/datadog/core/remote/transport/negotiation.rbs new file mode 100644 index 00000000..5a70cd70 --- /dev/null +++ b/vendor/rbs/ddtrace/0/datadog/core/remote/transport/negotiation.rbs @@ -0,0 +1,36 @@ +module Datadog + module Core + module Remote + module Transport + module Negotiation + class Request < Datadog::Core::Transport::Request + end + + module Response + attr_reader version: untyped + + attr_reader endpoints: untyped + + attr_reader config: untyped + end + + class Transport + attr_reader client: untyped + + attr_reader apis: untyped + + attr_reader default_api: untyped + + attr_reader current_api_id: untyped + + def initialize: (untyped apis, untyped default_api) -> void + + def send_info: () -> untyped + + def current_api: () -> untyped + end + end + end + end + end +end \ No newline at end of file From b394bc9263d5026119b3478c6fc60f16c64a986c Mon Sep 17 00:00:00 2001 From: Andrey Marchenko Date: Wed, 18 Oct 2023 12:57:06 +0200 Subject: [PATCH 14/24] require API builder from components --- lib/datadog/ci/configuration/components.rb | 1 + 1 file changed, 1 insertion(+) diff --git a/lib/datadog/ci/configuration/components.rb b/lib/datadog/ci/configuration/components.rb index ca007ec8..e8d1b18d 100644 --- a/lib/datadog/ci/configuration/components.rb +++ b/lib/datadog/ci/configuration/components.rb @@ -2,6 +2,7 @@ require_relative "../test_visibility/flush" require_relative "../test_visibility/transport" +require_relative "../transport/api/builder" module Datadog module CI From e67e618939ba1c5581517b6fec57318ce5a2e181 Mon Sep 17 00:00:00 2001 From: Andrey Marchenko Date: Wed, 18 Oct 2023 16:12:41 +0200 Subject: [PATCH 15/24] put trailing slash in EVP_PROXY_PATH_PREFIX as it is returned like that by agent in "endpoints" list --- lib/datadog/ci/configuration/components.rb | 2 +- lib/datadog/ci/ext/transport.rb | 2 +- lib/datadog/ci/transport/api/evp_proxy.rb | 2 +- lib/datadog/ci/transport/http.rb | 2 + sig/datadog/ci/ext/transport.rbs | 2 +- .../ci/transport/api/evp_proxy_spec.rb | 40 ++++++++++++++----- 6 files changed, 35 insertions(+), 15 deletions(-) diff --git a/lib/datadog/ci/configuration/components.rb b/lib/datadog/ci/configuration/components.rb index e8d1b18d..b926f2cd 100644 --- a/lib/datadog/ci/configuration/components.rb +++ b/lib/datadog/ci/configuration/components.rb @@ -52,7 +52,7 @@ def activate_ci!(settings) def can_use_evp_proxy?(settings, agent_settings) Datadog::Core::Remote::Negotiation.new(settings, agent_settings).endpoint?( - "#{Ext::Transport::EVP_PROXY_PATH_PREFIX}/" + Ext::Transport::EVP_PROXY_PATH_PREFIX ) end diff --git a/lib/datadog/ci/ext/transport.rb b/lib/datadog/ci/ext/transport.rb index 1810b62e..3f0385ca 100644 --- a/lib/datadog/ci/ext/transport.rb +++ b/lib/datadog/ci/ext/transport.rb @@ -10,7 +10,7 @@ module Transport HEADER_EVP_SUBDOMAIN = "X-Datadog-EVP-Subdomain" HEADER_CONTAINER_ID = "Datadog-Container-ID" - EVP_PROXY_PATH_PREFIX = "/evp_proxy/v2" + EVP_PROXY_PATH_PREFIX = "/evp_proxy/v2/" TEST_VISIBILITY_INTAKE_HOST_PREFIX = "citestcycle-intake" TEST_VISIBILITY_INTAKE_PATH = "/api/v2/citestcycle" diff --git a/lib/datadog/ci/transport/api/evp_proxy.rb b/lib/datadog/ci/transport/api/evp_proxy.rb index 510251c6..34ffb1bd 100644 --- a/lib/datadog/ci/transport/api/evp_proxy.rb +++ b/lib/datadog/ci/transport/api/evp_proxy.rb @@ -21,7 +21,7 @@ def initialize(host:, port:, ssl:, timeout:) end def request(path:, payload:, verb: "post") - path = "#{Ext::Transport::EVP_PROXY_PATH_PREFIX}#{path}" + path = "#{Ext::Transport::EVP_PROXY_PATH_PREFIX}#{path.sub(/^\//, "")}" http.request( path: path, diff --git a/lib/datadog/ci/transport/http.rb b/lib/datadog/ci/transport/http.rb index 39ca0d29..acbead5e 100644 --- a/lib/datadog/ci/transport/http.rb +++ b/lib/datadog/ci/transport/http.rb @@ -41,6 +41,8 @@ def request(path:, payload:, headers:, verb: "post") "compression_enabled=#{compress}; path=#{path}; payload_size=#{payload.size}" end + Datadog.logger.debug(headers) + ResponseDecorator.new( adapter.call( build_env(path: path, payload: payload, headers: headers, verb: verb) diff --git a/sig/datadog/ci/ext/transport.rbs b/sig/datadog/ci/ext/transport.rbs index 91f19a69..49f0b93c 100644 --- a/sig/datadog/ci/ext/transport.rbs +++ b/sig/datadog/ci/ext/transport.rbs @@ -12,7 +12,7 @@ module Datadog HEADER_CONTAINER_ID: "Datadog-Container-ID" - EVP_PROXY_PATH_PREFIX: "/evp_proxy/v2" + EVP_PROXY_PATH_PREFIX: "/evp_proxy/v2/" TEST_VISIBILITY_INTAKE_HOST_PREFIX: "citestcycle-intake" diff --git a/spec/datadog/ci/transport/api/evp_proxy_spec.rb b/spec/datadog/ci/transport/api/evp_proxy_spec.rb index aab6cec8..735f416c 100644 --- a/spec/datadog/ci/transport/api/evp_proxy_spec.rb +++ b/spec/datadog/ci/transport/api/evp_proxy_spec.rb @@ -40,18 +40,36 @@ context "without container id" do let(:container_id) { nil } - it "produces correct headers and forwards request to HTTP layer prepending path with evp_proxy" do - expect(http).to receive(:request).with( - path: "/evp_proxy/v2/path", - payload: "payload", - verb: "post", - headers: { - "Content-Type" => "application/msgpack", - "X-Datadog-EVP-Subdomain" => "citestcycle-intake" - } - ) + context "with path starting from / character" do + it "produces correct headers and forwards request to HTTP layer prepending path with evp_proxy" do + expect(http).to receive(:request).with( + path: "/evp_proxy/v2/path", + payload: "payload", + verb: "post", + headers: { + "Content-Type" => "application/msgpack", + "X-Datadog-EVP-Subdomain" => "citestcycle-intake" + } + ) - subject.request(path: "/path", payload: "payload") + subject.request(path: "/path", payload: "payload") + end + end + + context "with path without / in the beginning" do + it "constructs evp proxy path correctly" do + expect(http).to receive(:request).with( + path: "/evp_proxy/v2/path", + payload: "payload", + verb: "post", + headers: { + "Content-Type" => "application/msgpack", + "X-Datadog-EVP-Subdomain" => "citestcycle-intake" + } + ) + + subject.request(path: "path", payload: "payload") + end end end From 3302a7682fce3989c483b2066f219ac854294719 Mon Sep 17 00:00:00 2001 From: Andrey Marchenko Date: Wed, 18 Oct 2023 16:47:11 +0200 Subject: [PATCH 16/24] spec for setting up EVP proxy API --- lib/datadog/ci/configuration/components.rb | 4 ++ lib/datadog/ci/transport/http.rb | 2 - .../ci/configuration/components_spec.rb | 56 ++++++++++++++----- 3 files changed, 46 insertions(+), 16 deletions(-) diff --git a/lib/datadog/ci/configuration/components.rb b/lib/datadog/ci/configuration/components.rb index b926f2cd..dcb12fc5 100644 --- a/lib/datadog/ci/configuration/components.rb +++ b/lib/datadog/ci/configuration/components.rb @@ -72,6 +72,8 @@ def build_agentless_transport(settings) nil else + Datadog.logger.debug("CI visibility configured to use agentless transport") + Datadog::CI::TestVisibility::Transport.new( api: Transport::Api::Builder.build_ci_test_cycle_api(settings), dd_env: settings.env @@ -80,6 +82,8 @@ def build_agentless_transport(settings) end def build_evp_proxy_transport(settings, agent_settings) + Datadog.logger.debug("CI visibility configured to use agent transport via EVP proxy") + Datadog::CI::TestVisibility::Transport.new( api: Transport::Api::Builder.build_evp_proxy_api(agent_settings), dd_env: settings.env diff --git a/lib/datadog/ci/transport/http.rb b/lib/datadog/ci/transport/http.rb index acbead5e..39ca0d29 100644 --- a/lib/datadog/ci/transport/http.rb +++ b/lib/datadog/ci/transport/http.rb @@ -41,8 +41,6 @@ def request(path:, payload:, headers:, verb: "post") "compression_enabled=#{compress}; path=#{path}; payload_size=#{payload.size}" end - Datadog.logger.debug(headers) - ResponseDecorator.new( adapter.call( build_env(path: path, payload: payload, headers: headers, verb: verb) diff --git a/spec/datadog/ci/configuration/components_spec.rb b/spec/datadog/ci/configuration/components_spec.rb index 80a71f7d..5bd67681 100644 --- a/spec/datadog/ci/configuration/components_spec.rb +++ b/spec/datadog/ci/configuration/components_spec.rb @@ -54,6 +54,16 @@ .to receive(:api_key) .and_return(api_key) + negotiation = double(:negotiation) + + allow(Datadog::Core::Remote::Negotiation) + .to receive(:new) + .and_return(negotiation) + + allow(negotiation) + .to receive(:endpoint?).with("/evp_proxy/v2/") + .and_return(evp_proxy_supported) + # Spy on test mode behavior allow(settings.tracing.test_mode) .to receive(:enabled=) @@ -79,6 +89,7 @@ let(:api_key) { nil } let(:agentless_url) { nil } let(:dd_site) { nil } + let(:evp_proxy_supported) { false } context "is enabled" do let(:enabled) { true } @@ -87,22 +98,38 @@ context "is disabled" do let(:agentless_enabled) { false } - it do - expect(settings.tracing.test_mode) - .to have_received(:enabled=) - .with(true) - end + context "and when agent supports EVP proxy" do + let(:evp_proxy_supported) { true } - it do - expect(settings.tracing.test_mode) - .to have_received(:trace_flush=) - .with(settings.ci.trace_flush || kind_of(Datadog::CI::TestVisibility::Flush::Finished)) + it "sets async for test mode and constructs transport with EVP proxy API" do + expect(settings.tracing.test_mode) + .to have_received(:async=) + .with(true) + + expect(settings.tracing.test_mode).to have_received(:writer_options=) do |options| + expect(options[:transport]).to be_kind_of(Datadog::CI::TestVisibility::Transport) + expect(options[:transport].api).to be_kind_of(Datadog::CI::Transport::Api::EVPProxy) + expect(options[:shutdown_timeout]).to eq(60) + end + end end - it do - expect(settings.tracing.test_mode) - .to have_received(:writer_options=) - .with(settings.ci.writer_options) + context "and when agent does not support EVP proxy" do + let(:evp_proxy_supported) { false } + + it "falls back to default transport" do + expect(settings.tracing.test_mode) + .to have_received(:enabled=) + .with(true) + + expect(settings.tracing.test_mode) + .to have_received(:trace_flush=) + .with(settings.ci.trace_flush || kind_of(Datadog::CI::TestVisibility::Flush::Finished)) + + expect(settings.tracing.test_mode).to have_received(:writer_options=) do |options| + expect(options[:transport]).to be_nil + end + end end end @@ -112,13 +139,14 @@ context "when api key is set" do let(:api_key) { "api_key" } - it "sets async for test mode and provides transport and shutdown timeout to the writer" do + it "sets async for test mode and constructs transport with CI intake API" do expect(settings.tracing.test_mode) .to have_received(:async=) .with(true) expect(settings.tracing.test_mode).to have_received(:writer_options=) do |options| expect(options[:transport]).to be_kind_of(Datadog::CI::TestVisibility::Transport) + expect(options[:transport].api).to be_kind_of(Datadog::CI::Transport::Api::CIIntake) expect(options[:shutdown_timeout]).to eq(60) end end From 3005a8c3cea260f06aa921f2bd24c30c4a78efa4 Mon Sep 17 00:00:00 2001 From: Andrey Marchenko Date: Fri, 20 Oct 2023 12:56:50 +0200 Subject: [PATCH 17/24] introduce DEFAULT_DD_SITE constant --- lib/datadog/ci/ext/transport.rb | 2 ++ lib/datadog/ci/transport/api/builder.rb | 2 +- sig/datadog/ci/ext/transport.rbs | 2 ++ 3 files changed, 5 insertions(+), 1 deletion(-) diff --git a/lib/datadog/ci/ext/transport.rb b/lib/datadog/ci/ext/transport.rb index 3f0385ca..791f6085 100644 --- a/lib/datadog/ci/ext/transport.rb +++ b/lib/datadog/ci/ext/transport.rb @@ -4,6 +4,8 @@ module Datadog module CI module Ext module Transport + DEFAULT_DD_SITE = "datadoghq.com" + HEADER_DD_API_KEY = "DD-API-KEY" HEADER_CONTENT_TYPE = "Content-Type" HEADER_CONTENT_ENCODING = "Content-Encoding" diff --git a/lib/datadog/ci/transport/api/builder.rb b/lib/datadog/ci/transport/api/builder.rb index fd8126bf..2037689b 100644 --- a/lib/datadog/ci/transport/api/builder.rb +++ b/lib/datadog/ci/transport/api/builder.rb @@ -9,7 +9,7 @@ module Transport module Api module Builder def self.build_ci_test_cycle_api(settings) - dd_site = settings.site || "datadoghq.com" + dd_site = settings.site || Ext::Transport::DEFAULT_DD_SITE url = settings.ci.agentless_url || "https://#{Ext::Transport::TEST_VISIBILITY_INTAKE_HOST_PREFIX}.#{dd_site}:443" diff --git a/sig/datadog/ci/ext/transport.rbs b/sig/datadog/ci/ext/transport.rbs index 49f0b93c..8ca2b5a7 100644 --- a/sig/datadog/ci/ext/transport.rbs +++ b/sig/datadog/ci/ext/transport.rbs @@ -2,6 +2,8 @@ module Datadog module CI module Ext module Transport + DEFAULT_DD_SITE: "datadoghq.com" + HEADER_DD_API_KEY: "DD-API-KEY" HEADER_CONTENT_TYPE: "Content-Type" From d303a9e95568c55f8abc5a6b1ea8e7c2dd75ec4d Mon Sep 17 00:00:00 2001 From: Andrey Marchenko Date: Tue, 24 Oct 2023 12:19:36 +0200 Subject: [PATCH 18/24] rename CIIntake to CiTestCycle --- lib/datadog/ci/transport/api/builder.rb | 4 ++-- .../ci/transport/api/{ci_intake.rb => ci_test_cycle.rb} | 2 +- sig/datadog/ci/transport/api/builder.rbs | 2 +- .../ci/transport/api/{ci_intake.rbs => ci_test_cycle.rbs} | 2 +- spec/datadog/ci/configuration/components_spec.rb | 2 +- spec/datadog/ci/transport/api/builder_spec.rb | 6 +++--- .../api/{ci_intake_spec.rb => ci_test_cycle_spec.rb} | 4 ++-- 7 files changed, 11 insertions(+), 11 deletions(-) rename lib/datadog/ci/transport/api/{ci_intake.rb => ci_test_cycle.rb} (96%) rename sig/datadog/ci/transport/api/{ci_intake.rbs => ci_test_cycle.rbs} (94%) rename spec/datadog/ci/transport/api/{ci_intake_spec.rb => ci_test_cycle_spec.rb} (95%) diff --git a/lib/datadog/ci/transport/api/builder.rb b/lib/datadog/ci/transport/api/builder.rb index 2037689b..6dac9291 100644 --- a/lib/datadog/ci/transport/api/builder.rb +++ b/lib/datadog/ci/transport/api/builder.rb @@ -1,6 +1,6 @@ # frozen_string_literal: true -require_relative "ci_intake" +require_relative "ci_test_cycle" require_relative "evp_proxy" module Datadog @@ -13,7 +13,7 @@ def self.build_ci_test_cycle_api(settings) url = settings.ci.agentless_url || "https://#{Ext::Transport::TEST_VISIBILITY_INTAKE_HOST_PREFIX}.#{dd_site}:443" - CIIntake.new(api_key: settings.api_key, url: url) + CiTestCycle.new(api_key: settings.api_key, url: url) end def self.build_evp_proxy_api(agent_settings) diff --git a/lib/datadog/ci/transport/api/ci_intake.rb b/lib/datadog/ci/transport/api/ci_test_cycle.rb similarity index 96% rename from lib/datadog/ci/transport/api/ci_intake.rb rename to lib/datadog/ci/transport/api/ci_test_cycle.rb index 3bd5aa74..7b6219a1 100644 --- a/lib/datadog/ci/transport/api/ci_intake.rb +++ b/lib/datadog/ci/transport/api/ci_test_cycle.rb @@ -7,7 +7,7 @@ module Datadog module CI module Transport module Api - class CIIntake < Base + class CiTestCycle < Base attr_reader :api_key, :http def initialize(api_key:, url:) diff --git a/sig/datadog/ci/transport/api/builder.rbs b/sig/datadog/ci/transport/api/builder.rbs index 2c959713..967236b0 100644 --- a/sig/datadog/ci/transport/api/builder.rbs +++ b/sig/datadog/ci/transport/api/builder.rbs @@ -3,7 +3,7 @@ module Datadog module Transport module Api module Builder - def self.build_ci_test_cycle_api: (untyped settings) -> Datadog::CI::Transport::Api::CIIntake + def self.build_ci_test_cycle_api: (untyped settings) -> Datadog::CI::Transport::Api::CiTestCycle def self.build_evp_proxy_api: (untyped agent_settings) -> Datadog::CI::Transport::Api::EVPProxy end end diff --git a/sig/datadog/ci/transport/api/ci_intake.rbs b/sig/datadog/ci/transport/api/ci_test_cycle.rbs similarity index 94% rename from sig/datadog/ci/transport/api/ci_intake.rbs rename to sig/datadog/ci/transport/api/ci_test_cycle.rbs index f934fb76..c56d3756 100644 --- a/sig/datadog/ci/transport/api/ci_intake.rbs +++ b/sig/datadog/ci/transport/api/ci_test_cycle.rbs @@ -2,7 +2,7 @@ module Datadog module CI module Transport module Api - class CIIntake < Base + class CiTestCycle < Base attr_reader api_key: String attr_reader http: Datadog::CI::Transport::HTTP diff --git a/spec/datadog/ci/configuration/components_spec.rb b/spec/datadog/ci/configuration/components_spec.rb index 5bd67681..730ed93f 100644 --- a/spec/datadog/ci/configuration/components_spec.rb +++ b/spec/datadog/ci/configuration/components_spec.rb @@ -146,7 +146,7 @@ expect(settings.tracing.test_mode).to have_received(:writer_options=) do |options| expect(options[:transport]).to be_kind_of(Datadog::CI::TestVisibility::Transport) - expect(options[:transport].api).to be_kind_of(Datadog::CI::Transport::Api::CIIntake) + expect(options[:transport].api).to be_kind_of(Datadog::CI::Transport::Api::CiTestCycle) expect(options[:shutdown_timeout]).to eq(60) end end diff --git a/spec/datadog/ci/transport/api/builder_spec.rb b/spec/datadog/ci/transport/api/builder_spec.rb index d0e2e64d..07584a22 100644 --- a/spec/datadog/ci/transport/api/builder_spec.rb +++ b/spec/datadog/ci/transport/api/builder_spec.rb @@ -35,7 +35,7 @@ end it "creates CI Intake" do - expect(Datadog::CI::Transport::Api::CIIntake).to receive(:new).with( + expect(Datadog::CI::Transport::Api::CiTestCycle).to receive(:new).with( api_key: "api_key", url: "https://citestcycle-intake.datadoghq.com:443" ) subject @@ -45,7 +45,7 @@ let(:agentless_url) { "http://localhost:5555" } it "configures transport to use intake URL from settings" do - expect(Datadog::CI::Transport::Api::CIIntake).to receive(:new).with( + expect(Datadog::CI::Transport::Api::CiTestCycle).to receive(:new).with( api_key: "api_key", url: "http://localhost:5555" ) subject @@ -56,7 +56,7 @@ let(:dd_site) { "datadoghq.eu" } it "construct intake url using provided host" do - expect(Datadog::CI::Transport::Api::CIIntake).to receive(:new).with( + expect(Datadog::CI::Transport::Api::CiTestCycle).to receive(:new).with( api_key: "api_key", url: "https://citestcycle-intake.datadoghq.eu:443" ) subject diff --git a/spec/datadog/ci/transport/api/ci_intake_spec.rb b/spec/datadog/ci/transport/api/ci_test_cycle_spec.rb similarity index 95% rename from spec/datadog/ci/transport/api/ci_intake_spec.rb rename to spec/datadog/ci/transport/api/ci_test_cycle_spec.rb index a0d7a324..1e3aea0c 100644 --- a/spec/datadog/ci/transport/api/ci_intake_spec.rb +++ b/spec/datadog/ci/transport/api/ci_test_cycle_spec.rb @@ -1,6 +1,6 @@ -require_relative "../../../../../lib/datadog/ci/transport/api/ci_intake" +require_relative "../../../../../lib/datadog/ci/transport/api/ci_test_cycle" -RSpec.describe Datadog::CI::Transport::Api::CIIntake do +RSpec.describe Datadog::CI::Transport::Api::CiTestCycle do subject do described_class.new( api_key: api_key, From 6ba15b61008795c361b8b8270378be20a16e0146 Mon Sep 17 00:00:00 2001 From: Andrey Marchenko Date: Tue, 24 Oct 2023 12:21:04 +0200 Subject: [PATCH 19/24] rename EVPProxy to EvpProxy --- lib/datadog/ci/transport/api/builder.rb | 2 +- lib/datadog/ci/transport/api/evp_proxy.rb | 2 +- sig/datadog/ci/transport/api/builder.rbs | 2 +- sig/datadog/ci/transport/api/evp_proxy.rbs | 2 +- spec/datadog/ci/configuration/components_spec.rb | 2 +- spec/datadog/ci/transport/api/builder_spec.rb | 4 ++-- spec/datadog/ci/transport/api/evp_proxy_spec.rb | 2 +- 7 files changed, 8 insertions(+), 8 deletions(-) diff --git a/lib/datadog/ci/transport/api/builder.rb b/lib/datadog/ci/transport/api/builder.rb index 6dac9291..7bfa9fe3 100644 --- a/lib/datadog/ci/transport/api/builder.rb +++ b/lib/datadog/ci/transport/api/builder.rb @@ -17,7 +17,7 @@ def self.build_ci_test_cycle_api(settings) end def self.build_evp_proxy_api(agent_settings) - EVPProxy.new( + EvpProxy.new( host: agent_settings.hostname, port: agent_settings.port, ssl: agent_settings.ssl, diff --git a/lib/datadog/ci/transport/api/evp_proxy.rb b/lib/datadog/ci/transport/api/evp_proxy.rb index 34ffb1bd..f8dde07c 100644 --- a/lib/datadog/ci/transport/api/evp_proxy.rb +++ b/lib/datadog/ci/transport/api/evp_proxy.rb @@ -7,7 +7,7 @@ module Datadog module CI module Transport module Api - class EVPProxy < Base + class EvpProxy < Base attr_reader :http def initialize(host:, port:, ssl:, timeout:) diff --git a/sig/datadog/ci/transport/api/builder.rbs b/sig/datadog/ci/transport/api/builder.rbs index 967236b0..52217995 100644 --- a/sig/datadog/ci/transport/api/builder.rbs +++ b/sig/datadog/ci/transport/api/builder.rbs @@ -4,7 +4,7 @@ module Datadog module Api module Builder def self.build_ci_test_cycle_api: (untyped settings) -> Datadog::CI::Transport::Api::CiTestCycle - def self.build_evp_proxy_api: (untyped agent_settings) -> Datadog::CI::Transport::Api::EVPProxy + def self.build_evp_proxy_api: (untyped agent_settings) -> Datadog::CI::Transport::Api::EvpProxy end end end diff --git a/sig/datadog/ci/transport/api/evp_proxy.rbs b/sig/datadog/ci/transport/api/evp_proxy.rbs index 52bf2c0b..61ba52c0 100644 --- a/sig/datadog/ci/transport/api/evp_proxy.rbs +++ b/sig/datadog/ci/transport/api/evp_proxy.rbs @@ -2,7 +2,7 @@ module Datadog module CI module Transport module Api - class EVPProxy < Base + class EvpProxy < Base @http: Datadog::CI::Transport::HTTP @container_id: String? diff --git a/spec/datadog/ci/configuration/components_spec.rb b/spec/datadog/ci/configuration/components_spec.rb index 730ed93f..426baf99 100644 --- a/spec/datadog/ci/configuration/components_spec.rb +++ b/spec/datadog/ci/configuration/components_spec.rb @@ -108,7 +108,7 @@ expect(settings.tracing.test_mode).to have_received(:writer_options=) do |options| expect(options[:transport]).to be_kind_of(Datadog::CI::TestVisibility::Transport) - expect(options[:transport].api).to be_kind_of(Datadog::CI::Transport::Api::EVPProxy) + expect(options[:transport].api).to be_kind_of(Datadog::CI::Transport::Api::EvpProxy) expect(options[:shutdown_timeout]).to eq(60) end end diff --git a/spec/datadog/ci/transport/api/builder_spec.rb b/spec/datadog/ci/transport/api/builder_spec.rb index 07584a22..54b75b99 100644 --- a/spec/datadog/ci/transport/api/builder_spec.rb +++ b/spec/datadog/ci/transport/api/builder_spec.rb @@ -79,8 +79,8 @@ ) end - it "creates EVPProxy" do - expect(Datadog::CI::Transport::Api::EVPProxy).to receive(:new).with( + it "creates EvpProxy" do + expect(Datadog::CI::Transport::Api::EvpProxy).to receive(:new).with( host: "localhost", port: 5555, ssl: false, diff --git a/spec/datadog/ci/transport/api/evp_proxy_spec.rb b/spec/datadog/ci/transport/api/evp_proxy_spec.rb index 735f416c..cf4ee4b8 100644 --- a/spec/datadog/ci/transport/api/evp_proxy_spec.rb +++ b/spec/datadog/ci/transport/api/evp_proxy_spec.rb @@ -1,6 +1,6 @@ require_relative "../../../../../lib/datadog/ci/transport/api/evp_proxy" -RSpec.describe Datadog::CI::Transport::Api::EVPProxy do +RSpec.describe Datadog::CI::Transport::Api::EvpProxy do subject do described_class.new( host: host, From 2bfd58ecb83a27f0e9972d3c2db8b0e9b01f9f44 Mon Sep 17 00:00:00 2001 From: Andrey Marchenko Date: Tue, 24 Oct 2023 12:35:25 +0200 Subject: [PATCH 20/24] Transport::Api - pull up request method --- lib/datadog/ci/transport/api/base.rb | 8 ++++++++ lib/datadog/ci/transport/api/ci_test_cycle.rb | 11 +---------- lib/datadog/ci/transport/api/evp_proxy.rb | 7 ++----- sig/datadog/ci/transport/api/base.rbs | 2 ++ 4 files changed, 13 insertions(+), 15 deletions(-) diff --git a/lib/datadog/ci/transport/api/base.rb b/lib/datadog/ci/transport/api/base.rb index eb62a2d9..28a4ecf2 100644 --- a/lib/datadog/ci/transport/api/base.rb +++ b/lib/datadog/ci/transport/api/base.rb @@ -5,7 +5,15 @@ module CI module Transport module Api class Base + attr_reader :http + def request(path:, payload:, verb: "post") + http.request( + path: path, + payload: payload, + verb: verb, + headers: headers + ) end private diff --git a/lib/datadog/ci/transport/api/ci_test_cycle.rb b/lib/datadog/ci/transport/api/ci_test_cycle.rb index 7b6219a1..aa76c59b 100644 --- a/lib/datadog/ci/transport/api/ci_test_cycle.rb +++ b/lib/datadog/ci/transport/api/ci_test_cycle.rb @@ -8,7 +8,7 @@ module CI module Transport module Api class CiTestCycle < Base - attr_reader :api_key, :http + attr_reader :api_key def initialize(api_key:, url:) @api_key = api_key @@ -24,15 +24,6 @@ def initialize(api_key:, url:) ) end - def request(path:, payload:, verb: "post") - http.request( - path: path, - payload: payload, - verb: verb, - headers: headers - ) - end - private def headers diff --git a/lib/datadog/ci/transport/api/evp_proxy.rb b/lib/datadog/ci/transport/api/evp_proxy.rb index f8dde07c..fdb6c066 100644 --- a/lib/datadog/ci/transport/api/evp_proxy.rb +++ b/lib/datadog/ci/transport/api/evp_proxy.rb @@ -8,8 +8,6 @@ module CI module Transport module Api class EvpProxy < Base - attr_reader :http - def initialize(host:, port:, ssl:, timeout:) @http = Datadog::CI::Transport::HTTP.new( host: host, @@ -23,11 +21,10 @@ def initialize(host:, port:, ssl:, timeout:) def request(path:, payload:, verb: "post") path = "#{Ext::Transport::EVP_PROXY_PATH_PREFIX}#{path.sub(/^\//, "")}" - http.request( + super( path: path, payload: payload, - verb: verb, - headers: headers + verb: verb ) end diff --git a/sig/datadog/ci/transport/api/base.rbs b/sig/datadog/ci/transport/api/base.rbs index c0467e33..74f0757b 100644 --- a/sig/datadog/ci/transport/api/base.rbs +++ b/sig/datadog/ci/transport/api/base.rbs @@ -3,6 +3,8 @@ module Datadog module Transport module Api class Base + attr_reader http: Datadog::CI::Transport::HTTP + def request: (path: String, payload: String, ?verb: ::String) -> untyped private From 52b3a073ab49dbbd4682d77e7867653d5045e0ab Mon Sep 17 00:00:00 2001 From: Andrey Marchenko Date: Tue, 24 Oct 2023 12:43:32 +0200 Subject: [PATCH 21/24] Transport::Api::Base accepts HTTP client in intializer --- lib/datadog/ci/transport/api/base.rb | 4 ++++ lib/datadog/ci/transport/api/ci_test_cycle.rb | 12 +++++++----- lib/datadog/ci/transport/api/evp_proxy.rb | 14 ++++++++------ sig/datadog/ci/transport/api/base.rbs | 2 ++ 4 files changed, 21 insertions(+), 11 deletions(-) diff --git a/lib/datadog/ci/transport/api/base.rb b/lib/datadog/ci/transport/api/base.rb index 28a4ecf2..f231805f 100644 --- a/lib/datadog/ci/transport/api/base.rb +++ b/lib/datadog/ci/transport/api/base.rb @@ -7,6 +7,10 @@ module Api class Base attr_reader :http + def initialize(http:) + @http = http + end + def request(path:, payload:, verb: "post") http.request( path: path, diff --git a/lib/datadog/ci/transport/api/ci_test_cycle.rb b/lib/datadog/ci/transport/api/ci_test_cycle.rb index aa76c59b..b91955f2 100644 --- a/lib/datadog/ci/transport/api/ci_test_cycle.rb +++ b/lib/datadog/ci/transport/api/ci_test_cycle.rb @@ -16,11 +16,13 @@ def initialize(api_key:, url:) uri = URI.parse(url) raise "Invalid agentless mode URL: #{url}" if uri.host.nil? - @http = Datadog::CI::Transport::HTTP.new( - host: uri.host, - port: uri.port, - ssl: uri.scheme == "https" || uri.port == 443, - compress: true + super( + http: Datadog::CI::Transport::HTTP.new( + host: uri.host, + port: uri.port, + ssl: uri.scheme == "https" || uri.port == 443, + compress: true + ) ) end diff --git a/lib/datadog/ci/transport/api/evp_proxy.rb b/lib/datadog/ci/transport/api/evp_proxy.rb index fdb6c066..5e65e66d 100644 --- a/lib/datadog/ci/transport/api/evp_proxy.rb +++ b/lib/datadog/ci/transport/api/evp_proxy.rb @@ -9,12 +9,14 @@ module Transport module Api class EvpProxy < Base def initialize(host:, port:, ssl:, timeout:) - @http = Datadog::CI::Transport::HTTP.new( - host: host, - port: port, - ssl: ssl, - timeout: timeout, - compress: false + super( + http: Datadog::CI::Transport::HTTP.new( + host: host, + port: port, + ssl: ssl, + timeout: timeout, + compress: false + ) ) end diff --git a/sig/datadog/ci/transport/api/base.rbs b/sig/datadog/ci/transport/api/base.rbs index 74f0757b..00e70085 100644 --- a/sig/datadog/ci/transport/api/base.rbs +++ b/sig/datadog/ci/transport/api/base.rbs @@ -5,6 +5,8 @@ module Datadog class Base attr_reader http: Datadog::CI::Transport::HTTP + def initialize: (http: Datadog::CI::Transport::HTTP) -> void + def request: (path: String, payload: String, ?verb: ::String) -> untyped private From eb9a8ddd7b87d4043f98130fc487c173309018c2 Mon Sep 17 00:00:00 2001 From: Andrey Marchenko Date: Tue, 24 Oct 2023 12:59:15 +0200 Subject: [PATCH 22/24] move HTTP building to Builder from EvpProxy --- lib/datadog/ci/transport/api/builder.rb | 7 ++-- lib/datadog/ci/transport/api/evp_proxy.rb | 12 ------- sig/datadog/ci/transport/api/base.rbs | 2 ++ .../ci/transport/api/ci_test_cycle.rbs | 2 -- sig/datadog/ci/transport/api/evp_proxy.rbs | 6 ---- spec/datadog/ci/transport/api/builder_spec.rb | 17 ++++++--- .../ci/transport/api/evp_proxy_spec.rb | 36 +++++++------------ 7 files changed, 32 insertions(+), 50 deletions(-) diff --git a/lib/datadog/ci/transport/api/builder.rb b/lib/datadog/ci/transport/api/builder.rb index 7bfa9fe3..64de9b98 100644 --- a/lib/datadog/ci/transport/api/builder.rb +++ b/lib/datadog/ci/transport/api/builder.rb @@ -17,12 +17,15 @@ def self.build_ci_test_cycle_api(settings) end def self.build_evp_proxy_api(agent_settings) - EvpProxy.new( + http = Datadog::CI::Transport::HTTP.new( host: agent_settings.hostname, port: agent_settings.port, ssl: agent_settings.ssl, - timeout: agent_settings.timeout_seconds + timeout: agent_settings.timeout_seconds, + compress: false ) + + EvpProxy.new(http: http) end end end diff --git a/lib/datadog/ci/transport/api/evp_proxy.rb b/lib/datadog/ci/transport/api/evp_proxy.rb index 5e65e66d..6c520776 100644 --- a/lib/datadog/ci/transport/api/evp_proxy.rb +++ b/lib/datadog/ci/transport/api/evp_proxy.rb @@ -8,18 +8,6 @@ module CI module Transport module Api class EvpProxy < Base - def initialize(host:, port:, ssl:, timeout:) - super( - http: Datadog::CI::Transport::HTTP.new( - host: host, - port: port, - ssl: ssl, - timeout: timeout, - compress: false - ) - ) - end - def request(path:, payload:, verb: "post") path = "#{Ext::Transport::EVP_PROXY_PATH_PREFIX}#{path.sub(/^\//, "")}" diff --git a/sig/datadog/ci/transport/api/base.rbs b/sig/datadog/ci/transport/api/base.rbs index 00e70085..6fd14d70 100644 --- a/sig/datadog/ci/transport/api/base.rbs +++ b/sig/datadog/ci/transport/api/base.rbs @@ -5,6 +5,8 @@ module Datadog class Base attr_reader http: Datadog::CI::Transport::HTTP + @http: Datadog::CI::Transport::HTTP + def initialize: (http: Datadog::CI::Transport::HTTP) -> void def request: (path: String, payload: String, ?verb: ::String) -> untyped diff --git a/sig/datadog/ci/transport/api/ci_test_cycle.rbs b/sig/datadog/ci/transport/api/ci_test_cycle.rbs index c56d3756..d40f21ad 100644 --- a/sig/datadog/ci/transport/api/ci_test_cycle.rbs +++ b/sig/datadog/ci/transport/api/ci_test_cycle.rbs @@ -4,10 +4,8 @@ module Datadog module Api class CiTestCycle < Base attr_reader api_key: String - attr_reader http: Datadog::CI::Transport::HTTP @api_key: String - @http: Datadog::CI::Transport::HTTP def initialize: (api_key: String, url: String) -> void diff --git a/sig/datadog/ci/transport/api/evp_proxy.rbs b/sig/datadog/ci/transport/api/evp_proxy.rbs index 61ba52c0..7f989a19 100644 --- a/sig/datadog/ci/transport/api/evp_proxy.rbs +++ b/sig/datadog/ci/transport/api/evp_proxy.rbs @@ -3,14 +3,8 @@ module Datadog module Transport module Api class EvpProxy < Base - @http: Datadog::CI::Transport::HTTP - @container_id: String? - attr_reader http: Datadog::CI::Transport::HTTP - - def initialize: (host: String, port: Integer, ssl: bool, timeout: Integer) -> void - def request: (path: String, payload: String, ?verb: ::String) -> Datadog::CI::Transport::HTTP::ResponseDecorator private diff --git a/spec/datadog/ci/transport/api/builder_spec.rb b/spec/datadog/ci/transport/api/builder_spec.rb index 54b75b99..9a709a70 100644 --- a/spec/datadog/ci/transport/api/builder_spec.rb +++ b/spec/datadog/ci/transport/api/builder_spec.rb @@ -67,6 +67,9 @@ describe ".build_evp_proxy_api" do subject { described_class.build_evp_proxy_api(agent_settings) } + let(:api) { double(:api) } + let(:http) { double(:http) } + let(:agent_settings) do Datadog::Core::Configuration::AgentSettingsResolver::AgentSettings.new( adapter: nil, @@ -79,14 +82,18 @@ ) end - it "creates EvpProxy" do - expect(Datadog::CI::Transport::Api::EvpProxy).to receive(:new).with( + it "creates and configures http client and EvpProxy" do + expect(Datadog::CI::Transport::HTTP).to receive(:new).with( host: "localhost", port: 5555, ssl: false, - timeout: 42 - ) - subject + timeout: 42, + compress: false + ).and_return(http) + + expect(Datadog::CI::Transport::Api::EvpProxy).to receive(:new).with(http: http).and_return(api) + + expect(subject).to eq(api) end end end diff --git a/spec/datadog/ci/transport/api/evp_proxy_spec.rb b/spec/datadog/ci/transport/api/evp_proxy_spec.rb index cf4ee4b8..657c0457 100644 --- a/spec/datadog/ci/transport/api/evp_proxy_spec.rb +++ b/spec/datadog/ci/transport/api/evp_proxy_spec.rb @@ -2,34 +2,24 @@ RSpec.describe Datadog::CI::Transport::Api::EvpProxy do subject do - described_class.new( - host: host, - port: port, - ssl: ssl, - timeout: timeout - ) + described_class.new(http: http) end - let(:host) { "localhost" } - let(:port) { 5555 } - let(:ssl) { false } - let(:timeout) { 42 } - let(:http) { double(:http) } - describe "#initialize" do - it "creates HTTP transport" do - expect(Datadog::CI::Transport::HTTP).to receive(:new).with( - host: host, - port: port, - ssl: ssl, - timeout: timeout, - compress: false - ).and_return(http) + # describe "#initialize" do + # it "creates HTTP transport" do + # expect(Datadog::CI::Transport::HTTP).to receive(:new).with( + # host: host, + # port: port, + # ssl: ssl, + # timeout: timeout, + # compress: false + # ).and_return(http) - subject - end - end + # subject + # end + # end describe "#request" do before do From 37726332bb2259557149a41796004f91bf5834b5 Mon Sep 17 00:00:00 2001 From: Andrey Marchenko Date: Tue, 24 Oct 2023 13:26:09 +0200 Subject: [PATCH 23/24] move HTTP building to Builder from CiTestCycle --- lib/datadog/ci/transport/api/builder.rb | 12 ++++- lib/datadog/ci/transport/api/ci_test_cycle.rb | 14 +----- .../ci/transport/api/ci_test_cycle.rbs | 2 +- spec/datadog/ci/transport/api/builder_spec.rb | 46 +++++++++++++++---- .../ci/transport/api/ci_test_cycle_spec.rb | 34 +------------- .../ci/transport/api/evp_proxy_spec.rb | 14 ------ 6 files changed, 51 insertions(+), 71 deletions(-) diff --git a/lib/datadog/ci/transport/api/builder.rb b/lib/datadog/ci/transport/api/builder.rb index 64de9b98..0f379fba 100644 --- a/lib/datadog/ci/transport/api/builder.rb +++ b/lib/datadog/ci/transport/api/builder.rb @@ -13,7 +13,17 @@ def self.build_ci_test_cycle_api(settings) url = settings.ci.agentless_url || "https://#{Ext::Transport::TEST_VISIBILITY_INTAKE_HOST_PREFIX}.#{dd_site}:443" - CiTestCycle.new(api_key: settings.api_key, url: url) + uri = URI.parse(url) + raise "Invalid agentless mode URL: #{url}" if uri.host.nil? + + http = Datadog::CI::Transport::HTTP.new( + host: uri.host, + port: uri.port, + ssl: uri.scheme == "https" || uri.port == 443, + compress: true + ) + + CiTestCycle.new(api_key: settings.api_key, http: http) end def self.build_evp_proxy_api(agent_settings) diff --git a/lib/datadog/ci/transport/api/ci_test_cycle.rb b/lib/datadog/ci/transport/api/ci_test_cycle.rb index b91955f2..b2558256 100644 --- a/lib/datadog/ci/transport/api/ci_test_cycle.rb +++ b/lib/datadog/ci/transport/api/ci_test_cycle.rb @@ -10,20 +10,10 @@ module Api class CiTestCycle < Base attr_reader :api_key - def initialize(api_key:, url:) + def initialize(api_key:, http:) @api_key = api_key - uri = URI.parse(url) - raise "Invalid agentless mode URL: #{url}" if uri.host.nil? - - super( - http: Datadog::CI::Transport::HTTP.new( - host: uri.host, - port: uri.port, - ssl: uri.scheme == "https" || uri.port == 443, - compress: true - ) - ) + super(http: http) end private diff --git a/sig/datadog/ci/transport/api/ci_test_cycle.rbs b/sig/datadog/ci/transport/api/ci_test_cycle.rbs index d40f21ad..ef48bd45 100644 --- a/sig/datadog/ci/transport/api/ci_test_cycle.rbs +++ b/sig/datadog/ci/transport/api/ci_test_cycle.rbs @@ -7,7 +7,7 @@ module Datadog @api_key: String - def initialize: (api_key: String, url: String) -> void + def initialize: (api_key: String, http: Datadog::CI::Transport::HTTP) -> void def request: (path: String, payload: String, ?verb: ::String) -> Datadog::CI::Transport::HTTP::ResponseDecorator diff --git a/spec/datadog/ci/transport/api/builder_spec.rb b/spec/datadog/ci/transport/api/builder_spec.rb index 9a709a70..7331f37b 100644 --- a/spec/datadog/ci/transport/api/builder_spec.rb +++ b/spec/datadog/ci/transport/api/builder_spec.rb @@ -15,6 +15,8 @@ describe ".build_ci_test_cycle_api" do subject { described_class.build_ci_test_cycle_api(settings) } + let(:api) { double(:api) } + let(:http) { double(:http) } let(:agentless_url) { nil } let(:dd_site) { nil } let(:api_key) { "api_key" } @@ -34,21 +36,37 @@ .and_return(api_key) end - it "creates CI Intake" do + it "creates and configures http client and CiTestCycle" do + expect(Datadog::CI::Transport::HTTP).to receive(:new).with( + host: "citestcycle-intake.datadoghq.com", + port: 443, + ssl: true, + compress: true + ).and_return(http) + expect(Datadog::CI::Transport::Api::CiTestCycle).to receive(:new).with( - api_key: "api_key", url: "https://citestcycle-intake.datadoghq.com:443" - ) - subject + api_key: "api_key", http: http + ).and_return(api) + + expect(subject).to eq(api) end context "when agentless_url is provided" do let(:agentless_url) { "http://localhost:5555" } it "configures transport to use intake URL from settings" do + expect(Datadog::CI::Transport::HTTP).to receive(:new).with( + host: "localhost", + port: 5555, + ssl: false, + compress: true + ).and_return(http) + expect(Datadog::CI::Transport::Api::CiTestCycle).to receive(:new).with( - api_key: "api_key", url: "http://localhost:5555" - ) - subject + api_key: "api_key", http: http + ).and_return(api) + + expect(subject).to eq(api) end end @@ -56,10 +74,18 @@ let(:dd_site) { "datadoghq.eu" } it "construct intake url using provided host" do + expect(Datadog::CI::Transport::HTTP).to receive(:new).with( + host: "citestcycle-intake.datadoghq.eu", + port: 443, + ssl: true, + compress: true + ).and_return(http) + expect(Datadog::CI::Transport::Api::CiTestCycle).to receive(:new).with( - api_key: "api_key", url: "https://citestcycle-intake.datadoghq.eu:443" - ) - subject + api_key: "api_key", http: http + ).and_return(api) + + expect(subject).to eq(api) end end end diff --git a/spec/datadog/ci/transport/api/ci_test_cycle_spec.rb b/spec/datadog/ci/transport/api/ci_test_cycle_spec.rb index 1e3aea0c..4c961133 100644 --- a/spec/datadog/ci/transport/api/ci_test_cycle_spec.rb +++ b/spec/datadog/ci/transport/api/ci_test_cycle_spec.rb @@ -4,45 +4,13 @@ subject do described_class.new( api_key: api_key, - url: url + http: http ) end let(:api_key) { "api_key" } - let(:url) { "https://citestcycle-intake.datad0ghq.com:443" } - let(:http) { double(:http) } - describe "#initialize" do - context "with https URL" do - it "creates SSL transport" do - expect(Datadog::CI::Transport::HTTP).to receive(:new).with( - host: "citestcycle-intake.datad0ghq.com", - port: 443, - ssl: true, - compress: true - ).and_return(http) - - subject - end - end - - context "with http URL" do - let(:url) { "http://localhost:5555" } - - it "creates http transport without SSL" do - expect(Datadog::CI::Transport::HTTP).to receive(:new).with( - host: "localhost", - port: 5555, - ssl: false, - compress: true - ).and_return(http) - - subject - end - end - end - describe "#request" do before do allow(Datadog::CI::Transport::HTTP).to receive(:new).and_return(http) diff --git a/spec/datadog/ci/transport/api/evp_proxy_spec.rb b/spec/datadog/ci/transport/api/evp_proxy_spec.rb index 657c0457..dbd64519 100644 --- a/spec/datadog/ci/transport/api/evp_proxy_spec.rb +++ b/spec/datadog/ci/transport/api/evp_proxy_spec.rb @@ -7,20 +7,6 @@ let(:http) { double(:http) } - # describe "#initialize" do - # it "creates HTTP transport" do - # expect(Datadog::CI::Transport::HTTP).to receive(:new).with( - # host: host, - # port: port, - # ssl: ssl, - # timeout: timeout, - # compress: false - # ).and_return(http) - - # subject - # end - # end - describe "#request" do before do allow(Datadog::CI::Transport::HTTP).to receive(:new).and_return(http) From 34958c7581eb2fa3fb090d8fe5cffb5f8608b9df Mon Sep 17 00:00:00 2001 From: Andrey Marchenko Date: Tue, 24 Oct 2023 13:39:11 +0200 Subject: [PATCH 24/24] update requires --- lib/datadog/ci/configuration/components.rb | 4 ++++ lib/datadog/ci/test_visibility/transport.rb | 1 - lib/datadog/ci/transport/api/base.rb | 2 ++ lib/datadog/ci/transport/api/builder.rb | 2 ++ lib/datadog/ci/transport/api/ci_test_cycle.rb | 2 +- lib/datadog/ci/transport/api/evp_proxy.rb | 4 +++- 6 files changed, 12 insertions(+), 3 deletions(-) diff --git a/lib/datadog/ci/configuration/components.rb b/lib/datadog/ci/configuration/components.rb index dcb12fc5..4d84a413 100644 --- a/lib/datadog/ci/configuration/components.rb +++ b/lib/datadog/ci/configuration/components.rb @@ -1,5 +1,9 @@ # frozen_string_literal: true +require "datadog/core/configuration/agent_settings_resolver" +require "datadog/core/remote/negotiation" + +require_relative "../ext/transport" require_relative "../test_visibility/flush" require_relative "../test_visibility/transport" require_relative "../transport/api/builder" diff --git a/lib/datadog/ci/test_visibility/transport.rb b/lib/datadog/ci/test_visibility/transport.rb index 5313333c..03b12fec 100644 --- a/lib/datadog/ci/test_visibility/transport.rb +++ b/lib/datadog/ci/test_visibility/transport.rb @@ -9,7 +9,6 @@ require_relative "serializers/factories/test_level" require_relative "../ext/transport" -require_relative "../transport/http" module Datadog module CI diff --git a/lib/datadog/ci/transport/api/base.rb b/lib/datadog/ci/transport/api/base.rb index f231805f..6230b887 100644 --- a/lib/datadog/ci/transport/api/base.rb +++ b/lib/datadog/ci/transport/api/base.rb @@ -1,5 +1,7 @@ # frozen_string_literal: true +require_relative "../../ext/transport" + module Datadog module CI module Transport diff --git a/lib/datadog/ci/transport/api/builder.rb b/lib/datadog/ci/transport/api/builder.rb index 0f379fba..cf65674d 100644 --- a/lib/datadog/ci/transport/api/builder.rb +++ b/lib/datadog/ci/transport/api/builder.rb @@ -2,6 +2,8 @@ require_relative "ci_test_cycle" require_relative "evp_proxy" +require_relative "../http" +require_relative "../../ext/transport" module Datadog module CI diff --git a/lib/datadog/ci/transport/api/ci_test_cycle.rb b/lib/datadog/ci/transport/api/ci_test_cycle.rb index b2558256..b3cec84d 100644 --- a/lib/datadog/ci/transport/api/ci_test_cycle.rb +++ b/lib/datadog/ci/transport/api/ci_test_cycle.rb @@ -1,7 +1,7 @@ # frozen_string_literal: true require_relative "base" -require_relative "../http" +require_relative "../../ext/transport" module Datadog module CI diff --git a/lib/datadog/ci/transport/api/evp_proxy.rb b/lib/datadog/ci/transport/api/evp_proxy.rb index 6c520776..87ccd79c 100644 --- a/lib/datadog/ci/transport/api/evp_proxy.rb +++ b/lib/datadog/ci/transport/api/evp_proxy.rb @@ -1,7 +1,9 @@ # frozen_string_literal: true +require "datadog/core/environment/container" + require_relative "base" -require_relative "../http" +require_relative "../../ext/transport" module Datadog module CI