From 75b751f8df5b01ca86673877f84b2ba847135152 Mon Sep 17 00:00:00 2001 From: Andrey Marchenko Date: Thu, 2 Nov 2023 15:36:50 +0100 Subject: [PATCH] more specs for Datadog::CI::Span --- lib/datadog/ci/contrib/cucumber/formatter.rb | 2 +- lib/datadog/ci/contrib/minitest/hooks.rb | 2 +- lib/datadog/ci/contrib/rspec/example.rb | 2 +- lib/datadog/ci/span.rb | 2 +- sig/datadog/ci/span.rbs | 4 +- spec/datadog/ci/recorder_spec.rb | 208 ------------------- spec/datadog/ci/span_spec.rb | 76 +++++++ spec/support/tracer_helpers.rb | 4 +- 8 files changed, 84 insertions(+), 216 deletions(-) diff --git a/lib/datadog/ci/contrib/cucumber/formatter.rb b/lib/datadog/ci/contrib/cucumber/formatter.rb index a94139a2..01d0b5ba 100644 --- a/lib/datadog/ci/contrib/cucumber/formatter.rb +++ b/lib/datadog/ci/contrib/cucumber/formatter.rb @@ -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? diff --git a/lib/datadog/ci/contrib/minitest/hooks.rb b/lib/datadog/ci/contrib/minitest/hooks.rb index 6ca7258c..b02bf668 100644 --- a/lib/datadog/ci/contrib/minitest/hooks.rb +++ b/lib/datadog/ci/contrib/minitest/hooks.rb @@ -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 diff --git a/lib/datadog/ci/contrib/rspec/example.rb b/lib/datadog/ci/contrib/rspec/example.rb index e20307af..cf9355a0 100644 --- a/lib/datadog/ci/contrib/rspec/example.rb +++ b/lib/datadog/ci/contrib/rspec/example.rb @@ -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 diff --git a/lib/datadog/ci/span.rb b/lib/datadog/ci/span.rb index 0f0c82f2..990c4965 100644 --- a/lib/datadog/ci/span.rb +++ b/lib/datadog/ci/span.rb @@ -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? diff --git a/sig/datadog/ci/span.rbs b/sig/datadog/ci/span.rbs index 0815e323..10fedc27 100644 --- a/sig/datadog/ci/span.rbs +++ b/sig/datadog/ci/span.rbs @@ -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 diff --git a/spec/datadog/ci/recorder_spec.rb b/spec/datadog/ci/recorder_spec.rb index 2434f8c2..1138b11d 100644 --- a/spec/datadog/ci/recorder_spec.rb +++ b/spec/datadog/ci/recorder_spec.rb @@ -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=) @@ -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 @@ -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 diff --git a/spec/datadog/ci/span_spec.rb b/spec/datadog/ci/span_spec.rb index a9fccedd..767532d6 100644 --- a/spec/datadog/ci/span_spec.rb +++ b/spec/datadog/ci/span_spec.rb @@ -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 diff --git a/spec/support/tracer_helpers.rb b/spec/support/tracer_helpers.rb index 94115cee..73a0e914 100644 --- a/spec/support/tracer_helpers.rb +++ b/spec/support/tracer_helpers.rb @@ -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 @@ -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