2024-04-29 00:17:15 +02:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2022-02-28 13:16:27 +01:00
|
|
|
describe ProcedureArchiveService do
|
|
|
|
let(:procedure) { build(:procedure) }
|
|
|
|
let(:archive) { create(:archive) }
|
|
|
|
let(:file) { Tempfile.new }
|
2023-03-15 15:22:53 +01:00
|
|
|
let(:fixture_blob) { ActiveStorage::Blob.create_before_direct_upload!(filename: File.basename(file.path), byte_size: file.size, checksum: 'osf', content_type: 'text/plain') }
|
2022-02-28 13:16:27 +01:00
|
|
|
|
2022-04-08 17:09:22 +02:00
|
|
|
let(:uploader) { ArchiveUploader.new(procedure: procedure, filename: archive.filename(procedure), filepath: file.path) }
|
2022-02-28 13:16:27 +01:00
|
|
|
|
|
|
|
describe '.upload' do
|
|
|
|
context 'when active storage service is local' do
|
|
|
|
it 'uploads with upload_with_active_storage' do
|
|
|
|
expect(uploader).to receive(:active_storage_service_local?).and_return(true)
|
|
|
|
expect(uploader).to receive(:upload_with_active_storage).and_return(fixture_blob)
|
2022-04-08 17:09:22 +02:00
|
|
|
uploader.upload(archive)
|
2022-02-28 13:16:27 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'link the created blob as an attachment to the current archive instance' do
|
2022-04-08 17:09:22 +02:00
|
|
|
expect { uploader.upload(archive) }
|
2022-02-28 13:16:27 +01:00
|
|
|
.to change { ActiveStorage::Attachment.where(name: 'file', record_type: 'Archive', record_id: archive.id).count }.by(1)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when active storage service is not local' do
|
|
|
|
before do
|
|
|
|
expect(uploader).to receive(:active_storage_service_local?).and_return(false)
|
|
|
|
expect(File).to receive(:size).with(file.path).and_return(filesize)
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when file is smaller than MAX_FILE_SIZE_FOR_BACKEND_BEFORE_CHUNKING' do
|
|
|
|
let(:filesize) { ArchiveUploader::MAX_FILE_SIZE_FOR_BACKEND_BEFORE_CHUNKING - 1 }
|
|
|
|
|
|
|
|
it 'uploads with upload_with_active_storage' do
|
|
|
|
expect(uploader).to receive(:upload_with_active_storage).and_return(fixture_blob)
|
2022-04-08 17:09:22 +02:00
|
|
|
uploader.upload(archive)
|
2022-02-28 13:16:27 +01:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when file is bigger than MAX_FILE_SIZE_FOR_BACKEND_BEFORE_CHUNKING' do
|
|
|
|
let(:filesize) { ArchiveUploader::MAX_FILE_SIZE_FOR_BACKEND_BEFORE_CHUNKING + 1 }
|
|
|
|
|
|
|
|
it 'uploads with upload_with_chunking_wrapper' do
|
|
|
|
expect(uploader).to receive(:upload_with_chunking_wrapper).and_return(fixture_blob)
|
2022-04-08 17:09:22 +02:00
|
|
|
uploader.upload(archive)
|
2022-02-28 13:16:27 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'link the created blob as an attachment to the current archive instance' do
|
|
|
|
expect(uploader).to receive(:upload_with_chunking_wrapper).and_return(fixture_blob)
|
2022-04-08 17:09:22 +02:00
|
|
|
expect { uploader.upload(archive) }
|
2022-02-28 13:16:27 +01:00
|
|
|
.to change { ActiveStorage::Attachment.where(name: 'file', record_type: 'Archive', record_id: archive.id).count }.by(1)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe '.upload_with_chunking_wrapper' do
|
|
|
|
let(:fake_blob_checksum) { Digest::SHA256.file(file.path) }
|
|
|
|
let(:fake_blob_bytesize) { 100.gigabytes }
|
|
|
|
|
|
|
|
before do
|
|
|
|
expect(File).to receive(:size).with(file.path).and_return(fake_blob_bytesize)
|
|
|
|
expect(Digest::SHA256).to receive(:file).with(file.path).and_return(double(hexdigest: fake_blob_checksum.hexdigest))
|
|
|
|
end
|
|
|
|
|
2022-04-05 15:01:00 +02:00
|
|
|
context 'when it just works' do
|
|
|
|
it 'creates a blob' do
|
|
|
|
expect(uploader).to receive(:syscall_to_custom_uploader).and_return(true)
|
|
|
|
expect { uploader.send(:upload_with_chunking_wrapper) }
|
|
|
|
.to change { ActiveStorage::Blob.where(checksum: fake_blob_checksum.hexdigest, byte_size: fake_blob_bytesize).count }.by(1)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when it fails once (DS proxy a bit flacky with archive ±>20Go, fails once, accept other call' do
|
|
|
|
it 'retries' do
|
|
|
|
expect(uploader).to receive(:syscall_to_custom_uploader).with(anything).once.and_raise(StandardError, "BOOM")
|
|
|
|
expect(uploader).to receive(:syscall_to_custom_uploader).with(anything).once.and_return(true)
|
|
|
|
expect { uploader.send(:upload_with_chunking_wrapper) }
|
|
|
|
.to change { ActiveStorage::Blob.where(checksum: fake_blob_checksum.hexdigest, byte_size: fake_blob_bytesize).count }.by(1)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when it fails twice' do
|
|
|
|
it 'does not retry more than once' do
|
|
|
|
expect(uploader).to receive(:syscall_to_custom_uploader).with(anything).twice.and_raise(StandardError, "BOOM")
|
|
|
|
expect { uploader.send(:upload_with_chunking_wrapper) }
|
2022-04-05 15:05:22 +02:00
|
|
|
.to raise_error(RuntimeError, "custom archive attachment failed twice, retry later")
|
2022-04-05 15:01:00 +02:00
|
|
|
end
|
2022-02-28 13:16:27 +01:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|