|
| 1 | +# frozen_string_literal: true |
| 2 | + |
| 3 | +RSpec.describe BlobService do |
| 4 | + describe '.purge_blobs_with_variants' do |
| 5 | + # Blobs are created against the real (disk) service, then flipped to openstack |
| 6 | + # and purged against a mocked openstack service — installed by the subject, |
| 7 | + # once every blob exists. |
| 8 | + subject(:purge_blobs_with_variants) do |
| 9 | + service = double('openstack service', container: 'bucket', name: :openstack) |
| 10 | + allow(service).to receive(:client).and_return(client) # reached via service.send(:client) |
| 11 | + allow(ActiveStorage::Blob).to receive(:service).and_return(service) |
| 12 | + BlobService.purge_blobs_with_variants([blob.id]) |
| 13 | + end |
| 14 | + |
| 15 | + let(:client) { double('openstack client') } |
| 16 | + |
| 17 | + let!(:blob) do |
| 18 | + ActiveStorage::Blob.create_and_upload!(io: StringIO.new("old"), filename: "old.txt", content_type: "text/plain") |
| 19 | + .tap { it.update_column(:service_name, 'openstack') } |
| 20 | + end |
| 21 | + |
| 22 | + before do |
| 23 | + allow(client).to receive(:delete_multiple_objects).and_return(double(body: { 'Errors' => [] })) |
| 24 | + end |
| 25 | + |
| 26 | + it 'drops the blob row' do |
| 27 | + expect { purge_blobs_with_variants }.to change { ActiveStorage::Blob.exists?(id: blob.id) }.from(true).to(false) |
| 28 | + end |
| 29 | + |
| 30 | + it 'bulk-deletes the parent file (safety net against a missed X-Delete-At)' do |
| 31 | + expect(client).to receive(:delete_multiple_objects) |
| 32 | + .with('bucket', [blob.key]) |
| 33 | + .and_return(double(body: { 'Errors' => [] })) |
| 34 | + |
| 35 | + purge_blobs_with_variants |
| 36 | + end |
| 37 | + |
| 38 | + context 'when the blob is an image with variants' do |
| 39 | + let!(:variant_blob) do |
| 40 | + ActiveStorage::Blob.create_and_upload!(io: StringIO.new("variant"), filename: "v.png", content_type: "image/png") |
| 41 | + .tap { it.update_column(:service_name, 'openstack') } |
| 42 | + end |
| 43 | + let!(:variant_record) { ActiveStorage::VariantRecord.create!(blob:, variation_digest: "digest") } |
| 44 | + let!(:image_attachment) { ActiveStorage::Attachment.create!(name: "image", record: variant_record, blob: variant_blob) } |
| 45 | + |
| 46 | + it 'bulk-deletes both variant and parent files, then drops every row' do |
| 47 | + expect(client).to receive(:delete_multiple_objects) |
| 48 | + .with('bucket', match_array([variant_blob.key, blob.key])) |
| 49 | + .and_return(double(body: { 'Errors' => [] })) |
| 50 | + |
| 51 | + purge_blobs_with_variants |
| 52 | + |
| 53 | + expect(ActiveStorage::Blob.where(id: [blob.id, variant_blob.id])).not_to exist |
| 54 | + expect(ActiveStorage::VariantRecord.where(id: variant_record.id)).not_to exist |
| 55 | + expect(ActiveStorage::Attachment.where(id: image_attachment.id)).not_to exist |
| 56 | + end |
| 57 | + |
| 58 | + it 'reports per-object bulk delete errors to Sentry' do |
| 59 | + errors = [["bucket/#{variant_blob.key}", "409 Conflict"]] |
| 60 | + allow(client).to receive(:delete_multiple_objects).and_return(double(body: { 'Errors' => errors })) |
| 61 | + |
| 62 | + expect(Sentry).to receive(:capture_message).with("Bulk delete errors", extra: { errors: }) |
| 63 | + |
| 64 | + purge_blobs_with_variants |
| 65 | + end |
| 66 | + end |
| 67 | + |
| 68 | + context 'when the blob is stored on another service' do |
| 69 | + let!(:blob) do |
| 70 | + ActiveStorage::Blob.create_and_upload!(io: StringIO.new("other"), filename: "other.txt", content_type: "text/plain") |
| 71 | + end |
| 72 | + |
| 73 | + it 'leaves its file and row untouched' do |
| 74 | + expect(client).not_to receive(:delete_multiple_objects) |
| 75 | + |
| 76 | + expect { purge_blobs_with_variants }.not_to change { ActiveStorage::Blob.exists?(id: blob.id) } |
| 77 | + end |
| 78 | + end |
| 79 | + end |
| 80 | +end |
0 commit comments