Skip to content

Commit

Permalink
[NO-TICKET] Minor: Use fiddle to simulate crash in spec
Browse files Browse the repository at this point in the history
**What does this PR do?**

This PR adds an additional test case to the profiling crashtracker
as discussed in
#3384 (comment)
(thanks @lloeki for the suggestion).

**Motivation:**

Improve test coverage for the feature.

**Additional Notes:**

The diff looks more noiser than it is because of whitespace changes,
I recommend reviewing this PR without them.

**How to test the change?**

Check that the new test passes!
  • Loading branch information
ivoanjo committed Jun 12, 2024
1 parent bd08222 commit ac8e06f
Showing 1 changed file with 28 additions and 21 deletions.
49 changes: 28 additions & 21 deletions spec/datadog/profiling/crashtracker_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
require 'datadog/profiling/crashtracker'

require 'webrick'
require 'fiddle'

RSpec.describe Datadog::Profiling::Crashtracker do
before do
Expand Down Expand Up @@ -175,33 +176,39 @@

let(:exporter_configuration) { [:agent, "http://#{hostname}:#{port}"] }

it 'reports crashes via http' do
fork_expectations = proc do |status:, stdout:, stderr:|
expect(Signal.signame(status.termsig)).to eq('SEGV').or eq('ABRT')
expect(stderr).to include('[BUG] Segmentation fault')
end
[:fiddle, :signal].each do |trigger|
it "reports crashes via http when app crashes with #{trigger}" do
fork_expectations = proc do |status:, stdout:, stderr:|
expect(Signal.signame(status.termsig)).to eq('SEGV').or eq('ABRT')
expect(stderr).to include('[BUG] Segmentation fault')
end

expect_in_fork(fork_expectations: fork_expectations) do
crashtracker.start
expect_in_fork(fork_expectations: fork_expectations) do
crashtracker.start

Process.kill('SEGV', Process.pid)
end
if trigger == :fiddle
Fiddle.free(42)
else
Process.kill('SEGV', Process.pid)
end
end

crash_report = JSON.parse(request.body, symbolize_names: true)[:payload].first
crash_report = JSON.parse(request.body, symbolize_names: true)[:payload].first

expect(crash_report[:stack_trace]).to_not be_empty
expect(crash_report[:tags]).to include('signum:11', 'signame:SIGSEGV')
expect(crash_report[:stack_trace]).to_not be_empty
expect(crash_report[:tags]).to include('signum:11', 'signame:SIGSEGV')

crash_report_message = JSON.parse(crash_report[:message], symbolize_names: true)
crash_report_message = JSON.parse(crash_report[:message], symbolize_names: true)

expect(crash_report_message[:metadata]).to include(
profiling_library_name: 'dd-trace-rb',
profiling_library_version: Datadog::VERSION::STRING,
family: 'ruby',
tags: ['tag1:value1', 'tag2:value2'],
)
expect(crash_report_message[:files][:'/proc/self/maps']).to_not be_empty
expect(crash_report_message[:os_info]).to_not be_empty
expect(crash_report_message[:metadata]).to include(
profiling_library_name: 'dd-trace-rb',
profiling_library_version: Datadog::VERSION::STRING,
family: 'ruby',
tags: ['tag1:value1', 'tag2:value2'],
)
expect(crash_report_message[:files][:'/proc/self/maps']).to_not be_empty
expect(crash_report_message[:os_info]).to_not be_empty
end
end
end
end

0 comments on commit ac8e06f

Please sign in to comment.