Skip to content

Commit

Permalink
cleaning up tests, test serialization of failed test event
Browse files Browse the repository at this point in the history
  • Loading branch information
anmarchenko committed Sep 21, 2023
1 parent 622305a commit 777899f
Show file tree
Hide file tree
Showing 10 changed files with 118 additions and 107 deletions.
113 changes: 56 additions & 57 deletions lib/datadog/ci/test_visibility/transport.rb
Original file line number Diff line number Diff line change
@@ -1,57 +1,56 @@
# # frozen_string_literal: true

# require_relative "something_that_converts_traces"
# require "datadog/core/encoding"
# # use it to chunk payloads by size
# # require "datadog/core/chunker"

# module Datadog
# module CI
# module TestVisibility
# class Transport
# def initialize
# @encoder = Datadog::Core::Encoding::MsgpackEncoder
# end

# def send_traces(traces)
# # convert traces to events and construct payload
# events = traces.flat_map { |trace| SomethingThatConvertsTraces.convert(trace) }
# payload = Payload.new(events)
# # @encoder.encode(payload)
# end

# private

# # represents payload with some subset of serializable events to be sent to CI-APP intake
# class Payload
# def initialize(events)
# @events = events
# end

# def to_msgpack(packer)
# packer ||= MessagePack::Packer.new

# packer.write_map_header(3) # Set header with how many elements in the map
# packer.write("version")
# packer.write(1)

# packer.write("metadata")
# packer.write_map_header(3)

# packer.write("runtime-id")
# packer.write(@events.first.runtime_id)

# packer.write("language")
# packer.write("ruby")

# packer.write("library_version")
# packer.write(Datadog::CI::VERSION::STRING)

# packer.write_array_header(@events.size)
# packer.write(@events)
# end
# end
# end
# end
# end
# end
# frozen_string_literal: true

require "datadog/core/encoding"
# use it to chunk payloads by size
# require "datadog/core/chunker"

module Datadog
module CI
module TestVisibility
class Transport
# def initialize
# @encoder = Datadog::Core::Encoding::MsgpackEncoder
# end

# def send_traces(traces)
# # convert traces to events and construct payload
# events = traces.flat_map { |trace| SomethingThatConvertsTraces.convert(trace) }
# payload = Payload.new(events)
# # @encoder.encode(payload)
# end

# private

# # represents payload with some subset of serializable events to be sent to CI-APP intake
# class Payload
# def initialize(events)
# @events = events
# end

# def to_msgpack(packer)
# packer ||= MessagePack::Packer.new

# packer.write_map_header(3) # Set header with how many elements in the map
# packer.write("version")
# packer.write(1)

# packer.write("metadata")
# packer.write_map_header(3)

# packer.write("runtime-id")
# packer.write(@events.first.runtime_id)

# packer.write("language")
# packer.write("ruby")

# packer.write("library_version")
# packer.write(Datadog::CI::VERSION::STRING)

# packer.write_array_header(@events.size)
# packer.write(@events)
# end
# end
end
end
end
end
12 changes: 0 additions & 12 deletions lib/datadog/ci/transport/http.rb

This file was deleted.

8 changes: 8 additions & 0 deletions sig/datadog/ci/test_visibility/transport.rbs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
module Datadog
module CI
module TestVisibility
class Transport
end
end
end
end
9 changes: 0 additions & 9 deletions sig/datadog/ci/transport/http.rbs

This file was deleted.

19 changes: 6 additions & 13 deletions spec/datadog/ci/test_visibility/serializers/span_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@
let(:integration_name) { :rspec }
end

include_context "Test visibility event serialized" do
subject { described_class.new(trace, tracer_span) }
end

let(:test_span) do
spans.find { |span| span.type == "test" }
end
Expand All @@ -14,24 +18,14 @@
spans.find { |span| span.type != "test" }
end

subject { described_class.new(trace, tracer_span) }

describe "#to_msgpack" do
context "traced a single test execution with Recorder" do
before do
produce_test_trace(with_http_span: true)
end

