debian-mirror-gitlab/spec/lib/gitlab/ci/artifact_file_reader_spec.rb

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

147 lines
5 KiB
Ruby
Raw Permalink Normal View History

2020-04-08 14:13:33 +05:30
# frozen_string_literal: true
require 'spec_helper'
2020-07-28 23:09:34 +05:30
RSpec.describe Gitlab::Ci::ArtifactFileReader do
2020-04-08 14:13:33 +05:30
let(:job) { create(:ci_build) }
let(:path) { 'generated.yml' } # included in the ci_build_artifacts.zip
describe '#read' do
subject { described_class.new(job).read(path) }
context 'when job has artifacts and metadata' do
2023-02-26 17:17:37 +05:30
shared_examples 'extracting job artifact archive' do
it 'returns the content at the path' do
is_expected.to be_present
expect(YAML.safe_load(subject).keys).to contain_exactly('rspec', 'time', 'custom')
end
2020-04-08 14:13:33 +05:30
2023-02-26 17:17:37 +05:30
context 'when path does not exist' do
let(:path) { 'file/does/not/exist.txt' }
let(:expected_error) do
"Path `#{path}` does not exist inside the `#{job.name}` artifacts archive!"
end
2020-04-08 14:13:33 +05:30
2023-02-26 17:17:37 +05:30
it 'raises an error' do
expect { subject }.to raise_error(described_class::Error, expected_error)
end
2020-04-08 14:13:33 +05:30
end
2023-02-26 17:17:37 +05:30
context 'when path points to a directory' do
let(:path) { 'other_artifacts_0.1.2' }
let(:expected_error) do
"Path `#{path}` was expected to be a file but it was a directory!"
end
it 'raises an error' do
expect { subject }.to raise_error(described_class::Error, expected_error)
end
2020-04-08 14:13:33 +05:30
end
2023-02-26 17:17:37 +05:30
context 'when path is nested' do
# path exists in ci_build_artifacts.zip
let(:path) { 'other_artifacts_0.1.2/doc_sample.txt' }
it 'returns the content at the nested path' do
is_expected.to be_present
end
2020-04-08 14:13:33 +05:30
end
2023-02-26 17:17:37 +05:30
context 'when artifact archive size is greater than the limit' do
let(:expected_error) do
"Artifacts archive for job `#{job.name}` is too large: max 1 KB"
end
before do
stub_const("#{described_class}::MAX_ARCHIVE_SIZE", 1.kilobyte)
end
it 'raises an error' do
expect { subject }.to raise_error(described_class::Error, expected_error)
end
2020-04-08 14:13:33 +05:30
end
2023-02-26 17:17:37 +05:30
context 'when metadata entry shows size greater than the limit' do
let(:expected_error) do
"Artifacts archive for job `#{job.name}` is too large: max 5 MB"
end
2020-04-08 14:13:33 +05:30
2023-02-26 17:17:37 +05:30
before do
expect_next_instance_of(Gitlab::Ci::Build::Artifacts::Metadata::Entry) do |entry|
expect(entry).to receive(:total_size).and_return(10.megabytes)
end
end
it 'raises an error' do
expect { subject }.to raise_error(described_class::Error, expected_error)
end
2020-04-08 14:13:33 +05:30
end
end
2023-02-26 17:17:37 +05:30
context 'when job artifact is on local storage' do
let!(:artifacts) { create(:ci_job_artifact, :archive, job: job) }
let!(:metadata) { create(:ci_job_artifact, :metadata, job: job) }
it_behaves_like 'extracting job artifact archive'
end
2020-04-08 14:13:33 +05:30
2023-02-26 17:17:37 +05:30
context 'when job artifact is on remote storage' do
2020-04-08 14:13:33 +05:30
before do
2023-02-26 17:17:37 +05:30
stub_artifacts_object_storage
stub_request(:get, %r{https://artifacts.+ci_build_artifacts\.zip})
.to_return(
status: 200,
body: File.open(Rails.root.join('spec/fixtures/ci_build_artifacts.zip')),
headers: {}
)
stub_request(:get, %r{https://artifacts.+ci_build_artifacts_metadata})
.to_return(
status: 200,
body: File.open(Rails.root.join('spec/fixtures/ci_build_artifacts_metadata.gz')),
headers: {}
)
2020-04-08 14:13:33 +05:30
end
2023-02-26 17:17:37 +05:30
let!(:artifacts) { create(:ci_job_artifact, :archive, :remote_store, job: job) }
let!(:metadata) { create(:ci_job_artifact, :metadata, :remote_store, job: job) }
it_behaves_like 'extracting job artifact archive'
2020-04-08 14:13:33 +05:30
end
2023-02-26 17:17:37 +05:30
context 'when extracting job artifact raises entry size error' do
let!(:artifacts) { create(:ci_job_artifact, :archive, job: job) }
let!(:metadata) { create(:ci_job_artifact, :metadata, job: job) }
2020-04-08 14:13:33 +05:30
before do
2023-02-26 17:17:37 +05:30
allow_next_instance_of(SafeZip::Extract, anything) do |extractor|
allow(extractor).to receive(:extract).and_raise(SafeZip::Extract::EntrySizeError)
2020-04-08 14:13:33 +05:30
end
end
it 'raises an error' do
2023-02-26 17:17:37 +05:30
expected_error = "Path `#{path}` has invalid size in the zip!"
2020-04-08 14:13:33 +05:30
expect { subject }.to raise_error(described_class::Error, expected_error)
end
end
end
context 'when job does not have metadata artifacts' do
let!(:artifacts) { create(:ci_job_artifact, :archive, job: job) }
let(:expected_error) do
"Job `#{job.name}` has missing artifacts metadata and cannot be extracted!"
end
it 'raises an error' do
expect { subject }.to raise_error(described_class::Error, expected_error)
end
end
context 'when job does not have artifacts' do
it 'raises ArgumentError' do
expect { subject }.to raise_error(ArgumentError, 'Job does not have artifacts')
end
end
end
end