Skip to content
Snippets Groups Projects
Commit a2726aee authored by Marius Bobin's avatar Marius Bobin :two: Committed by Grzegorz Bizon
Browse files

Prepare to remove trace column from ci_builds

parent df67f9da
No related branches found
No related tags found
1 merge request!96865Prepare to remove trace column from ci_builds
......@@ -706,24 +706,6 @@ def has_test_reports?
job_artifacts.of_report_type(:test).exists?
end
def has_old_trace?
old_trace.present?
end
def trace=(data)
raise NotImplementedError
end
def old_trace
read_attribute(:trace)
end
def erase_old_trace!
return unless has_old_trace?
update_column(:trace, nil)
end
def ensure_trace_metadata!
Ci::BuildTraceMetadata.find_or_upsert_for!(id)
end
......
......@@ -8,8 +8,10 @@ class CommitStatus < Ci::ApplicationRecord
include EnumWithNil
include BulkInsertableAssociations
include TaggableQueries
include IgnorableColumns
self.table_name = 'ci_builds'
ignore_column :trace, remove_with: '15.6', remove_after: '2022-10-22'
belongs_to :user
belongs_to :project
......
......@@ -23,7 +23,6 @@ class Trace
attr_reader :job
delegate :old_trace, to: :job
delegate :can_attempt_archival_now?, :increment_archival_attempts!,
:archival_attempts_message, :archival_attempts_available?, to: :trace_metadata
......@@ -82,7 +81,7 @@ def archived?
end
def live?
job.trace_chunks.any? || current_path.present? || old_trace.present?
job.trace_chunks.any? || current_path.present?
end
def read(&block)
......@@ -111,7 +110,6 @@ def erase!
# Erase the live trace
erase_trace_chunks!
FileUtils.rm_f(current_path) if current_path # Remove a trace file of a live trace
job.erase_old_trace! if job.has_old_trace? # Remove a trace in database of a live trace
ensure
@current_path = nil
end
......@@ -162,8 +160,6 @@ def read_stream
Gitlab::Ci::Trace::ChunkedIO.new(job)
elsif current_path
File.open(current_path, "rb")
elsif old_trace
StringIO.new(old_trace)
end
end
......@@ -210,11 +206,6 @@ def unsafe_archive!
archive_stream!(stream)
FileUtils.rm(current_path)
end
elsif old_trace
StringIO.new(old_trace, 'rb').tap do |stream|
archive_stream!(stream)
job.erase_old_trace!
end
end
end
......
......@@ -1218,37 +1218,6 @@ def post_erase
end
end
context "when job has a trace in database" do
let(:job) { create(:ci_build, pipeline: pipeline) }
before do
job.update_column(:trace, "Sample trace")
end
it 'sends a trace file' do
response = subject
expect(response).to have_gitlab_http_status(:ok)
expect(response.headers['Content-Type']).to eq('text/plain; charset=utf-8')
expect(response.headers['Content-Disposition']).to match(/^inline/)
expect(response.body).to eq('Sample trace')
end
context 'when trace format is not text/plain' do
before do
job.update_column(:trace, '<html></html>')
end
it 'sets content disposition to attachment' do
response = subject
expect(response).to have_gitlab_http_status(:ok)
expect(response.headers['Content-Type']).to eq('text/plain; charset=utf-8')
expect(response.headers['Content-Disposition']).to match(/^attachment/)
end
end
end
context 'when job does not have a trace file' do
let(:job) { create(:ci_build, pipeline: pipeline) }
......
......@@ -10,7 +10,6 @@
describe "associations" do
it { expect(trace).to respond_to(:job) }
it { expect(trace).to delegate_method(:old_trace).to(:job) }
end
context 'when trace is migrated to object storage' do
......
......@@ -1225,70 +1225,6 @@
end
end
describe '#has_old_trace?' do
subject { build.has_old_trace? }
context 'when old trace exists' do
before do
build.update_column(:trace, 'old trace')
end
it { is_expected.to be_truthy }
end
context 'when old trace does not exist' do
it { is_expected.to be_falsy }
end
end
describe '#trace=' do
it "expect to fail trace=" do
expect { build.trace = "new" }.to raise_error(NotImplementedError)
end
end
describe '#old_trace' do
subject { build.old_trace }
before do
build.update_column(:trace, 'old trace')
end
it "expect to receive data from database" do
is_expected.to eq('old trace')
end
end
describe '#erase_old_trace!' do
subject { build.erase_old_trace! }
context 'when old trace exists' do
before do
build.update_column(:trace, 'old trace')
end
it "erases old trace" do
subject
expect(build.old_trace).to be_nil
end
it "executes UPDATE query" do
recorded = ActiveRecord::QueryRecorder.new { subject }
expect(recorded.log.count { |l| l.match?(/UPDATE.*ci_builds/) }).to eq(1)
end
end
context 'when old trace does not exist' do
it 'does not execute UPDATE query' do
recorded = ActiveRecord::QueryRecorder.new { subject }
expect(recorded.log.count { |l| l.match?(/UPDATE.*ci_builds/) }).to eq(0)
end
end
end
describe '#hide_secrets' do
let(:metrics) { spy('metrics') }
let(:subject) { build.hide_secrets(data) }
......
......@@ -57,8 +57,7 @@
let(:clone_accessors) { ::Ci::Build.clone_accessors.without(::Ci::Build.extra_accessors) }
let(:reject_accessors) do
%i[id status user token_encrypted coverage trace runner
artifacts_expire_at
%i[id status user token_encrypted coverage runner artifacts_expire_at
created_at updated_at started_at finished_at queued_at erased_by
erased_at auto_canceled_by job_artifacts job_artifacts_archive
job_artifacts_metadata job_artifacts_trace job_artifacts_junit
......
......@@ -370,15 +370,6 @@
end
end
shared_examples 'read successfully with StringIO' do
it 'yields with source' do
trace.read do |stream|
expect(stream).to be_a(Gitlab::Ci::Trace::Stream)
expect(stream.stream).to be_a(StringIO)
end
end
end
shared_examples 'failed to read' do
it 'yields without source' do
trace.read do |stream|
......@@ -404,14 +395,6 @@
it_behaves_like 'read successfully with IO'
end
context 'when db trace exists' do
before do
build.send(:write_attribute, :trace, "data")
end
it_behaves_like 'read successfully with StringIO'
end
context 'when no sources exist' do
it_behaves_like 'failed to read'
end
......@@ -462,25 +445,6 @@
expect(trace.exist?).to be(false)
end
end
context 'stored in database' do
before do
build.send(:write_attribute, :trace, "data")
end
it "trace exist" do
expect(trace.exist?).to be(true)
end
it "can be erased" do
trace.erase!
expect(trace.exist?).to be(false)
end
it "returns database data" do
expect(trace.raw).to eq("data")
end
end
end
describe '#archive!' do
......@@ -520,24 +484,12 @@
expect(build.trace.exist?).to be_truthy
expect(build.job_artifacts_trace.file.exists?).to be_truthy
expect(build.job_artifacts_trace.file.filename).to eq('job.log')
expect(build.old_trace).to be_nil
expect(src_checksum)
.to eq(described_class.sha256_hexdigest(build.job_artifacts_trace.file.path))
expect(build.job_artifacts_trace.file_sha256).to eq(src_checksum)
end
end
shared_examples 'source trace in database stays intact' do |error:|
it do
expect { subject }.to raise_error(error)
build.reload
expect(build.trace.exist?).to be_truthy
expect(build.job_artifacts_trace).to be_nil
expect(build.old_trace).to eq(trace_content)
end
end
context 'when job does not have trace artifact' do
context 'when trace file stored in default path' do
let!(:build) { create(:ci_build, :success, :trace_live) }
......@@ -564,58 +516,6 @@
it_behaves_like 'source trace file stays intact', error: ActiveRecord::RecordInvalid
end
end
context 'when trace is stored in database' do
let(:build) { create(:ci_build, :success) }
let(:trace_content) { 'Sample trace' }
let(:src_checksum) { Digest::SHA256.hexdigest(trace_content) }
before do
build.update_column(:trace, trace_content)
end
it_behaves_like 'archive trace in database'
context 'when failed to create clone file' do
before do
allow(IO).to receive(:copy_stream).and_return(0)
end
it_behaves_like 'source trace in database stays intact', error: Gitlab::Ci::Trace::ArchiveError
end
context 'when failed to create job artifact record' do
before do
allow_any_instance_of(Ci::JobArtifact).to receive(:save).and_return(false)
allow_any_instance_of(Ci::JobArtifact).to receive_message_chain(:errors, :full_messages)
.and_return(%w[Error Error])
end
it_behaves_like 'source trace in database stays intact', error: ActiveRecord::RecordInvalid
end
context 'when there is a validation error on Ci::Build' do
before do
allow_any_instance_of(Ci::Build).to receive(:save).and_return(false)
allow_any_instance_of(Ci::Build).to receive_message_chain(:errors, :full_messages)
.and_return(%w[Error Error])
end
context "when erase old trace with 'save'" do
before do
build.send(:write_attribute, :trace, nil)
build.save # rubocop:disable Rails/SaveBang
end
it 'old trace is not deleted' do
build.reload
expect(build.trace.raw).to eq(trace_content)
end
end
it_behaves_like 'archive trace in database'
end
end
end
context 'when job has trace artifact' do
......@@ -645,22 +545,6 @@
subject { trace.erase! }
context 'when it is a live trace' do
context 'when trace is stored in database' do
let(:build) { create(:ci_build) }
before do
build.update_column(:trace, 'sample trace')
end
it { expect(trace.raw).not_to be_nil }
it "removes trace" do
subject
expect(trace.raw).to be_nil
end
end
context 'when trace is stored in file storage' do
let(:build) { create(:ci_build, :trace_live) }
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment