Skip to content

Commit

Permalink
auto instrumentation tests for knapsack
Browse files Browse the repository at this point in the history
  • Loading branch information
anmarchenko committed Nov 25, 2024
1 parent c184f56 commit a8d7c5c
Show file tree
Hide file tree
Showing 2 changed files with 84 additions and 0 deletions.
4 changes: 4 additions & 0 deletions Rakefile
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,9 @@ TEST_METADATA = {
"knapsack_rspec" => {
"knapsack_pro-7-rspec-3" => "✅ 2.7 / ✅ 3.0 / ✅ 3.1 / ✅ 3.2 / ✅ 3.3 / ✅ 3.4 / ❌ jruby"
},
"knapsack_auto_instrument" => {
"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 @@ -157,6 +160,7 @@ namespace :spec do
ci_queue_minitest
ci_queue_rspec
knapsack_rspec
knapsack_auto_instrument
selenium timecop
].each do |contrib|
desc "" # "Explicitly hiding from `rake -T`"
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
RSpec.describe "Knapsack Pro runner when Datadog::CI is auto instrumented" do
let(:integration) { Datadog::CI::Contrib::Instrumentation.fetch_integration(:rspec) }

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

before do
require_relative "../../../../../lib/datadog/ci/auto_instrument"

require "fileutils"
require "knapsack_pro"

expect(Datadog::CI).to receive(:start_test_session).never
expect(Datadog::CI).to receive(:start_test_module).never
expect(Datadog::CI).to receive(:start_test_suite).never
expect(Datadog::CI).to receive(:start_test).never

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" => "144",
"KNAPSACK_PRO_TEST_SUITE_TOKEN_RSPEC" => "example_token",
"KNAPSACK_PRO_FIXED_QUEUE_SPLIT" => "true",
"KNAPSACK_PRO_QUEUE_ID" => nil
) do
KnapsackPro::Adapters::RSpecAdapter.bind
KnapsackPro::Runners::Queue::RSpecRunner.run("", devnull, devnull)
rescue ArgumentError
# suppress invalid API key error
end
end

# test session and module traced
expect(test_session_span).not_to be_nil
expect(test_session_span).to have_test_tag(:framework, "rspec")
expect(test_session_span).to have_test_tag(:framework_version, integration.version.to_s)

expect(test_module_span).not_to be_nil

# test session and module are failed
expect([test_session_span, test_module_span]).to all have_fail_status

# single test suite span
expect(test_suite_spans).to have(1).item
expect(test_suite_spans.first).to have_test_tag(:status, Datadog::CI::Ext::Test::Status::FAIL)
expect(test_suite_spans.first).to have_test_tag(
:suite,
"SomeTest at ./spec/datadog/ci/contrib/knapsack_rspec/suite_under_test/some_test_rspec.rb"
)

# there is test span for every test case
expect(test_spans).to have(2).items
# test spans belong to a single test suite
expect(test_spans).to have_unique_tag_values_count(:test_suite_id, 1)
expect(test_spans).to have_tag_values_no_order(
:status,
[Datadog::CI::Ext::Test::Status::FAIL, Datadog::CI::Ext::Test::Status::PASS]
)

# every test span is connected to test module and test session
expect(test_spans).to all have_test_tag(:test_module_id)
expect(test_spans).to all have_test_tag(:test_session_id)
end
end

0 comments on commit a8d7c5c

Please sign in to comment.