Skip to content

Commit 247e506

Browse files
committed
fix stack_trace spec for new architecture
1 parent 0ac9467 commit 247e506

File tree

1 file changed

+71
-59
lines changed

1 file changed

+71
-59
lines changed
+71-59
Original file line numberDiff line numberDiff line change
@@ -1,90 +1,78 @@
11
require 'datadog/appsec/stack_trace'
22
require 'support/thread_backtrace_helpers'
33

4-
RSpec.describe Datadog::AppSec::StackTrace do
5-
subject(:dd_stack_trace) { described_class.new(id, stack_trace, message: message) }
4+
RSpec.describe Datadog::AppSec::StackTrace::Collection do
5+
subject(:collection) { described_class.new(frames) }
66

77
before do
88
Datadog.configure do |c|
9-
c.appsec.stack_trace.enabled = stack_trace_enabled if stack_trace_enabled
109
c.appsec.stack_trace.max_depth = max_depth if max_depth
1110
c.appsec.stack_trace.max_depth_top_percent = max_depth_top_percent if max_depth_top_percent
12-
c.appsec.stack_trace.max_collect = max_collect if max_collect
1311
end
1412
end
1513

16-
let(:id) { 'b7e2370c-5d7e-4d32-990b-8812abf36588' }
1714
# "/app/spec/support/thread_backtrace_helpers.rb:12:in `block in locations_inside_nested_blocks'",
1815
# "/app/spec/support/thread_backtrace_helpers.rb:14:in `block (2 levels) in locations_inside_nested_blocks'",
1916
# "/app/spec/support/thread_backtrace_helpers.rb:16:in `block (3 levels) in locations_inside_nested_blocks'",
2017
# "/app/spec/support/thread_backtrace_helpers.rb:16:in `block (4 levels) in locations_inside_nested_blocks'",
2118
# "/app/spec/support/thread_backtrace_helpers.rb:16:in `block (5 levels) in locations_inside_nested_blocks'",
22-
let(:stack_trace) { ThreadBacktraceHelper.locations_inside_nested_blocks }
23-
let(:message) { 'This is a test message' }
19+
let(:frames) { ThreadBacktraceHelper.locations_inside_nested_blocks }
2420

2521
# This should use default values
26-
let(:stack_trace_enabled) { nil }
2722
let(:max_depth) { nil }
2823
let(:max_depth_top_percent) { nil }
29-
let(:max_collect) { nil }
3024

3125
describe '::new' do
3226
context 'with default values' do
3327
it 'creates a stack trace with default values' do
34-
expect(dd_stack_trace.id).to eq(id)
35-
expect(dd_stack_trace.id.encoding).to eq(Encoding::UTF_8)
36-
expect(dd_stack_trace.message).to eq(message)
37-
expect(dd_stack_trace.message.encoding).to eq(Encoding::UTF_8)
38-
expect(dd_stack_trace.frames.size).to eq(5)
28+
expect(collection.count).to eq(5)
3929
end
4030
end
4131

4232
context 'without values' do
43-
let(:id) { nil }
44-
let(:stack_trace) { nil }
45-
let(:message) { nil }
33+
let(:frames) { nil }
4634

4735
it 'does not cause an error' do
48-
expect { dd_stack_trace }.to_not raise_error
36+
expect { collection }.to_not raise_error
4937
end
5038
end
5139

5240
context 'with max_depth set to 4' do
5341
let(:max_depth) { 4 }
5442

5543
it 'creates a stack trace with 4 frames, 3 top' do
56-
expect(dd_stack_trace.frames.size).to eq(4)
57-
expect(dd_stack_trace.frames[2].text).to eq(stack_trace[2].to_s)
58-
expect(dd_stack_trace.frames[3].text).to eq(stack_trace[4].to_s)
44+
expect(collection.count).to eq(4)
45+
expect(collection[2].text).to eq(frames[2].to_s)
46+
expect(collection[3].text).to eq(frames[4].to_s)
5947
end
6048

6149
context 'with max_depth_top_percent set to 25' do
6250
let(:max_depth_top_percent) { 25 }
6351

6452
it 'creates a stack trace with 4 frames, 1 top' do
65-
expect(dd_stack_trace.frames.size).to eq(4)
66-
expect(dd_stack_trace.frames[0].text).to eq(stack_trace[0].to_s)
67-
expect(dd_stack_trace.frames[1].text).to eq(stack_trace[2].to_s)
53+
expect(collection.count).to eq(4)
54+
expect(collection[0].text).to eq(frames[0].to_s)
55+
expect(collection[1].text).to eq(frames[2].to_s)
6856
end
6957
end
7058

7159
context 'with max_depth_top_percent set to 100' do
7260
let(:max_depth_top_percent) { 100 }
7361

7462
it 'creates a stack trace with 4 top frames' do
75-
expect(dd_stack_trace.frames.size).to eq(4)
76-
expect(dd_stack_trace.frames[0].text).to eq(stack_trace[0].to_s)
77-
expect(dd_stack_trace.frames[3].text).to eq(stack_trace[3].to_s)
63+
expect(collection.count).to eq(4)
64+
expect(collection[0].text).to eq(frames[0].to_s)
65+
expect(collection[3].text).to eq(frames[3].to_s)
7866
end
7967
end
8068

