Skip to content

Commit

Permalink
more specs for Datadog::CI::Span
Browse files Browse the repository at this point in the history
  • Loading branch information
anmarchenko committed Nov 2, 2023
1 parent fb5fdee commit 75b751f
Show file tree
Hide file tree
Showing 8 changed files with 84 additions and 216 deletions.
2 changes: 1 addition & 1 deletion lib/datadog/ci/contrib/cucumber/formatter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ def on_test_step_finished(event)
return if @current_step_span.nil?

if event.result.skipped?
@current_step_span.skipped!(@current_step_span)
@current_step_span.skipped!
elsif event.result.ok?
@current_step_span.passed!
elsif event.result.failed?
Expand Down
2 changes: 1 addition & 1 deletion lib/datadog/ci/contrib/minitest/hooks.rb
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ def after_teardown
when "E", "F"
test_span.failed!(exception: failure)
when "S"
test_span.skipped!(nil, failure.message)
test_span.skipped!(reason: failure.message)
end

test_span.finish
Expand Down
2 changes: 1 addition & 1 deletion lib/datadog/ci/contrib/rspec/example.rb
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ def run(example_group_instance, reporter)
when :failed
test_span.failed!(exception: execution_result.exception)
else
test_span.skipped!(execution_result.exception) if execution_result.example_skipped?
test_span.skipped!(exception: execution_result.exception) if execution_result.example_skipped?
end

result
Expand Down
2 changes: 1 addition & 1 deletion lib/datadog/ci/span.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ def failed!(exception: nil)
tracer_span.set_error(exception) unless exception.nil?
end

def skipped!(exception = nil, reason = nil)
def skipped!(exception: nil, reason: nil)
tracer_span.set_tag(Ext::Test::TAG_STATUS, Ext::Test::Status::SKIP)
tracer_span.set_error(exception) unless exception.nil?
tracer_span.set_tag(CI::Ext::Test::TAG_SKIP_REASON, reason) unless reason.nil?
Expand Down
4 changes: 2 additions & 2 deletions sig/datadog/ci/span.rbs
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@ module Datadog

def passed!: () -> void

def failed!: (?untyped? exception) -> void
def failed!: (?exception: untyped?) -> void

def skipped!: (?untyped? exception, ?String? reason) -> void
def skipped!: (?exception: untyped?, ?reason: String?) -> void

def set_tag: (String key, untyped? value) -> untyped

Expand Down
208 changes: 0 additions & 208 deletions spec/datadog/ci/recorder_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,19 +17,6 @@
.with(span_op)
end

# TODO: move to Span
# it do
# expect(span_op.get_tag(Datadog::CI::Ext::Test::TAG_SPAN_KIND))
# .to eq(Datadog::CI::Ext::AppTypes::TYPE_TEST)
# end

# it do
# Datadog::CI::Ext::Environment.tags(ENV).each do |key, value|
# expect(span_op.get_tag(key))
# .to eq(value)
# end
# end

it do
expect(trace_op)
.to have_received(:origin=)
Expand Down Expand Up @@ -211,90 +198,6 @@
end
end

# TODO: move to span
# describe "::set_tags!" do
# subject(:set_tags!) { described_class.set_tags!(trace_op, span_op, tags) }
# let(:span_op) { Datadog::Tracing::SpanOperation.new(operation_name) }
# let(:tags) { {} }

# before do
# allow(Datadog::Tracing::Contrib::Analytics).to receive(:set_measured)
# end

# it_behaves_like "default test span operation tags" do
# before { set_tags! }
# end

# context "when trace operation is given" do
# before { set_tags! }

# it do
# expect(trace_op)
# .to have_received(:origin=)
# .with(Datadog::CI::Ext::Test::CONTEXT_ORIGIN)
# end
# end

# context "when :framework is given" do
# let(:tags) { {framework: framework} }
# let(:framework) { "framework" }

# before { set_tags! }

# it do
# expect(span_op.get_tag(Datadog::CI::Ext::Test::TAG_FRAMEWORK))
# .to eq(framework)
# end
# end

# context "when :framework_version is given" do
# let(:tags) { {framework_version: framework_version} }
# let(:framework_version) { "framework_version" }

# before { set_tags! }

# it do
# expect(span_op.get_tag(Datadog::CI::Ext::Test::TAG_FRAMEWORK_VERSION))
# .to eq(framework_version)
# end
# end

# context "when :test_name is given" do
# let(:tags) { {test_name: test_name} }
# let(:test_name) { "test name" }

# before { set_tags! }

# it do
# expect(span_op.get_tag(Datadog::CI::Ext::Test::TAG_NAME))
# .to eq(test_name)
# end
# end

# context "when :test_suite is given" do
# let(:tags) { {test_suite: test_suite} }
# let(:test_suite) { "test suite" }

# before { set_tags! }

# it do
# expect(span_op.get_tag(Datadog::CI::Ext::Test::TAG_SUITE))
# .to eq(test_suite)
# end
# end

# context "when :test_type is given" do
# let(:tags) { {test_type: test_type} }
# let(:test_type) { "test type" }

# before { set_tags! }

# it do
# expect(span_op.get_tag(Datadog::CI::Ext::Test::TAG_TYPE))
# .to eq(test_type)
# end
# end

# context "with environment runtime information" do
# context "for the architecture platform" do
# subject(:tag) do
Expand Down Expand Up @@ -371,115 +274,4 @@
# end
# end
# end

# TODO: move to Span
# describe "::passed!" do
# subject(:passed!) { described_class.passed!(span_op) }
# let(:span_op) { instance_double(Datadog::Tracing::SpanOperation) }

