From b522dfec9bcb60662822d0cefb8efea387d7e9fd Mon Sep 17 00:00:00 2001 From: st0012 Date: Sat, 2 Oct 2021 16:31:24 +0800 Subject: [PATCH] Setup active storage subscriber tests --- .../app/controllers/posts_controller.rb | 6 ++ .../tracing/active_storage_subscriber_spec.rb | 49 ++++++++++ .../spec/support/test_rails_app/app.rb | 86 +++++++++++++++++- .../test_rails_app/config/database.yml | 6 ++ .../support/test_rails_app/config/storage.yml | 4 + .../test_rails_app/public/sentry-logo.png | Bin 0 -> 1460 bytes 6 files changed, 148 insertions(+), 3 deletions(-) create mode 100644 sentry-rails/spec/sentry/rails/tracing/active_storage_subscriber_spec.rb create mode 100644 sentry-rails/spec/support/test_rails_app/config/database.yml create mode 100644 sentry-rails/spec/support/test_rails_app/config/storage.yml create mode 100644 sentry-rails/spec/support/test_rails_app/public/sentry-logo.png diff --git a/sentry-rails/examples/rails-6.0/app/controllers/posts_controller.rb b/sentry-rails/examples/rails-6.0/app/controllers/posts_controller.rb index 74402cda0..15888b5c2 100644 --- a/sentry-rails/examples/rails-6.0/app/controllers/posts_controller.rb +++ b/sentry-rails/examples/rails-6.0/app/controllers/posts_controller.rb @@ -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 diff --git a/sentry-rails/spec/sentry/rails/tracing/active_storage_subscriber_spec.rb b/sentry-rails/spec/sentry/rails/tracing/active_storage_subscriber_spec.rb new file mode 100644 index 000000000..7de6e90a9 --- /dev/null +++ b/sentry-rails/spec/sentry/rails/tracing/active_storage_subscriber_spec.rb @@ -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 diff --git a/sentry-rails/spec/support/test_rails_app/app.rb b/sentry-rails/spec/support/test_rails_app/app.rb index acd1a3577..b354ac490 100644 --- a/sentry-rails/spec/support/test_rails_app/app.rb +++ b/sentry-rails/spec/support/test_rails_app/app.rb @@ -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" @@ -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 @@ -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 @@ -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 @@ -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 @@ -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 @@ -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 diff --git a/sentry-rails/spec/support/test_rails_app/config/database.yml b/sentry-rails/spec/support/test_rails_app/config/database.yml new file mode 100644 index 000000000..edd919ddc --- /dev/null +++ b/sentry-rails/spec/support/test_rails_app/config/database.yml @@ -0,0 +1,6 @@ +default: &default + adapter: sqlite3 + +test: + <<: *default + database: db diff --git a/sentry-rails/spec/support/test_rails_app/config/storage.yml b/sentry-rails/spec/support/test_rails_app/config/storage.yml new file mode 100644 index 000000000..c7061bb25 --- /dev/null +++ b/sentry-rails/spec/support/test_rails_app/config/storage.yml @@ -0,0 +1,4 @@ +test: + service: Disk + root: <%= Rails.root.join("tmp/storage") %> + diff --git a/sentry-rails/spec/support/test_rails_app/public/sentry-logo.png b/sentry-rails/spec/support/test_rails_app/public/sentry-logo.png new file mode 100644 index 0000000000000000000000000000000000000000..dfce5d4d3cadaff4a4fdaf3108b33eeb4908ce29 GIT binary patch literal 1460 zcmV;l1xxygP)6h5ISEkTK7uL2bOge3XJ1E;R->Ki^a{GQ}hB1BMyDOM^@0@ebch2{n zdnc05=kpS-z+@m20umT3G>{OGVBrx05->bMK(-7ZD=TY~PG#glZdpD$5SMq`T7tH> zO&L$w8q)RQklg)qNuG;6D-Uyj%f$I%Y24MYDU%SSpvwZ}%FLXkQ)gw*d(E=H^Bn;= zbuZUS_NU`P+S{g#rDQ-rRqx091gDovznqb1v`R*XGjjFvybKI>DL~;WKLaHLa%yx^ zE_^?`F8Kgy@7sM6iHQ6>`xvBB7T&)wbxjr*Zp+OZe<=0EVl|RTejwXpHEIf7dG+#d zo@POtpwaOYS!rqA=~^7ggU4h?-HUSgXt&^Rzcp~AOCNzp-d4nOxQ+LAhimYE!E_?z>o0@4f`l!L&FS+oUkom$-X zSu7w(Dmuz(*r+rhh_Nl+ClQHgM`Io7KPmIq7YoxTwYNH2B~ur_a_#b>0D-YX2h)=1 zZ7(DZ-GX*sU0u^oVKNzrUxJxv*x{7cliVk$GN)B)hp3+1SyCFs87Y zfY75Tu~0k%N4uY(BKXFDf4By@IsA&*wpv|Vjc_0tx- zkBUR(Q7FryEZ!WDUfH#v7fxBFiaHX%{rSfe7^)t{<3-P2gMw2YJOSaDixO7Q?mnj9wA8P z$jf&15;9cTxy-brMtaxHEMR?CBW<^#3q)ydeT zP~`0w^y*?bqbF|rMuG3*8pQrN!OJeAuQLE~}Qw{{7Yu zY8#x7YXts}&o+9`RgU&-=64)L9rsmv$rlbe+#rRKT4D<=XMPh9lV`)*VE;@nN$uzCH#{bl_Hk$|An?%n-M_<6CXcmJ@r?h}rHn3I*$)(HrO-F}9`Zyt(T zTANC~1>U-8ci)-s!SMf>l&87p5Rj*teFxtmAR!