Skip to content

Commit

Permalink
send itr_correlation_id in the content of test event payload
Browse files Browse the repository at this point in the history
  • Loading branch information
anmarchenko committed Apr 24, 2024
1 parent 5f01d0f commit f8d1a7c
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 3 deletions.
16 changes: 14 additions & 2 deletions lib/datadog/ci/test_visibility/serializers/test_v2.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,22 +10,34 @@ module Serializers
class TestV2 < TestV1
CONTENT_FIELDS = (["test_session_id", "test_module_id", "test_suite_id"] + TestV1::CONTENT_FIELDS).freeze

CONTENT_FIELDS_WITH_ITR_CORRELATION_ID = (CONTENT_FIELDS + ["itr_correlation_id"]).freeze

CONTENT_MAP_SIZE = calculate_content_map_size(CONTENT_FIELDS)

CONTENT_MAP_SIZE_WITH_ITR_CORRELATION_ID = calculate_content_map_size(CONTENT_FIELDS_WITH_ITR_CORRELATION_ID)

REQUIRED_FIELDS = (["test_session_id", "test_module_id", "test_suite_id"] + TestV1::REQUIRED_FIELDS).freeze

def content_fields
CONTENT_FIELDS
return CONTENT_FIELDS if itr_correlation_id.nil?

CONTENT_FIELDS_WITH_ITR_CORRELATION_ID
end

def content_map_size
CONTENT_MAP_SIZE
return CONTENT_MAP_SIZE if itr_correlation_id.nil?

CONTENT_MAP_SIZE_WITH_ITR_CORRELATION_ID
end

def version
2
end

def itr_correlation_id
options[:itr_correlation_id]
end

private

def required_fields
Expand Down
6 changes: 6 additions & 0 deletions sig/datadog/ci/test_visibility/serializers/test_v2.rbs
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,12 @@ module Datadog
class TestV2 < TestV1
CONTENT_FIELDS: ::Array[String | ::Hash[::String, String]]

CONTENT_FIELDS_WITH_ITR_CORRELATION_ID: ::Array[String | ::Hash[::String, String]]

CONTENT_MAP_SIZE: Integer

CONTENT_MAP_SIZE_WITH_ITR_CORRELATION_ID: Integer

REQUIRED_FIELDS: ::Array[String]

def content_fields: () -> ::Array[String | ::Hash[::String, String]]
Expand All @@ -15,6 +19,8 @@ module Datadog

def version: () -> 2

def itr_correlation_id: () -> String?

private

def required_fields: () -> Array[String]
Expand Down
15 changes: 14 additions & 1 deletion spec/datadog/ci/test_visibility/serializers/test_v2_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,9 @@
let(:integration_name) { :rspec }
end

let(:options) { {} }
include_context "msgpack serializer" do
subject { described_class.new(trace_for_span(first_test_span), first_test_span) }
subject { described_class.new(trace_for_span(first_test_span), first_test_span, options: options) }
end

describe "#to_msgpack" do
Expand Down Expand Up @@ -110,6 +111,18 @@
})
end
end

context "with itr correlation id" do
let(:options) { {itr_correlation_id: "itr-correlation-id"} }

before do
produce_test_session_trace
end

it "correctly serializes itr correlation id" do
expect(content).to include("itr_correlation_id" => "itr-correlation-id")
end
end
end

describe "#valid?" do
Expand Down
24 changes: 24 additions & 0 deletions spec/datadog/ci/test_visibility/transport_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,30 @@
end
end

context "with itr correlation id" do
let(:serializers_factory) { Datadog::CI::TestVisibility::Serializers::Factories::TestSuiteLevel }

before do
allow_any_instance_of(Datadog::CI::ITR::Runner).to receive(:correlation_id).and_return("correlation-id")

produce_test_session_trace
end

it "passes itr correlation id to serializer" do
subject.send_events([trace_for_span(first_test_span)])

expect(api).to have_received(:citestcycle_request) do |args|
payload = MessagePack.unpack(args[:payload])
expect(payload["version"]).to eq(1)

events = payload["events"]
expect(events.count).to eq(1)
expect(events.first["content"]["resource"]).to include("calculator_tests")
expect(events.first["content"]["itr_correlation_id"]).to eq("correlation-id")
end
end
end

context "multiple traces with 2 spans each" do
let(:traces_count) { 2 }
let(:expected_events_count) { 4 }
Expand Down

0 comments on commit f8d1a7c

Please sign in to comment.