Skip to content

Commit

Permalink
Merge branch 'tonycthsu/spec-for-crash'
Browse files Browse the repository at this point in the history
  • Loading branch information
TonyCTHsu committed Aug 13, 2024
2 parents cd7f2ec + 62057e8 commit 2c547ac
Show file tree
Hide file tree
Showing 8 changed files with 175 additions and 4 deletions.
49 changes: 45 additions & 4 deletions .github/workflows/vaccine.yml
Original file line number Diff line number Diff line change
Expand Up @@ -25,27 +25,56 @@ jobs:
engine:
- name: ruby
version: '3.4'
validate: false
- name: ruby
version: '3.3'
validate: false
- name: ruby
version: '3.2'
validate: true
- name: ruby
version: '3.1'
validate: true
- name: ruby
version: '3.0'
validate: true
- name: ruby
version: '2.7'
validate: true
- name: ruby
version: '2.6'
validate: false
- name: ruby
version: '2.5'
validate: false
- name: ruby
version: '2.4'
validate: false
- name: ruby
version: '2.3'
validate: false
steps:
- uses: actions/checkout@v4
- uses: ruby/setup-ruby@v1
with:
ruby-version: .ruby-version
- run: bundle install
# Prepare mock server 'catadog', https://github.com/DataDog/catadog
- uses: actions/checkout@v4
with:
repository: datadog/catadog
ref: tonycthsu/daemon
path: catadog
- run: mkdir $GITHUB_WORKSPACE/records
- run: |
cd catadog
bundle install
bundle exec catadog -h 0.0.0.0 --mock :Sink --no-forward --daemon --record $GITHUB_WORKSPACE/records
- run: curl -I http://localhost:8128/catadog

# Prepare vaccine
- name: Prepare vaccine volume
run: |
docker create \
--name init \
-v vaccine:/datadog-init/package \
Expand All @@ -55,19 +84,31 @@ jobs:
--tag patient:${{ matrix.engine.name }}-${{ matrix.engine.version }} \
--file src/${{ matrix.engine.name }}/${{ matrix.engine.version }}/Dockerfile \
src
- run: |
- name: Inject vaccine
run: |
docker run -d \
--name inoculation \
--network host \
-v vaccine:/vaccine \
-e RUBYOPT=-r/vaccine/host_inject \
-e DD_TRACE_DEBUG=true \
-e DD_INSTRUMENTATION_TELEMETRY_ENABLED=false \
-e DD_AGENT_HOST=localhost \
-e DD_TRACE_AGENT_PORT=8128 \
-p 3000:3000 \
patient:${{ matrix.engine.name }}-${{ matrix.engine.version }} \
"${{ matrix.command }} -b 0.0.0.0"
- run: sleep 10
- run: docker logs inoculation
- run: curl -v --retry 10 -f --retry-all-errors --retry-delay 5 -s -o /dev/null http://localhost:3000
- name: Generate traces
run: curl -v --retry 12 -f --retry-all-errors --retry-delay 5 -s -o /dev/null http://localhost:3000
- name: Inspect injection logs
run: docker logs inoculation
- name: Crash puma server
run: docker exec inoculation bash -c "pgrep -f puma | xargs kill -SEGV" || true
- run: sleep 2 && ls -al $GITHUB_WORKSPACE/records
- run: docker logs inoculation
- if: ${{ matrix.engine.validate }}
name: Validate crash report
run: RECORDS_DIR=$GITHUB_WORKSPACE/records bundle exec rspec

notify:
runs-on: ubuntu-latest
Expand Down
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/records
2 changes: 2 additions & 0 deletions .rspec
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
--require spec_helper
--format documentation
1 change: 1 addition & 0 deletions .ruby-version
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
3.2.2
4 changes: 4 additions & 0 deletions Gemfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
source "https://rubygems.org"

gem "pry"
gem "rspec"
33 changes: 33 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
GEM
remote: https://rubygems.org/
specs:
coderay (1.1.3)
diff-lcs (1.5.1)
method_source (1.1.0)
pry (0.14.2)
coderay (~> 1.1)
method_source (~> 1.0)
rspec (3.13.0)
rspec-core (~> 3.13.0)
rspec-expectations (~> 3.13.0)
rspec-mocks (~> 3.13.0)
rspec-core (3.13.0)
rspec-support (~> 3.13.0)
rspec-expectations (3.13.1)
diff-lcs (>= 1.2.0, < 2.0)
rspec-support (~> 3.13.0)
rspec-mocks (3.13.1)
diff-lcs (>= 1.2.0, < 2.0)
rspec-support (~> 3.13.0)
rspec-support (3.13.1)

PLATFORMS
arm64-darwin-22
ruby

DEPENDENCIES
pry
rspec

BUNDLED WITH
2.5.16
71 changes: 71 additions & 0 deletions spec/records_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
require "json"

RSpec.describe "Records" do
def records
Dir.glob("#{ENV["RECORDS_DIR"] || "records"}/**.json")
end

describe "trace records" do
it do
trace_report = records.first

data = JSON.parse(File.read(trace_report), symbolize_names: true)

expect(data).to a_hash_including(
request: hash_including(
method: "POST",
path: "/v0.4/traces",
headers: hash_including("content-type": "application/msgpack"),
body: an_instance_of(Array).and(having_attributes(length: 1))
)
)

render_span, controller_span, rack_span = trace = data[:request][:body].first

expect(trace.map { |s| s[:trace_id] }.uniq.length).to eq(1)

expect(render_span).to a_hash_including(name: "rails.render_template")
expect(controller_span).to a_hash_including(name: "rails.action_controller")
expect(rack_span).to a_hash_including(name: "rack.request")
end
end

describe "crash report records" do
it do
crash_report = records.last

data = JSON.parse(File.read(crash_report), symbolize_names: true)

expect(data).to a_hash_including(
request: a_hash_including(
method: "POST",
path: "/telemetry/proxy/api/v2/apmtelemetry",
body: a_hash_including(
request_type: "logs",
payload: an_instance_of(Array)
)
)
)

payloads = data.dig(:request, :body, :payload)
expect(payloads.length).to eq(1)

payload = payloads.first
expect(payload).to a_hash_including(
message: an_instance_of(String),
level: "ERROR",
tags: a_string_matching(/SIGSEGV/)
)

message = JSON.parse(payload[:message], symbolize_names: true)
expect(message).to a_hash_including(
:additional_stacktraces,
:files,
metadata: a_hash_including(
:tags,
family: "ruby"
)
)
end
end
end
18 changes: 18 additions & 0 deletions spec/spec_helper.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
require "pry"

RSpec.configure do |config|
config.expect_with :rspec do |expectations|
expectations.include_chain_clauses_in_custom_matcher_descriptions = true
end

config.mock_with :rspec do |mocks|
mocks.verify_partial_doubles = true
end

# This option will default to `:apply_to_host_groups` in RSpec 4 (and will
# have no way to turn it off -- the option exists only for backwards
# compatibility in RSpec 3). It causes shared context metadata to be
# inherited by the metadata hash of host groups and examples, rather than
# triggering implicit auto-inclusion in groups with matching metadata.
config.shared_context_metadata_behavior = :apply_to_host_groups
end

0 comments on commit 2c547ac

Please sign in to comment.