let(:payload) { MessagePack.unpack(MessagePack.pack(subject)) }

it "serializes test event to messagepack" do
expect(payload).to include(
{
"version" => 1,
"type" => "span"
}
)
content = payload["content"]
expect_event_header(type: "span")
expect(content).to include(
{
"trace_id" => trace.id,
Expand All @@ -45,8 +39,7 @@
}
)

tags = content["meta"]
expect(tags).to include(
expect(meta).to include(
{
"custom_tag" => "custom_tag_value",
"_dd.origin" => "ciapp-test"
Expand Down
32 changes: 20 additions & 12 deletions spec/datadog/ci/test_visibility/serializers/test_v1_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,24 +6,19 @@
let(:integration_name) { :rspec }
end

subject { described_class.new(trace, span) }
include_context "Test visibility event serialized" do
subject { described_class.new(trace, span) }
end

describe "#to_msgpack" do
context "traced a single test execution with Recorder" do
before do
produce_test_trace
end

let(:payload) { MessagePack.unpack(MessagePack.pack(subject)) }

it "serializes test event to messagepack" do
expect(payload).to include(
{
"version" => 1,
"type" => "test"
}
)
content = payload["content"]
expect_event_header

expect(content).to include(
{
"trace_id" => trace.id,
Expand All @@ -35,10 +30,10 @@
}
)

tags = content["meta"]
expect(tags).to include(
expect(meta).to include(
{
"test.framework" => "rspec",
"test.status" => "pass",
"_dd.origin" => "ciapp-test"
}
)
Expand All @@ -48,5 +43,18 @@
#
end
end

context "trace a failed test" do
before do
produce_test_trace(result: "FAILED", exception: StandardError.new("1 + 2 are not equal to 5"))
end

it "has error" do
expect_event_header

expect(content).to include({"error" => 1})
expect(meta).to include({"test.status" => "fail"})
end
end
end
end
2 changes: 0 additions & 2 deletions spec/datadog/ci/test_visibility/serializers_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,6 @@
produce_test_trace(with_http_span: true)
end

let(:payload) { MessagePack.unpack(MessagePack.pack(subject)) }

it "converts trace to an array of serializable events" do
expect(subject.count).to eq(2)
end
Expand Down
1 change: 1 addition & 0 deletions spec/spec_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
require_relative "support/git_helpers"
require_relative "support/provider_test_helpers"
require_relative "support/ci_mode_helpers"
require_relative "support/test_visibility_event_serialized"

require "rspec/collection_matchers"
require "climate_control"
Expand Down
17 changes: 17 additions & 0 deletions spec/support/test_visibility_event_serialized.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
RSpec.shared_context "Test visibility event serialized" do
subject {}

let(:msgpack_json) { MessagePack.unpack(MessagePack.pack(subject)) }
let(:content) { msgpack_json["content"] }
let(:meta) { content["meta"] }
let(:metrics) { content["metrics"] }

def expect_event_header(version: 1, type: "test")
expect(msgpack_json).to include(
{
"version" => version,
"type" => type
}
)
end
end
12 changes: 10 additions & 2 deletions spec/support/tracer_helpers.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@ def tracer
def produce_test_trace(
framework: "rspec", operation: "rspec.example",
test_name: "test_add", test_suite: "calculator_tests",
service: "rspec-test-suite", with_http_span: false
service: "rspec-test-suite", result: "PASSED", exception: nil,
with_http_span: false
)
Datadog::CI::Recorder.trace(
operation,
Expand All @@ -33,7 +34,14 @@ def produce_test_trace(
end
end

Datadog::CI::Recorder.passed!(span)
case result
when "FAILED"
Datadog::CI::Recorder.failed!(span, exception)
when "SKIPPED"
Datadog::CI::Recorder.skipped!(span, exception)
else
Datadog::CI::Recorder.passed!(span)
end
end
end

Expand Down

0 comments on commit 777899f

Please sign in to comment.