8169
context 'with max_depth_top_percent set to 0' do
8270
let(:max_depth_top_percent) { 0 }
8371

8472
it 'creates a stack trace with 4 bottom frames' do
85-
expect(dd_stack_trace.frames.size).to eq(4)
86-
expect(dd_stack_trace.frames[0].text).to eq(stack_trace[1].to_s)
87-
expect(dd_stack_trace.frames[3].text).to eq(stack_trace[4].to_s)
73+
expect(collection.count).to eq(4)
74+
expect(collection[0].text).to eq(frames[1].to_s)
75+
expect(collection[3].text).to eq(frames[4].to_s)
8876
end
8977
end
9078
end
@@ -96,9 +84,9 @@
9684
let(:max_depth_top_percent) { 200 / 3.0 }
9785

9886
it 'creates a stack trace with 3 frames, 2 top' do
99-
expect(dd_stack_trace.frames.size).to eq(3)
100-
expect(dd_stack_trace.frames[1].text).to eq(stack_trace[1].to_s)
101-
expect(dd_stack_trace.frames[2].text).to eq(stack_trace[4].to_s)
87+
expect(collection.count).to eq(3)
88+
expect(collection[1].text).to eq(frames[1].to_s)
89+
expect(collection[2].text).to eq(frames[4].to_s)
10290
end
10391
end
10492
end
@@ -107,50 +95,31 @@
10795
let(:max_depth) { 0 }
10896

10997
it 'does not apply any limit' do
110-
expect(dd_stack_trace.frames.size).to eq(5)
98+
expect(collection.count).to eq(5)
11199
end
112100
end
113101
end
114102

115-
describe '#to_h' do
116-
# To simplify the test, we set max_depth to 1
117-
let(:max_depth) { 1 }
118-
119-
it 'returns a hash with correct values' do
120-
expect(dd_stack_trace.to_h).to eq(
121-
{
122-
language: 'ruby',
123-
id: id,
124-
message: message,
125-
frames: [
126-
{
127-
id: 0,
128-
text: stack_trace.first.to_s,
129-
file: stack_trace.first.absolute_path,
130-
line: stack_trace.first.lineno,
131-
function: stack_trace.first.label
132-
}
133-
]
134-
}
135-
)
103+
describe '#each' do
104+
it 'iterates over the frames' do
105+
expect(collection.each.to_a).to eq(collection.instance_variable_get('@frames'))
136106
end
137107
end
138108

139109
describe '#to_msgpack' do
140110
it 'returns a MessagePack object with correct values' do
141-
stack_trace_msgpack = dd_stack_trace.to_msgpack
111+
stack_trace_msgpack = collection.to_msgpack
142112

143113
expect(stack_trace_msgpack).to be_a(MessagePack::Packer)
144114
# As message pack converts keys to string,
145115
# we must also convert the keys of the hash to string in this test
146-
result = dd_stack_trace.to_h.transform_keys(&:to_s)
147-
result['frames'] = result['frames'].map { |f| f.transform_keys(&:to_s) }
116+
result = collection.map { |f| f.to_h.transform_keys(&:to_s) }
148117

149118
expect(MessagePack.unpack(stack_trace_msgpack.to_s)).to eq(result)
150119
end
151120
end
152121

153-
# add_stack_trace must be tested in integration tests, as it uses the active context
122+
# Integration tests done in rails integration test spec
154123
end
155124

156125
RSpec.describe Datadog::AppSec::StackTrace::Frame do
@@ -200,3 +169,46 @@
200169
end
201170
end
202171
end
172+
173+
RSpec.describe Datadog::AppSec::StackTrace::Representation do
174+
subject(:dd_stack_trace) { described_class.new(id, stack_trace, message: message) }
175+
176+
let(:id) { 'stack_id' }
177+
let(:stack_trace) { Datadog::AppSec::StackTrace::Collection.new(ThreadBacktraceHelper.locations_inside_nested_blocks) }
178+
let(:message) { 'Test message' }
179+
180+
describe '::new' do
181+
it 'creates a stack trace representation with correct values' do
182+
expect(dd_stack_trace.id).to eq(id)
183+
expect(dd_stack_trace.stack_trace).to eq(stack_trace)
184+
expect(dd_stack_trace.message).to eq(message)
185+
end
186+
end
187+
188+
describe '#to_h' do
189+
it 'returns a hash with correct values' do
190+
expect(dd_stack_trace.to_h).to eq(
191+
{
192+
language: 'ruby',
193+
id: id,
194+
message: message,
195+
frames: stack_trace.map(&:to_h)
196+
}
197+
)
198+
end
199+
end
200+
201+
describe '#to_msgpack' do
202+
it 'returns a MessagePack object with correct values' do
203+
stack_trace_msgpack = dd_stack_trace.to_msgpack
204+
205+
expect(stack_trace_msgpack).to be_a(MessagePack::Packer)
206+
# As message pack converts keys to string,
207+
# we must also convert the keys of the hash to string in this test
208+
result = dd_stack_trace.to_h.transform_keys(&:to_s)
209+
result['frames'] = result['frames'].map { |f| f.transform_keys(&:to_s) }
210+
211+
expect(MessagePack.unpack(stack_trace_msgpack.to_s)).to eq(result)
212+
end
213+
end
214+
end

0 commit comments

Comments
 (0)