From eb7066a202340aa5326397e92095beced60f236f Mon Sep 17 00:00:00 2001 From: Andrey Marchenko Date: Thu, 11 Jan 2024 16:55:02 +0100 Subject: [PATCH 1/2] add parametrized shared examples to our rspec instrumentation tests --- spec/datadog/ci/contrib/rspec/instrumentation_spec.rb | 11 ++++++++--- spec/datadog/ci/contrib/rspec/some_shared_examples.rb | 4 ++-- 2 files changed, 10 insertions(+), 5 deletions(-) diff --git a/spec/datadog/ci/contrib/rspec/instrumentation_spec.rb b/spec/datadog/ci/contrib/rspec/instrumentation_spec.rb index 5a56a030..ddf8f61a 100644 --- a/spec/datadog/ci/contrib/rspec/instrumentation_spec.rb +++ b/spec/datadog/ci/contrib/rspec/instrumentation_spec.rb @@ -246,7 +246,8 @@ def rspec_session_run(with_failed_test: false, with_shared_test: false) if with_shared_test require_relative "some_shared_examples" - include_examples "Testing shared examples" + include_examples "Testing shared examples", 2 + include_examples "Testing shared examples", 1 end end @@ -371,8 +372,12 @@ def rspec_session_run(with_failed_test: false, with_shared_test: false) let!(:spec) { rspec_session_run(with_shared_test: true) } it "creates correct test spans connects all tests to a single test suite" do - shared_test_span = test_spans.find { |test_span| test_span.name == "SomeTest shared examples adds 1 and 1" } - expect(shared_test_span.get_tag(Datadog::CI::Ext::Test::TAG_SUITE)).to eq(spec.file_path) + shared_test_spans = test_spans.filter { |test_span| test_span.name == "SomeTest shared examples adds 1 and 1" } + expect(shared_test_spans).to have(2).items + + shared_test_spans.each do |shared_test_span| + expect(shared_test_span.get_tag(Datadog::CI::Ext::Test::TAG_SUITE)).to eq(spec.file_path) + end test_spans.each do |test_span| expect(test_span.get_tag(Datadog::CI::Ext::Test::TAG_TEST_SUITE_ID)).to eq(test_suite_span.id.to_s) diff --git a/spec/datadog/ci/contrib/rspec/some_shared_examples.rb b/spec/datadog/ci/contrib/rspec/some_shared_examples.rb index f563e388..6700d0fe 100644 --- a/spec/datadog/ci/contrib/rspec/some_shared_examples.rb +++ b/spec/datadog/ci/contrib/rspec/some_shared_examples.rb @@ -1,7 +1,7 @@ -RSpec.shared_examples "Testing shared examples" do +RSpec.shared_examples "Testing shared examples" do |expected_result| context "shared examples" do it "adds 1 and 1" do - expect(1 + 1).to eq(2) + expect(1 + 1).to eq(expected_result) end end end From 76f2a35c9db8d509ef7b6ec6a6f21becca7f342a Mon Sep 17 00:00:00 2001 From: Andrey Marchenko Date: Fri, 12 Jan 2024 14:08:37 +0100 Subject: [PATCH 2/2] exploring using RSpec scoped_id metadata as parameters metadata to make sure examples have unique fingerprint --- lib/datadog/ci/contrib/rspec/example.rb | 2 ++ lib/datadog/ci/span.rb | 2 +- spec/datadog/ci/contrib/rspec/instrumentation_spec.rb | 6 +++++- 3 files changed, 8 insertions(+), 2 deletions(-) diff --git a/lib/datadog/ci/contrib/rspec/example.rb b/lib/datadog/ci/contrib/rspec/example.rb index 78c92a62..a23677cb 100644 --- a/lib/datadog/ci/contrib/rspec/example.rb +++ b/lib/datadog/ci/contrib/rspec/example.rb @@ -37,6 +37,8 @@ def run(example_group_instance, reporter) }, service: configuration[:service_name] ) do |test_span| + test_span.set_parameters({}, {"scoped_id" => metadata[:scoped_id]}) + result = super case execution_result.status diff --git a/lib/datadog/ci/span.rb b/lib/datadog/ci/span.rb index 6bf9dd3f..e0609b05 100644 --- a/lib/datadog/ci/span.rb +++ b/lib/datadog/ci/span.rb @@ -139,7 +139,7 @@ def set_default_tags # @param [Hash] metadata optional metadata # @return [void] def set_parameters(arguments, metadata = {}) - return if arguments.nil? || arguments.empty? + return if arguments.nil? set_tag( Ext::Test::TAG_PARAMETERS, diff --git a/spec/datadog/ci/contrib/rspec/instrumentation_spec.rb b/spec/datadog/ci/contrib/rspec/instrumentation_spec.rb index ddf8f61a..4ea570d6 100644 --- a/spec/datadog/ci/contrib/rspec/instrumentation_spec.rb +++ b/spec/datadog/ci/contrib/rspec/instrumentation_spec.rb @@ -375,8 +375,12 @@ def rspec_session_run(with_failed_test: false, with_shared_test: false) shared_test_spans = test_spans.filter { |test_span| test_span.name == "SomeTest shared examples adds 1 and 1" } expect(shared_test_spans).to have(2).items - shared_test_spans.each do |shared_test_span| + shared_test_spans.each_with_index do |shared_test_span, index| expect(shared_test_span.get_tag(Datadog::CI::Ext::Test::TAG_SUITE)).to eq(spec.file_path) + + expect(shared_test_span.get_tag(Datadog::CI::Ext::Test::TAG_PARAMETERS)).to eq( + "{\"arguments\":{},\"metadata\":{\"scoped_id\":\"1:#{2 + index}:1\"}}" + ) end test_spans.each do |test_span|