Skip to content

Commit 9547588

Browse files
committed
Add a basic e2e spec for tracing
1 parent 785e524 commit 9547588

6 files changed

Lines changed: 235 additions & 1 deletion

File tree

.rspec

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
--require spec_helper
2+
--format progress
3+
--color
4+
--order rand

Procfile

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
rails: cd spec/apps/rails-mini && bundle exec ruby app.rb
2+
svelte: cd spec/apps/svelte-mini && npm run dev

Rakefile

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
# frozen_string_literal: true
2+
3+
require "rake/clean"
4+
require_relative "lib/sentry/test/rake_tasks"
5+
6+
Sentry::Test::RakeTasks.define_spec_tasks()
7+
8+
# Define E2E test tasks
9+
namespace :spec do
10+
desc "Run end-to-end tests (requires background services)"
11+
task :e2e do
12+
puts "Running E2E tests..."
13+
puts "Note: Make sure background services are running:"
14+
puts " Foreman: foreman start"
15+
puts " Docker: cd .devcontainer && docker-compose --profile e2e up -d"
16+
17+
system("bundle exec rspec spec/features/ --format progress") || exit(1)
18+
end
19+
20+
desc "Run E2E tests with Docker Compose (devcontainer setup)"
21+
task :e2e_docker do
22+
puts "Starting E2E services with devcontainer Docker Compose and running tests..."
23+
24+
begin
25+
# Start services using devcontainer setup
26+
Dir.chdir('.devcontainer') do
27+
unless system("docker-compose --profile e2e up -d rails-mini svelte-mini")
28+
puts "Failed to start E2E services"
29+
exit(1)
30+
end
31+
end
32+
33+
# Wait for services to be ready
34+
puts "Waiting for services to be ready..."
35+
puts "Checking Rails mini app health..."
36+
37+
# Load environment variables
38+
env_vars = {}
39+
File.readlines('.devcontainer/.env').each do |line|
40+
next if line.strip.empty? || line.strip.start_with?('#')
41+
key, value = line.strip.split('=', 2)
42+
env_vars[key] = value.gsub(/^["']|["']$/, '') if key && value
43+
end
44+
45+
rails_port = env_vars['SENTRY_E2E_RAILS_APP_PORT'] || '5002'
46+
unless system("timeout 60 bash -c 'until curl -s http://localhost:#{rails_port}/health | grep -q \"ok\"; do echo \"Waiting for Rails app...\"; sleep 2; done'")
47+
puts "Rails mini app failed to become ready"
48+
Dir.chdir('.devcontainer') { system("docker-compose --profile e2e down") }
49+
exit(1)
50+
end
51+
puts "✅ Rails mini app is ready"
52+
53+
puts "Checking Svelte mini app health..."
54+
svelte_port = env_vars['SENTRY_E2E_SVELTE_APP_PORT'] || '5001'
55+
unless system("timeout 60 bash -c 'until curl -s http://localhost:#{svelte_port}/health | grep -q \"ok\"; do echo \"Waiting for Svelte app...\"; sleep 2; done'")
56+
puts "Svelte mini app failed to become ready"
57+
Dir.chdir('.devcontainer') { system("docker-compose --profile e2e down") }
58+
exit(1)
59+
end
60+
puts "✅ Svelte mini app is ready"
61+
puts "All services are healthy!"
62+
63+
# Run tests in sentry container with proper environment variables
64+
Dir.chdir('.devcontainer') do
65+
test_env_vars = {
66+
"SENTRY_E2E_RAILS_APP_URL" => env_vars['SENTRY_E2E_RAILS_APP_URL'] || "http://rails-mini:5000",
67+
"SENTRY_E2E_SVELTE_APP_URL" => env_vars['SENTRY_E2E_SVELTE_APP_URL'] || "http://svelte-mini:5001"
68+
}
69+
env_string = test_env_vars.map { |k, v| "#{k}=#{v}" }.join(" ")
70+
71+
unless system("#{env_string} docker-compose run --rm sentry bundle exec rspec spec/features/ --format progress")
72+
puts "E2E tests failed"
73+
exit(1)
74+
end
75+
end
76+
77+
ensure
78+
# Always stop services
79+
Dir.chdir('.devcontainer') { system("docker-compose --profile e2e down") }
80+
end
81+
end
82+
end
83+
84+
task default: :spec

sentry-ruby/spec/spec_helper.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,8 @@
2727

2828
require "sentry-ruby"
2929
require "sentry/test_helper"
30-
require "webmock/rspec"
3130

31+
require "webmock/rspec"
3232
require_relative "support/profiler"
3333
require_relative "support/stacktrace_test_fixture"
3434

spec/features/tracing_spec.rb

Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
1+
# frozen_string_literal: true
2+
3+
RSpec.describe "Tracing", type: :feature do
4+
it "works", js: false do
5+
visit "/error"
6+
7+
expect(page).to have_content("Svelte Mini App")
8+
expect(page).to have_button("Trigger Error")
9+
click_button "trigger-error-btn"
10+
expect(page).to have_content("Error:")
11+
12+
events_data = get_rails_events
13+
14+
expect(events_data["event_count"]).to be > 0
15+
16+
error_events = events_data["events"].select { |event| event["exception"] }
17+
expect(error_events).not_to be_empty
18+
19+
error_event = error_events.first
20+
exception_values = error_event.dig("exception", "values")
21+
expect(exception_values).not_to be_empty
22+
expect(exception_values.first["type"]).to eq("ZeroDivisionError")
23+
24+
transaction_events = events_data["events"].select { |event| event["type"] == "transaction" }
25+
26+
expect(error_event.dig("contexts", "trace")).not_to be_nil
27+
error_trace_id = error_event.dig("contexts", "trace", "trace_id")
28+
expect(error_trace_id).to match(/\A[a-f0-9]{32}\z/)
29+
30+
if transaction_events.any?
31+
transaction_event = transaction_events.first
32+
trace_context = transaction_event.dig("contexts", "trace")
33+
34+
expect(trace_context).not_to be_nil
35+
36+
transaction_trace_id = trace_context["trace_id"]
37+
38+
expect(transaction_trace_id).to match(/\A[a-f0-9]{32}\z/)
39+
expect(error_trace_id).to eq(transaction_trace_id)
40+
41+
if transaction_event["_meta"] && transaction_event["_meta"]["dsc"]
42+
dsc = transaction_event["_meta"]["dsc"]
43+
expect(dsc).to include("sample_rand")
44+
45+
sample_rand = dsc["sample_rand"]
46+
expect(sample_rand).to match(/\A0\.\d{6}\z/)
47+
end
48+
end
49+
50+
events_data["envelopes"].each do |envelope|
51+
envelope["items"].each do |item|
52+
if item["payload"] && item["payload"]["_meta"] && item["payload"]["_meta"]["dsc"]
53+
dsc = item["payload"]["_meta"]["dsc"]
54+
55+
if dsc["sample_rand"]
56+
expect(dsc["sample_rand"]).to match(/\A0\.\d{6}\z/)
57+
end
58+
end
59+
end
60+
end
61+
end
62+
63+
private
64+
65+
def get_rails_events
66+
log_file_path = File.join(Dir.pwd, "log", "sentry_debug_events.log")
67+
68+
return { "events" => [], "envelopes" => [], "event_count" => 0, "envelope_count" => 0 } unless File.exist?(log_file_path)
69+
70+
events = []
71+
File.readlines(log_file_path).each do |line|
72+
line = line.strip
73+
next if line.empty?
74+
75+
begin
76+
events << JSON.parse(line)
77+
rescue JSON::ParserError => e
78+
puts "Failed to parse line: #{line}, error: #{e.message}"
79+
end
80+
end
81+
82+
extracted_events = []
83+
envelopes = []
84+
85+
events.each do |event_data|
86+
envelopes << {
87+
"headers" => event_data["envelope_headers"],
88+
"items" => event_data["items"]
89+
}
90+
91+
# Extract actual events from envelope items
92+
event_data["items"].each do |item|
93+
if item["headers"]["type"] == "event"
94+
extracted_events << item["payload"]
95+
end
96+
end
97+
end
98+
99+
{
100+
"events" => extracted_events,
101+
"envelopes" => envelopes,
102+
"event_count" => extracted_events.length,
103+
"envelope_count" => envelopes.length,
104+
"raw_events" => events
105+
}
106+
end
107+
108+
def clear_rails_events
109+
log_file_path = File.join(Dir.pwd, "log", "sentry_debug_events.log")
110+
File.write(log_file_path, "") if File.exist?(log_file_path)
111+
end
112+
end

spec/spec_helper.rb

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
# frozen_string_literal: true
2+
3+
require "sentry-ruby"
4+
require "sentry/test_helper"
5+
6+
require "selenium-webdriver"
7+
8+
require "capybara"
9+
require "capybara/rspec"
10+
11+
Capybara.configure do |config|
12+
config.default_driver = :selenium_headless_chrome
13+
config.javascript_driver = :selenium_headless_chrome
14+
config.app_host = ENV.fetch("SENTRY_E2E_SVELTE_APP_URL", "http://localhost:4001")
15+
end
16+
17+
Capybara.register_driver :selenium_headless_chrome do |app|
18+
options = Selenium::WebDriver::Chrome::Options.new
19+
20+
options.add_argument("--headless")
21+
options.add_argument("--disable-dev-shm-usage")
22+
options.add_argument("--no-sandbox")
23+
options.add_argument("--disable-gpu")
24+
options.add_argument("--temp-profile")
25+
options.binary = "/usr/bin/chromium" if File.exist?("/usr/bin/chromium")
26+
27+
Capybara::Selenium::Driver.new(app, browser: :chrome, options: options)
28+
end
29+
30+
RSpec.configure do |config|
31+
config.include(Capybara::DSL, type: :e2e)
32+
end

0 commit comments

Comments
 (0)