Skip to content

Commit

Permalink
fix knapsack integration and remove duplicated knapsack instrumentati…
Browse files Browse the repository at this point in the history
…on specs
  • Loading branch information
anmarchenko committed Nov 14, 2024
1 parent c898a26 commit 8e7e063
Show file tree
Hide file tree
Showing 9 changed files with 43 additions and 112 deletions.
4 changes: 0 additions & 4 deletions Rakefile
Original file line number Diff line number Diff line change
Expand Up @@ -76,9 +76,6 @@ TEST_METADATA = {
"knapsack_rspec" => {
"knapsack_pro-7-rspec-3" => "✅ 2.7 / ✅ 3.0 / ✅ 3.1 / ✅ 3.2 / ✅ 3.3 / ✅ 3.4 / ❌ jruby"
},
"knapsack_rspec_go" => {
"knapsack_pro-7-rspec-3" => "✅ 2.7 / ✅ 3.0 / ✅ 3.1 / ✅ 3.2 / ✅ 3.3 / ✅ 3.4 / ❌ jruby"
},
"selenium" => {
"selenium-4-capybara-3" => "❌ 2.7 / ✅ 3.0 / ✅ 3.1 / ✅ 3.2 / ✅ 3.3 / ❌ 3.4 / ✅ jruby"
},
Expand Down Expand Up @@ -156,7 +153,6 @@ namespace :spec do
ci_queue_minitest
ci_queue_rspec
knapsack_rspec
knapsack_rspec_go
selenium timecop
].each do |contrib|
desc "" # "Explicitly hiding from `rake -T`"
Expand Down
19 changes: 15 additions & 4 deletions lib/datadog/ci/contrib/instrumentation.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,24 @@ def self.instrument(integration_name, options = {}, &block)
return unless integration.enabled

patch_results = integration.patch
return if patch_results == true
if patch_results == true
# try to patch dependant integrations (for example knapsack that depends on rspec)
dependants = integration.dependants
.map { |name| fetch_integration(name) }
.filter { |integration| integration.patchable? }

error_message = <<-ERROR
Datadog.logger.debug("Found dependent integrations for #{integration_name}: #{dependants}")

dependants.each do |dependent_integration|
dependent_integration.patch
end
else
error_message = <<-ERROR
Available?: #{patch_results[:available]}, Loaded?: #{patch_results[:loaded]},
Compatible?: #{patch_results[:compatible]}, Patchable?: #{patch_results[:patchable]}"
ERROR
Datadog.logger.warn("Unable to patch #{integration_name} (#{error_message})")
ERROR
Datadog.logger.warn("Unable to patch #{integration_name} (#{error_message})")
end
end

def self.fetch_integration(name)
Expand Down
7 changes: 7 additions & 0 deletions lib/datadog/ci/contrib/integration.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,13 @@ def self.registry
@registry
end

# List of integrations names that depend on this integration.
# Specify when you might need to automatically instrument other integrations (like test runner for the
# test framework).
def dependants
[]
end

# Version of the integration target code in the environment.
#
# This is the gem version, when the instrumentation target is a Ruby gem.
Expand Down
3 changes: 2 additions & 1 deletion lib/datadog/ci/contrib/knapsack/integration.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@ def version
end

def loaded?
!defined?(::KnapsackPro).nil? && !defined?(::KnapsackPro::Extensions::RSpecExtension).nil? &&
!defined?(::KnapsackPro).nil? &&
!defined?(::KnapsackPro::Extensions::RSpecExtension).nil? &&
!defined?(::KnapsackPro::Extensions::RSpecExtension::Runner).nil?
end

Expand Down
4 changes: 2 additions & 2 deletions lib/datadog/ci/contrib/knapsack/runner.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# frozen_string_literal: true

require_relative "../../../ext/test"
require_relative "../ext"
require_relative "../../ext/test"
require_relative "../rspec/ext"
require_relative "../instrumentation"

module Datadog
Expand Down
4 changes: 4 additions & 0 deletions lib/datadog/ci/contrib/rspec/integration.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,10 @@ module RSpec
class Integration < Contrib::Integration
MINIMUM_VERSION = Gem::Version.new("3.0.0")

def dependants
%i[knapsack]
end

def version
Gem.loaded_specs["rspec-core"]&.version
end
Expand Down
20 changes: 13 additions & 7 deletions spec/datadog/ci/contrib/knapsack_rspec/instrumentation_spec.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
require "knapsack_pro"
require "fileutils"

RSpec.describe "RSpec instrumentation with Knapsack Pro runner in queue mode" do
RSpec.describe "Knapsack Pro runner when Datadog::CI is configured during the knapsack run like in rspec_go rake task" do
let(:integration) { Datadog::CI::Contrib::Instrumentation.fetch_integration(:rspec) }

before do
Expand All @@ -12,28 +12,34 @@
expect(Datadog::CI).to receive(:start_test).never
end

include_context "CI mode activated" do
let(:integration_name) { :rspec }
end
include_context "CI mode activated"

before do
allow_any_instance_of(Datadog::Core::Remote::Negotiation).to(
receive(:endpoint?).with("/evp_proxy/v4/").and_return(true)
)

allow(Datadog::CI::Utils::TestRun).to receive(:command).and_return("knapsack:queue:rspec")

allow_any_instance_of(KnapsackPro::Runners::Queue::RSpecRunner).to receive(:test_file_paths).and_return(
["./spec/datadog/ci/contrib/knapsack_rspec/suite_under_test/some_test_rspec.rb"],
[]
)

# raise to prevent Knapsack from running Kernel.exit(0)
allow(KnapsackPro::Report).to receive(:save_node_queue_to_api).and_raise(ArgumentError)
end

it "instruments this rspec session" do
with_new_rspec_environment do
ClimateControl.modify(
"KNAPSACK_PRO_CI_NODE_BUILD_ID" => "142",
"KNAPSACK_PRO_CI_NODE_BUILD_ID" => "144",
"KNAPSACK_PRO_TEST_SUITE_TOKEN_RSPEC" => "example_token",
"KNAPSACK_PRO_FIXED_QUEUE_SPLIT" => "true"
"KNAPSACK_PRO_FIXED_QUEUE_SPLIT" => "true",
"KNAPSACK_PRO_QUEUE_ID" => nil
) do
KnapsackPro::Adapters::RSpecAdapter.bind
KnapsackPro::Runners::Queue::RSpecRunner.run("", devnull, devnull)
KnapsackPro::Runners::Queue::RSpecRunner.run("--require knapsack_helper", devnull, devnull)
rescue ArgumentError
# suppress invalid API key error
end
Expand Down
81 changes: 0 additions & 81 deletions spec/datadog/ci/contrib/knapsack_rspec_go/instrumentation_spec.rb

This file was deleted.

This file was deleted.

0 comments on commit 8e7e063

Please sign in to comment.