From 70859780a97cd20f3894528cbc3df676c8bcaae8 Mon Sep 17 00:00:00 2001 From: Andrey Marchenko Date: Tue, 30 Jan 2024 10:21:53 +0100 Subject: [PATCH 01/10] rename ConfigurationHelpers to GemsHelpers, simplify setup --- spec/datadog/ci/contrib/cucumber/instrumentation_spec.rb | 2 -- spec/datadog/ci/contrib/cucumber/integration_spec.rb | 2 +- spec/datadog/ci/contrib/minitest/integration_spec.rb | 2 +- spec/datadog/ci/contrib/rspec/integration_spec.rb | 2 +- spec/spec_helper.rb | 3 +-- spec/support/{configuration_helpers.rb => gems_helpers.rb} | 2 +- 6 files changed, 5 insertions(+), 8 deletions(-) rename spec/support/{configuration_helpers.rb => gems_helpers.rb} (98%) diff --git a/spec/datadog/ci/contrib/cucumber/instrumentation_spec.rb b/spec/datadog/ci/contrib/cucumber/instrumentation_spec.rb index c97dfe38..e1fb5468 100644 --- a/spec/datadog/ci/contrib/cucumber/instrumentation_spec.rb +++ b/spec/datadog/ci/contrib/cucumber/instrumentation_spec.rb @@ -3,8 +3,6 @@ require "cucumber" RSpec.describe "Cucumber formatter" do - extend ConfigurationHelpers - include_context "CI mode activated" do let(:integration_name) { :cucumber } let(:integration_options) { {service_name: "jalapenos"} } diff --git a/spec/datadog/ci/contrib/cucumber/integration_spec.rb b/spec/datadog/ci/contrib/cucumber/integration_spec.rb index be1a4e81..de70ee02 100644 --- a/spec/datadog/ci/contrib/cucumber/integration_spec.rb +++ b/spec/datadog/ci/contrib/cucumber/integration_spec.rb @@ -1,5 +1,5 @@ RSpec.describe Datadog::CI::Contrib::Cucumber::Integration do - extend ConfigurationHelpers + extend GemsHelpers let(:integration) { described_class.new } diff --git a/spec/datadog/ci/contrib/minitest/integration_spec.rb b/spec/datadog/ci/contrib/minitest/integration_spec.rb index 5de418f6..105f17da 100644 --- a/spec/datadog/ci/contrib/minitest/integration_spec.rb +++ b/spec/datadog/ci/contrib/minitest/integration_spec.rb @@ -1,5 +1,5 @@ RSpec.describe Datadog::CI::Contrib::Minitest::Integration do - extend ConfigurationHelpers + extend GemsHelpers let(:integration) { described_class.new } diff --git a/spec/datadog/ci/contrib/rspec/integration_spec.rb b/spec/datadog/ci/contrib/rspec/integration_spec.rb index 3588365e..b387a13a 100644 --- a/spec/datadog/ci/contrib/rspec/integration_spec.rb +++ b/spec/datadog/ci/contrib/rspec/integration_spec.rb @@ -1,5 +1,5 @@ RSpec.describe Datadog::CI::Contrib::RSpec::Integration do - extend ConfigurationHelpers + extend GemsHelpers let(:integration) { described_class.new } diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index fa396071..af11e759 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -8,7 +8,7 @@ require_relative "../lib/datadog/ci" -require_relative "support/configuration_helpers" +require_relative "support/gems_helpers" require_relative "support/log_helpers" require_relative "support/tracer_helpers" require_relative "support/span_helpers" @@ -58,7 +58,6 @@ def self.load_plugins end RSpec.configure do |config| - config.include ConfigurationHelpers config.include TracerHelpers config.include SpanHelpers diff --git a/spec/support/configuration_helpers.rb b/spec/support/gems_helpers.rb similarity index 98% rename from spec/support/configuration_helpers.rb rename to spec/support/gems_helpers.rb index 5d850bbf..e850b3fa 100644 --- a/spec/support/configuration_helpers.rb +++ b/spec/support/gems_helpers.rb @@ -1,6 +1,6 @@ require "bundler" -module ConfigurationHelpers +module GemsHelpers shared_context "loaded gems" do |gems = {}| before do allow(Gem.loaded_specs).to receive(:[]).and_call_original From d9aa7203bd6b42e50e083efc795d4f35ab3fefdc Mon Sep 17 00:00:00 2001 From: Andrey Marchenko Date: Tue, 30 Jan 2024 10:26:39 +0100 Subject: [PATCH 02/10] move ci mode and concurrency test to contexts folder --- spec/spec_helper.rb | 7 +++++-- spec/support/{ci_mode_helpers.rb => contexts/ci_mode.rb} | 8 ++++++++ .../concurrency_test.rb} | 5 +++++ 3 files changed, 18 insertions(+), 2 deletions(-) rename spec/support/{ci_mode_helpers.rb => contexts/ci_mode.rb} (75%) rename spec/support/{concurrency_helpers.rb => contexts/concurrency_test.rb} (51%) diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index af11e759..a33bce44 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -8,6 +8,7 @@ require_relative "../lib/datadog/ci" +# rspec helpers and matchers require_relative "support/gems_helpers" require_relative "support/log_helpers" require_relative "support/tracer_helpers" @@ -15,9 +16,11 @@ require_relative "support/platform_helpers" 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_relative "support/concurrency_helpers" + +# shared contexts +require_relative "support/contexts/ci_mode" +require_relative "support/contexts/concurrency_test" require "rspec/collection_matchers" require "climate_control" diff --git a/spec/support/ci_mode_helpers.rb b/spec/support/contexts/ci_mode.rb similarity index 75% rename from spec/support/ci_mode_helpers.rb rename to spec/support/contexts/ci_mode.rb index a1e76841..4f754d3f 100644 --- a/spec/support/ci_mode_helpers.rb +++ b/spec/support/contexts/ci_mode.rb @@ -1,3 +1,11 @@ +# CI mode shared context sets up the CI recorder and configures the CI mode for tracer like customers do. +# Example usage: +# +# include_context "CI mode activated" do +# let(:integration_name) { :cucumber } +# let(:integration_options) { {service_name: "jalapenos"} } +# end + RSpec.shared_context "CI mode activated" do let(:test_command) { "command" } let(:integration_name) { :no_instrument } diff --git a/spec/support/concurrency_helpers.rb b/spec/support/contexts/concurrency_test.rb similarity index 51% rename from spec/support/concurrency_helpers.rb rename to spec/support/contexts/concurrency_test.rb index 4fb80840..47b7c335 100644 --- a/spec/support/concurrency_helpers.rb +++ b/spec/support/contexts/concurrency_test.rb @@ -1,3 +1,8 @@ +# Concurreny test shared context sets up the concurrency test environment. +# +# It provides some convenience methods to run code concurrently in tests, such as: +# - repeat: run a block of code multiple times +# - run_concurrently: run a block of code concurrently in multiple threads RSpec.shared_context "Concurrency test" do let(:threads_count) { 10 } let(:repeat_count) { 20 } From 1dbdae4cd5c646a6b2e66132aed44cf406fb3b4a Mon Sep 17 00:00:00 2001 From: Andrey Marchenko Date: Tue, 30 Jan 2024 10:46:18 +0100 Subject: [PATCH 03/10] move git fixture to contexts --- spec/spec_helper.rb | 2 +- spec/support/{git_helpers.rb => contexts/git_fixture.rb} | 5 ++++- 2 files changed, 5 insertions(+), 2 deletions(-) rename spec/support/{git_helpers.rb => contexts/git_fixture.rb} (64%) diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index a33bce44..024a6072 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -14,13 +14,13 @@ require_relative "support/tracer_helpers" require_relative "support/span_helpers" require_relative "support/platform_helpers" -require_relative "support/git_helpers" require_relative "support/provider_test_helpers" require_relative "support/test_visibility_event_serialized" # shared contexts require_relative "support/contexts/ci_mode" require_relative "support/contexts/concurrency_test" +require_relative "support/contexts/git_fixture" require "rspec/collection_matchers" require "climate_control" diff --git a/spec/support/git_helpers.rb b/spec/support/contexts/git_fixture.rb similarity index 64% rename from spec/support/git_helpers.rb rename to spec/support/contexts/git_fixture.rb index 70579e9d..78c50bb8 100644 --- a/spec/support/git_helpers.rb +++ b/spec/support/contexts/git_fixture.rb @@ -1,4 +1,7 @@ -FIXTURE_DIR = "#{File.dirname(__FILE__)}/fixtures/" # rubocop:disable all +# Git fixture shared context sets up an example git repository in the ../fixture directory. +# Used to test git integration. + +FIXTURE_DIR = "#{File.dirname(__FILE__)}/../fixtures/" # rubocop:disable all shared_context "with git fixture" do |git_fixture| let(:environment_variables) do From d9ecd78d3f4f5df442155cb308f0c52cd295b3ee Mon Sep 17 00:00:00 2001 From: Andrey Marchenko Date: Tue, 30 Jan 2024 15:45:44 +0100 Subject: [PATCH 04/10] remove log_helpers as they are not used --- spec/spec_helper.rb | 2 +- spec/support/log_helpers.rb | 172 ------------------------------------ 2 files changed, 1 insertion(+), 173 deletions(-) delete mode 100644 spec/support/log_helpers.rb diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index 024a6072..1392a87f 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -10,7 +10,7 @@ # rspec helpers and matchers require_relative "support/gems_helpers" -require_relative "support/log_helpers" + require_relative "support/tracer_helpers" require_relative "support/span_helpers" require_relative "support/platform_helpers" diff --git a/spec/support/log_helpers.rb b/spec/support/log_helpers.rb deleted file mode 100644 index a00cf071..00000000 --- a/spec/support/log_helpers.rb +++ /dev/null @@ -1,172 +0,0 @@ -module LogHelpers - def without_warnings(&block) - LogHelpers.without_warnings(&block) - end - - def self.without_warnings - v = $VERBOSE - $VERBOSE = nil - begin - yield - ensure - $VERBOSE = v - end - end - - def without_errors - level = Datadog.logger.level - Datadog.configure { |c| c.logger.level = Logger::FATAL } - - begin - yield - ensure - Datadog.configure { |c| c.logger.level = level } - end - end - - RSpec::Matchers.define :have_lazy_debug_logged do |expected| - match do |actual| - expect(actual).to have_received(:debug) do |*_args, &block| - result = case expected - when String - begin - expect(block.call).to include(expected) - rescue RSpec::Expectations::ExpectationNotMetError - false - end - when Regexp - begin - expect(block.call).to match(expected) - rescue RSpec::Expectations::ExpectationNotMetError - false - end - else - raise "Don't know how to match '#{expected}'." - end - - return true if result - end - - false - end - end - - shared_context "tracer logging" do - let(:log_buffer) { StringIO.new } - - before do - @default_logger = Datadog.logger - Datadog.configure do |c| - c.logger.instance = Datadog::Core::Logger.new(log_buffer) - c.logger.level = ::Logger::WARN - end - end - - after do - Datadog.configure do |c| - c.logger.instance = @default_logger - c.diagnostics.debug = false - end - end - - # Checks buffer to see if it contains lines that match all patterns. - # Limited to only checking for one kind of message. - RSpec::Matchers.define :contain_line_with do |*patterns| - attr_accessor \ - :comparison, - :repetitions - - match do |buffer| - repetitions ||= 1 - - # Creates a Hash that counts number of matches per pattern e.g. 'a' => 0, 'b' => 0 - pattern_matches = patterns.zip(Array.new(patterns.length) { 0 }).to_h - - # Test below iterates on lines, this is required for Ruby 1.9 backward compatibility. - # Scans each pattern against each line, increments count if it matches. - lines = buffer.string.lines - lines.each do |line| - pattern_matches.each_key do |pattern| - pattern_matches[pattern] += 1 if line.match(pattern) - end - end - - # If all patterns were matched for required number of repetitions: success. - patterns_match_expectations = pattern_matches.values.all? do |value| - case comparison - when :gte - value >= repetitions - when :lte - value <= repetitions - else - value == repetitions - end - end - - expect(patterns_match_expectations).to be true - end - - chain :at_least do |count| - @repetitions = count - @comparison = :gte - end - - chain :no_more_than do |count| - @repetitions = count - @comparison = :lte - end - - chain :exactly do |count| - @repetitions = count - end - - chain :times do - # Do nothing - end - - chain :once do - @repetitions = 1 - end - end - end - - # Matches Datadog.logger.warn messages - RSpec::Matchers.define :emit_deprecation_warning do |expected| - match do |actual| - captured_log_entries = [] - allow(Datadog.logger).to receive(:warn) do |arg, &block| - captured_log_entries << if block - block.call - else - arg - end - end - - actual.call - - @actual = captured_log_entries.join('\n') - - # Matches any output with the word deprecation (or equivalent variants) - # in case no expectation is specified. - expected ||= /deprecat(e|ion)/i - - values_match?(expected, @actual) - end - - def failure_message - "expected Datadog.logger.warn output #{description_of @actual} to #{description}".dup - end - - def failure_message_when_negated - "expected Datadog.logger.warn output #{description_of @actual} not to #{description}".dup - end - - diffable - - # Only allow matching with blocks - supports_block_expectations - def supports_value_expectations? - false - end - end -end From 1cb2f0130598c19cf340ec18dba5e91968813794 Mon Sep 17 00:00:00 2001 From: Andrey Marchenko Date: Tue, 30 Jan 2024 15:48:13 +0100 Subject: [PATCH 05/10] remove PlatformHelpers --- spec/spec_helper.rb | 1 - spec/support/platform_helpers.rb | 46 -------------------------------- 2 files changed, 47 deletions(-) delete mode 100644 spec/support/platform_helpers.rb diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index 1392a87f..2eab9e3e 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -13,7 +13,6 @@ require_relative "support/tracer_helpers" require_relative "support/span_helpers" -require_relative "support/platform_helpers" require_relative "support/provider_test_helpers" require_relative "support/test_visibility_event_serialized" diff --git a/spec/support/platform_helpers.rb b/spec/support/platform_helpers.rb deleted file mode 100644 index 84aca0d4..00000000 --- a/spec/support/platform_helpers.rb +++ /dev/null @@ -1,46 +0,0 @@ -require "os" - -module PlatformHelpers - module_function - - # Ruby runtime engines - - def mri? - "ruby".freeze == RUBY_ENGINE - end - - def jruby? - "jruby".freeze == RUBY_ENGINE - end - - def truffleruby? - "truffleruby".freeze == RUBY_ENGINE - end - - def engine_version - version = defined?(RUBY_ENGINE_VERSION) ? RUBY_ENGINE_VERSION : RUBY_VERSION - Gem::Version.new(version) - end - - # Operating systems - - def linux? - OS.linux? - end - - def mac? - OS.mac? - end - - # Environment - - def ci? - ENV.key?("CI") - end - - # Feature support - - def supports_fork? - Process.respond_to?(:fork) - end -end From 73cf1ad09b85cf6fe267de88232d5d5be32a092a Mon Sep 17 00:00:00 2001 From: Andrey Marchenko Date: Tue, 30 Jan 2024 15:54:02 +0100 Subject: [PATCH 06/10] rename provider_test_helpers to extract_environment_tags shared context --- spec/datadog/ci/ext/environment/providers/appveyor_spec.rb | 2 +- spec/datadog/ci/ext/environment/providers/azure_spec.rb | 2 +- spec/datadog/ci/ext/environment/providers/base_spec.rb | 2 +- spec/datadog/ci/ext/environment/providers/bitbucket_spec.rb | 2 +- spec/datadog/ci/ext/environment/providers/bitrise_spec.rb | 2 +- spec/datadog/ci/ext/environment/providers/buddy_spec.rb | 2 +- spec/datadog/ci/ext/environment/providers/buildkite_spec.rb | 2 +- spec/datadog/ci/ext/environment/providers/circleci_spec.rb | 2 +- spec/datadog/ci/ext/environment/providers/codefresh_spec.rb | 2 +- .../ci/ext/environment/providers/github_actions_spec.rb | 2 +- spec/datadog/ci/ext/environment/providers/gitlab_spec.rb | 2 +- spec/datadog/ci/ext/environment/providers/jenkins_spec.rb | 2 +- spec/datadog/ci/ext/environment/providers/local_git_spec.rb | 2 +- spec/datadog/ci/ext/environment/providers/teamcity_spec.rb | 2 +- spec/datadog/ci/ext/environment/providers/travis_spec.rb | 2 +- .../ci/ext/environment/providers/user_defined_tags_spec.rb | 2 +- spec/spec_helper.rb | 4 ++-- .../extract_environment_tags.rb} | 4 +++- 18 files changed, 21 insertions(+), 19 deletions(-) rename spec/support/{provider_test_helpers.rb => contexts/extract_environment_tags.rb} (56%) diff --git a/spec/datadog/ci/ext/environment/providers/appveyor_spec.rb b/spec/datadog/ci/ext/environment/providers/appveyor_spec.rb index 6c0fa412..f6a05e8c 100644 --- a/spec/datadog/ci/ext/environment/providers/appveyor_spec.rb +++ b/spec/datadog/ci/ext/environment/providers/appveyor_spec.rb @@ -1,6 +1,6 @@ RSpec.describe ::Datadog::CI::Ext::Environment::Providers::Appveyor do describe ".tags" do - include_context "extract tags from environment with given provider and use a subject" + include_context "extract environment tags" context "example fixture" do let(:env) do diff --git a/spec/datadog/ci/ext/environment/providers/azure_spec.rb b/spec/datadog/ci/ext/environment/providers/azure_spec.rb index 02e75162..4570d549 100644 --- a/spec/datadog/ci/ext/environment/providers/azure_spec.rb +++ b/spec/datadog/ci/ext/environment/providers/azure_spec.rb @@ -1,6 +1,6 @@ RSpec.describe ::Datadog::CI::Ext::Environment::Providers::Azure do describe ".tags" do - include_context "extract tags from environment with given provider and use a subject" + include_context "extract environment tags" context "example fixture" do let(:env) do diff --git a/spec/datadog/ci/ext/environment/providers/base_spec.rb b/spec/datadog/ci/ext/environment/providers/base_spec.rb index 3a1b5d6a..40ea1bb9 100644 --- a/spec/datadog/ci/ext/environment/providers/base_spec.rb +++ b/spec/datadog/ci/ext/environment/providers/base_spec.rb @@ -1,6 +1,6 @@ RSpec.describe ::Datadog::CI::Ext::Environment::Providers::Base do describe ".tags" do - include_context "extract tags from environment with given provider and use a subject" + include_context "extract environment tags" context "example fixture" do let(:env) do diff --git a/spec/datadog/ci/ext/environment/providers/bitbucket_spec.rb b/spec/datadog/ci/ext/environment/providers/bitbucket_spec.rb index 70e0e4f7..2f8e2bf8 100644 --- a/spec/datadog/ci/ext/environment/providers/bitbucket_spec.rb +++ b/spec/datadog/ci/ext/environment/providers/bitbucket_spec.rb @@ -1,6 +1,6 @@ RSpec.describe ::Datadog::CI::Ext::Environment::Providers::Bitbucket do describe ".tags" do - include_context "extract tags from environment with given provider and use a subject" + include_context "extract environment tags" context "example fixture" do let(:env) do diff --git a/spec/datadog/ci/ext/environment/providers/bitrise_spec.rb b/spec/datadog/ci/ext/environment/providers/bitrise_spec.rb index aed03e21..420d42d7 100644 --- a/spec/datadog/ci/ext/environment/providers/bitrise_spec.rb +++ b/spec/datadog/ci/ext/environment/providers/bitrise_spec.rb @@ -1,6 +1,6 @@ RSpec.describe ::Datadog::CI::Ext::Environment::Providers::Bitrise do describe ".tags" do - include_context "extract tags from environment with given provider and use a subject" + include_context "extract environment tags" context "example fixture" do let(:env) do diff --git a/spec/datadog/ci/ext/environment/providers/buddy_spec.rb b/spec/datadog/ci/ext/environment/providers/buddy_spec.rb index d731b318..4b90a8e8 100644 --- a/spec/datadog/ci/ext/environment/providers/buddy_spec.rb +++ b/spec/datadog/ci/ext/environment/providers/buddy_spec.rb @@ -1,6 +1,6 @@ RSpec.describe ::Datadog::CI::Ext::Environment::Providers::Buddy do describe ".tags" do - include_context "extract tags from environment with given provider and use a subject" + include_context "extract environment tags" context "example fixture" do let(:env) do diff --git a/spec/datadog/ci/ext/environment/providers/buildkite_spec.rb b/spec/datadog/ci/ext/environment/providers/buildkite_spec.rb index b1bd484b..138805de 100644 --- a/spec/datadog/ci/ext/environment/providers/buildkite_spec.rb +++ b/spec/datadog/ci/ext/environment/providers/buildkite_spec.rb @@ -1,6 +1,6 @@ RSpec.describe ::Datadog::CI::Ext::Environment::Providers::Buildkite do describe ".tags" do - include_context "extract tags from environment with given provider and use a subject" + include_context "extract environment tags" context "example fixture" do let(:env) do diff --git a/spec/datadog/ci/ext/environment/providers/circleci_spec.rb b/spec/datadog/ci/ext/environment/providers/circleci_spec.rb index 8de10e6d..8937daff 100644 --- a/spec/datadog/ci/ext/environment/providers/circleci_spec.rb +++ b/spec/datadog/ci/ext/environment/providers/circleci_spec.rb @@ -1,6 +1,6 @@ RSpec.describe ::Datadog::CI::Ext::Environment::Providers::Circleci do describe ".tags" do - include_context "extract tags from environment with given provider and use a subject" + include_context "extract environment tags" context "example fixture" do let(:env) do diff --git a/spec/datadog/ci/ext/environment/providers/codefresh_spec.rb b/spec/datadog/ci/ext/environment/providers/codefresh_spec.rb index 37a1e69a..8a9617c6 100644 --- a/spec/datadog/ci/ext/environment/providers/codefresh_spec.rb +++ b/spec/datadog/ci/ext/environment/providers/codefresh_spec.rb @@ -1,6 +1,6 @@ RSpec.describe ::Datadog::CI::Ext::Environment::Providers::Codefresh do describe ".tags" do - include_context "extract tags from environment with given provider and use a subject" + include_context "extract environment tags" context "example fixture" do let(:env) do diff --git a/spec/datadog/ci/ext/environment/providers/github_actions_spec.rb b/spec/datadog/ci/ext/environment/providers/github_actions_spec.rb index aa0ac8af..edd7d614 100644 --- a/spec/datadog/ci/ext/environment/providers/github_actions_spec.rb +++ b/spec/datadog/ci/ext/environment/providers/github_actions_spec.rb @@ -1,6 +1,6 @@ RSpec.describe ::Datadog::CI::Ext::Environment::Providers::GithubActions do describe ".tags" do - include_context "extract tags from environment with given provider and use a subject" + include_context "extract environment tags" context "example fixture" do let(:env) do diff --git a/spec/datadog/ci/ext/environment/providers/gitlab_spec.rb b/spec/datadog/ci/ext/environment/providers/gitlab_spec.rb index 7b1a22c9..2af2eb4b 100644 --- a/spec/datadog/ci/ext/environment/providers/gitlab_spec.rb +++ b/spec/datadog/ci/ext/environment/providers/gitlab_spec.rb @@ -1,6 +1,6 @@ RSpec.describe ::Datadog::CI::Ext::Environment::Providers::Gitlab do describe ".tags" do - include_context "extract tags from environment with given provider and use a subject" + include_context "extract environment tags" context "example fixture" do let(:env) do diff --git a/spec/datadog/ci/ext/environment/providers/jenkins_spec.rb b/spec/datadog/ci/ext/environment/providers/jenkins_spec.rb index 3604c267..debfa8b2 100644 --- a/spec/datadog/ci/ext/environment/providers/jenkins_spec.rb +++ b/spec/datadog/ci/ext/environment/providers/jenkins_spec.rb @@ -1,6 +1,6 @@ RSpec.describe ::Datadog::CI::Ext::Environment::Providers::Jenkins do describe ".tags" do - include_context "extract tags from environment with given provider and use a subject" + include_context "extract environment tags" context "example fixture" do let(:env) do diff --git a/spec/datadog/ci/ext/environment/providers/local_git_spec.rb b/spec/datadog/ci/ext/environment/providers/local_git_spec.rb index 891d11f2..c2d3a93b 100644 --- a/spec/datadog/ci/ext/environment/providers/local_git_spec.rb +++ b/spec/datadog/ci/ext/environment/providers/local_git_spec.rb @@ -3,7 +3,7 @@ let(:environment_variables) { {} } describe "#tags" do - include_context "extract tags from environment with given provider and use a subject" + include_context "extract environment tags" context "example git repository" do include_context "with git fixture", "gitdir_with_commit" diff --git a/spec/datadog/ci/ext/environment/providers/teamcity_spec.rb b/spec/datadog/ci/ext/environment/providers/teamcity_spec.rb index 20308ed7..bf355aa8 100644 --- a/spec/datadog/ci/ext/environment/providers/teamcity_spec.rb +++ b/spec/datadog/ci/ext/environment/providers/teamcity_spec.rb @@ -1,6 +1,6 @@ RSpec.describe ::Datadog::CI::Ext::Environment::Providers::Teamcity do describe ".tags" do - include_context "extract tags from environment with given provider and use a subject" + include_context "extract environment tags" context "example fixture" do let(:env) do diff --git a/spec/datadog/ci/ext/environment/providers/travis_spec.rb b/spec/datadog/ci/ext/environment/providers/travis_spec.rb index b71a13f8..115167ae 100644 --- a/spec/datadog/ci/ext/environment/providers/travis_spec.rb +++ b/spec/datadog/ci/ext/environment/providers/travis_spec.rb @@ -1,6 +1,6 @@ RSpec.describe ::Datadog::CI::Ext::Environment::Providers::Travis do describe ".tags" do - include_context "extract tags from environment with given provider and use a subject" + include_context "extract environment tags" context "example fixture" do let(:env) do diff --git a/spec/datadog/ci/ext/environment/providers/user_defined_tags_spec.rb b/spec/datadog/ci/ext/environment/providers/user_defined_tags_spec.rb index c7d25efe..709e5b35 100644 --- a/spec/datadog/ci/ext/environment/providers/user_defined_tags_spec.rb +++ b/spec/datadog/ci/ext/environment/providers/user_defined_tags_spec.rb @@ -1,6 +1,6 @@ RSpec.describe ::Datadog::CI::Ext::Environment::Providers::UserDefinedTags do describe ".tags" do - include_context "extract tags from environment with given provider and use a subject" + include_context "extract environment tags" context "example fixture" do let(:env) do diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index 2eab9e3e..0502a458 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -11,15 +11,15 @@ # rspec helpers and matchers require_relative "support/gems_helpers" +require_relative "support/test_visibility_event_serialized" require_relative "support/tracer_helpers" require_relative "support/span_helpers" -require_relative "support/provider_test_helpers" -require_relative "support/test_visibility_event_serialized" # shared contexts require_relative "support/contexts/ci_mode" require_relative "support/contexts/concurrency_test" require_relative "support/contexts/git_fixture" +require_relative "support/contexts/extract_environment_tags" require "rspec/collection_matchers" require "climate_control" diff --git a/spec/support/provider_test_helpers.rb b/spec/support/contexts/extract_environment_tags.rb similarity index 56% rename from spec/support/provider_test_helpers.rb rename to spec/support/contexts/extract_environment_tags.rb index b9e98d15..ad109962 100644 --- a/spec/support/provider_test_helpers.rb +++ b/spec/support/contexts/extract_environment_tags.rb @@ -1,4 +1,6 @@ -shared_context "extract tags from environment with given provider and use a subject" do |git_fixture| +# Extract environment tags using described Provider. +# let(:environment_variables) can be used to set environment variables for the test. +shared_context "extract environment tags" do |git_fixture| subject(:extracted_tags) do ClimateControl.modify(environment_variables) do ::Datadog::CI::Ext::Environment::Extractor.new(env, provider_klass: described_class).tags From a9297035d1bca719058e274109d5f5b5633db0c3 Mon Sep 17 00:00:00 2001 From: Andrey Marchenko Date: Tue, 30 Jan 2024 16:52:46 +0100 Subject: [PATCH 07/10] move "citestcycle serializer" shared context to contexts folder --- spec/datadog/ci/test_visibility/serializers/span_spec.rb | 2 +- .../ci/test_visibility/serializers/test_module_spec.rb | 2 +- .../ci/test_visibility/serializers/test_session_spec.rb | 2 +- .../ci/test_visibility/serializers/test_suite_spec.rb | 2 +- spec/datadog/ci/test_visibility/serializers/test_v1_spec.rb | 2 +- spec/datadog/ci/test_visibility/serializers/test_v2_spec.rb | 2 +- spec/spec_helper.rb | 2 +- .../citestcycle_serializer.rb} | 5 ++++- 8 files changed, 11 insertions(+), 8 deletions(-) rename spec/support/{test_visibility_event_serialized.rb => contexts/citestcycle_serializer.rb} (75%) diff --git a/spec/datadog/ci/test_visibility/serializers/span_spec.rb b/spec/datadog/ci/test_visibility/serializers/span_spec.rb index 1cdad491..b33a1f66 100644 --- a/spec/datadog/ci/test_visibility/serializers/span_spec.rb +++ b/spec/datadog/ci/test_visibility/serializers/span_spec.rb @@ -6,7 +6,7 @@ let(:integration_name) { :rspec } end - include_context "Test visibility event serialized" do + include_context "citestcycle serializer" do subject { described_class.new(trace_for_span(first_other_span), first_other_span) } end diff --git a/spec/datadog/ci/test_visibility/serializers/test_module_spec.rb b/spec/datadog/ci/test_visibility/serializers/test_module_spec.rb index c3f61799..26ff5326 100644 --- a/spec/datadog/ci/test_visibility/serializers/test_module_spec.rb +++ b/spec/datadog/ci/test_visibility/serializers/test_module_spec.rb @@ -3,7 +3,7 @@ let(:integration_name) { :rspec } end - include_context "Test visibility event serialized" do + include_context "citestcycle serializer" do subject { described_class.new(trace_for_span(test_module_span), test_module_span) } end diff --git a/spec/datadog/ci/test_visibility/serializers/test_session_spec.rb b/spec/datadog/ci/test_visibility/serializers/test_session_spec.rb index 6eb38231..fd07996c 100644 --- a/spec/datadog/ci/test_visibility/serializers/test_session_spec.rb +++ b/spec/datadog/ci/test_visibility/serializers/test_session_spec.rb @@ -3,7 +3,7 @@ let(:integration_name) { :rspec } end - include_context "Test visibility event serialized" do + include_context "citestcycle serializer" do subject { described_class.new(trace_for_span(test_session_span), test_session_span) } end diff --git a/spec/datadog/ci/test_visibility/serializers/test_suite_spec.rb b/spec/datadog/ci/test_visibility/serializers/test_suite_spec.rb index 6a5c2e6a..1279f1d4 100644 --- a/spec/datadog/ci/test_visibility/serializers/test_suite_spec.rb +++ b/spec/datadog/ci/test_visibility/serializers/test_suite_spec.rb @@ -3,7 +3,7 @@ let(:integration_name) { :rspec } end - include_context "Test visibility event serialized" do + include_context "citestcycle serializer" do subject { described_class.new(trace_for_span(test_suite_span), test_suite_span) } end diff --git a/spec/datadog/ci/test_visibility/serializers/test_v1_spec.rb b/spec/datadog/ci/test_visibility/serializers/test_v1_spec.rb index 64b57c34..88baa26f 100644 --- a/spec/datadog/ci/test_visibility/serializers/test_v1_spec.rb +++ b/spec/datadog/ci/test_visibility/serializers/test_v1_spec.rb @@ -6,7 +6,7 @@ let(:integration_name) { :rspec } end - include_context "Test visibility event serialized" do + include_context "citestcycle serializer" do subject { described_class.new(trace_for_span(span), span) } end diff --git a/spec/datadog/ci/test_visibility/serializers/test_v2_spec.rb b/spec/datadog/ci/test_visibility/serializers/test_v2_spec.rb index 51f438d1..5b03fa89 100644 --- a/spec/datadog/ci/test_visibility/serializers/test_v2_spec.rb +++ b/spec/datadog/ci/test_visibility/serializers/test_v2_spec.rb @@ -6,7 +6,7 @@ let(:integration_name) { :rspec } end - include_context "Test visibility event serialized" do + include_context "citestcycle serializer" do subject { described_class.new(trace_for_span(first_test_span), first_test_span) } end diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index 0502a458..66706ded 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -11,7 +11,6 @@ # rspec helpers and matchers require_relative "support/gems_helpers" -require_relative "support/test_visibility_event_serialized" require_relative "support/tracer_helpers" require_relative "support/span_helpers" @@ -20,6 +19,7 @@ require_relative "support/contexts/concurrency_test" require_relative "support/contexts/git_fixture" require_relative "support/contexts/extract_environment_tags" +require_relative "support/contexts/citestcycle_serializer" require "rspec/collection_matchers" require "climate_control" diff --git a/spec/support/test_visibility_event_serialized.rb b/spec/support/contexts/citestcycle_serializer.rb similarity index 75% rename from spec/support/test_visibility_event_serialized.rb rename to spec/support/contexts/citestcycle_serializer.rb index e4a76618..33889029 100644 --- a/spec/support/test_visibility_event_serialized.rb +++ b/spec/support/contexts/citestcycle_serializer.rb @@ -1,4 +1,7 @@ -RSpec.shared_context "Test visibility event serialized" do +# "citestcycle serializer" shared context uses serializer defined in `subject` +# to serialize the data and then unpacks it to JSON. + +RSpec.shared_context "citestcycle serializer" do subject {} let(:msgpack_jsons) do From 2d4d6f497f255fd804e0ea0993dc0086350b96b5 Mon Sep 17 00:00:00 2001 From: Andrey Marchenko Date: Tue, 30 Jan 2024 17:45:05 +0100 Subject: [PATCH 08/10] rename and reorganize some tracer_helpers; better specs for boolean methods --- .../contrib/cucumber/instrumentation_spec.rb | 34 ++++++----- .../contrib/minitest/instrumentation_spec.rb | 22 +++---- .../ci/contrib/rspec/instrumentation_spec.rb | 18 +++--- spec/datadog/ci/span_spec.rb | 32 +++------- .../factories/test_suite_level_spec.rb | 4 +- .../test_visibility/serializers/span_spec.rb | 6 +- .../serializers/test_module_spec.rb | 32 +++++----- .../serializers/test_session_spec.rb | 8 +-- .../serializers/test_suite_spec.rb | 34 ++++------- .../serializers/test_v2_spec.rb | 26 +++----- spec/support/tracer_helpers.rb | 60 +++++++++++++------ 11 files changed, 126 insertions(+), 150 deletions(-) diff --git a/spec/datadog/ci/contrib/cucumber/instrumentation_spec.rb b/spec/datadog/ci/contrib/cucumber/instrumentation_spec.rb index e1fb5468..e8219c3b 100644 --- a/spec/datadog/ci/contrib/cucumber/instrumentation_spec.rb +++ b/spec/datadog/ci/contrib/cucumber/instrumentation_spec.rb @@ -173,27 +173,27 @@ end it "creates test suite span" do - expect(test_suite_span).not_to be_nil - expect(test_suite_span.name).to eq("Datadog integration at spec/datadog/ci/contrib/cucumber/features/passing.feature") - expect(test_suite_span.service).to eq("jalapenos") - expect(test_suite_span.get_tag(Datadog::CI::Ext::Test::TAG_SPAN_KIND)).to eq( + expect(first_test_suite_span).not_to be_nil + expect(first_test_suite_span.name).to eq("Datadog integration at spec/datadog/ci/contrib/cucumber/features/passing.feature") + expect(first_test_suite_span.service).to eq("jalapenos") + expect(first_test_suite_span.get_tag(Datadog::CI::Ext::Test::TAG_SPAN_KIND)).to eq( Datadog::CI::Ext::Test::SPAN_KIND_TEST ) - expect(test_suite_span.get_tag(Datadog::CI::Ext::Test::TAG_FRAMEWORK)).to eq( + expect(first_test_suite_span.get_tag(Datadog::CI::Ext::Test::TAG_FRAMEWORK)).to eq( Datadog::CI::Contrib::Cucumber::Ext::FRAMEWORK ) - expect(test_suite_span.get_tag(Datadog::CI::Ext::Test::TAG_FRAMEWORK_VERSION)).to eq( + expect(first_test_suite_span.get_tag(Datadog::CI::Ext::Test::TAG_FRAMEWORK_VERSION)).to eq( Datadog::CI::Contrib::Cucumber::Integration.version.to_s ) - expect(test_suite_span.get_tag(Datadog::CI::Ext::Test::TAG_STATUS)).to eq(Datadog::CI::Ext::Test::Status::PASS) + expect(first_test_suite_span.get_tag(Datadog::CI::Ext::Test::TAG_STATUS)).to eq(Datadog::CI::Ext::Test::Status::PASS) end it "connects scenario span to test session and test module" do expect(first_test_span.get_tag(Datadog::CI::Ext::Test::TAG_TEST_MODULE_ID)).to eq(test_module_span.id.to_s) expect(first_test_span.get_tag(Datadog::CI::Ext::Test::TAG_MODULE)).to eq(test_command) expect(first_test_span.get_tag(Datadog::CI::Ext::Test::TAG_TEST_SESSION_ID)).to eq(test_session_span.id.to_s) - expect(first_test_span.get_tag(Datadog::CI::Ext::Test::TAG_TEST_SUITE_ID)).to eq(test_suite_span.id.to_s) - expect(first_test_span.get_tag(Datadog::CI::Ext::Test::TAG_SUITE)).to eq(test_suite_span.name) + expect(first_test_span.get_tag(Datadog::CI::Ext::Test::TAG_TEST_SUITE_ID)).to eq(first_test_suite_span.id.to_s) + expect(first_test_span.get_tag(Datadog::CI::Ext::Test::TAG_SUITE)).to eq(first_test_suite_span.name) end end @@ -213,10 +213,10 @@ Datadog::CI::Ext::Test::Status::FAIL ) - expect(test_suite_span.name).to eq( + expect(first_test_suite_span.name).to eq( "Datadog integration - test failing features at spec/datadog/ci/contrib/cucumber/features/failing.feature" ) - expect(test_suite_span.get_tag(Datadog::CI::Ext::Test::TAG_STATUS)).to eq( + expect(first_test_suite_span.get_tag(Datadog::CI::Ext::Test::TAG_STATUS)).to eq( Datadog::CI::Ext::Test::Status::FAIL ) @@ -252,7 +252,7 @@ expect(span.get_tag(Datadog::CI::Ext::Test::TAG_SUITE)).to eq( "Datadog integration for parametrized tests at spec/datadog/ci/contrib/cucumber/features/with_parameters.feature" ) - expect(span.get_tag(Datadog::CI::Ext::Test::TAG_TEST_SUITE_ID)).to eq(test_suite_span.id.to_s) + expect(span.get_tag(Datadog::CI::Ext::Test::TAG_TEST_SUITE_ID)).to eq(first_test_suite_span.id.to_s) expect(span.get_tag(Datadog::CI::Ext::Test::TAG_STATUS)).to eq( Datadog::CI::Ext::Test::Status::PASS ) @@ -316,8 +316,8 @@ end it "marks test suite as failed" do - expect(test_suite_span).not_to be_nil - expect(test_suite_span.get_tag(Datadog::CI::Ext::Test::TAG_STATUS)).to eq(Datadog::CI::Ext::Test::Status::FAIL) + expect(first_test_suite_span).not_to be_nil + expect(first_test_suite_span.get_tag(Datadog::CI::Ext::Test::TAG_STATUS)).to eq(Datadog::CI::Ext::Test::Status::FAIL) end it "marks undefined cucumber scenario as failed" do @@ -362,8 +362,10 @@ end it "marks test suite as skipped" do - expect(test_suite_span).not_to be_nil - expect(test_suite_span.get_tag(Datadog::CI::Ext::Test::TAG_STATUS)).to eq(Datadog::CI::Ext::Test::Status::SKIP) + expect(first_test_suite_span).not_to be_nil + expect(first_test_suite_span.get_tag(Datadog::CI::Ext::Test::TAG_STATUS)).to eq( + Datadog::CI::Ext::Test::Status::SKIP + ) end end end diff --git a/spec/datadog/ci/contrib/minitest/instrumentation_spec.rb b/spec/datadog/ci/contrib/minitest/instrumentation_spec.rb index 69c14b70..e4063206 100644 --- a/spec/datadog/ci/contrib/minitest/instrumentation_spec.rb +++ b/spec/datadog/ci/contrib/minitest/instrumentation_spec.rb @@ -443,21 +443,21 @@ def test_pass_other end it "creates a test suite span" do - expect(test_suite_span).not_to be_nil + expect(first_test_suite_span).not_to be_nil - expect(test_suite_span.type).to eq(Datadog::CI::Ext::AppTypes::TYPE_TEST_SUITE) - expect(test_suite_span.name).to eq("SomeTest at spec/datadog/ci/contrib/minitest/instrumentation_spec.rb") + expect(first_test_suite_span.type).to eq(Datadog::CI::Ext::AppTypes::TYPE_TEST_SUITE) + expect(first_test_suite_span.name).to eq("SomeTest at spec/datadog/ci/contrib/minitest/instrumentation_spec.rb") - expect(test_suite_span.get_tag(Datadog::CI::Ext::Test::TAG_SPAN_KIND)).to eq( + expect(first_test_suite_span.get_tag(Datadog::CI::Ext::Test::TAG_SPAN_KIND)).to eq( Datadog::CI::Ext::Test::SPAN_KIND_TEST ) - expect(test_suite_span.get_tag(Datadog::CI::Ext::Test::TAG_FRAMEWORK)).to eq( + expect(first_test_suite_span.get_tag(Datadog::CI::Ext::Test::TAG_FRAMEWORK)).to eq( Datadog::CI::Contrib::Minitest::Ext::FRAMEWORK ) - expect(test_suite_span.get_tag(Datadog::CI::Ext::Test::TAG_FRAMEWORK_VERSION)).to eq( + expect(first_test_suite_span.get_tag(Datadog::CI::Ext::Test::TAG_FRAMEWORK_VERSION)).to eq( Datadog::CI::Contrib::Minitest::Integration.version.to_s ) - expect(test_suite_span.get_tag(Datadog::CI::Ext::Test::TAG_STATUS)).to eq( + expect(first_test_suite_span.get_tag(Datadog::CI::Ext::Test::TAG_STATUS)).to eq( Datadog::CI::Ext::Test::Status::PASS ) end @@ -489,7 +489,7 @@ def test_pass_other expect(test_module_ids.first).to eq(test_module_span.id.to_s) expect(test_suite_ids.count).to eq(1) - expect(test_suite_ids.first).to eq(test_suite_span.id.to_s) + expect(test_suite_ids.first).to eq(first_test_suite_span.id.to_s) end end @@ -516,7 +516,7 @@ def test_fail expect(test_module_span.get_tag(Datadog::CI::Ext::Test::TAG_STATUS)).to eq( Datadog::CI::Ext::Test::Status::FAIL ) - expect(test_suite_span.get_tag(Datadog::CI::Ext::Test::TAG_STATUS)).to eq( + expect(first_test_suite_span.get_tag(Datadog::CI::Ext::Test::TAG_STATUS)).to eq( Datadog::CI::Ext::Test::Status::FAIL ) end @@ -704,8 +704,8 @@ def test_2 end it "marks test suite as skipped" do - expect(test_suite_span).not_to be_nil - expect(test_suite_span.get_tag(Datadog::CI::Ext::Test::TAG_STATUS)).to eq(Datadog::CI::Ext::Test::Status::SKIP) + expect(first_test_suite_span).not_to be_nil + expect(first_test_suite_span.get_tag(Datadog::CI::Ext::Test::TAG_STATUS)).to eq(Datadog::CI::Ext::Test::Status::SKIP) end end end diff --git a/spec/datadog/ci/contrib/rspec/instrumentation_spec.rb b/spec/datadog/ci/contrib/rspec/instrumentation_spec.rb index 43748763..5073b7e6 100644 --- a/spec/datadog/ci/contrib/rspec/instrumentation_spec.rb +++ b/spec/datadog/ci/contrib/rspec/instrumentation_spec.rb @@ -127,9 +127,9 @@ def with_new_rspec_environment end expect(test_spans).to have(1).items - expect(tracer_spans).to have(1).items + expect(custom_spans).to have(1).items - tracer_spans.each do |span| + custom_spans.each do |span| expect(span.get_tag(Datadog::Tracing::Metadata::Ext::Distributed::TAG_ORIGIN)) .to eq(Datadog::CI::Ext::Test::CONTEXT_ORIGIN) end @@ -477,10 +477,10 @@ def rspec_session_run(with_failed_test: false, with_shared_test: false) it "creates test suite span" do spec = rspec_session_run - expect(test_suite_span).not_to be_nil + expect(first_test_suite_span).not_to be_nil - expect(test_suite_span.type).to eq(Datadog::CI::Ext::AppTypes::TYPE_TEST_SUITE) - expect(test_suite_span.name).to eq("SomeTest at #{spec.file_path}") + expect(first_test_suite_span.type).to eq(Datadog::CI::Ext::AppTypes::TYPE_TEST_SUITE) + expect(first_test_suite_span.name).to eq("SomeTest at #{spec.file_path}") expect(test_module_span.get_tag(Datadog::CI::Ext::Test::TAG_SPAN_KIND)).to eq( Datadog::CI::Ext::Test::SPAN_KIND_TEST @@ -501,7 +501,7 @@ def rspec_session_run(with_failed_test: false, with_shared_test: false) expect(first_test_span.get_tag(Datadog::CI::Ext::Test::TAG_TEST_SESSION_ID)).to eq(test_session_span.id.to_s) expect(first_test_span.get_tag(Datadog::CI::Ext::Test::TAG_TEST_MODULE_ID)).to eq(test_module_span.id.to_s) - expect(first_test_span.get_tag(Datadog::CI::Ext::Test::TAG_TEST_SUITE_ID)).to eq(test_suite_span.id.to_s) + expect(first_test_span.get_tag(Datadog::CI::Ext::Test::TAG_TEST_SUITE_ID)).to eq(first_test_suite_span.id.to_s) end context "with failures" do @@ -526,8 +526,8 @@ def rspec_session_run(with_failed_test: false, with_shared_test: false) it "creates test suite span with failed state" do rspec_session_run(with_failed_test: true) - expect(test_suite_span).not_to be_nil - expect(test_suite_span.get_tag(Datadog::CI::Ext::Test::TAG_STATUS)).to eq( + expect(first_test_suite_span).not_to be_nil + expect(first_test_suite_span.get_tag(Datadog::CI::Ext::Test::TAG_STATUS)).to eq( Datadog::CI::Ext::Test::Status::FAIL ) end @@ -549,7 +549,7 @@ def rspec_session_run(with_failed_test: false, with_shared_test: false) 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) + expect(test_span.get_tag(Datadog::CI::Ext::Test::TAG_TEST_SUITE_ID)).to eq(first_test_suite_span.id.to_s) end end end diff --git a/spec/datadog/ci/span_spec.rb b/spec/datadog/ci/span_spec.rb index 362ced9f..897b779a 100644 --- a/spec/datadog/ci/span_spec.rb +++ b/spec/datadog/ci/span_spec.rb @@ -14,9 +14,7 @@ allow(tracer_span).to receive(:get_tag).with("test.status").and_return("pass") end - it "returns true" do - expect(span.passed?).to eq(true) - end + it { is_expected.to be_passed } end context "when status is not PASS" do @@ -24,9 +22,7 @@ allow(tracer_span).to receive(:get_tag).with("test.status").and_return("fail") end - it "returns false" do - expect(span.passed?).to eq(false) - end + it { is_expected.not_to be_passed } end end @@ -36,9 +32,7 @@ allow(tracer_span).to receive(:get_tag).with("test.status").and_return("fail") end - it "returns true" do - expect(span.failed?).to eq(true) - end + it { is_expected.to be_failed } end context "when status is not FAIL" do @@ -46,9 +40,7 @@ allow(tracer_span).to receive(:get_tag).with("test.status").and_return("pass") end - it "returns false" do - expect(span.failed?).to eq(false) - end + it { is_expected.not_to be_failed } end end @@ -58,9 +50,7 @@ allow(tracer_span).to receive(:get_tag).with("test.status").and_return("skip") end - it "returns true" do - expect(span.skipped?).to eq(true) - end + it { is_expected.to be_skipped } end context "when status is not SKIP" do @@ -68,9 +58,7 @@ allow(tracer_span).to receive(:get_tag).with("test.status").and_return("pass") end - it "returns false" do - expect(span.skipped?).to eq(false) - end + it { is_expected.not_to be_skipped } end end @@ -80,9 +68,7 @@ allow(tracer_span).to receive(:get_tag).with("test.status").and_return(nil) end - it "returns true" do - expect(span.undefined?).to eq(true) - end + it { is_expected.to be_undefined } end context "when status is not nil" do @@ -90,9 +76,7 @@ allow(tracer_span).to receive(:get_tag).with("test.status").and_return("pass") end - it "returns false" do - expect(span.undefined?).to eq(false) - end + it { is_expected.not_to be_undefined } end end diff --git a/spec/datadog/ci/test_visibility/serializers/factories/test_suite_level_spec.rb b/spec/datadog/ci/test_visibility/serializers/factories/test_suite_level_spec.rb index aa4bc63a..c2ab81e9 100644 --- a/spec/datadog/ci/test_visibility/serializers/factories/test_suite_level_spec.rb +++ b/spec/datadog/ci/test_visibility/serializers/factories/test_suite_level_spec.rb @@ -26,7 +26,7 @@ end context "with a suite span" do - let(:ci_span) { test_suite_span } + let(:ci_span) { first_test_suite_span } it { is_expected.to be_kind_of(Datadog::CI::TestVisibility::Serializers::TestSuite) } end @@ -36,7 +36,7 @@ end context "with a http request span" do - let(:ci_span) { first_other_span } + let(:ci_span) { first_custom_span } it { is_expected.to be_kind_of(Datadog::CI::TestVisibility::Serializers::Span) } end end diff --git a/spec/datadog/ci/test_visibility/serializers/span_spec.rb b/spec/datadog/ci/test_visibility/serializers/span_spec.rb index b33a1f66..2e87290f 100644 --- a/spec/datadog/ci/test_visibility/serializers/span_spec.rb +++ b/spec/datadog/ci/test_visibility/serializers/span_spec.rb @@ -7,7 +7,7 @@ end include_context "citestcycle serializer" do - subject { described_class.new(trace_for_span(first_other_span), first_other_span) } + subject { described_class.new(trace_for_span(first_custom_span), first_custom_span) } end describe "#to_msgpack" do @@ -22,7 +22,7 @@ expect(content).to include( { "trace_id" => first_test_span.trace_id, - "span_id" => first_other_span.id, + "span_id" => first_custom_span.id, "parent_id" => first_test_span.id, "name" => "http-call", "service" => "net-http", @@ -50,7 +50,7 @@ before do produce_test_trace(with_http_span: true) - first_other_span.name = nil + first_custom_span.name = nil end it { is_expected.not_to be_valid } diff --git a/spec/datadog/ci/test_visibility/serializers/test_module_spec.rb b/spec/datadog/ci/test_visibility/serializers/test_module_spec.rb index 26ff5326..b611d9e5 100644 --- a/spec/datadog/ci/test_visibility/serializers/test_module_spec.rb +++ b/spec/datadog/ci/test_visibility/serializers/test_module_spec.rb @@ -58,47 +58,43 @@ end describe "#valid?" do - context "test_session_id" do - before do - produce_test_session_trace - end + before do + produce_test_session_trace + end + context "test_session_id" do context "when test_session_id is not nil" do - it "returns true" do - expect(subject.valid?).to eq(true) - end + it { is_expected.to be_valid } end context "when test_session_id is nil" do before do test_module_span.clear_tag("_test.session_id") + subject.valid? end - it "returns false" do - expect(subject.valid?).to eq(false) + it { is_expected.not_to be_valid } + + it "returns a correct validation error" do expect(subject.validation_errors["test_session_id"]).to include("is required") end end end context "test_module_id" do - before do - produce_test_session_trace - end - context "when test_module_id is not nil" do - it "returns true" do - expect(subject.valid?).to eq(true) - end + it { is_expected.to be_valid } end context "when test_module_id is nil" do before do test_module_span.clear_tag("_test.module_id") + subject.valid? end - it "returns false" do - expect(subject.valid?).to eq(false) + it { is_expected.not_to be_valid } + + it "returns a correct validation error" do expect(subject.validation_errors["test_module_id"]).to include("is required") end end diff --git a/spec/datadog/ci/test_visibility/serializers/test_session_spec.rb b/spec/datadog/ci/test_visibility/serializers/test_session_spec.rb index fd07996c..f25ce073 100644 --- a/spec/datadog/ci/test_visibility/serializers/test_session_spec.rb +++ b/spec/datadog/ci/test_visibility/serializers/test_session_spec.rb @@ -61,9 +61,7 @@ end context "when test_session_id is not nil" do - it "returns true" do - expect(subject.valid?).to eq(true) - end + it { is_expected.to be_valid } end context "when test_session_id is nil" do @@ -71,9 +69,7 @@ test_session_span.clear_tag("_test.session_id") end - it "returns false" do - expect(subject.valid?).to eq(false) - end + it { is_expected.not_to be_valid } end end end diff --git a/spec/datadog/ci/test_visibility/serializers/test_suite_spec.rb b/spec/datadog/ci/test_visibility/serializers/test_suite_spec.rb index 1279f1d4..56c61334 100644 --- a/spec/datadog/ci/test_visibility/serializers/test_suite_spec.rb +++ b/spec/datadog/ci/test_visibility/serializers/test_suite_spec.rb @@ -4,7 +4,7 @@ end include_context "citestcycle serializer" do - subject { described_class.new(trace_for_span(test_suite_span), test_suite_span) } + subject { described_class.new(trace_for_span(first_test_suite_span), first_test_suite_span) } end describe "#to_msgpack" do @@ -20,7 +20,7 @@ { "test_session_id" => test_session_span.id, "test_module_id" => test_module_span.id, - "test_suite_id" => test_suite_span.id, + "test_suite_id" => first_test_suite_span.id, "name" => "rspec.test_suite", "error" => 0, "service" => "rspec-test-suite", @@ -68,19 +68,15 @@ end context "when test_session_id is not nil" do - it "returns true" do - expect(subject.valid?).to eq(true) - end + it { is_expected.to be_valid } end context "when test_session_id is nil" do before do - test_suite_span.clear_tag("_test.session_id") + first_test_suite_span.clear_tag("_test.session_id") end - it "returns false" do - expect(subject.valid?).to eq(false) - end + it { is_expected.not_to be_valid } end end @@ -90,19 +86,15 @@ end context "when test_module_id is not nil" do - it "returns true" do - expect(subject.valid?).to eq(true) - end + it { is_expected.to be_valid } end context "when test_module_id is nil" do before do - test_suite_span.clear_tag("_test.module_id") + first_test_suite_span.clear_tag("_test.module_id") end - it "returns false" do - expect(subject.valid?).to eq(false) - end + it { is_expected.not_to be_valid } end end @@ -112,19 +104,15 @@ end context "when test_suite_id is not nil" do - it "returns true" do - expect(subject.valid?).to eq(true) - end + it { is_expected.to be_valid } end context "when test_suite_id is nil" do before do - test_suite_span.clear_tag("_test.suite_id") + first_test_suite_span.clear_tag("_test.suite_id") end - it "returns false" do - expect(subject.valid?).to eq(false) - end + it { is_expected.not_to be_valid } end end end diff --git a/spec/datadog/ci/test_visibility/serializers/test_v2_spec.rb b/spec/datadog/ci/test_visibility/serializers/test_v2_spec.rb index 5b03fa89..217b3f54 100644 --- a/spec/datadog/ci/test_visibility/serializers/test_v2_spec.rb +++ b/spec/datadog/ci/test_visibility/serializers/test_v2_spec.rb @@ -29,7 +29,7 @@ "resource" => "calculator_tests.test_add.run.0", "test_session_id" => test_session_span.id, "test_module_id" => test_module_span.id, - "test_suite_id" => test_suite_span.id + "test_suite_id" => first_test_suite_span.id } ) @@ -119,9 +119,7 @@ end context "when test_session_id is not nil" do - it "returns true" do - expect(subject.valid?).to eq(true) - end + it { is_expected.to be_valid } end context "when test_session_id is nil" do @@ -129,9 +127,7 @@ first_test_span.clear_tag("_test.session_id") end - it "returns false" do - expect(subject.valid?).to eq(false) - end + it { is_expected.not_to be_valid } end end @@ -141,9 +137,7 @@ end context "when test_module_id is not nil" do - it "returns true" do - expect(subject.valid?).to eq(true) - end + it { is_expected.to be_valid } end context "when test_module_id is nil" do @@ -151,9 +145,7 @@ first_test_span.clear_tag("_test.module_id") end - it "returns false" do - expect(subject.valid?).to eq(false) - end + it { is_expected.not_to be_valid } end end @@ -163,9 +155,7 @@ end context "when test_suite_id is not nil" do - it "returns true" do - expect(subject.valid?).to eq(true) - end + it { is_expected.to be_valid } end context "when test_suite_id is nil" do @@ -173,9 +163,7 @@ first_test_span.clear_tag("_test.suite_id") end - it "returns false" do - expect(subject.valid?).to eq(false) - end + it { is_expected.not_to be_valid } end end end diff --git a/spec/support/tracer_helpers.rb b/spec/support/tracer_helpers.rb index b208fd61..baa7762c 100644 --- a/spec/support/tracer_helpers.rb +++ b/spec/support/tracer_helpers.rb @@ -3,11 +3,8 @@ # For contrib, we only allow one tracer to be active: # the global tracer in +Datadog::Tracing+. module TracerHelpers - # Returns the current tracer instance - def tracer - Datadog::Tracing.send(:tracer) - end - + # traces a single test with given parameters + # optionally adds a http span using `Datadog::Tracing.trace` def produce_test_trace( framework: "rspec", test_name: "test_add", test_suite: "calculator_tests", @@ -49,6 +46,7 @@ def produce_test_trace( Timecop.return end + # traces a test session with given parameters and number of tests def produce_test_session_trace( tests_count: 1, framework: "rspec", test_name: "test_add", test_suite: "calculator_tests", test_module_name: "arithmetic", @@ -100,36 +98,60 @@ def produce_test_session_trace( end end + # returns test session span and asserts that there must be no more than one def test_session_span - spans.find { |span| span.type == "test_session_end" } + @test_session_span ||= begin + test_session_spans = spans.filter { |span| span.type == "test_session_end" } + expect(test_session_spans.count).to be <= 1 + test_session_spans.first + end end + # returns test module span and asserts that there must be no more than one def test_module_span - spans.find { |span| span.type == "test_module_end" } + @test_module_span ||= begin + test_module_spans = spans.filter { |span| span.type == "test_module_end" } + expect(test_module_spans.count).to be <= 1 + test_module_spans.first + end end - def test_suite_span - test_suite_spans.first + # returns all test spans + def test_spans + @test_spans ||= spans.filter { |span| span.type == "test" } end - def first_test_span - test_spans.first + # returns all test suite spans + def test_suite_spans + @test_suite_spans ||= spans.filter { |span| span.type == "test_suite_end" } end - def first_other_span - tracer_spans.first + # returns all custom (i.e. not test/suite/module/session) spans + def custom_spans + @custom_spans ||= spans.filter do |span| + !Datadog::CI::Ext::AppTypes::CI_SPAN_TYPES.include?(span.type) + end end - def test_spans - spans.filter { |span| span.type == "test" } + def first_test_span + @first_span_span ||= test_spans.first end - def test_suite_spans - spans.filter { |span| span.type == "test_suite_end" } + def first_test_suite_span + @first_stest_suite_span ||= test_suite_spans.first + end + + def first_custom_span + @first_custom_span ||= custom_spans.first end - def tracer_spans - spans.filter { |span| !Datadog::CI::Ext::AppTypes::CI_SPAN_TYPES.include?(span.type) } + # ------------------------------------- + # methods inherited from dd-trace-rb + # ------------------------------------- + + # Returns the current tracer instance + def tracer + Datadog::Tracing.send(:tracer) end # Returns traces and caches it (similar to +let(:traces)+). From b7a4ddeba334c29d524e72a91c7626cbe6fd4327 Mon Sep 17 00:00:00 2001 From: Andrey Marchenko Date: Tue, 30 Jan 2024 17:52:06 +0100 Subject: [PATCH 09/10] remove unused span helpers --- spec/support/span_helpers.rb | 33 --------------------------------- 1 file changed, 33 deletions(-) diff --git a/spec/support/span_helpers.rb b/spec/support/span_helpers.rb index 27759191..74eed5ff 100644 --- a/spec/support/span_helpers.rb +++ b/spec/support/span_helpers.rb @@ -52,30 +52,6 @@ def description_of(actual) # rubocop:disable Lint/NestedMethodDefinition define_have_error_tag(:stack, Datadog::Tracing::Metadata::Ext::Errors::TAG_STACK) define_have_error_tag(:type, Datadog::Tracing::Metadata::Ext::Errors::TAG_TYPE) - # @param tags [Hash] key value pairs to tags/metrics to assert on - RSpec::Matchers.define :have_metadata do |tags| - match do |actual| - tags.all? do |key, value| - values_match? value, actual.get_tag(key) - end - end - - match_when_negated do |actual| - if tags.respond_to?(:any?) - tags.any? do |key, value| - !values_match? value, actual.get_tag(key) - end - else - # Allows for `expect(span).to_not have_metadata('my.tag')` syntax - values_match? nil, actual.get_tag(tags) - end - end - - def description_of(actual) - "Span with metadata #{actual.send(:meta).merge(actual.send(:metrics))}" - end - end - RSpec::Matchers.define :a_span_with do |expected| match do |actual| actual.instance_of?(Datadog::Tracing::Span) && @@ -84,13 +60,4 @@ def description_of(actual) end end end - - RSpec::Matchers.define :a_span_operation_with do |expected| - match do |actual| - actual.instance_of?(Datadog::Tracing::SpanOperation) && - expected.all? do |key, value| - actual.__send__(key) == value - end - end - end end From a18860c0d49831a58d270c35fcef6fae7e9f39cd Mon Sep 17 00:00:00 2001 From: Andrey Marchenko Date: Wed, 31 Jan 2024 13:54:04 +0100 Subject: [PATCH 10/10] add more rspec matchers to span_helpers and greatly simplify tracing specs --- .../ci_queue_minitest/instrumentation_spec.rb | 23 +- .../ci_queue_rspec/instrumentation_spec.rb | 22 +- .../contrib/cucumber/instrumentation_spec.rb | 206 +++++++---------- .../contrib/minitest/instrumentation_spec.rb | 203 +++++++---------- .../ci/contrib/rspec/instrumentation_spec.rb | 207 +++++++++--------- spec/datadog/ci/test_visibility/flush_spec.rb | 5 +- .../ci/test_visibility/recorder_spec.rb | 104 ++++----- spec/spec_helper.rb | 1 - spec/support/span_helpers.rb | 116 +++++++++- 9 files changed, 430 insertions(+), 457 deletions(-) diff --git a/spec/datadog/ci/contrib/ci_queue_minitest/instrumentation_spec.rb b/spec/datadog/ci/contrib/ci_queue_minitest/instrumentation_spec.rb index 110a1b71..013aa2ca 100644 --- a/spec/datadog/ci/contrib/ci_queue_minitest/instrumentation_spec.rb +++ b/spec/datadog/ci/contrib/ci_queue_minitest/instrumentation_spec.rb @@ -40,19 +40,13 @@ expect(test_module_span).not_to be_nil # test session and module are failed - expect(test_session_span.get_tag(Datadog::CI::Ext::Test::TAG_STATUS)).to eq( - Datadog::CI::Ext::Test::Status::FAIL - ) - expect(test_module_span.get_tag(Datadog::CI::Ext::Test::TAG_STATUS)).to eq( - Datadog::CI::Ext::Test::Status::FAIL - ) + expect([test_session_span, test_module_span]).to all have_fail_status # test suite spans are created for each test as for parallel execution expect(test_suite_spans).to have(3).items - expect(test_suite_spans.map { |span| span.get_tag(Datadog::CI::Ext::Test::TAG_STATUS) }.sort).to eq( - [Datadog::CI::Ext::Test::Status::FAIL, Datadog::CI::Ext::Test::Status::PASS, Datadog::CI::Ext::Test::Status::PASS] - ) - expect(test_suite_spans.map { |span| span.get_tag(Datadog::CI::Ext::Test::TAG_SUITE) }.sort).to eq( + expect(test_suite_spans).to have_tag_values_no_order(:status, ["pass", "pass", "fail"]) + expect(test_suite_spans).to have_tag_values_no_order( + :suite, [ "SomeTest at spec/datadog/ci/contrib/ci_queue_minitest/fake_test.rb (test_fail concurrently)", "SomeTest at spec/datadog/ci/contrib/ci_queue_minitest/fake_test.rb (test_pass concurrently)", @@ -63,13 +57,10 @@ # there is test span for every test case expect(test_spans).to have(3).items # each test span has its own test suite - expect(test_spans.map { |span| span.get_tag(Datadog::CI::Ext::Test::TAG_TEST_SUITE_ID) }.uniq).to have(3).items + expect(test_spans).to have_unique_tag_values_count(:test_suite_id, 3) # every test span is connected to test module and test session - test_spans.each do |test_span| - [Datadog::CI::Ext::Test::TAG_TEST_MODULE_ID, Datadog::CI::Ext::Test::TAG_TEST_SESSION_ID].each do |tag| - expect(test_span.get_tag(tag)).not_to be_nil - end - end + expect(test_spans).to all have_test_tag(:test_module_id) + expect(test_spans).to all have_test_tag(:test_session_id) end end diff --git a/spec/datadog/ci/contrib/ci_queue_rspec/instrumentation_spec.rb b/spec/datadog/ci/contrib/ci_queue_rspec/instrumentation_spec.rb index fef844b7..9e02bbf6 100644 --- a/spec/datadog/ci/contrib/ci_queue_rspec/instrumentation_spec.rb +++ b/spec/datadog/ci/contrib/ci_queue_rspec/instrumentation_spec.rb @@ -59,19 +59,16 @@ def with_new_rspec_environment expect(test_module_span).not_to be_nil # test session and module are failed - expect(test_session_span.get_tag(Datadog::CI::Ext::Test::TAG_STATUS)).to eq( - Datadog::CI::Ext::Test::Status::FAIL - ) - expect(test_module_span.get_tag(Datadog::CI::Ext::Test::TAG_STATUS)).to eq( - Datadog::CI::Ext::Test::Status::FAIL - ) + expect([test_session_span, test_module_span]).to all have_fail_status # test suite spans are created for each test as for parallel execution expect(test_suite_spans).to have(2).items - expect(test_suite_spans.map { |span| span.get_tag(Datadog::CI::Ext::Test::TAG_STATUS) }.sort).to eq( + expect(test_suite_spans).to have_tag_values_no_order( + :status, [Datadog::CI::Ext::Test::Status::FAIL, Datadog::CI::Ext::Test::Status::PASS] ) - expect(test_suite_spans.map { |span| span.get_tag(Datadog::CI::Ext::Test::TAG_SUITE) }.sort).to eq( + expect(test_suite_spans).to have_tag_values_no_order( + :suite, [ "SomeTest at ./spec/datadog/ci/contrib/ci_queue_rspec/suite_under_test/some_test_rspec.rb (ci-queue running example [nested fails])", "SomeTest at ./spec/datadog/ci/contrib/ci_queue_rspec/suite_under_test/some_test_rspec.rb (ci-queue running example [nested foo])" @@ -81,13 +78,10 @@ def with_new_rspec_environment # there is test span for every test case expect(test_spans).to have(2).items # each test span has its own test suite - expect(test_spans.map { |span| span.get_tag(Datadog::CI::Ext::Test::TAG_TEST_SUITE_ID) }.uniq).to have(2).items + expect(test_spans).to have_unique_tag_values_count(:test_suite_id, 2) # every test span is connected to test module and test session - test_spans.each do |test_span| - [Datadog::CI::Ext::Test::TAG_TEST_MODULE_ID, Datadog::CI::Ext::Test::TAG_TEST_SESSION_ID].each do |tag| - expect(test_span.get_tag(tag)).not_to be_nil - end - end + expect(test_spans).to all have_test_tag(:test_module_id) + expect(test_spans).to all have_test_tag(:test_session_id) end end diff --git a/spec/datadog/ci/contrib/cucumber/instrumentation_spec.rb b/spec/datadog/ci/contrib/cucumber/instrumentation_spec.rb index e8219c3b..581e40aa 100644 --- a/spec/datadog/ci/contrib/cucumber/instrumentation_spec.rb +++ b/spec/datadog/ci/contrib/cucumber/instrumentation_spec.rb @@ -72,128 +72,109 @@ scenario_span = spans.find { |s| s.resource == "cucumber scenario" } - expect(scenario_span.type).to eq(Datadog::CI::Ext::AppTypes::TYPE_TEST) + expect(scenario_span.type).to eq("test") expect(scenario_span.name).to eq("cucumber scenario") expect(scenario_span.resource).to eq("cucumber scenario") expect(scenario_span.service).to eq("jalapenos") - expect(scenario_span.get_tag(Datadog::CI::Ext::Test::TAG_SPAN_KIND)).to eq(Datadog::CI::Ext::Test::SPAN_KIND_TEST) - expect(scenario_span.get_tag(Datadog::CI::Ext::Test::TAG_NAME)).to eq("cucumber scenario") - expect(scenario_span.get_tag(Datadog::CI::Ext::Test::TAG_SUITE)).to eq( + expect(scenario_span).to have_test_tag(:span_kind, "test") + expect(scenario_span).to have_test_tag(:name, "cucumber scenario") + expect(scenario_span).to have_test_tag( + :suite, "Datadog integration at spec/datadog/ci/contrib/cucumber/features/passing.feature" ) - expect(scenario_span.get_tag(Datadog::CI::Ext::Test::TAG_TYPE)).to eq(Datadog::CI::Ext::Test::Type::TEST) - expect(scenario_span.get_tag(Datadog::CI::Ext::Test::TAG_FRAMEWORK)).to eq( - Datadog::CI::Contrib::Cucumber::Ext::FRAMEWORK - ) - expect(scenario_span.get_tag(Datadog::CI::Ext::Test::TAG_FRAMEWORK_VERSION)).to eq( + expect(scenario_span).to have_test_tag(:type, "test") + + expect(scenario_span).to have_test_tag(:framework, "cucumber") + expect(scenario_span).to have_test_tag( + :framework_version, Datadog::CI::Contrib::Cucumber::Integration.version.to_s ) - expect(scenario_span.get_tag(Datadog::CI::Ext::Test::TAG_STATUS)).to eq(Datadog::CI::Ext::Test::Status::PASS) - expect(scenario_span.get_tag(Datadog::CI::Ext::Test::TAG_SOURCE_FILE)).to eq( + expect(scenario_span).to have_pass_status + + expect(scenario_span).to have_test_tag( + :source_file, "spec/datadog/ci/contrib/cucumber/features/passing.feature" ) - expect(scenario_span.get_tag(Datadog::CI::Ext::Test::TAG_SOURCE_START)).to eq("3") - expect(scenario_span.get_tag(Datadog::CI::Ext::Test::TAG_CODEOWNERS)).to eq( + expect(scenario_span).to have_test_tag(:source_start, "3") + + expect(scenario_span).to have_test_tag( + :codeowners, "[\"@DataDog/ruby-guild\", \"@DataDog/ci-app-libraries\"]" ) step_span = spans.find { |s| s.resource == "datadog" } - expect(step_span.resource).to eq("datadog") + expect(step_span.name).to eq("datadog") - spans.each do |span| - expect(span.get_tag(Datadog::Tracing::Metadata::Ext::Distributed::TAG_ORIGIN)) - .to eq(Datadog::CI::Ext::Test::CONTEXT_ORIGIN) - end + expect(spans).to all have_origin(Datadog::CI::Ext::Test::CONTEXT_ORIGIN) end it "marks undefined cucumber scenario as skipped" do undefined_scenario_span = spans.find { |s| s.resource == "undefined scenario" } expect(undefined_scenario_span).not_to be_nil - expect(undefined_scenario_span.get_tag(Datadog::CI::Ext::Test::TAG_STATUS)).to eq( - Datadog::CI::Ext::Test::Status::SKIP - ) - expect(undefined_scenario_span.get_tag(Datadog::CI::Ext::Test::TAG_SKIP_REASON)).to eq( - 'Undefined step: "undefined"' - ) + expect(undefined_scenario_span).to have_skip_status + expect(undefined_scenario_span).to have_test_tag(:skip_reason, 'Undefined step: "undefined"') end it "marks pending cucumber scenario as skipped" do pending_scenario_span = spans.find { |s| s.resource == "pending scenario" } expect(pending_scenario_span).not_to be_nil - expect(pending_scenario_span.get_tag(Datadog::CI::Ext::Test::TAG_STATUS)).to eq( - Datadog::CI::Ext::Test::Status::SKIP - ) - expect(pending_scenario_span.get_tag(Datadog::CI::Ext::Test::TAG_SKIP_REASON)).to eq( - "implementation" - ) + expect(pending_scenario_span).to have_skip_status + expect(pending_scenario_span).to have_test_tag(:skip_reason, "implementation") end it "marks skipped cucumber scenario as skipped" do skipped_scenario_span = spans.find { |s| s.resource == "skipped scenario" } expect(skipped_scenario_span).not_to be_nil - expect(skipped_scenario_span.get_tag(Datadog::CI::Ext::Test::TAG_STATUS)).to eq( - Datadog::CI::Ext::Test::Status::SKIP - ) - expect(skipped_scenario_span.get_tag(Datadog::CI::Ext::Test::TAG_SKIP_REASON)).to eq( - "Scenario skipped" - ) + expect(skipped_scenario_span).to have_skip_status + expect(skipped_scenario_span).to have_test_tag(:skip_reason, "Scenario skipped") end it "creates test session span" do expect(test_session_span).not_to be_nil expect(test_session_span.service).to eq("jalapenos") - expect(test_session_span.get_tag(Datadog::CI::Ext::Test::TAG_SPAN_KIND)).to eq( - Datadog::CI::Ext::Test::SPAN_KIND_TEST - ) - expect(test_session_span.get_tag(Datadog::CI::Ext::Test::TAG_FRAMEWORK)).to eq( - Datadog::CI::Contrib::Cucumber::Ext::FRAMEWORK - ) - expect(test_session_span.get_tag(Datadog::CI::Ext::Test::TAG_FRAMEWORK_VERSION)).to eq( + expect(test_session_span).to have_test_tag(:span_kind, "test") + expect(test_session_span).to have_test_tag(:framework, "cucumber") + expect(test_session_span).to have_test_tag( + :framework_version, Datadog::CI::Contrib::Cucumber::Integration.version.to_s ) - expect(test_session_span.get_tag(Datadog::CI::Ext::Test::TAG_STATUS)).to eq(Datadog::CI::Ext::Test::Status::PASS) + expect(test_session_span).to have_pass_status end it "creates test module span" do expect(test_module_span).not_to be_nil expect(test_module_span.name).to eq(test_command) expect(test_module_span.service).to eq("jalapenos") - expect(test_module_span.get_tag(Datadog::CI::Ext::Test::TAG_SPAN_KIND)).to eq( - Datadog::CI::Ext::Test::SPAN_KIND_TEST - ) - expect(test_module_span.get_tag(Datadog::CI::Ext::Test::TAG_FRAMEWORK)).to eq( - Datadog::CI::Contrib::Cucumber::Ext::FRAMEWORK - ) - expect(test_module_span.get_tag(Datadog::CI::Ext::Test::TAG_FRAMEWORK_VERSION)).to eq( + expect(test_module_span).to have_test_tag(:span_kind, "test") + expect(test_module_span).to have_test_tag(:framework, "cucumber") + expect(test_module_span).to have_test_tag( + :framework_version, Datadog::CI::Contrib::Cucumber::Integration.version.to_s ) - expect(test_module_span.get_tag(Datadog::CI::Ext::Test::TAG_STATUS)).to eq(Datadog::CI::Ext::Test::Status::PASS) + expect(test_module_span).to have_pass_status end it "creates test suite span" do expect(first_test_suite_span).not_to be_nil expect(first_test_suite_span.name).to eq("Datadog integration at spec/datadog/ci/contrib/cucumber/features/passing.feature") expect(first_test_suite_span.service).to eq("jalapenos") - expect(first_test_suite_span.get_tag(Datadog::CI::Ext::Test::TAG_SPAN_KIND)).to eq( - Datadog::CI::Ext::Test::SPAN_KIND_TEST - ) - expect(first_test_suite_span.get_tag(Datadog::CI::Ext::Test::TAG_FRAMEWORK)).to eq( - Datadog::CI::Contrib::Cucumber::Ext::FRAMEWORK - ) - expect(first_test_suite_span.get_tag(Datadog::CI::Ext::Test::TAG_FRAMEWORK_VERSION)).to eq( + expect(first_test_suite_span).to have_test_tag(:span_kind, "test") + expect(first_test_suite_span).to have_test_tag(:framework, "cucumber") + expect(first_test_suite_span).to have_test_tag( + :framework_version, Datadog::CI::Contrib::Cucumber::Integration.version.to_s ) - expect(first_test_suite_span.get_tag(Datadog::CI::Ext::Test::TAG_STATUS)).to eq(Datadog::CI::Ext::Test::Status::PASS) + expect(first_test_suite_span).to have_pass_status end - it "connects scenario span to test session and test module" do - expect(first_test_span.get_tag(Datadog::CI::Ext::Test::TAG_TEST_MODULE_ID)).to eq(test_module_span.id.to_s) - expect(first_test_span.get_tag(Datadog::CI::Ext::Test::TAG_MODULE)).to eq(test_command) - expect(first_test_span.get_tag(Datadog::CI::Ext::Test::TAG_TEST_SESSION_ID)).to eq(test_session_span.id.to_s) - expect(first_test_span.get_tag(Datadog::CI::Ext::Test::TAG_TEST_SUITE_ID)).to eq(first_test_suite_span.id.to_s) - expect(first_test_span.get_tag(Datadog::CI::Ext::Test::TAG_SUITE)).to eq(first_test_suite_span.name) + it "connects test span to test session, test module, and test suite" do + expect(first_test_span).to have_test_tag(:test_module_id, test_module_span.id.to_s) + expect(first_test_span).to have_test_tag(:module, test_command) + expect(first_test_span).to have_test_tag(:test_session_id, test_session_span.id.to_s) + expect(first_test_span).to have_test_tag(:test_suite_id, first_test_suite_span.id.to_s) + expect(first_test_span).to have_test_tag(:suite, first_test_suite_span.name) end end @@ -203,29 +184,17 @@ it "creates all CI spans with failed state" do expect(first_test_span.name).to eq("cucumber failing scenario") - expect(first_test_span.get_tag(Datadog::CI::Ext::Test::TAG_STATUS)).to eq( - Datadog::CI::Ext::Test::Status::FAIL - ) + expect(first_test_span).to have_fail_status step_span = spans.find { |s| s.resource == "failure" } expect(step_span.name).to eq("failure") - expect(step_span.get_tag(Datadog::CI::Ext::Test::TAG_STATUS)).to eq( - Datadog::CI::Ext::Test::Status::FAIL - ) + expect(step_span).to have_fail_status expect(first_test_suite_span.name).to eq( "Datadog integration - test failing features at spec/datadog/ci/contrib/cucumber/features/failing.feature" ) - expect(first_test_suite_span.get_tag(Datadog::CI::Ext::Test::TAG_STATUS)).to eq( - Datadog::CI::Ext::Test::Status::FAIL - ) - expect(test_session_span.get_tag(Datadog::CI::Ext::Test::TAG_STATUS)).to eq( - Datadog::CI::Ext::Test::Status::FAIL - ) - expect(test_module_span.get_tag(Datadog::CI::Ext::Test::TAG_STATUS)).to eq( - Datadog::CI::Ext::Test::Status::FAIL - ) + expect([first_test_suite_span, test_session_span, test_module_span]).to all have_fail_status end end @@ -239,23 +208,24 @@ test_spans.each_with_index do |span, index| # test parameters are available since cucumber 4 if cucumber_4_or_above - expect(span.get_tag(Datadog::CI::Ext::Test::TAG_NAME)).to eq("scenario with examples") - - expect(span.get_tag(Datadog::CI::Ext::Test::TAG_PARAMETERS)).to eq( + expect(span).to have_test_tag(:name, "scenario with examples") + expect(span).to have_test_tag( + :parameters, "{\"arguments\":{\"num1\":\"#{index}\",\"num2\":\"#{index + 1}\",\"total\":\"#{index + index + 1}\"},\"metadata\":{}}" ) else - expect(span.get_tag(Datadog::CI::Ext::Test::TAG_NAME)).to eq( + expect(span).to have_test_tag( + :name, "scenario with examples, Examples (##{index + 1})" ) + expect(span).not_to have_test_tag(:parameters) end - expect(span.get_tag(Datadog::CI::Ext::Test::TAG_SUITE)).to eq( + expect(span).to have_test_tag( + :suite, "Datadog integration for parametrized tests at spec/datadog/ci/contrib/cucumber/features/with_parameters.feature" ) - expect(span.get_tag(Datadog::CI::Ext::Test::TAG_TEST_SUITE_ID)).to eq(first_test_suite_span.id.to_s) - expect(span.get_tag(Datadog::CI::Ext::Test::TAG_STATUS)).to eq( - Datadog::CI::Ext::Test::Status::PASS - ) + expect(span).to have_test_tag(:test_suite_id, first_test_suite_span.id.to_s) + expect(span).to have_pass_status end end end @@ -268,33 +238,20 @@ it "creates a test suite span for each feature" do expect(test_suite_spans).to have(4).items - expect(passing_test_suite.get_tag(Datadog::CI::Ext::Test::TAG_STATUS)).to eq( - Datadog::CI::Ext::Test::Status::PASS - ) - expect(failing_test_suite.get_tag(Datadog::CI::Ext::Test::TAG_STATUS)).to eq( - Datadog::CI::Ext::Test::Status::FAIL - ) + expect(passing_test_suite).to have_pass_status + expect(failing_test_suite).to have_fail_status end it "connects tests with their respective test suites" do cucumber_scenario = test_spans.find { |span| span.name =~ /cucumber scenario/ } - expect(cucumber_scenario.get_tag(Datadog::CI::Ext::Test::TAG_TEST_SUITE_ID)).to eq( - passing_test_suite.id.to_s - ) + expect(cucumber_scenario).to have_test_tag(:test_suite_id, passing_test_suite.id.to_s) cucumber_failing_scenario = test_spans.find { |span| span.name =~ /cucumber failing scenario/ } - expect(cucumber_failing_scenario.get_tag(Datadog::CI::Ext::Test::TAG_TEST_SUITE_ID)).to eq( - failing_test_suite.id.to_s - ) + expect(cucumber_failing_scenario).to have_test_tag(:test_suite_id, failing_test_suite.id.to_s) end it "sets failed status for module and session" do - expect(test_session_span.get_tag(Datadog::CI::Ext::Test::TAG_STATUS)).to eq( - Datadog::CI::Ext::Test::Status::FAIL - ) - expect(test_module_span.get_tag(Datadog::CI::Ext::Test::TAG_STATUS)).to eq( - Datadog::CI::Ext::Test::Status::FAIL - ) + expect([test_session_span, test_module_span]).to all have_fail_status end end @@ -311,38 +268,29 @@ end it "marks test session as failed" do - expect(test_session_span).not_to be_nil - expect(test_session_span.get_tag(Datadog::CI::Ext::Test::TAG_STATUS)).to eq(Datadog::CI::Ext::Test::Status::FAIL) + expect(test_session_span).to have_fail_status end it "marks test suite as failed" do - expect(first_test_suite_span).not_to be_nil - expect(first_test_suite_span.get_tag(Datadog::CI::Ext::Test::TAG_STATUS)).to eq(Datadog::CI::Ext::Test::Status::FAIL) + expect(first_test_suite_span).to have_fail_status end it "marks undefined cucumber scenario as failed" do undefined_scenario_span = spans.find { |s| s.resource == "undefined scenario" } expect(undefined_scenario_span).not_to be_nil - expect(undefined_scenario_span.get_tag(Datadog::CI::Ext::Test::TAG_STATUS)).to eq( - Datadog::CI::Ext::Test::Status::FAIL - ) + expect(undefined_scenario_span).to have_fail_status + expect(undefined_scenario_span).to have_error expect(undefined_scenario_span).to have_error_message("Undefined step: \"undefined\"") end it "marks pending cucumber scenario as failed" do pending_scenario_span = spans.find { |s| s.resource == "pending scenario" } - expect(pending_scenario_span).not_to be_nil - expect(pending_scenario_span.get_tag(Datadog::CI::Ext::Test::TAG_STATUS)).to eq( - Datadog::CI::Ext::Test::Status::FAIL - ) + expect(pending_scenario_span).to have_fail_status end it "marks skipped cucumber scenario as skipped" do skipped_scenario_span = spans.find { |s| s.resource == "skipped scenario" } - expect(skipped_scenario_span).not_to be_nil - expect(skipped_scenario_span.get_tag(Datadog::CI::Ext::Test::TAG_STATUS)).to eq( - Datadog::CI::Ext::Test::Status::SKIP - ) + expect(skipped_scenario_span).to have_skip_status end end @@ -351,21 +299,15 @@ it "marks all test spans as skipped" do expect(test_spans).to have(2).items - expect(test_spans.map { |span| span.get_tag(Datadog::CI::Ext::Test::TAG_STATUS) }.uniq).to eq( - [Datadog::CI::Ext::Test::Status::SKIP] - ) + expect(test_spans).to all have_skip_status end it "marks test session as passed" do - expect(test_session_span).not_to be_nil - expect(test_session_span.get_tag(Datadog::CI::Ext::Test::TAG_STATUS)).to eq(Datadog::CI::Ext::Test::Status::PASS) + expect(test_session_span).to have_pass_status end it "marks test suite as skipped" do - expect(first_test_suite_span).not_to be_nil - expect(first_test_suite_span.get_tag(Datadog::CI::Ext::Test::TAG_STATUS)).to eq( - Datadog::CI::Ext::Test::Status::SKIP - ) + expect(first_test_suite_span).to have_skip_status end end end diff --git a/spec/datadog/ci/contrib/minitest/instrumentation_spec.rb b/spec/datadog/ci/contrib/minitest/instrumentation_spec.rb index e4063206..c3e019dc 100644 --- a/spec/datadog/ci/contrib/minitest/instrumentation_spec.rb +++ b/spec/datadog/ci/contrib/minitest/instrumentation_spec.rb @@ -54,22 +54,29 @@ def test_foo expect(span.name).to eq("test_foo") expect(span.resource).to eq("test_foo") expect(span.service).to eq("ltest") - expect(span.get_tag(Datadog::CI::Ext::Test::TAG_NAME)).to eq("test_foo") - expect(span.get_tag(Datadog::CI::Ext::Test::TAG_SUITE)).to eq( + + expect(span).to have_test_tag(:name, "test_foo") + expect(span).to have_test_tag( + :suite, "SomeTest at spec/datadog/ci/contrib/minitest/instrumentation_spec.rb" ) - expect(span.get_tag(Datadog::CI::Ext::Test::TAG_SPAN_KIND)).to eq(Datadog::CI::Ext::Test::SPAN_KIND_TEST) - expect(span.get_tag(Datadog::CI::Ext::Test::TAG_TYPE)).to eq(Datadog::CI::Ext::Test::Type::TEST) - expect(span.get_tag(Datadog::CI::Ext::Test::TAG_FRAMEWORK)).to eq(Datadog::CI::Contrib::Minitest::Ext::FRAMEWORK) - expect(span.get_tag(Datadog::CI::Ext::Test::TAG_FRAMEWORK_VERSION)).to eq( + expect(span).to have_test_tag(:span_kind, "test") + expect(span).to have_test_tag(:type, "test") + expect(span).to have_test_tag(:framework, "minitest") + expect(span).to have_test_tag( + :framework_version, Datadog::CI::Contrib::Minitest::Integration.version.to_s ) - expect(span.get_tag(Datadog::CI::Ext::Test::TAG_STATUS)).to eq(Datadog::CI::Ext::Test::Status::PASS) - expect(span.get_tag(Datadog::CI::Ext::Test::TAG_SOURCE_FILE)).to eq( + + expect(span).to have_pass_status + + expect(span).to have_test_tag( + :source_file, "spec/datadog/ci/contrib/minitest/instrumentation_spec.rb" ) - expect(span.get_tag(Datadog::CI::Ext::Test::TAG_SOURCE_START)).to eq("47") - expect(span.get_tag(Datadog::CI::Ext::Test::TAG_CODEOWNERS)).to eq( + expect(span).to have_test_tag(:source_start, "47") + expect(span).to have_test_tag( + :codeowners, "[\"@DataDog/ruby-guild\", \"@DataDog/ci-app-libraries\"]" ) end @@ -112,8 +119,9 @@ def self.name expect(span.type).to eq(Datadog::CI::Ext::AppTypes::TYPE_TEST) expect(span.resource).to eq(method_name) expect(span.service).to eq("ltest") - expect(span.get_tag(Datadog::CI::Ext::Test::TAG_NAME)).to eq(method_name) - expect(span.get_tag(Datadog::CI::Ext::Test::TAG_SUITE)).to eq( + expect(span).to have_test_tag(:name, method_name) + expect(span).to have_test_tag( + :suite, "SomeSpec at spec/datadog/ci/contrib/minitest/instrumentation_spec.rb" ) end @@ -155,16 +163,12 @@ def test_foo klass.new(:test_foo).run expect(spans).to have(2).items - - spans.each do |span| - expect(span.get_tag(Datadog::Tracing::Metadata::Ext::Distributed::TAG_ORIGIN)) - .to eq(Datadog::CI::Ext::Test::CONTEXT_ORIGIN) - end + expect(spans).to all have_origin(Datadog::CI::Ext::Test::CONTEXT_ORIGIN) end context "catches failures" do def expect_failure - expect(span.get_tag(Datadog::CI::Ext::Test::TAG_STATUS)).to eq(Datadog::CI::Ext::Test::Status::FAIL) + expect(span).to have_fail_status expect(span).to have_error expect(span).to have_error_type expect(span).to have_error_message @@ -228,7 +232,7 @@ def test_foo context "catches errors" do def expect_failure - expect(span.get_tag(Datadog::CI::Ext::Test::TAG_STATUS)).to eq(Datadog::CI::Ext::Test::Status::FAIL) + expect(span).to have_fail_status expect(span).to have_error expect(span).to have_error_type expect(span).to have_error_message @@ -292,7 +296,7 @@ def test_foo context "catches skips" do def expect_skip - expect(span.get_tag(Datadog::CI::Ext::Test::TAG_STATUS)).to eq(Datadog::CI::Ext::Test::Status::SKIP) + expect(span).to have_skip_status expect(span).to_not have_error end @@ -310,7 +314,7 @@ def test_foo klass.new(:test_foo).run expect_skip - expect(span.get_tag(Datadog::CI::Ext::Test::TAG_SKIP_REASON)).to eq("Skip!") + expect(span).to have_test_tag(:skip_reason, "Skip!") end it "without reason" do @@ -327,7 +331,7 @@ def test_foo klass.new(:test_foo).run expect_skip - expect(span.get_tag(Datadog::CI::Ext::Test::TAG_SKIP_REASON)).to eq("Skipped, no message given") + expect(span).to have_test_tag(:skip_reason, "Skipped, no message given") end it "within test" do @@ -407,89 +411,67 @@ def test_pass_other it "creates a test session span" do expect(test_session_span).not_to be_nil - expect(test_session_span.type).to eq(Datadog::CI::Ext::AppTypes::TYPE_TEST_SESSION) - expect(test_session_span.get_tag(Datadog::CI::Ext::Test::TAG_SPAN_KIND)).to eq( - Datadog::CI::Ext::Test::SPAN_KIND_TEST - ) - expect(test_session_span.get_tag(Datadog::CI::Ext::Test::TAG_FRAMEWORK)).to eq( - Datadog::CI::Contrib::Minitest::Ext::FRAMEWORK - ) - expect(test_session_span.get_tag(Datadog::CI::Ext::Test::TAG_FRAMEWORK_VERSION)).to eq( + expect(test_session_span.type).to eq("test_session_end") + expect(test_session_span).to have_test_tag(:span_kind, "test") + expect(test_session_span).to have_test_tag(:framework, "minitest") + expect(test_session_span).to have_test_tag( + :framework_version, Datadog::CI::Contrib::Minitest::Integration.version.to_s ) - expect(test_session_span.get_tag(Datadog::CI::Ext::Test::TAG_STATUS)).to eq( - Datadog::CI::Ext::Test::Status::PASS - ) + expect(test_session_span).to have_pass_status end it "creates a test module span" do expect(test_module_span).not_to be_nil - expect(test_module_span.type).to eq(Datadog::CI::Ext::AppTypes::TYPE_TEST_MODULE) + expect(test_module_span.type).to eq("test_module_end") expect(test_module_span.name).to eq(test_command) - expect(test_module_span.get_tag(Datadog::CI::Ext::Test::TAG_SPAN_KIND)).to eq( - Datadog::CI::Ext::Test::SPAN_KIND_TEST - ) - expect(test_module_span.get_tag(Datadog::CI::Ext::Test::TAG_FRAMEWORK)).to eq( - Datadog::CI::Contrib::Minitest::Ext::FRAMEWORK - ) - expect(test_module_span.get_tag(Datadog::CI::Ext::Test::TAG_FRAMEWORK_VERSION)).to eq( + expect(test_module_span).to have_test_tag(:span_kind, "test") + expect(test_module_span).to have_test_tag(:framework, "minitest") + expect(test_module_span).to have_test_tag( + :framework_version, Datadog::CI::Contrib::Minitest::Integration.version.to_s ) - expect(test_module_span.get_tag(Datadog::CI::Ext::Test::TAG_STATUS)).to eq( - Datadog::CI::Ext::Test::Status::PASS - ) + expect(test_module_span).to have_pass_status end it "creates a test suite span" do expect(first_test_suite_span).not_to be_nil - expect(first_test_suite_span.type).to eq(Datadog::CI::Ext::AppTypes::TYPE_TEST_SUITE) + expect(first_test_suite_span.type).to eq("test_suite_end") expect(first_test_suite_span.name).to eq("SomeTest at spec/datadog/ci/contrib/minitest/instrumentation_spec.rb") - expect(first_test_suite_span.get_tag(Datadog::CI::Ext::Test::TAG_SPAN_KIND)).to eq( - Datadog::CI::Ext::Test::SPAN_KIND_TEST - ) - expect(first_test_suite_span.get_tag(Datadog::CI::Ext::Test::TAG_FRAMEWORK)).to eq( - Datadog::CI::Contrib::Minitest::Ext::FRAMEWORK - ) - expect(first_test_suite_span.get_tag(Datadog::CI::Ext::Test::TAG_FRAMEWORK_VERSION)).to eq( + expect(first_test_suite_span).to have_test_tag(:span_kind, "test") + expect(first_test_suite_span).to have_test_tag(:framework, "minitest") + expect(first_test_suite_span).to have_test_tag( + :framework_version, Datadog::CI::Contrib::Minitest::Integration.version.to_s ) - expect(first_test_suite_span.get_tag(Datadog::CI::Ext::Test::TAG_STATUS)).to eq( - Datadog::CI::Ext::Test::Status::PASS - ) + expect(first_test_suite_span).to have_pass_status end it "creates test spans and connects them to the session, module, and suite" do - expect(test_spans.count).to eq(2) + expect(test_spans).to have(2).items - expect(first_test_span.get_tag(Datadog::CI::Ext::Test::TAG_SUITE)).to eq( + expect(test_spans).to have_unique_tag_values_count(:test_session_id, 1) + expect(test_spans).to have_unique_tag_values_count(:test_module_id, 1) + expect(test_spans).to have_unique_tag_values_count(:test_suite_id, 1) + + expect(first_test_span).to have_test_tag( + :suite, "SomeTest at spec/datadog/ci/contrib/minitest/instrumentation_spec.rb" ) - expect(first_test_span.get_tag(Datadog::CI::Ext::Test::TAG_FRAMEWORK)).to eq( - Datadog::CI::Contrib::Minitest::Ext::FRAMEWORK - ) - expect(first_test_span.get_tag(Datadog::CI::Ext::Test::TAG_FRAMEWORK_VERSION)).to eq( + expect(first_test_span).to have_test_tag(:framework, "minitest") + expect(first_test_span).to have_test_tag( + :framework_version, Datadog::CI::Contrib::Minitest::Integration.version.to_s ) - expect(first_test_span.get_tag(Datadog::CI::Ext::Test::TAG_STATUS)).to eq( - Datadog::CI::Ext::Test::Status::PASS - ) - - test_session_ids = test_spans.map { |span| span.get_tag(Datadog::CI::Ext::Test::TAG_TEST_SESSION_ID) }.uniq - test_module_ids = test_spans.map { |span| span.get_tag(Datadog::CI::Ext::Test::TAG_TEST_MODULE_ID) }.uniq - test_suite_ids = test_spans.map { |span| span.get_tag(Datadog::CI::Ext::Test::TAG_TEST_SUITE_ID) }.uniq - - expect(test_session_ids.count).to eq(1) - expect(test_session_ids.first).to eq(test_session_span.id.to_s) - - expect(test_module_ids.count).to eq(1) - expect(test_module_ids.first).to eq(test_module_span.id.to_s) + expect(first_test_span).to have_pass_status - expect(test_suite_ids.count).to eq(1) - expect(test_suite_ids.first).to eq(first_test_suite_span.id.to_s) + expect(first_test_span).to have_test_tag(:test_session_id, test_session_span.id.to_s) + expect(first_test_span).to have_test_tag(:test_module_id, test_module_span.id.to_s) + expect(first_test_span).to have_test_tag(:test_suite_id, first_test_suite_span.id.to_s) end end @@ -505,20 +487,11 @@ def test_fail end it "traces test, test session, test module with failed status" do - expect(first_test_span.get_tag(Datadog::CI::Ext::Test::TAG_NAME)).to eq("test_fail") - expect(first_test_span.get_tag(Datadog::CI::Ext::Test::TAG_STATUS)).to eq( - Datadog::CI::Ext::Test::Status::FAIL - ) + expect(first_test_span).to have_test_tag(:name, "test_fail") - expect(test_session_span.get_tag(Datadog::CI::Ext::Test::TAG_STATUS)).to eq( - Datadog::CI::Ext::Test::Status::FAIL - ) - expect(test_module_span.get_tag(Datadog::CI::Ext::Test::TAG_STATUS)).to eq( - Datadog::CI::Ext::Test::Status::FAIL - ) - expect(first_test_suite_span.get_tag(Datadog::CI::Ext::Test::TAG_STATUS)).to eq( - Datadog::CI::Ext::Test::Status::FAIL - ) + expect( + [first_test_span, first_test_suite_span, test_session_span, test_module_span] + ).to all have_fail_status end end @@ -548,9 +521,8 @@ class SomeSpec < Minitest::Spec end it "traces tests with unique names" do - test_names = test_spans.map { |span| span.get_tag(Datadog::CI::Ext::Test::TAG_NAME) }.sort - - expect(test_names).to eq( + expect(test_spans).to have_tag_values_no_order( + :name, [ "test_0001_does not fail", "test_0001_does not fail", @@ -561,11 +533,9 @@ class SomeSpec < Minitest::Spec end it "connects tests to different test suites (one per spec context)" do - test_suite_ids = test_spans.map { |span| span.get_tag(Datadog::CI::Ext::Test::TAG_TEST_SUITE_ID) }.uniq - test_suite_names = test_spans.map { |span| span.get_tag(Datadog::CI::Ext::Test::TAG_SUITE) }.sort - - expect(test_suite_ids).to have(4).items - expect(test_suite_names).to eq( + expect(test_spans).to have_unique_tag_values_count(:test_suite_id, 4) + expect(test_spans).to have_tag_values_no_order( + :suite, [ "SomeSpec at spec/datadog/ci/contrib/minitest/instrumentation_spec.rb", "in context at spec/datadog/ci/contrib/minitest/instrumentation_spec.rb", @@ -576,10 +546,8 @@ class SomeSpec < Minitest::Spec end it "connects tests to a single test session" do - test_session_ids = test_spans.map { |span| span.get_tag(Datadog::CI::Ext::Test::TAG_TEST_SESSION_ID) }.uniq - - expect(test_session_ids.count).to eq(1) - expect(test_session_ids.first).to eq(test_session_span.id.to_s) + expect(test_spans).to have_unique_tag_values_count(:test_session_id, 1) + expect(first_test_span).to have_test_tag(:test_session_id, test_session_span.id.to_s) end end @@ -623,8 +591,8 @@ def test_b_2 # note that this test could be flaky expect(test_threads.count).to be > 1 - test_names = test_spans.map { |span| span.get_tag(Datadog::CI::Ext::Test::TAG_NAME) }.sort - expect(test_names).to eq( + expect(test_spans).to have_tag_values_no_order( + :name, [ "test_a_1", "test_a_2", @@ -632,20 +600,15 @@ def test_b_2 "test_b_2" ] ) - - test_suite_ids = test_spans.map { |span| span.get_tag(Datadog::CI::Ext::Test::TAG_TEST_SUITE_ID) }.uniq - expect(test_suite_ids).to have(4).items + expect(test_spans).to have_unique_tag_values_count(:test_suite_id, 4) end it "connects tests to a single test session and a single test module" do - test_session_ids = test_spans.map { |span| span.get_tag(Datadog::CI::Ext::Test::TAG_TEST_SESSION_ID) }.uniq - test_module_ids = test_spans.map { |span| span.get_tag(Datadog::CI::Ext::Test::TAG_TEST_MODULE_ID) }.uniq + expect(test_spans).to have_unique_tag_values_count(:test_module_id, 1) + expect(test_spans).to have_unique_tag_values_count(:test_session_id, 1) - expect(test_session_ids.count).to eq(1) - expect(test_session_ids.first).to eq(test_session_span.id.to_s) - - expect(test_module_ids.count).to eq(1) - expect(test_module_ids.first).to eq(test_module_span.id.to_s) + expect(first_test_span).to have_test_tag(:test_module_id, test_module_span.id.to_s) + expect(first_test_span).to have_test_tag(:test_session_id, test_session_span.id.to_s) end it "correctly tracks test and session durations" do @@ -664,8 +627,8 @@ def test_b_2 it "creates test suite spans" do expect(test_suite_spans).to have(4).items - test_suite_names = test_suite_spans.map { |span| span.name }.sort - expect(test_suite_names).to eq( + expect(test_suite_spans).to have_tag_values_no_order( + :suite, [ "TestA at spec/datadog/ci/contrib/minitest/instrumentation_spec.rb (test_a_1 concurrently)", "TestA at spec/datadog/ci/contrib/minitest/instrumentation_spec.rb (test_a_2 concurrently)", @@ -693,19 +656,15 @@ def test_2 it "marks all test spans as skipped" do expect(test_spans).to have(2).items - expect(test_spans.map { |span| span.get_tag(Datadog::CI::Ext::Test::TAG_STATUS) }.uniq).to eq( - [Datadog::CI::Ext::Test::Status::SKIP] - ) + expect(test_spans).to all have_skip_status end it "marks test session as passed" do - expect(test_session_span).not_to be_nil - expect(test_session_span.get_tag(Datadog::CI::Ext::Test::TAG_STATUS)).to eq(Datadog::CI::Ext::Test::Status::PASS) + expect(test_session_span).to have_pass_status end it "marks test suite as skipped" do - expect(first_test_suite_span).not_to be_nil - expect(first_test_suite_span.get_tag(Datadog::CI::Ext::Test::TAG_STATUS)).to eq(Datadog::CI::Ext::Test::Status::SKIP) + expect(first_test_suite_span).to have_skip_status end end end diff --git a/spec/datadog/ci/contrib/rspec/instrumentation_spec.rb b/spec/datadog/ci/contrib/rspec/instrumentation_spec.rb index 5073b7e6..83d23894 100644 --- a/spec/datadog/ci/contrib/rspec/instrumentation_spec.rb +++ b/spec/datadog/ci/contrib/rspec/instrumentation_spec.rb @@ -34,22 +34,28 @@ def with_new_rspec_environment expect(first_test_span.name).to eq("foo") expect(first_test_span.resource).to eq("foo") - expect(first_test_span.get_tag(Datadog::CI::Ext::Test::TAG_NAME)).to eq("foo") - expect(first_test_span.get_tag(Datadog::CI::Ext::Test::TAG_SUITE)).to eq("some test at #{spec.file_path}") - expect(first_test_span.get_tag(Datadog::CI::Ext::Test::TAG_SPAN_KIND)).to eq(Datadog::CI::Ext::Test::SPAN_KIND_TEST) - expect(first_test_span.get_tag(Datadog::CI::Ext::Test::TAG_TYPE)).to eq(Datadog::CI::Ext::Test::Type::TEST) - expect(first_test_span.get_tag(Datadog::CI::Ext::Test::TAG_FRAMEWORK)).to eq(Datadog::CI::Contrib::RSpec::Ext::FRAMEWORK) - expect(first_test_span.get_tag(Datadog::CI::Ext::Test::TAG_FRAMEWORK_VERSION)).to eq( + expect(first_test_span).to have_test_tag(:name, "foo") + expect(first_test_span).to have_test_tag(:suite, "some test at #{spec.file_path}") + + expect(first_test_span).to have_test_tag(:span_kind, "test") + expect(first_test_span).to have_test_tag(:type, "test") + + expect(first_test_span).to have_test_tag(:framework, "rspec") + expect(first_test_span).to have_test_tag( + :framework_version, Datadog::CI::Contrib::RSpec::Integration.version.to_s ) - expect(first_test_span.get_tag(Datadog::CI::Ext::Test::TAG_STATUS)).to eq(Datadog::CI::Ext::Test::Status::PASS) - expect(first_test_span.get_tag(Datadog::CI::Ext::Test::TAG_SOURCE_FILE)).to eq( + expect(first_test_span).to have_pass_status + + expect(first_test_span).to have_test_tag( + :source_file, "spec/datadog/ci/contrib/rspec/instrumentation_spec.rb" ) - expect(first_test_span.get_tag(Datadog::CI::Ext::Test::TAG_SOURCE_START)).to eq("26") - expect(first_test_span.get_tag(Datadog::CI::Ext::Test::TAG_CODEOWNERS)).to eq( + expect(first_test_span).to have_test_tag(:source_start, "26") + expect(first_test_span).to have_test_tag( + :codeowners, "[\"@DataDog/ruby-guild\", \"@DataDog/ci-app-libraries\"]" ) end @@ -78,7 +84,7 @@ def with_new_rspec_environment end.run end - expect(first_test_span.get_tag(Datadog::CI::Ext::Test::TAG_NAME)).to match(/example at .+/) + expect(first_test_span).to have_test_tag(:name, /example at .+/) end it "creates span for deeply nested examples" do @@ -111,8 +117,8 @@ def with_new_rspec_environment end expect(first_test_span.resource).to eq("1 2 3 4 5 6 7 8 9 10 foo") - expect(first_test_span.get_tag(Datadog::CI::Ext::Test::TAG_NAME)).to eq("1 2 3 4 5 6 7 8 9 10 foo") - expect(first_test_span.get_tag(Datadog::CI::Ext::Test::TAG_SUITE)).to eq("some nested test at #{spec.file_path}") + expect(first_test_span).to have_test_tag(:name, "1 2 3 4 5 6 7 8 9 10 foo") + expect(first_test_span).to have_test_tag(:suite, "some nested test at #{spec.file_path}") end it "creates spans for example with instrumentation" do @@ -128,16 +134,12 @@ def with_new_rspec_environment expect(test_spans).to have(1).items expect(custom_spans).to have(1).items - - custom_spans.each do |span| - expect(span.get_tag(Datadog::Tracing::Metadata::Ext::Distributed::TAG_ORIGIN)) - .to eq(Datadog::CI::Ext::Test::CONTEXT_ORIGIN) - end + expect(custom_spans).to all have_origin(Datadog::CI::Ext::Test::CONTEXT_ORIGIN) end context "catches failures" do def expect_failure - expect(first_test_span.get_tag(Datadog::CI::Ext::Test::TAG_STATUS)).to eq(Datadog::CI::Ext::Test::Status::FAIL) + expect(first_test_span).to have_fail_status expect(first_test_span).to have_error expect(first_test_span).to have_error_type expect(first_test_span).to have_error_message @@ -218,9 +220,10 @@ def expect_failure end.run end - expect(first_test_span.get_tag(Datadog::CI::Ext::Test::TAG_STATUS)).to eq(Datadog::CI::Ext::Test::Status::SKIP) - expect(first_test_span.get_tag(Datadog::CI::Ext::Test::TAG_NAME)).to eq("foo") - expect(first_test_span.get_tag(Datadog::CI::Ext::Test::TAG_SKIP_REASON)).to eq("No reason given") + expect(first_test_span).to have_test_tag(:name, "foo") + + expect(first_test_span).to have_skip_status + expect(first_test_span).to have_test_tag(:skip_reason, "No reason given") expect(first_test_span).not_to have_error end @@ -233,9 +236,10 @@ def expect_failure end.run end - expect(first_test_span.get_tag(Datadog::CI::Ext::Test::TAG_STATUS)).to eq(Datadog::CI::Ext::Test::Status::SKIP) - expect(first_test_span.get_tag(Datadog::CI::Ext::Test::TAG_NAME)).to eq("foo") - expect(first_test_span.get_tag(Datadog::CI::Ext::Test::TAG_SKIP_REASON)).to eq("reason in it block") + expect(first_test_span).to have_test_tag(:name, "foo") + + expect(first_test_span).to have_skip_status + expect(first_test_span).to have_test_tag(:skip_reason, "reason in it block") expect(first_test_span).not_to have_error end @@ -248,9 +252,10 @@ def expect_failure end.run end - expect(first_test_span.get_tag(Datadog::CI::Ext::Test::TAG_STATUS)).to eq(Datadog::CI::Ext::Test::Status::SKIP) - expect(first_test_span.get_tag(Datadog::CI::Ext::Test::TAG_NAME)).to eq("foo") - expect(first_test_span.get_tag(Datadog::CI::Ext::Test::TAG_SKIP_REASON)).to eq("No reason given") + expect(first_test_span).to have_test_tag(:name, "foo") + + expect(first_test_span).to have_skip_status + expect(first_test_span).to have_test_tag(:skip_reason, "No reason given") expect(first_test_span).not_to have_error end @@ -263,9 +268,10 @@ def expect_failure end.run end - expect(first_test_span.get_tag(Datadog::CI::Ext::Test::TAG_STATUS)).to eq(Datadog::CI::Ext::Test::Status::SKIP) - expect(first_test_span.get_tag(Datadog::CI::Ext::Test::TAG_NAME)).to eq("foo") - expect(first_test_span.get_tag(Datadog::CI::Ext::Test::TAG_SKIP_REASON)).to eq("Temporarily skipped with xit") + expect(first_test_span).to have_test_tag(:name, "foo") + + expect(first_test_span).to have_skip_status + expect(first_test_span).to have_test_tag(:skip_reason, "Temporarily skipped with xit") expect(first_test_span).not_to have_error end @@ -279,9 +285,10 @@ def expect_failure end.run end - expect(first_test_span.get_tag(Datadog::CI::Ext::Test::TAG_STATUS)).to eq(Datadog::CI::Ext::Test::Status::SKIP) - expect(first_test_span.get_tag(Datadog::CI::Ext::Test::TAG_NAME)).to eq("foo") - expect(first_test_span.get_tag(Datadog::CI::Ext::Test::TAG_SKIP_REASON)).to eq("No reason given") + expect(first_test_span).to have_test_tag(:name, "foo") + + expect(first_test_span).to have_skip_status + expect(first_test_span).to have_test_tag(:skip_reason, "No reason given") expect(first_test_span).not_to have_error end @@ -295,9 +302,10 @@ def expect_failure end.run end - expect(first_test_span.get_tag(Datadog::CI::Ext::Test::TAG_STATUS)).to eq(Datadog::CI::Ext::Test::Status::SKIP) - expect(first_test_span.get_tag(Datadog::CI::Ext::Test::TAG_NAME)).to eq("foo") - expect(first_test_span.get_tag(Datadog::CI::Ext::Test::TAG_SKIP_REASON)).to eq("reason") + expect(first_test_span).to have_test_tag(:name, "foo") + + expect(first_test_span).to have_skip_status + expect(first_test_span).to have_test_tag(:skip_reason, "reason") expect(first_test_span).not_to have_error end @@ -308,9 +316,10 @@ def expect_failure end.run end - expect(first_test_span.get_tag(Datadog::CI::Ext::Test::TAG_STATUS)).to eq(Datadog::CI::Ext::Test::Status::SKIP) - expect(first_test_span.get_tag(Datadog::CI::Ext::Test::TAG_NAME)).to eq("foo") - expect(first_test_span.get_tag(Datadog::CI::Ext::Test::TAG_SKIP_REASON)).to eq("Not yet implemented") + expect(first_test_span).to have_test_tag(:name, "foo") + + expect(first_test_span).to have_skip_status + expect(first_test_span).to have_test_tag(:skip_reason, "Not yet implemented") expect(first_test_span).not_to have_error end @@ -325,9 +334,10 @@ def expect_failure end.run end - expect(first_test_span.get_tag(Datadog::CI::Ext::Test::TAG_STATUS)).to eq(Datadog::CI::Ext::Test::Status::SKIP) - expect(first_test_span.get_tag(Datadog::CI::Ext::Test::TAG_NAME)).to eq("foo bar") - expect(first_test_span.get_tag(Datadog::CI::Ext::Test::TAG_SKIP_REASON)).to eq("Temporarily skipped with xcontext") + expect(first_test_span).to have_test_tag(:name, "foo bar") + + expect(first_test_span).to have_skip_status + expect(first_test_span).to have_test_tag(:skip_reason, "Temporarily skipped with xcontext") expect(first_test_span).not_to have_error end @@ -340,9 +350,10 @@ def expect_failure end.run end - expect(first_test_span.get_tag(Datadog::CI::Ext::Test::TAG_STATUS)).to eq(Datadog::CI::Ext::Test::Status::SKIP) - expect(first_test_span.get_tag(Datadog::CI::Ext::Test::TAG_NAME)).to eq("foo") - expect(first_test_span.get_tag(Datadog::CI::Ext::Test::TAG_SKIP_REASON)).to eq("did not fix the math yet") + expect(first_test_span).to have_test_tag(:name, "foo") + + expect(first_test_span).to have_skip_status + expect(first_test_span).to have_test_tag(:skip_reason, "did not fix the math yet") expect(first_test_span).to have_error end @@ -355,8 +366,9 @@ def expect_failure end.run end - expect(first_test_span.get_tag(Datadog::CI::Ext::Test::TAG_STATUS)).to eq(Datadog::CI::Ext::Test::Status::FAIL) - expect(first_test_span.get_tag(Datadog::CI::Ext::Test::TAG_NAME)).to eq("foo") + expect(first_test_span).to have_test_tag(:name, "foo") + + expect(first_test_span).to have_fail_status expect(first_test_span).to have_error expect(first_test_span).to have_error_message("Expected example to fail since it is pending, but it passed.") end @@ -371,9 +383,10 @@ def expect_failure end.run end - expect(first_test_span.get_tag(Datadog::CI::Ext::Test::TAG_STATUS)).to eq(Datadog::CI::Ext::Test::Status::SKIP) - expect(first_test_span.get_tag(Datadog::CI::Ext::Test::TAG_NAME)).to eq("foo") - expect(first_test_span.get_tag(Datadog::CI::Ext::Test::TAG_SKIP_REASON)).to eq("did not fix the math yet") + expect(first_test_span).to have_test_tag(:name, "foo") + + expect(first_test_span).to have_skip_status + expect(first_test_span).to have_test_tag(:skip_reason, "did not fix the math yet") expect(first_test_span).to have_error end end @@ -392,7 +405,8 @@ def expect_failure end.tap(&:run) end - expect(first_test_span.get_tag(Datadog::CI::Ext::Test::TAG_SOURCE_FILE)).to eq( + expect(first_test_span).to have_test_tag( + :source_file, "datadog/ci/contrib/rspec/instrumentation_spec.rb" ) end @@ -437,19 +451,16 @@ def rspec_session_run(with_failed_test: false, with_shared_test: false) expect(test_session_span).not_to be_nil - expect(test_session_span.type).to eq(Datadog::CI::Ext::AppTypes::TYPE_TEST_SESSION) - expect(test_session_span.get_tag(Datadog::CI::Ext::Test::TAG_SPAN_KIND)).to eq( - Datadog::CI::Ext::Test::SPAN_KIND_TEST - ) - expect(test_session_span.get_tag(Datadog::CI::Ext::Test::TAG_FRAMEWORK)).to eq( - Datadog::CI::Contrib::RSpec::Ext::FRAMEWORK - ) - expect(test_session_span.get_tag(Datadog::CI::Ext::Test::TAG_FRAMEWORK_VERSION)).to eq( + expect(test_session_span.type).to eq("test_session_end") + + expect(test_session_span).to have_test_tag(:span_kind, "test") + expect(test_session_span).to have_test_tag(:framework, "rspec") + expect(test_session_span).to have_test_tag( + :framework_version, Datadog::CI::Contrib::RSpec::Integration.version.to_s ) - expect(test_session_span.get_tag(Datadog::CI::Ext::Test::TAG_STATUS)).to eq( - Datadog::CI::Ext::Test::Status::PASS - ) + + expect(test_session_span).to have_pass_status end it "creates test module span" do @@ -457,21 +468,16 @@ def rspec_session_run(with_failed_test: false, with_shared_test: false) expect(test_module_span).not_to be_nil - expect(test_module_span.type).to eq(Datadog::CI::Ext::AppTypes::TYPE_TEST_MODULE) + expect(test_module_span.type).to eq("test_module_end") expect(test_module_span.name).to eq(test_command) - expect(test_module_span.get_tag(Datadog::CI::Ext::Test::TAG_SPAN_KIND)).to eq( - Datadog::CI::Ext::Test::SPAN_KIND_TEST - ) - expect(test_module_span.get_tag(Datadog::CI::Ext::Test::TAG_FRAMEWORK)).to eq( - Datadog::CI::Contrib::RSpec::Ext::FRAMEWORK - ) - expect(test_module_span.get_tag(Datadog::CI::Ext::Test::TAG_FRAMEWORK_VERSION)).to eq( + expect(test_module_span).to have_test_tag(:span_kind, "test") + expect(test_module_span).to have_test_tag(:framework, "rspec") + expect(test_module_span).to have_test_tag( + :framework_version, Datadog::CI::Contrib::RSpec::Integration.version.to_s ) - expect(test_module_span.get_tag(Datadog::CI::Ext::Test::TAG_STATUS)).to eq( - Datadog::CI::Ext::Test::Status::PASS - ) + expect(test_module_span).to have_pass_status end it "creates test suite span" do @@ -479,57 +485,43 @@ def rspec_session_run(with_failed_test: false, with_shared_test: false) expect(first_test_suite_span).not_to be_nil - expect(first_test_suite_span.type).to eq(Datadog::CI::Ext::AppTypes::TYPE_TEST_SUITE) + expect(first_test_suite_span.type).to eq("test_suite_end") expect(first_test_suite_span.name).to eq("SomeTest at #{spec.file_path}") - expect(test_module_span.get_tag(Datadog::CI::Ext::Test::TAG_SPAN_KIND)).to eq( - Datadog::CI::Ext::Test::SPAN_KIND_TEST - ) - expect(test_module_span.get_tag(Datadog::CI::Ext::Test::TAG_FRAMEWORK)).to eq( - Datadog::CI::Contrib::RSpec::Ext::FRAMEWORK - ) - expect(test_module_span.get_tag(Datadog::CI::Ext::Test::TAG_FRAMEWORK_VERSION)).to eq( + expect(first_test_suite_span).to have_test_tag(:span_kind, "test") + expect(first_test_suite_span).to have_test_tag(:framework, "rspec") + expect(first_test_suite_span).to have_test_tag( + :framework_version, Datadog::CI::Contrib::RSpec::Integration.version.to_s ) - expect(test_module_span.get_tag(Datadog::CI::Ext::Test::TAG_STATUS)).to eq( - Datadog::CI::Ext::Test::Status::PASS - ) + expect(first_test_suite_span).to have_pass_status end it "connects test to the session, module, and suite" do rspec_session_run - expect(first_test_span.get_tag(Datadog::CI::Ext::Test::TAG_TEST_SESSION_ID)).to eq(test_session_span.id.to_s) - expect(first_test_span.get_tag(Datadog::CI::Ext::Test::TAG_TEST_MODULE_ID)).to eq(test_module_span.id.to_s) - expect(first_test_span.get_tag(Datadog::CI::Ext::Test::TAG_TEST_SUITE_ID)).to eq(first_test_suite_span.id.to_s) + expect(first_test_span).to have_test_tag(:test_session_id, test_session_span.id.to_s) + expect(first_test_span).to have_test_tag(:test_module_id, test_module_span.id.to_s) + expect(first_test_span).to have_test_tag(:test_suite_id, first_test_suite_span.id.to_s) end context "with failures" do it "creates test session span with failed state" do rspec_session_run(with_failed_test: true) - expect(test_session_span).not_to be_nil - expect(test_session_span.get_tag(Datadog::CI::Ext::Test::TAG_STATUS)).to eq( - Datadog::CI::Ext::Test::Status::FAIL - ) + expect(test_session_span).to have_fail_status end it "creates test module span with failed state" do rspec_session_run(with_failed_test: true) - expect(test_module_span).not_to be_nil - expect(test_module_span.get_tag(Datadog::CI::Ext::Test::TAG_STATUS)).to eq( - Datadog::CI::Ext::Test::Status::FAIL - ) + expect(test_module_span).to have_fail_status end it "creates test suite span with failed state" do rspec_session_run(with_failed_test: true) - expect(first_test_suite_span).not_to be_nil - expect(first_test_suite_span.get_tag(Datadog::CI::Ext::Test::TAG_STATUS)).to eq( - Datadog::CI::Ext::Test::Status::FAIL - ) + expect(first_test_suite_span).to have_fail_status end end @@ -541,16 +533,15 @@ def rspec_session_run(with_failed_test: false, with_shared_test: false) expect(shared_test_spans).to have(2).items 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("SomeTest at #{spec.file_path}") + expect(shared_test_span).to have_test_tag(:suite, "SomeTest at #{spec.file_path}") - expect(shared_test_span.get_tag(Datadog::CI::Ext::Test::TAG_PARAMETERS)).to eq( + expect(shared_test_span).to have_test_tag( + :parameters, "{\"arguments\":{},\"metadata\":{\"scoped_id\":\"1:1:#{2 + index}:1\"}}" ) end - test_spans.each do |test_span| - expect(test_span.get_tag(Datadog::CI::Ext::Test::TAG_TEST_SUITE_ID)).to eq(first_test_suite_span.id.to_s) - end + expect(test_spans).to all have_test_tag(:test_suite_id, first_test_suite_span.id.to_s) end end @@ -588,8 +579,7 @@ def rspec_skipped_session_run end it "marks test session as passed" do - expect(test_session_span).not_to be_nil - expect(test_session_span.get_tag(Datadog::CI::Ext::Test::TAG_STATUS)).to eq(Datadog::CI::Ext::Test::Status::PASS) + expect(test_session_span).to have_pass_status end it "marks test suite as skipped" do @@ -597,8 +587,7 @@ def rspec_skipped_session_run suite_span.get_tag(Datadog::CI::Ext::Test::TAG_SUITE).include?("SkippedTest") end - expect(skipped_suite).not_to be_nil - expect(skipped_suite.get_tag(Datadog::CI::Ext::Test::TAG_STATUS)).to eq(Datadog::CI::Ext::Test::Status::SKIP) + expect(skipped_suite).to have_skip_status end end end diff --git a/spec/datadog/ci/test_visibility/flush_spec.rb b/spec/datadog/ci/test_visibility/flush_spec.rb index 034dd006..88cdf21e 100644 --- a/spec/datadog/ci/test_visibility/flush_spec.rb +++ b/spec/datadog/ci/test_visibility/flush_spec.rb @@ -25,10 +25,7 @@ is_expected.to eq(trace) # Expect each span to have an attached origin - trace.spans.each do |span| - expect(span.get_tag(Datadog::Tracing::Metadata::Ext::Distributed::TAG_ORIGIN)) - .to eq(trace.origin) - end + expect(trace.spans).to all have_origin(trace.origin) end end end diff --git a/spec/datadog/ci/test_visibility/recorder_spec.rb b/spec/datadog/ci/test_visibility/recorder_spec.rb index 190ee6a6..7232d36e 100644 --- a/spec/datadog/ci/test_visibility/recorder_spec.rb +++ b/spec/datadog/ci/test_visibility/recorder_spec.rb @@ -13,7 +13,7 @@ it "has all the environment tags" do environment_tags.each do |key, value| - expect(span_under_test.get_tag(key)).to eq(value) + expect(span_under_test).to have_test_tag(key, value) end end end @@ -23,8 +23,8 @@ it "span.kind is equal to test" do expect( - span_under_test.get_tag(Datadog::CI::Ext::Test::TAG_SPAN_KIND) - ).to eq(Datadog::CI::Ext::Test::SPAN_KIND_TEST) + span_under_test + ).to have_test_tag(:span_kind, "test") end end @@ -38,9 +38,9 @@ Datadog::CI::Ext::Test::TAG_RUNTIME_NAME, Datadog::CI::Ext::Test::TAG_RUNTIME_VERSION ].each do |tag| - expect(span_under_test.get_tag(tag)).not_to be_nil + expect(span_under_test).to have_test_tag(tag) end - expect(span_under_test.get_tag(Datadog::CI::Ext::Test::TAG_COMMAND)).to eq(test_command) + expect(span_under_test).to have_test_tag(:command, test_command) end end @@ -133,8 +133,8 @@ end it "sets the tags correctly" do - expect(subject.get_tag("test.framework")).to eq("my-framework") - expect(subject.get_tag("my.tag")).to eq("my_value") + expect(subject).to have_test_tag("test.framework", "my-framework") + expect(subject).to have_test_tag("my.tag", "my_value") end it_behaves_like "span with environment tags" @@ -150,8 +150,8 @@ end it "sets the tags correctly" do - expect(subject.get_tag("test.framework")).to eq("my-framework") - expect(subject.get_tag("my.tag")).to eq("my_value") + expect(subject).to have_test_tag("test.framework", "my-framework") + expect(subject).to have_test_tag("my.tag", "my_value") end it "sets correct resource and span type for the underlying tracer span" do @@ -193,17 +193,17 @@ end it "sets the provided tags correctly" do - expect(subject.get_tag("test.framework")).to eq("my-framework") - expect(subject.get_tag("my.tag")).to eq("my_value") + expect(subject).to have_test_tag("test.framework", "my-framework") + expect(subject).to have_test_tag("my.tag", "my_value") end it "does not connect the test span to the test session" do - expect(subject.get_tag(Datadog::CI::Ext::Test::TAG_TEST_SESSION_ID)).to be_nil + expect(subject).not_to have_test_tag(:test_session_id) end it "sets the test suite name as one of the tags" do - expect(subject.get_tag(Datadog::CI::Ext::Test::TAG_SUITE)).to eq(test_suite_name) - expect(subject.get_tag(Datadog::CI::Ext::Test::TAG_TEST_SUITE_ID)).to be_nil + expect(subject).to have_test_tag(:suite, test_suite_name) + expect(subject).not_to have_test_tag(:test_suite_id) end it_behaves_like "span with environment tags" @@ -237,14 +237,14 @@ end it "sets the provided tags correctly while inheriting some tags from the session" do - expect(subject.get_tag("test.framework")).to eq("my-framework") - expect(subject.get_tag("test.framework_version")).to eq("1.0") - expect(subject.get_tag("my.tag")).to eq("my_value") - expect(subject.get_tag("my.session.tag")).to be_nil + expect(subject).to have_test_tag("test.framework", "my-framework") + expect(subject).to have_test_tag("test.framework_version", "1.0") + expect(subject).to have_test_tag("my.tag", "my_value") + expect(subject).not_to have_test_tag("my.session.tag") end it "connects the test span to the test session" do - expect(subject.get_tag(Datadog::CI::Ext::Test::TAG_TEST_SESSION_ID).to_s).to eq(test_session.id.to_s) + expect(subject).to have_test_tag(:test_session_id, test_session.id.to_s) end it "starts a new trace" do @@ -281,14 +281,14 @@ end it "sets the provided tags correctly while inheriting some tags from the session" do - expect(subject.get_tag("test.framework")).to eq("my-framework") - expect(subject.get_tag("test.framework_version")).to eq("1.0") - expect(subject.get_tag("my.tag")).to eq("my_value") + expect(subject).to have_test_tag("test.framework", "my-framework") + expect(subject).to have_test_tag("test.framework_version", "1.0") + expect(subject).to have_test_tag("my.tag", "my_value") end it "connects the test span to the test module" do - expect(subject.get_tag(Datadog::CI::Ext::Test::TAG_TEST_MODULE_ID).to_s).to eq(test_module.id.to_s) - expect(subject.get_tag(Datadog::CI::Ext::Test::TAG_MODULE)).to eq(module_name) + expect(subject).to have_test_tag(:test_module_id, test_module.id.to_s) + expect(subject).to have_test_tag(:module, module_name) end context "when there is an active test suite" do @@ -301,8 +301,8 @@ end it "connects the test span to the test suite" do - expect(subject.get_tag(Datadog::CI::Ext::Test::TAG_TEST_SUITE_ID).to_s).to eq(test_suite.id.to_s) - expect(subject.get_tag(Datadog::CI::Ext::Test::TAG_SUITE)).to eq(test_suite_name) + expect(subject).to have_test_tag(:test_suite_id, test_suite.id.to_s) + expect(subject).to have_test_tag(:suite, test_suite_name) end end end @@ -323,19 +323,19 @@ subject { span } it "traces and finishes a test" do - expect(subject.get_tag(Datadog::CI::Ext::Test::TAG_NAME)).to eq(test_name) + expect(subject).to have_test_tag(:name, test_name) expect(subject.service).to eq(test_service) expect(subject.name).to eq(test_name) expect(subject.type).to eq(Datadog::CI::Ext::AppTypes::TYPE_TEST) end it "sets the provided tags correctly" do - expect(subject.get_tag("test.framework")).to eq("my-framework") - expect(subject.get_tag("my.tag")).to eq("my_value") + expect(subject).to have_test_tag("test.framework", "my-framework") + expect(subject).to have_test_tag("my.tag", "my_value") end it "sets the suite name in tags" do - expect(subject.get_tag(Datadog::CI::Ext::Test::TAG_SUITE)).to eq(test_suite_name) + expect(subject).to have_test_tag(:suite, test_suite_name) end it_behaves_like "span with environment tags" @@ -363,12 +363,12 @@ end it "sets the test session id" do - expect(subject.get_tag(Datadog::CI::Ext::Test::TAG_TEST_SESSION_ID).to_s).to eq(subject.id.to_s) + expect(subject).to have_test_tag(:test_session_id, subject.id.to_s) end it "sets the provided tags correctly" do - expect(subject.get_tag("test.framework")).to eq("my-framework") - expect(subject.get_tag("my.tag")).to eq("my_value") + expect(subject).to have_test_tag("test.framework", "my-framework") + expect(subject).to have_test_tag("my.tag", "my_value") end it_behaves_like "span with environment tags" @@ -399,20 +399,20 @@ end it "sets the test module id" do - expect(subject.get_tag(Datadog::CI::Ext::Test::TAG_TEST_MODULE_ID).to_s).to eq(subject.id.to_s) + expect(subject).to have_test_tag(:test_module_id, subject.id.to_s) end it "sets the test module tag" do - expect(subject.get_tag(Datadog::CI::Ext::Test::TAG_MODULE)).to eq(module_name) + expect(subject).to have_test_tag(:module, module_name) end it "doesn't connect the test module span to the test session" do - expect(subject.get_tag(Datadog::CI::Ext::Test::TAG_TEST_SESSION_ID)).to be_nil + expect(subject).not_to have_test_tag(:test_session_id) end it "sets the provided tags correctly" do - expect(subject.get_tag("test.framework")).to eq("my-framework") - expect(subject.get_tag("my.tag")).to eq("my_value") + expect(subject).to have_test_tag("test.framework", "my-framework") + expect(subject).to have_test_tag("my.tag", "my_value") end it_behaves_like "span with environment tags" @@ -444,14 +444,14 @@ end it "sets the provided tags correctly while inheriting some tags from the session" do - expect(subject.get_tag("test.framework")).to eq("my-framework") - expect(subject.get_tag("test.framework_version")).to eq("1.0") - expect(subject.get_tag("my.tag")).to eq("my_value") - expect(subject.get_tag("my.session.tag")).to be_nil + expect(subject).to have_test_tag("test.framework", "my-framework") + expect(subject).to have_test_tag("test.framework_version", "1.0") + expect(subject).to have_test_tag("my.tag", "my_value") + expect(subject).not_to have_test_tag("my.session.tag") end it "connects the test module span to the test session" do - expect(subject.get_tag(Datadog::CI::Ext::Test::TAG_TEST_SESSION_ID).to_s).to eq(test_session.id.to_s) + expect(subject).to have_test_tag(:test_session_id, test_session.id.to_s) end it "does not start a new trace" do @@ -487,20 +487,20 @@ end it "sets the provided tags correctly while inheriting some tags from the session" do - expect(subject.get_tag("test.framework_version")).to eq("1.0") - expect(subject.get_tag("my.tag")).to eq("my_value") - expect(subject.get_tag("my.session.tag")).to be_nil + expect(subject).to have_test_tag("test.framework_version", "1.0") + expect(subject).to have_test_tag("my.tag", "my_value") + expect(subject).not_to have_test_tag("my.session.tag") end it "sets the test suite context" do - expect(subject.get_tag(Datadog::CI::Ext::Test::TAG_TEST_SUITE_ID).to_s).to eq(subject.id.to_s) - expect(subject.get_tag(Datadog::CI::Ext::Test::TAG_SUITE)).to eq(suite_name) + expect(subject).to have_test_tag(:test_suite_id, subject.id.to_s) + expect(subject).to have_test_tag(:suite, suite_name) end it "sets test session and test module contexts" do - expect(subject.get_tag(Datadog::CI::Ext::Test::TAG_TEST_SESSION_ID).to_s).to eq(test_session.id.to_s) - expect(subject.get_tag(Datadog::CI::Ext::Test::TAG_TEST_MODULE_ID).to_s).to eq(test_module.id.to_s) - expect(subject.get_tag(Datadog::CI::Ext::Test::TAG_MODULE)).to eq(module_name) + expect(subject).to have_test_tag(:test_session_id, test_session.id.to_s) + expect(subject).to have_test_tag(:test_module_id, test_module.id.to_s) + expect(subject).to have_test_tag(:module, module_name) end it_behaves_like "span with environment tags" @@ -521,7 +521,7 @@ it "returns the already running test suite" do expect(subject.id).to eq(already_running_test_suite.id) - expect(subject.get_tag("my.tag")).to eq("my_value") + expect(subject).to have_test_tag("my.tag", "my_value") end end end diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index 66706ded..e5f22d74 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -10,7 +10,6 @@ # rspec helpers and matchers require_relative "support/gems_helpers" - require_relative "support/tracer_helpers" require_relative "support/span_helpers" diff --git a/spec/support/span_helpers.rb b/spec/support/span_helpers.rb index 74eed5ff..d3d81190 100644 --- a/spec/support/span_helpers.rb +++ b/spec/support/span_helpers.rb @@ -39,7 +39,7 @@ def self.define_have_error_tag(tag_name, tag) return true end - values_match? expected, @actual + !values_match?(expected, @actual) end def description_of(actual) # rubocop:disable Lint/NestedMethodDefinition @@ -52,12 +52,114 @@ def description_of(actual) # rubocop:disable Lint/NestedMethodDefinition define_have_error_tag(:stack, Datadog::Tracing::Metadata::Ext::Errors::TAG_STACK) define_have_error_tag(:type, Datadog::Tracing::Metadata::Ext::Errors::TAG_TYPE) - RSpec::Matchers.define :a_span_with do |expected| - match do |actual| - actual.instance_of?(Datadog::Tracing::Span) && - expected.all? do |key, value| - actual.__send__(key) == value - end + def test_tag_name(tag) + if tag.is_a?(Symbol) + Object.const_get("Datadog::CI::Ext::Test::TAG_#{tag.to_s.upcase}") + else + tag + end + end + + RSpec::Matchers.define "have_test_tag" do |*args| + match do |span| + tag = args.first + expected = args.last + + @tag_name = test_tag_name(tag) + @actual = span.get_tag(@tag_name) + + if args.count == 1 && @actual + # This condition enables the default matcher: + # expect(foo).to have_test_tag(:framework) + return true + end + + values_match? expected, @actual + end + + match_when_negated do |span| + tag = args.first + expected = args.last + + @tag_name = test_tag_name(tag) + @actual = span.get_tag(@tag_name) + + if args.count == 1 && @actual.nil? + # This condition enables the default matcher: + # expect(foo).to_not have_test_tag(:framework) + return true + end + + !values_match?(expected, @actual) + end + + def description_of(actual) # rubocop:disable Lint/NestedMethodDefinition + "Span with tag #{@tag_name} #{super}" + end + end + + RSpec::Matchers.define "have_origin" do |*args| + match do |span| + expected = args.first + + @actual = span.get_tag(Datadog::Tracing::Metadata::Ext::Distributed::TAG_ORIGIN) + + if args.empty? && @actual + # This condition enables the default matcher: + # expect(foo).to have_origin + return true + end + + values_match? expected, @actual + end + + def description_of(actual) # rubocop:disable Lint/NestedMethodDefinition + "Span with origin #{super}" + end + end + + ["skip", "pass", "fail"].each do |status| + RSpec::Matchers.define "have_#{status}_status" do + match do |span| + @actual = span.get_tag(Datadog::CI::Ext::Test::TAG_STATUS) + values_match? status, @actual + end + + def description_of(actual) # rubocop:disable Lint/NestedMethodDefinition + "Span with status #{super}" + end + end + end + + RSpec::Matchers.define "have_tag_values_no_order" do |*args| + match do |spans| + tag = args.first + expected = args.last + + @tag_name = test_tag_name(tag) + @actual = spans.map { |span| span.get_tag(@tag_name) }.sort + + values_match? expected.sort, @actual + end + + def description_of(actual) # rubocop:disable Lint/NestedMethodDefinition + "spans with tags #{@tag_name} #{super}" + end + end + + RSpec::Matchers.define "have_unique_tag_values_count" do |*args| + match do |spans| + tag = args.first + expected = args.last + + @tag_name = test_tag_name(tag) + @actual = spans.map { |span| span.get_tag(@tag_name) }.uniq.count + + values_match? expected, @actual + end + + def description_of(actual) # rubocop:disable Lint/NestedMethodDefinition + "spans with tags #{@tag_name} #{super}" end end end