2019-10-14 23:06:19 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2017-09-21 04:34:12 -04:00
|
|
|
require 'spec_helper'
|
|
|
|
|
|
|
|
describe JobArtifactUploader do
|
2018-02-02 08:59:43 -05:00
|
|
|
let(:store) { described_class::Store::LOCAL }
|
2017-12-05 09:31:33 -05:00
|
|
|
let(:job_artifact) { create(:ci_job_artifact, file_store: store) }
|
2017-09-21 04:34:12 -04:00
|
|
|
let(:uploader) { described_class.new(job_artifact, :file) }
|
|
|
|
|
2018-02-02 08:59:43 -05:00
|
|
|
subject { uploader }
|
2017-09-21 04:34:12 -04:00
|
|
|
|
2018-02-02 08:59:43 -05:00
|
|
|
it_behaves_like "builds correct paths",
|
|
|
|
store_dir: %r[\h{2}/\h{2}/\h{64}/\d{4}_\d{1,2}_\d{1,2}/\d+/\d+\z],
|
|
|
|
cache_dir: %r[artifacts/tmp/cache],
|
|
|
|
work_dir: %r[artifacts/tmp/work]
|
2017-11-23 13:41:49 -05:00
|
|
|
|
2018-02-02 08:59:43 -05:00
|
|
|
context "object store is REMOTE" do
|
|
|
|
before do
|
|
|
|
stub_artifacts_object_storage
|
2017-11-23 13:41:49 -05:00
|
|
|
end
|
2017-11-23 12:51:20 -05:00
|
|
|
|
2018-02-02 08:59:43 -05:00
|
|
|
include_context 'with storage', described_class::Store::REMOTE
|
2017-11-23 12:51:20 -05:00
|
|
|
|
2018-02-02 08:59:43 -05:00
|
|
|
it_behaves_like "builds correct paths",
|
|
|
|
store_dir: %r[\h{2}/\h{2}/\h{64}/\d{4}_\d{1,2}_\d{1,2}/\d+/\d+\z]
|
2017-11-23 12:51:20 -05:00
|
|
|
end
|
|
|
|
|
2017-11-23 13:41:49 -05:00
|
|
|
context 'file is stored in valid local_path' do
|
2017-11-23 12:51:20 -05:00
|
|
|
let(:file) do
|
2018-06-06 17:26:00 -04:00
|
|
|
fixture_file_upload('spec/fixtures/ci_build_artifacts.zip', 'application/zip')
|
2017-11-23 12:51:20 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
before do
|
|
|
|
uploader.store!(file)
|
|
|
|
end
|
|
|
|
|
|
|
|
subject { uploader.file.path }
|
|
|
|
|
2018-02-02 08:59:43 -05:00
|
|
|
it { is_expected.to start_with("#{uploader.root}/#{uploader.class.base_dir}") }
|
2017-11-23 12:51:20 -05:00
|
|
|
it { is_expected.to include("/#{job_artifact.created_at.utc.strftime('%Y_%m_%d')}/") }
|
2018-01-17 06:30:25 -05:00
|
|
|
it { is_expected.to include("/#{job_artifact.job_id}/#{job_artifact.id}/") }
|
2017-11-23 12:51:20 -05:00
|
|
|
it { is_expected.to end_with("ci_build_artifacts.zip") }
|
|
|
|
end
|
2018-02-27 08:09:33 -05:00
|
|
|
|
2018-08-17 00:33:15 -04:00
|
|
|
describe '#dynamic_segment' do
|
|
|
|
let(:uploaded_content) { File.binread(Rails.root + 'spec/fixtures/ci_build_artifacts.zip') }
|
|
|
|
let(:model) { uploader.model }
|
|
|
|
|
|
|
|
shared_examples_for 'Read file from legacy path' do
|
|
|
|
it 'store_path returns the legacy path' do
|
|
|
|
expect(model.file.store_path).to eq(File.join(model.created_at.utc.strftime('%Y_%m'), model.project_id.to_s, model.job_id.to_s, 'ci_build_artifacts.zip'))
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'has exactly the same content' do
|
|
|
|
expect(::File.binread(model.file.path)).to eq(uploaded_content)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
shared_examples_for 'Read file from hashed path' do
|
|
|
|
it 'store_path returns hashed path' do
|
|
|
|
expect(model.file.store_path).to eq(File.join(disk_hash[0..1], disk_hash[2..3], disk_hash, creation_date, model.job_id.to_s, model.id.to_s, 'ci_build_artifacts.zip'))
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'has exactly the same content' do
|
|
|
|
expect(::File.binread(model.file.path)).to eq(uploaded_content)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when a job artifact is stored in legacy_path' do
|
|
|
|
let(:job_artifact) { create(:ci_job_artifact, :legacy_archive) }
|
|
|
|
|
|
|
|
it_behaves_like 'Read file from legacy path'
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when the artifact file is stored in hashed_path' do
|
|
|
|
let(:job_artifact) { create(:ci_job_artifact, :archive) }
|
|
|
|
let(:disk_hash) { Digest::SHA2.hexdigest(model.project_id.to_s) }
|
|
|
|
let(:creation_date) { model.created_at.utc.strftime('%Y_%m_%d') }
|
|
|
|
|
|
|
|
it_behaves_like 'Read file from hashed path'
|
|
|
|
|
|
|
|
context 'when file_location column is empty' do
|
|
|
|
before do
|
|
|
|
job_artifact.update_column(:file_location, nil)
|
|
|
|
end
|
|
|
|
|
|
|
|
it_behaves_like 'Read file from hashed path'
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2018-02-27 08:09:33 -05:00
|
|
|
describe "#migrate!" do
|
|
|
|
before do
|
2018-06-06 17:26:00 -04:00
|
|
|
uploader.store!(fixture_file_upload('spec/fixtures/trace/sample_trace'))
|
2018-02-27 08:09:33 -05:00
|
|
|
stub_artifacts_object_storage
|
|
|
|
end
|
|
|
|
|
|
|
|
it_behaves_like "migrates", to_store: described_class::Store::REMOTE
|
|
|
|
it_behaves_like "migrates", from_store: described_class::Store::REMOTE, to_store: described_class::Store::LOCAL
|
2020-03-24 14:07:55 -04:00
|
|
|
|
|
|
|
# CI job artifacts usually are shown as text/plain, but they contain
|
|
|
|
# escape characters so MIME detectors usually fail to determine what
|
|
|
|
# the Content-Type is.
|
|
|
|
it 'does not set Content-Type' do
|
|
|
|
expect(uploader.file.content_type).to be_blank
|
|
|
|
end
|
2018-02-27 08:09:33 -05:00
|
|
|
end
|
2017-09-21 04:34:12 -04:00
|
|
|
end
|