job_artifact_uploader_spec.rb 3.39 KB
Newer Older
1 2 3
require 'spec_helper'

describe JobArtifactUploader do
4
  let(:store) { described_class::Store::LOCAL }
5
  let(:job_artifact) { create(:ci_job_artifact, file_store: store) }
6 7
  let(:uploader) { described_class.new(job_artifact, :file) }

8
  subject { uploader }
9

10 11 12 13
  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]
14

15 16 17
  context "object store is REMOTE" do
    before do
      stub_artifacts_object_storage
18
    end
19

20
    include_context 'with storage', described_class::Store::REMOTE
21

22 23
    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]
24 25
  end

26
  context 'file is stored in valid local_path' do
27
    let(:file) do
28
      fixture_file_upload('spec/fixtures/ci_build_artifacts.zip', 'application/zip')
29 30 31 32 33 34 35 36
    end

    before do
      uploader.store!(file)
    end

    subject { uploader.file.path }

37
    it { is_expected.to start_with("#{uploader.root}/#{uploader.class.base_dir}") }
38
    it { is_expected.to include("/#{job_artifact.created_at.utc.strftime('%Y_%m_%d')}/") }
39
    it { is_expected.to include("/#{job_artifact.job_id}/#{job_artifact.id}/") }
40 41
    it { is_expected.to end_with("ci_build_artifacts.zip") }
  end
42

43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89
  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

90 91
  describe "#migrate!" do
    before do
92
      uploader.store!(fixture_file_upload('spec/fixtures/trace/sample_trace'))
93 94 95 96 97 98
      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
  end
99
end