debian-mirror-gitlab/spec/services/ci/job_artifacts/create_service_spec.rb

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

498 lines
17 KiB
Ruby
Raw Normal View History

2020-03-13 15:44:24 +05:30
# frozen_string_literal: true
require 'spec_helper'
2023-06-20 00:43:36 +05:30
RSpec.describe Ci::JobArtifacts::CreateService, :clean_gitlab_redis_shared_state, feature_category: :build_artifacts do
include WorkhorseHelpers
include Gitlab::Utils::Gzip
2020-04-08 14:13:33 +05:30
let_it_be(:project) { create(:project) }
2021-06-08 01:23:25 +05:30
2020-07-28 23:09:34 +05:30
let(:service) { described_class.new(job) }
2020-04-08 14:13:33 +05:30
let(:job) { create(:ci_build, project: project) }
2020-03-13 15:44:24 +05:30
2023-06-20 00:43:36 +05:30
describe '#authorize', :aggregate_failures do
let(:artifact_type) { 'archive' }
let(:filesize) { nil }
2020-03-13 15:44:24 +05:30
2023-06-20 00:43:36 +05:30
subject(:authorize) { service.authorize(artifact_type: artifact_type, filesize: filesize) }
2023-05-27 22:25:52 +05:30
2023-06-20 00:43:36 +05:30
shared_examples_for 'handling lsif artifact' do
context 'when artifact is lsif' do
let(:artifact_type) { 'lsif' }
2023-05-27 22:25:52 +05:30
2023-06-20 00:43:36 +05:30
it 'includes ProcessLsif in the headers' do
expect(authorize[:headers][:ProcessLsif]).to eq(true)
end
2023-05-27 22:25:52 +05:30
end
end
2023-06-20 00:43:36 +05:30
shared_examples_for 'validating requirements' do
context 'when filesize is specified' do
let(:max_artifact_size) { 10 }
2023-05-27 22:25:52 +05:30
2023-06-20 00:43:36 +05:30
before do
allow(Ci::JobArtifact)
.to receive(:max_artifact_size)
.with(type: artifact_type, project: project)
.and_return(max_artifact_size)
end
2023-05-27 22:25:52 +05:30
2023-06-20 00:43:36 +05:30
context 'and filesize exceeds the limit' do
let(:filesize) { max_artifact_size + 1 }
2023-05-27 22:25:52 +05:30
2023-06-20 00:43:36 +05:30
it 'returns error' do
expect(authorize[:status]).to eq(:error)
end
end
2023-05-27 22:25:52 +05:30
2023-06-20 00:43:36 +05:30
context 'and filesize does not exceed the limit' do
let(:filesize) { max_artifact_size - 1 }
2023-05-27 22:25:52 +05:30
2023-06-20 00:43:36 +05:30
it 'returns success' do
expect(authorize[:status]).to eq(:success)
end
end
2023-05-27 22:25:52 +05:30
end
end
2023-06-20 00:43:36 +05:30
shared_examples_for 'uploading to temp location' do |store_type|
# We are not testing the entire headers here because this is fully tested
# in workhorse_authorize's spec. We just want to confirm that it indeed used the temp path
# by checking some indicators in the headers returned.
if store_type == :object_storage
it 'includes the authorize headers' do
expect(authorize[:status]).to eq(:success)
expect(authorize[:headers][:RemoteObject][:StoreURL]).to include(ObjectStorage::TMP_UPLOAD_PATH)
end
else
it 'includes the authorize headers' do
expect(authorize[:status]).to eq(:success)
expect(authorize[:headers][:TempPath]).to include(ObjectStorage::TMP_UPLOAD_PATH)
end
2023-05-27 22:25:52 +05:30
end
2023-06-20 00:43:36 +05:30
it_behaves_like 'handling lsif artifact'
it_behaves_like 'validating requirements'
2023-05-27 22:25:52 +05:30
end
2023-06-20 00:43:36 +05:30
context 'when object storage is enabled' do
context 'and direct upload is enabled' do
before do
stub_artifacts_object_storage(JobArtifactUploader, direct_upload: true)
end
it_behaves_like 'uploading to temp location', :object_storage
2023-05-27 22:25:52 +05:30
end
2023-06-20 00:43:36 +05:30
context 'and direct upload is disabled' do
before do
stub_artifacts_object_storage(JobArtifactUploader, direct_upload: false)
end
2023-05-27 22:25:52 +05:30
2023-06-20 00:43:36 +05:30
it_behaves_like 'uploading to temp location', :local_storage
2023-05-27 22:25:52 +05:30
end
end
2023-06-20 00:43:36 +05:30
context 'when object storage is disabled' do
it_behaves_like 'uploading to temp location', :local_storage
end
end
2022-08-27 11:52:29 +05:30
2023-06-20 00:43:36 +05:30
describe '#execute' do
let(:artifacts_sha256) { '0' * 64 }
let(:metadata_file) { nil }
let(:params) do
{
'artifact_type' => 'archive',
'artifact_format' => 'zip'
}.with_indifferent_access
end
2022-08-27 11:52:29 +05:30
2023-06-20 00:43:36 +05:30
subject(:execute) { service.execute(artifacts_file, params, metadata_file: metadata_file) }
2022-07-16 23:28:13 +05:30
2023-06-20 00:43:36 +05:30
shared_examples_for 'handling accessibility' do
shared_examples 'public accessibility' do
it 'sets accessibility to public level' do
expect(job.job_artifacts).to all be_public_accessibility
end
2022-07-16 23:28:13 +05:30
end
2023-06-20 00:43:36 +05:30
shared_examples 'private accessibility' do
it 'sets accessibility to private level' do
expect(job.job_artifacts).to all be_private_accessibility
end
2020-03-13 15:44:24 +05:30
end
2023-06-20 00:43:36 +05:30
context 'when non_public_artifacts flag is disabled' do
2023-03-17 16:20:25 +05:30
before do
2023-05-27 22:25:52 +05:30
stub_feature_flags(non_public_artifacts: false)
2023-03-17 16:20:25 +05:30
end
2023-06-20 00:43:36 +05:30
it_behaves_like 'public accessibility'
end
context 'when non_public_artifacts flag is enabled' do
context 'and accessibility is defined in the params' do
context 'and is passed as private' do
before do
params.merge!('accessibility' => 'private')
end
it_behaves_like 'private accessibility'
end
context 'and is passed as public' do
before do
params.merge!('accessibility' => 'public')
end
it_behaves_like 'public accessibility'
end
2023-03-17 16:20:25 +05:30
end
2023-06-20 00:43:36 +05:30
context 'and accessibility is not defined in the params' do
context 'and job has no public artifacts defined in its CI config' do
it_behaves_like 'public accessibility'
end
context 'and job artifacts defined as private in the CI config' do
let(:job) { create(:ci_build, :with_private_artifacts_config, project: project) }
it_behaves_like 'private accessibility'
end
context 'and job artifacts defined as public in the CI config' do
let(:job) { create(:ci_build, :with_public_artifacts_config, project: project) }
it_behaves_like 'public accessibility'
end
end
2023-03-17 16:20:25 +05:30
end
context 'when accessibility passed as invalid value' do
before do
2023-06-20 00:43:36 +05:30
params.merge!('accessibility' => 'foo')
2023-03-17 16:20:25 +05:30
end
it 'fails with argument error' do
2023-06-20 00:43:36 +05:30
expect { execute }.to raise_error(ArgumentError, "'foo' is not a valid accessibility")
2023-03-17 16:20:25 +05:30
end
end
2023-06-20 00:43:36 +05:30
end
2023-03-17 16:20:25 +05:30
2023-06-20 00:43:36 +05:30
shared_examples_for 'handling metadata file' do
2020-03-13 15:44:24 +05:30
context 'when metadata file is also uploaded' do
let(:metadata_file) do
file_to_upload('spec/fixtures/ci_build_artifacts_metadata.gz', sha256: artifacts_sha256)
end
before do
stub_application_setting(default_artifacts_expire_in: '1 day')
end
2023-06-20 00:43:36 +05:30
it 'creates a new metadata job artifact' do
expect { execute }.to change { Ci::JobArtifact.where(file_type: :metadata).count }.by(1)
2020-03-13 15:44:24 +05:30
new_artifact = job.job_artifacts.last
expect(new_artifact.project).to eq(job.project)
expect(new_artifact.file).to be_present
expect(new_artifact.file_type).to eq('metadata')
expect(new_artifact.file_format).to eq('gzip')
expect(new_artifact.file_sha256).to eq(artifacts_sha256)
2021-12-11 22:18:48 +05:30
expect(new_artifact.locked).to eq(job.pipeline.locked)
2020-03-13 15:44:24 +05:30
end
2023-04-23 21:23:45 +05:30
it 'logs the created artifact and metadata' do
expect(Gitlab::Ci::Artifacts::Logger)
.to receive(:log_created)
.with(an_instance_of(Ci::JobArtifact)).twice
subject
end
2023-06-20 00:43:36 +05:30
it_behaves_like 'handling accessibility'
2020-03-13 15:44:24 +05:30
it 'sets expiration date according to application settings' do
expected_expire_at = 1.day.from_now
2023-06-20 00:43:36 +05:30
expect(execute).to match(a_hash_including(status: :success, artifact: anything))
2020-03-13 15:44:24 +05:30
archive_artifact, metadata_artifact = job.job_artifacts.last(2)
expect(job.artifacts_expire_at).to be_within(1.minute).of(expected_expire_at)
expect(archive_artifact.expire_at).to be_within(1.minute).of(expected_expire_at)
expect(metadata_artifact.expire_at).to be_within(1.minute).of(expected_expire_at)
end
2020-10-24 23:57:45 +05:30
context 'when expire_in params is set to a specific value' do
2020-03-13 15:44:24 +05:30
before do
params.merge!('expire_in' => '2 hours')
end
it 'sets expiration date according to the parameter' do
expected_expire_at = 2.hours.from_now
2023-06-20 00:43:36 +05:30
expect(execute).to match(a_hash_including(status: :success, artifact: anything))
2020-03-13 15:44:24 +05:30
archive_artifact, metadata_artifact = job.job_artifacts.last(2)
expect(job.artifacts_expire_at).to be_within(1.minute).of(expected_expire_at)
expect(archive_artifact.expire_at).to be_within(1.minute).of(expected_expire_at)
expect(metadata_artifact.expire_at).to be_within(1.minute).of(expected_expire_at)
end
end
2020-10-24 23:57:45 +05:30
context 'when expire_in params is set to `never`' do
before do
params.merge!('expire_in' => 'never')
end
it 'sets expiration date according to the parameter' do
expected_expire_at = nil
2023-06-20 00:43:36 +05:30
expect(execute).to be_truthy
2020-10-24 23:57:45 +05:30
archive_artifact, metadata_artifact = job.job_artifacts.last(2)
expect(job.artifacts_expire_at).to eq(expected_expire_at)
expect(archive_artifact.expire_at).to eq(expected_expire_at)
expect(metadata_artifact.expire_at).to eq(expected_expire_at)
end
end
2020-03-13 15:44:24 +05:30
end
end
2023-06-20 00:43:36 +05:30
shared_examples_for 'handling dotenv' do |storage_type|
context 'when artifact type is dotenv' do
let(:params) do
{
'artifact_type' => 'dotenv',
'artifact_format' => 'gzip'
}.with_indifferent_access
end
if storage_type == :object_storage
let(:object_body) { File.read('spec/fixtures/build.env.gz') }
let(:upload_filename) { 'build.env.gz' }
before do
stub_request(:get, %r{s3.amazonaws.com/#{remote_path}})
.to_return(status: 200, body: File.read('spec/fixtures/build.env.gz'))
end
else
let(:artifacts_file) do
file_to_upload('spec/fixtures/build.env.gz', sha256: artifacts_sha256)
end
end
2020-03-13 15:44:24 +05:30
2023-06-20 00:43:36 +05:30
it 'calls parse service' do
expect_any_instance_of(Ci::ParseDotenvArtifactService) do |service|
expect(service).to receive(:execute).once.and_call_original
end
2020-03-13 15:44:24 +05:30
2023-06-20 00:43:36 +05:30
expect(execute[:status]).to eq(:success)
expect(job.job_variables.as_json(only: [:key, :value, :source])).to contain_exactly(
hash_including('key' => 'KEY1', 'value' => 'VAR1', 'source' => 'dotenv'),
hash_including('key' => 'KEY2', 'value' => 'VAR2', 'source' => 'dotenv'))
2020-03-13 15:44:24 +05:30
end
end
2023-06-20 00:43:36 +05:30
end
2020-03-13 15:44:24 +05:30
2023-06-20 00:43:36 +05:30
shared_examples_for 'handling object storage errors' do
shared_examples 'rescues object storage error' do |klass, message, expected_message|
it "handles #{klass}" do
allow_next_instance_of(JobArtifactUploader) do |uploader|
allow(uploader).to receive(:store!).and_raise(klass, message)
end
2020-03-13 15:44:24 +05:30
2023-06-20 00:43:36 +05:30
expect(Gitlab::ErrorTracking)
.to receive(:track_exception)
.and_call_original
2020-03-13 15:44:24 +05:30
2023-06-20 00:43:36 +05:30
expect(execute).to match(
2022-10-11 01:57:18 +05:30
a_hash_including(
2023-06-20 00:43:36 +05:30
http_status: :service_unavailable,
message: expected_message || message,
status: :error))
2020-03-13 15:44:24 +05:30
end
end
2023-06-20 00:43:36 +05:30
it_behaves_like 'rescues object storage error',
Errno::EIO, 'some/path', 'Input/output error - some/path'
it_behaves_like 'rescues object storage error',
Google::Apis::ServerError, 'Server error'
it_behaves_like 'rescues object storage error',
Signet::RemoteServerError, 'The service is currently unavailable'
2020-03-13 15:44:24 +05:30
end
2020-04-08 14:13:33 +05:30
2023-06-20 00:43:36 +05:30
shared_examples_for 'validating requirements' do
context 'when filesize is specified' do
let(:max_artifact_size) { 10 }
before do
allow(Ci::JobArtifact)
.to receive(:max_artifact_size)
.with(type: 'archive', project: project)
.and_return(max_artifact_size)
allow(artifacts_file).to receive(:size).and_return(filesize)
end
context 'and filesize exceeds the limit' do
let(:filesize) { max_artifact_size + 1 }
it 'returns error' do
expect(execute[:status]).to eq(:error)
end
end
2020-04-08 14:13:33 +05:30
2023-06-20 00:43:36 +05:30
context 'and filesize does not exceed the limit' do
let(:filesize) { max_artifact_size - 1 }
it 'returns success' do
expect(execute[:status]).to eq(:success)
end
end
2020-04-08 14:13:33 +05:30
end
2023-06-20 00:43:36 +05:30
end
2020-04-08 14:13:33 +05:30
2023-06-20 00:43:36 +05:30
shared_examples_for 'handling existing artifact' do
context 'when job already has an artifact of the same file type' do
let!(:existing_artifact) do
create(:ci_job_artifact, params[:artifact_type], file_sha256: existing_sha256, job: job)
2020-04-08 14:13:33 +05:30
end
2023-06-20 00:43:36 +05:30
context 'when sha256 of uploading artifact is the same of the existing one' do
let(:existing_sha256) { artifacts_sha256 }
it 'ignores the changes' do
expect { execute }.not_to change { Ci::JobArtifact.count }
expect(execute).to match(a_hash_including(status: :success))
end
end
context 'when sha256 of uploading artifact is different than the existing one' do
let(:existing_sha256) { '1' * 64 }
it 'returns error status' do
expect(Gitlab::ErrorTracking).to receive(:track_exception).and_call_original
expect { execute }.not_to change { Ci::JobArtifact.count }
expect(execute).to match(
a_hash_including(
http_status: :bad_request,
message: 'another artifact of the same type already exists',
status: :error
)
)
end
end
end
end
shared_examples_for 'logging artifact' do
it 'logs the created artifact' do
expect(Gitlab::Ci::Artifacts::Logger)
.to receive(:log_created)
.with(an_instance_of(Ci::JobArtifact))
execute
2020-04-08 14:13:33 +05:30
end
2020-05-24 23:13:21 +05:30
end
2023-06-20 00:43:36 +05:30
shared_examples_for 'handling remote uploads to temporary location' do
context 'when artifacts file is uploaded' do
it 'creates a new job artifact' do
expect { execute }.to change { Ci::JobArtifact.count }.by(1)
2022-10-11 01:57:18 +05:30
2023-06-20 00:43:36 +05:30
new_artifact = execute[:artifact]
expect(new_artifact).to eq(job.job_artifacts.last)
expect(new_artifact.project).to eq(job.project)
expect(new_artifact.file.filename).to eq(artifacts_file.original_filename)
expect(new_artifact.file_identifier).to eq(artifacts_file.original_filename)
expect(new_artifact.file_type).to eq(params['artifact_type'])
expect(new_artifact.file_format).to eq(params['artifact_format'])
expect(new_artifact.file_sha256).to eq(artifacts_sha256)
expect(new_artifact.locked).to eq(job.pipeline.locked)
expect(new_artifact.size).to eq(artifacts_file.size)
2022-10-11 01:57:18 +05:30
2023-06-20 00:43:36 +05:30
expect(execute[:status]).to eq(:success)
end
2022-10-11 01:57:18 +05:30
2023-06-20 00:43:36 +05:30
it_behaves_like 'handling accessibility'
it_behaves_like 'handling metadata file'
it_behaves_like 'handling partitioning'
it_behaves_like 'logging artifact'
2022-10-11 01:57:18 +05:30
end
end
2023-06-20 00:43:36 +05:30
shared_examples_for 'handling partitioning' do
context 'with job partitioned', :ci_partitionable do
let(:pipeline) { create(:ci_pipeline, project: project, partition_id: ci_testing_partition_id) }
let(:job) { create(:ci_build, pipeline: pipeline) }
it 'sets partition_id on artifacts' do
expect { execute }.to change { Ci::JobArtifact.count }
artifacts_partitions = job.job_artifacts.map(&:partition_id).uniq
expect(artifacts_partitions).to eq([ci_testing_partition_id])
2020-04-08 14:13:33 +05:30
end
2023-06-20 00:43:36 +05:30
end
end
2020-04-08 14:13:33 +05:30
2023-06-20 00:43:36 +05:30
context 'when object storage and direct upload is enabled' do
let(:fog_connection) { stub_artifacts_object_storage(JobArtifactUploader, direct_upload: true) }
let(:remote_path) { File.join(remote_store_path, remote_id) }
let(:object_body) { File.open('spec/fixtures/ci_build_artifacts.zip') }
let(:upload_filename) { 'artifacts.zip' }
let(:object) do
fog_connection.directories
.new(key: 'artifacts')
.files
.create( # rubocop:disable Rails/SaveBang
key: remote_path,
body: object_body
)
end
2020-04-08 14:13:33 +05:30
2023-06-20 00:43:36 +05:30
let(:artifacts_file) do
fog_to_uploaded_file(
object,
filename: upload_filename,
sha256: artifacts_sha256,
remote_id: remote_id
)
2020-04-08 14:13:33 +05:30
end
2023-06-20 00:43:36 +05:30
let(:remote_id) { 'generated-remote-id-12345' }
let(:remote_store_path) { ObjectStorage::TMP_UPLOAD_PATH }
it_behaves_like 'handling remote uploads to temporary location'
it_behaves_like 'handling dotenv', :object_storage
it_behaves_like 'handling object storage errors'
it_behaves_like 'validating requirements'
2020-04-08 14:13:33 +05:30
end
2023-06-20 00:43:36 +05:30
context 'when using local storage' do
let(:artifacts_file) do
file_to_upload('spec/fixtures/ci_build_artifacts.zip', sha256: artifacts_sha256)
end
2020-04-08 14:13:33 +05:30
2023-06-20 00:43:36 +05:30
it_behaves_like 'handling remote uploads to temporary location'
it_behaves_like 'handling dotenv', :local_storage
it_behaves_like 'validating requirements'
end
end
def file_to_upload(path, params = {})
upload = Tempfile.new('upload')
FileUtils.copy(path, upload.path)
# This is a workaround for https://github.com/docker/for-linux/issues/1015
FileUtils.touch(upload.path)
2020-04-08 14:13:33 +05:30
2023-06-20 00:43:36 +05:30
UploadedFile.new(upload.path, **params)
2020-03-13 15:44:24 +05:30
end
end