# before do
# allow(span_op).to receive(:set_tag)
# passed!
# end

# it do
# expect(span_op)
# .to have_received(:set_tag)
# .with(
# Datadog::CI::Ext::Test::TAG_STATUS,
# Datadog::CI::Ext::Test::Status::PASS
# )
# end
# end

# TODO: move to Span
# describe "::failed!" do
# let(:span_op) { instance_double(Datadog::Tracing::SpanOperation) }

# before do
# allow(span_op).to receive(:status=)
# allow(span_op).to receive(:set_tag)
# allow(span_op).to receive(:set_error)
# failed!
# end

# shared_examples "failed test span operation" do
# it do
# expect(span_op)
# .to have_received(:status=)
# .with(1)
# end

# it do
# expect(span_op)
# .to have_received(:set_tag)
# .with(
# Datadog::CI::Ext::Test::TAG_STATUS,
# Datadog::CI::Ext::Test::Status::FAIL
# )
# end
# end

# context "when no exception is given" do
# subject(:failed!) { described_class.failed!(span_op) }

# it_behaves_like "failed test span operation"
# it { expect(span_op).to_not have_received(:set_error) }
# end

# context "when exception is given" do
# subject(:failed!) { described_class.failed!(span_op, exception) }
# let(:exception) { instance_double(StandardError) }

# it_behaves_like "failed test span operation"

# it do
# expect(span_op)
# .to have_received(:set_error)
# .with(exception)
# end
# end
# end

# TODO: move to Span
# describe "::skipped!" do
# let(:span_op) { instance_double(Datadog::Tracing::SpanOperation) }

# before do
# allow(span_op).to receive(:set_tag)
# allow(span_op).to receive(:set_error)
# skipped!
# end

# shared_examples "skipped test span operation" do
# it do
# expect(span_op)
# .to have_received(:set_tag)
# .with(
# Datadog::CI::Ext::Test::TAG_STATUS,
# Datadog::CI::Ext::Test::Status::SKIP
# )
# end
# end

# context "when no exception is given" do
# subject(:skipped!) { described_class.skipped!(span_op) }

# it_behaves_like "skipped test span operation"
# it { expect(span_op).to_not have_received(:set_error) }
# end

# context "when exception is given" do
# subject(:skipped!) { described_class.skipped!(span_op, exception) }
# let(:exception) { instance_double(StandardError) }

# it_behaves_like "skipped test span operation"

# it do
# expect(span_op)
# .to have_received(:set_error)
# .with(exception)
# end
# end
# end
end
76 changes: 76 additions & 0 deletions spec/datadog/ci/span_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -67,4 +67,80 @@
end
end
end

describe "#skipped!" do
let(:tracer_span) { instance_double(Datadog::Tracing::SpanOperation) }
let(:span) { described_class.new(tracer_span) }

context "when exception is nil" do
it "sets the status to SKIP" do
expect(tracer_span).to receive(:set_tag).with("test.status", "skip")
expect(tracer_span).not_to receive(:set_error)

span.skipped!
end
end

context "when exception is provided" do
it "sets the status to SKIP and sets error" do
expect(tracer_span).to receive(:set_tag).with("test.status", "skip")
expect(tracer_span).to receive(:set_error).with("error")

span.skipped!(exception: "error")
end
end

context "when reason is nil" do
it "doesn't set the skip reason tag" do
expect(tracer_span).to receive(:set_tag).with("test.status", "skip")
expect(tracer_span).not_to receive(:set_tag).with("test.skip_reason", "reason")

span

span.skipped!
end
end

context "when reason is provided" do
it "sets the skip reason tag" do
expect(tracer_span).to receive(:set_tag).with("test.status", "skip")
expect(tracer_span).to receive(:set_tag).with("test.skip_reason", "reason")

span.skipped!(reason: "reason")
end
end
end

describe "#set_tag" do
let(:tracer_span) { instance_double(Datadog::Tracing::SpanOperation) }
let(:span) { described_class.new(tracer_span) }

it "sets the tag" do
expect(tracer_span).to receive(:set_tag).with("foo", "bar")

span.set_tag("foo", "bar")
end
end

describe "#set_metric" do
let(:tracer_span) { instance_double(Datadog::Tracing::SpanOperation) }
let(:span) { described_class.new(tracer_span) }

it "sets the metric" do
expect(tracer_span).to receive(:set_metric).with("foo", "bar")

span.set_metric("foo", "bar")
end
end

describe "#finish" do
let(:tracer_span) { instance_double(Datadog::Tracing::SpanOperation) }
let(:span) { described_class.new(tracer_span) }

it "finishes the span" do
expect(tracer_span).to receive(:finish)

span.finish
end
end
end
4 changes: 2 additions & 2 deletions spec/support/tracer_helpers.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ def produce_test_trace(
framework: "rspec", operation: "rspec.example",
test_name: "test_add", test_suite: "calculator_tests",
service: "rspec-test-suite", result: "PASSED", exception: nil,
start_time: Time.now, duration_seconds: 2,
skip_reason: nil, start_time: Time.now, duration_seconds: 2,
with_http_span: false
)
# each time monotonic clock is called it will return a number that is
Expand Down Expand Up @@ -47,7 +47,7 @@ def produce_test_trace(
when "FAILED"
test.failed!(exception: exception)
when "SKIPPED"
test.skipped!(exception)
test.skipped!(exception: exception, reason: skip_reason)
else
test.passed!
end
Expand Down

0 comments on commit 75b751f

Please sign in to comment.