Skip to content

Commit

Permalink
Setup active storage subscriber tests
Browse files Browse the repository at this point in the history
  • Loading branch information
st0012 committed Oct 2, 2021
1 parent 35899ac commit b522dfe
Show file tree
Hide file tree
Showing 6 changed files with 148 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,12 @@ def index
# GET /posts/1
# GET /posts/1.json
def show
@post.cover.attach(
io: File.open(File.join(Rails.root, 'public', 'favicon.ico')),
filename: 'favicon.ico',
identify: false
)
@post
end

# GET /posts/new
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
require "spec_helper"

RSpec.describe Sentry::Rails::Tracing::ActiveStorageSubscriber, :subscriber, type: :request, skip: Rails.version.to_f <= 5.2 do
let(:transport) do
Sentry.get_current_client.transport
end

context "when transaction is sampled" do
before do
make_basic_app do |config|
config.traces_sample_rate = 1.0
config.rails.tracing_subscribers = [described_class]
end
end

it "records the upload event" do
p = Post.create!
get "/posts/#{p.id}/attach"

expect(response).to have_http_status(:ok)
expect(transport.events.count).to eq(1)

transaction = transport.events.first.to_hash
expect(transaction[:type]).to eq("transaction")
expect(transaction[:spans].count).to eq(1)

span = transaction[:spans][0]
expect(span[:op]).to eq("service_upload.active_storage")
expect(span[:description]).to eq("Disk")
expect(span.dig(:data, :key)).to eq(p.cover.key)
expect(span[:trace_id]).to eq(transaction.dig(:contexts, :trace, :trace_id))
end
end

context "when transaction is not sampled" do
before do
make_basic_app
end

it "doesn't record spans" do
p = Post.create!
get "/posts/#{p.id}/attach"

expect(response).to have_http_status(:ok)

expect(transport.events.count).to eq(0)
end
end
end
86 changes: 83 additions & 3 deletions sentry-rails/spec/support/test_rails_app/app.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
ENV["RAILS_ENV"] = "test"

require 'rails'
require "active_record"
require "active_job/railtie"
require "active_storage/engine" if Rails.version.to_f >= 5.2
require "action_view/railtie"
require "action_controller/railtie"

# require "action_mailer/railtie"
# require "action_cable/engine"
# require "sprockets/railtie"
Expand All @@ -18,6 +23,34 @@ class TestApp < Rails::Application
ActiveRecord::Base.logger = Logger.new(nil)

ActiveRecord::Schema.define do
create_table "active_storage_attachments", force: :cascade do |t|
t.string "name", null: false
t.string "record_type", null: false
t.integer "record_id", null: false
t.integer "blob_id", null: false
t.datetime "created_at", null: false
t.index ["blob_id"], name: "index_active_storage_attachments_on_blob_id"
t.index ["record_type", "record_id", "name", "blob_id"], name: "index_active_storage_attachments_uniqueness", unique: true
end

create_table "active_storage_blobs", force: :cascade do |t|
t.string "key", null: false
t.string "filename", null: false
t.string "content_type"
t.text "metadata"
t.string "service_name"
t.bigint "byte_size", null: false
t.string "checksum", null: false
t.datetime "created_at", null: false
t.index ["key"], name: "index_active_storage_blobs_on_key", unique: true
end

create_table "active_storage_variant_records", force: :cascade do |t|
t.integer "blob_id", null: false
t.string "variation_digest", null: false
t.index ["blob_id", "variation_digest"], name: "index_active_storage_variant_records_uniqueness", unique: true
end

create_table :posts, force: true do |t|
end

Expand All @@ -26,11 +59,26 @@ class TestApp < Rails::Application
end
end

class Post < ActiveRecord::Base
class ApplicationRecord < ActiveRecord::Base
self.abstract_class = true

if defined?(ActiveStorage)
if Rails.version.to_f < 6.0
extend ActiveStorage::Attached::Macros
else
include ActiveStorage::Attached::Model
include ActiveStorage::Reflection::ActiveRecordExtensions
ActiveRecord::Reflection.singleton_class.prepend(ActiveStorage::Reflection::ReflectionExtension)
end
end
end

class Post < ApplicationRecord
has_many :comments
has_one_attached :cover if defined?(ActiveStorage)
end

class Comment < ActiveRecord::Base
class Comment < ApplicationRecord
belongs_to :post
end

Expand All @@ -45,6 +93,22 @@ def show

render plain: p.id
end

def attach
p = Post.find(params[:id])

attach_params = {
io: File.open(File.join(Rails.root, 'public', 'sentry-logo.png')),
filename: 'sentry-logo.png',
}

unless Rails.version.to_f < 6.1
attach_params[:service_name] = "test"
end

p.cover.attach(attach_params)

render plain: p.id end
end

class HelloController < ActionController::Base
Expand Down Expand Up @@ -102,6 +166,15 @@ def self.name
app.config.hosts = nil
app.config.secret_key_base = "test"

if ::Rails.version.to_f >= 5.2
app.config.active_storage.service = :test
end

if ::Rails.version.to_f == 6.0
app.config.active_record.sqlite3 = ActiveSupport::OrderedOptions.new
app.config.active_record.sqlite3.represent_boolean_as_integer = nil
end

# Usually set for us in production.rb
app.config.eager_load = true
app.routes.append do
Expand All @@ -111,7 +184,11 @@ def self.name
get "/not_found", :to => "hello#not_found"
get "/world", to: "hello#world"
get "/with_custom_instrumentation", to: "hello#with_custom_instrumentation"
resources :posts, only: [:index, :show]
resources :posts, only: [:index, :show] do
member do
get :attach
end
end
get "500", to: "hello#reporting"
root :to => "hello#world"
end
Expand All @@ -132,6 +209,9 @@ def self.name

app.initialize!

Post.all.to_a # to run the sqlte version query first
Sentry.get_current_scope.clear_breadcrumbs # and then clear breadcrumbs in case the above query is recorded

Rails.application = app
app
end
6 changes: 6 additions & 0 deletions sentry-rails/spec/support/test_rails_app/config/database.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
default: &default
adapter: sqlite3

test:
<<: *default
database: db
4 changes: 4 additions & 0 deletions sentry-rails/spec/support/test_rails_app/config/storage.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
test:
service: Disk
root: <%= Rails.root.join("tmp/storage") %>

Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit b522dfe

Please sign in to comment.