debian-mirror-gitlab/spec/services/ci/archive_trace_service_spec.rb

130 lines
4.1 KiB
Ruby
Raw Normal View History

2019-07-31 22:56:46 +05:30
# frozen_string_literal: true
2019-02-15 15:39:39 +05:30
require 'spec_helper'
2020-07-28 23:09:34 +05:30
RSpec.describe Ci::ArchiveTraceService, '#execute' do
2019-10-12 21:52:04 +05:30
subject { described_class.new.execute(job, worker_name: ArchiveTraceWorker.name) }
2019-02-15 15:39:39 +05:30
context 'when job is finished' do
let(:job) { create(:ci_build, :success, :trace_live) }
it 'creates an archived trace' do
expect { subject }.not_to raise_error
expect(job.reload.job_artifacts_trace).to be_exist
end
context 'when trace is already archived' do
let!(:job) { create(:ci_build, :success, :trace_artifact) }
it 'ignores an exception' do
expect { subject }.not_to raise_error
end
it 'does not create an archived trace' do
expect { subject }.not_to change { Ci::JobArtifact.trace.count }
end
2021-04-29 21:17:54 +05:30
context 'when live trace chunks still exist' do
before do
create(:ci_build_trace_chunk, build: job)
end
context 'when the feature flag `erase_traces_from_already_archived_jobs_when_archiving_again` is enabled' do
before do
stub_feature_flags(erase_traces_from_already_archived_jobs_when_archiving_again: true)
end
it 'removes the trace chunks' do
expect { subject }.to change { job.trace_chunks.count }.to(0)
end
context 'when associated data does not exist' do
before do
job.job_artifacts_trace.file.remove!
end
it 'removes the trace artifact' do
expect { subject }.to change { job.reload.job_artifacts_trace }.to(nil)
end
end
end
context 'when the feature flag `erase_traces_from_already_archived_jobs_when_archiving_again` is disabled' do
before do
stub_feature_flags(erase_traces_from_already_archived_jobs_when_archiving_again: false)
end
it 'does not remove the trace chunks' do
expect { subject }.not_to change { job.trace_chunks.count }
end
context 'when associated data does not exist' do
before do
job.job_artifacts_trace.file.remove!
end
it 'does not remove the trace artifact' do
expect { subject }.not_to change { job.reload.job_artifacts_trace }
end
end
end
end
2019-02-15 15:39:39 +05:30
end
2019-10-12 21:52:04 +05:30
context 'when job does not have trace' do
let(:job) { create(:ci_build, :success) }
it 'leaves a warning message in sidekiq log' do
expect(Sidekiq.logger).to receive(:warn).with(
class: ArchiveTraceWorker.name,
message: 'The job does not have live trace but going to be archived.',
job_id: job.id)
subject
end
end
context 'when job failed to archive trace but did not raise an exception' do
before do
2020-01-01 13:55:28 +05:30
allow_next_instance_of(Gitlab::Ci::Trace) do |instance|
allow(instance).to receive(:archive!) {}
end
2019-10-12 21:52:04 +05:30
end
it 'leaves a warning message in sidekiq log' do
expect(Sidekiq.logger).to receive(:warn).with(
class: ArchiveTraceWorker.name,
message: 'The job does not have archived trace after archiving.',
job_id: job.id)
subject
end
end
2019-02-15 15:39:39 +05:30
end
context 'when job is running' do
let(:job) { create(:ci_build, :running, :trace_live) }
it 'increments Prometheus counter, sends crash report to Sentry and ignore an error for continuing to archive' do
2020-01-01 13:55:28 +05:30
expect(Gitlab::ErrorTracking)
.to receive(:track_and_raise_for_dev_exception)
2019-02-15 15:39:39 +05:30
.with(::Gitlab::Ci::Trace::ArchiveError,
2019-12-04 20:38:33 +05:30
issue_url: 'https://gitlab.com/gitlab-org/gitlab-foss/issues/51502',
2020-01-01 13:55:28 +05:30
job_id: job.id).once
2019-02-15 15:39:39 +05:30
2019-10-12 21:52:04 +05:30
expect(Sidekiq.logger).to receive(:warn).with(
class: ArchiveTraceWorker.name,
message: "Failed to archive trace. message: Job is not finished yet.",
job_id: job.id).and_call_original
2019-02-15 15:39:39 +05:30
expect(Gitlab::Metrics)
.to receive(:counter)
.with(:job_trace_archive_failed_total, "Counter of failed attempts of trace archiving")
.and_call_original
expect { subject }.not_to raise_error
end
end
end