debian-mirror-gitlab/spec/lib/gitlab/ci/trace/archive_spec.rb

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

155 lines
4.7 KiB
Ruby
Raw Permalink Normal View History

2021-11-18 22:05:49 +05:30
# frozen_string_literal: true
require 'spec_helper'
2023-04-23 21:23:45 +05:30
RSpec.describe Gitlab::Ci::Trace::Archive, feature_category: :scalability do
2021-12-11 22:18:48 +05:30
context 'with transactional fixtures' do
2022-10-11 01:57:18 +05:30
let_it_be_with_reload(:job) { create(:ci_build, :success, :trace_live) }
2021-12-11 22:18:48 +05:30
let_it_be_with_reload(:trace_metadata) { create(:ci_build_trace_metadata, build: job) }
let_it_be(:src_checksum) do
job.trace.read { |stream| Digest::MD5.hexdigest(stream.raw) }
2021-11-18 22:05:49 +05:30
end
2021-12-11 22:18:48 +05:30
let(:metrics) { spy('metrics') }
2021-11-18 22:05:49 +05:30
2021-12-11 22:18:48 +05:30
describe '#execute' do
subject { described_class.new(job, trace_metadata, metrics) }
2021-11-18 22:05:49 +05:30
2021-12-11 22:18:48 +05:30
it 'computes and assigns checksum' do
Gitlab::Ci::Trace::ChunkedIO.new(job) do |stream|
expect { subject.execute!(stream) }.to change { Ci::JobArtifact.count }.by(1)
2021-11-18 22:05:49 +05:30
end
2021-12-11 22:18:48 +05:30
expect(trace_metadata.checksum).to eq(src_checksum)
expect(trace_metadata.trace_artifact).to eq(job.job_artifacts_trace)
2021-11-18 22:05:49 +05:30
end
2021-12-11 22:18:48 +05:30
context 'validating artifact checksum' do
let(:trace) { 'abc' }
let(:stream) { StringIO.new(trace, 'rb') }
let(:src_checksum) { Digest::MD5.hexdigest(trace) }
2021-11-18 22:05:49 +05:30
2022-07-23 23:45:48 +05:30
shared_examples 'valid' do
it 'does not count as invalid' do
subject.execute!(stream)
expect(metrics)
.not_to have_received(:increment_error_counter)
.with(error_reason: :archive_invalid_checksum)
2021-12-11 22:18:48 +05:30
end
2022-07-23 23:45:48 +05:30
end
2021-11-18 22:05:49 +05:30
2022-07-23 23:45:48 +05:30
shared_examples 'local checksum only' do
it 'generates only local checksum' do
2021-12-11 22:18:48 +05:30
subject.execute!(stream)
2022-07-23 23:45:48 +05:30
2021-12-11 22:18:48 +05:30
expect(trace_metadata.checksum).to eq(src_checksum)
expect(trace_metadata.remote_checksum).to be_nil
end
2021-11-18 22:05:49 +05:30
end
2022-07-23 23:45:48 +05:30
shared_examples 'skips validations' do
it_behaves_like 'valid'
it_behaves_like 'local checksum only'
end
shared_context 'with FIPS' do
context 'with FIPS enabled', :fips_mode do
it_behaves_like 'valid'
it 'does not generate md5 checksums' do
subject.execute!(stream)
expect(trace_metadata.checksum).to be_nil
expect(trace_metadata.remote_checksum).to be_nil
end
end
end
context 'when the object store is disabled' do
2021-12-11 22:18:48 +05:30
before do
2022-07-23 23:45:48 +05:30
stub_artifacts_object_storage(enabled: false)
2021-12-11 22:18:48 +05:30
end
2021-11-18 22:05:49 +05:30
2022-07-23 23:45:48 +05:30
it_behaves_like 'skips validations'
include_context 'with FIPS'
end
2021-11-18 22:05:49 +05:30
2021-12-11 22:18:48 +05:30
context 'with direct_upload enabled' do
2021-11-18 22:05:49 +05:30
before do
2021-12-11 22:18:48 +05:30
stub_artifacts_object_storage(direct_upload: true)
2021-11-18 22:05:49 +05:30
end
2022-07-23 23:45:48 +05:30
it_behaves_like 'valid'
it 'checksums match' do
2021-11-18 22:05:49 +05:30
subject.execute!(stream)
expect(trace_metadata.checksum).to eq(src_checksum)
2021-12-11 22:18:48 +05:30
expect(trace_metadata.remote_checksum).to eq(src_checksum)
end
context 'when the checksum does not match' do
let(:invalid_remote_checksum) { SecureRandom.hex }
before do
2022-07-23 23:45:48 +05:30
allow(::Gitlab::Ci::Trace::RemoteChecksum)
2021-12-11 22:18:48 +05:30
.to receive(:new)
.with(an_instance_of(Ci::JobArtifact))
.and_return(double(md5_checksum: invalid_remote_checksum))
end
2022-07-23 23:45:48 +05:30
it 'counts as invalid' do
2021-12-11 22:18:48 +05:30
subject.execute!(stream)
expect(trace_metadata.checksum).to eq(src_checksum)
expect(trace_metadata.remote_checksum).to eq(invalid_remote_checksum)
expect(metrics)
.to have_received(:increment_error_counter)
.with(error_reason: :archive_invalid_checksum)
end
2022-07-23 23:45:48 +05:30
include_context 'with FIPS'
2021-11-18 22:05:49 +05:30
end
2022-07-23 23:45:48 +05:30
include_context 'with FIPS'
2021-11-18 22:05:49 +05:30
end
end
end
end
2021-12-11 22:18:48 +05:30
context 'without transactional fixtures', :delete do
let(:job) { create(:ci_build, :success, :trace_live) }
let(:trace_metadata) { create(:ci_build_trace_metadata, build: job) }
let(:stream) { StringIO.new('abc', 'rb') }
describe '#execute!' do
subject(:execute) do
::Gitlab::Ci::Trace::Archive.new(job, trace_metadata).execute!(stream)
end
before do
stub_artifacts_object_storage(direct_upload: true)
end
it 'does not upload the trace inside a database transaction', :delete do
expect(Ci::ApplicationRecord.connection.transaction_open?).to be_falsey
allow_next_instance_of(Ci::JobArtifact) do |artifact|
artifact.job_id = job.id
expect(artifact)
.to receive(:store_file!)
.and_wrap_original do |store_method, *args|
expect(Ci::ApplicationRecord.connection.transaction_open?).to be_falsey
store_method.call(*args)
end
end
execute
end
end
end
2021-11-18 22:05:49 +05:30
end