debian-mirror-gitlab/spec/uploaders/workers/object_storage/migrate_uploads_worker_spec.rb

170 lines
4.7 KiB
Ruby
Raw Normal View History

2018-05-09 12:01:36 +05:30
require 'spec_helper'
describe ObjectStorage::MigrateUploadsWorker, :sidekiq do
shared_context 'sanity_check! fails' do
before do
expect(described_class).to receive(:sanity_check!).and_raise(described_class::SanityCheckError)
end
end
let(:model_class) { Project }
2018-10-15 14:42:47 +05:30
let(:uploads) { Upload.all }
2018-05-09 12:01:36 +05:30
let(:to_store) { ObjectStorage::Store::REMOTE }
2018-11-08 19:23:39 +05:30
def perform(uploads)
described_class.new.perform(uploads.ids, model_class.to_s, mounted_as, to_store)
rescue ObjectStorage::MigrateUploadsWorker::Report::MigrationFailures
# swallow
end
2018-10-15 14:42:47 +05:30
shared_examples "uploads migration worker" do
describe '.enqueue!' do
def enqueue!
described_class.enqueue!(uploads, Project, mounted_as, to_store)
end
2018-05-09 12:01:36 +05:30
2018-10-15 14:42:47 +05:30
it 'is guarded by .sanity_check!' do
expect(described_class).to receive(:perform_async)
expect(described_class).to receive(:sanity_check!)
2018-05-09 12:01:36 +05:30
2018-10-15 14:42:47 +05:30
enqueue!
end
2018-05-09 12:01:36 +05:30
2018-10-15 14:42:47 +05:30
context 'sanity_check! fails' do
include_context 'sanity_check! fails'
2018-05-09 12:01:36 +05:30
2018-10-15 14:42:47 +05:30
it 'does not enqueue a job' do
expect(described_class).not_to receive(:perform_async)
2018-05-09 12:01:36 +05:30
2018-10-15 14:42:47 +05:30
expect { enqueue! }.to raise_error(described_class::SanityCheckError)
end
2018-05-09 12:01:36 +05:30
end
end
2018-10-15 14:42:47 +05:30
describe '.sanity_check!' do
shared_examples 'raises a SanityCheckError' do
let(:mount_point) { nil }
2018-05-09 12:01:36 +05:30
2018-10-15 14:42:47 +05:30
it do
expect { described_class.sanity_check!(uploads, model_class, mount_point) }
.to raise_error(described_class::SanityCheckError)
end
2018-05-09 12:01:36 +05:30
end
2018-10-15 14:42:47 +05:30
before do
stub_const("WrongModel", Class.new)
end
2018-05-09 12:01:36 +05:30
2018-10-15 14:42:47 +05:30
context 'uploader types mismatch' do
let!(:outlier) { create(:upload, uploader: 'GitlabUploader') }
2018-05-09 12:01:36 +05:30
2018-10-15 14:42:47 +05:30
include_examples 'raises a SanityCheckError'
end
2018-05-09 12:01:36 +05:30
2018-10-15 14:42:47 +05:30
context 'model types mismatch' do
let!(:outlier) { create(:upload, model_type: 'WrongModel') }
2018-05-09 12:01:36 +05:30
2018-10-15 14:42:47 +05:30
include_examples 'raises a SanityCheckError'
2018-05-09 12:01:36 +05:30
end
2018-10-15 14:42:47 +05:30
context 'mount point not found' do
include_examples 'raises a SanityCheckError' do
let(:mount_point) { :potato }
end
end
2018-05-09 12:01:36 +05:30
end
2018-10-15 14:42:47 +05:30
describe '#perform' do
shared_examples 'outputs correctly' do |success: 0, failures: 0|
total = success + failures
2018-05-09 12:01:36 +05:30
2018-10-15 14:42:47 +05:30
if success > 0
it 'outputs the reports' do
expect(Rails.logger).to receive(:info).with(%r{Migrated #{success}/#{total} files})
2018-05-09 12:01:36 +05:30
2018-11-08 19:23:39 +05:30
perform(uploads)
2018-10-15 14:42:47 +05:30
end
2018-05-09 12:01:36 +05:30
end
2018-10-15 14:42:47 +05:30
if failures > 0
it 'outputs upload failures' do
expect(Rails.logger).to receive(:warn).with(/Error .* I am a teapot/)
2018-05-09 12:01:36 +05:30
2018-11-08 19:23:39 +05:30
perform(uploads)
2018-10-15 14:42:47 +05:30
end
2018-05-09 12:01:36 +05:30
end
end
2018-10-15 14:42:47 +05:30
it_behaves_like 'outputs correctly', success: 10
it 'migrates files' do
2018-11-08 19:23:39 +05:30
perform(uploads)
2018-05-09 12:01:36 +05:30
2018-10-15 14:42:47 +05:30
expect(Upload.where(store: ObjectStorage::Store::LOCAL).count).to eq(0)
end
2018-05-09 12:01:36 +05:30
2018-10-15 14:42:47 +05:30
context 'migration is unsuccessful' do
before do
allow_any_instance_of(ObjectStorage::Concern)
.to receive(:migrate!).and_raise(CarrierWave::UploadError, "I am a teapot.")
2018-05-09 12:01:36 +05:30
end
2018-10-15 14:42:47 +05:30
it_behaves_like 'outputs correctly', failures: 10
2018-05-09 12:01:36 +05:30
end
end
2018-10-15 14:42:47 +05:30
end
2018-05-09 12:01:36 +05:30
2018-10-15 14:42:47 +05:30
context "for AvatarUploader" do
let!(:projects) { create_list(:project, 10, :with_avatar) }
let(:mounted_as) { :avatar }
2018-05-09 12:01:36 +05:30
2018-10-15 14:42:47 +05:30
before do
stub_uploads_object_storage(AvatarUploader)
end
it_behaves_like "uploads migration worker"
2018-11-08 19:23:39 +05:30
describe "limits N+1 queries" do
it "to N*5" do
query_count = ActiveRecord::QueryRecorder.new { perform(uploads) }
more_projects = create_list(:project, 3, :with_avatar)
expected_queries_per_migration = 5 * more_projects.count
expect { perform(Upload.all) }.not_to exceed_query_limit(query_count).with_threshold(expected_queries_per_migration)
end
end
2018-10-15 14:42:47 +05:30
end
context "for FileUploader" do
let!(:projects) { create_list(:project, 10) }
let(:secret) { SecureRandom.hex }
let(:mounted_as) { nil }
2018-11-08 19:23:39 +05:30
def upload_file(project)
uploader = FileUploader.new(project)
uploader.store!(fixture_file_upload('spec/fixtures/doc_sample.txt'))
end
2018-10-15 14:42:47 +05:30
before do
stub_uploads_object_storage(FileUploader)
2018-11-08 19:23:39 +05:30
projects.map(&method(:upload_file))
2018-05-09 12:01:36 +05:30
end
2018-10-15 14:42:47 +05:30
it_behaves_like "uploads migration worker"
2018-11-08 19:23:39 +05:30
describe "limits N+1 queries" do
it "to N*5" do
query_count = ActiveRecord::QueryRecorder.new { perform(uploads) }
more_projects = create_list(:project, 3)
more_projects.map(&method(:upload_file))
expected_queries_per_migration = 5 * more_projects.count
expect { perform(Upload.all) }.not_to exceed_query_limit(query_count).with_threshold(expected_queries_per_migration)
end
end
2018-05-09 12:01:36 +05:30
end
end