2019-04-11 12:17:24 +00:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2018-11-29 05:34:14 +00:00
|
|
|
require 'spec_helper'
|
|
|
|
|
2020-06-24 06:09:01 +00:00
|
|
|
RSpec.describe Ci::ArchiveTraceService, '#execute' do
|
2021-07-13 15:08:38 +00:00
|
|
|
subject { described_class.new.execute(job, worker_name: Ci::ArchiveTraceWorker.name) }
|
2018-11-29 05:34:14 +00:00
|
|
|
|
|
|
|
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
|
2021-09-06 18:11:17 +00:00
|
|
|
expect(job.trace_metadata.trace_artifact).to eq(job.job_artifacts_trace)
|
2018-11-29 05:34:14 +00:00
|
|
|
end
|
2018-11-29 05:51:03 +00:00
|
|
|
|
2022-01-19 00:12:04 +00:00
|
|
|
context 'integration hooks' do
|
|
|
|
it do
|
|
|
|
stub_feature_flags(datadog_integration_logs_collection: [job.project])
|
|
|
|
|
|
|
|
expect(job.project).to receive(:execute_integrations) do |data, hook_type|
|
|
|
|
expect(data).to eq Gitlab::DataBuilder::ArchiveTrace.build(job)
|
|
|
|
expect(hook_type).to eq :archive_trace_hooks
|
|
|
|
end
|
|
|
|
expect { subject }.not_to raise_error
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'with feature flag disabled' do
|
|
|
|
stub_feature_flags(datadog_integration_logs_collection: false)
|
|
|
|
|
|
|
|
expect(job.project).not_to receive(:execute_integrations)
|
|
|
|
expect { subject }.not_to raise_error
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2018-11-29 05:51:03 +00:00
|
|
|
context 'when trace is already archived' do
|
|
|
|
let!(:job) { create(:ci_build, :success, :trace_artifact) }
|
2018-11-29 07:16:04 +00:00
|
|
|
|
2018-11-29 05:51:03 +00:00
|
|
|
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-06 12:09:21 +00:00
|
|
|
|
|
|
|
context 'when live trace chunks still exist' do
|
|
|
|
before do
|
2021-09-13 18:11:46 +00:00
|
|
|
create(:ci_build_trace_chunk, build: job, chunk_index: 0)
|
2021-04-06 12:09:21 +00:00
|
|
|
end
|
|
|
|
|
2021-07-01 15:07:40 +00:00
|
|
|
it 'removes the trace chunks' do
|
|
|
|
expect { subject }.to change { job.trace_chunks.count }.to(0)
|
2021-04-06 12:09:21 +00:00
|
|
|
end
|
|
|
|
|
2021-07-01 15:07:40 +00:00
|
|
|
context 'when associated data does not exist' do
|
2021-04-06 12:09:21 +00:00
|
|
|
before do
|
2021-07-01 15:07:40 +00:00
|
|
|
job.job_artifacts_trace.file.remove!
|
2021-04-06 12:09:21 +00:00
|
|
|
end
|
|
|
|
|
2021-09-13 18:11:46 +00:00
|
|
|
it 'removes the trace artifact and builds a new one' do
|
|
|
|
existing_trace = job.job_artifacts_trace
|
|
|
|
expect(existing_trace).to receive(:destroy!).and_call_original
|
|
|
|
|
|
|
|
subject
|
|
|
|
|
|
|
|
expect(job.reload.job_artifacts_trace).to be_present
|
|
|
|
expect(job.reload.job_artifacts_trace.file.file).to be_present
|
2021-04-06 12:09:21 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2018-11-29 05:51:03 +00:00
|
|
|
end
|
2019-07-18 09:22:46 +00:00
|
|
|
|
|
|
|
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(
|
2021-07-13 15:08:38 +00:00
|
|
|
class: Ci::ArchiveTraceWorker.name,
|
2019-07-18 09:22:46 +00:00
|
|
|
message: 'The job does not have live trace but going to be archived.',
|
|
|
|
job_id: job.id)
|
|
|
|
|
|
|
|
subject
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2021-09-06 18:11:17 +00:00
|
|
|
context 'when the job is out of archival attempts' do
|
|
|
|
before do
|
|
|
|
create(:ci_build_trace_metadata,
|
|
|
|
build: job,
|
|
|
|
archival_attempts: Ci::BuildTraceMetadata::MAX_ATTEMPTS + 1,
|
|
|
|
last_archival_attempt_at: 1.week.ago)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'skips archiving' do
|
|
|
|
expect(job.trace).not_to receive(:archive!)
|
|
|
|
|
|
|
|
subject
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'leaves a warning message in sidekiq log' do
|
|
|
|
expect(Sidekiq.logger).to receive(:warn).with(
|
|
|
|
class: Ci::ArchiveTraceWorker.name,
|
|
|
|
message: 'The job is out of archival attempts.',
|
|
|
|
job_id: job.id)
|
|
|
|
|
|
|
|
subject
|
|
|
|
end
|
2021-10-12 12:09:36 +00:00
|
|
|
|
|
|
|
context 'job has archive and chunks' do
|
|
|
|
let(:job) { create(:ci_build, :success, :trace_artifact) }
|
|
|
|
|
|
|
|
before do
|
|
|
|
create(:ci_build_trace_chunk, build: job, chunk_index: 0)
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'archive is not completed' do
|
|
|
|
before do
|
|
|
|
job.job_artifacts_trace.file.remove!
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'cleanups any stale archive data' do
|
|
|
|
expect(job.job_artifacts_trace).to be_present
|
|
|
|
|
|
|
|
subject
|
|
|
|
|
|
|
|
expect(job.reload.job_artifacts_trace).to be_nil
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'removes trace chunks' do
|
|
|
|
expect { subject }.to change { job.trace_chunks.count }.to(0)
|
|
|
|
end
|
|
|
|
end
|
2021-09-06 18:11:17 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
context 'when the archival process is backed off' do
|
|
|
|
before do
|
|
|
|
create(:ci_build_trace_metadata,
|
|
|
|
build: job,
|
|
|
|
archival_attempts: Ci::BuildTraceMetadata::MAX_ATTEMPTS - 1,
|
|
|
|
last_archival_attempt_at: 1.hour.ago)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'skips archiving' do
|
|
|
|
expect(job.trace).not_to receive(:archive!)
|
|
|
|
|
|
|
|
subject
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'leaves a warning message in sidekiq log' do
|
|
|
|
expect(Sidekiq.logger).to receive(:warn).with(
|
|
|
|
class: Ci::ArchiveTraceWorker.name,
|
|
|
|
message: 'The job can not be archived right now.',
|
|
|
|
job_id: job.id)
|
|
|
|
|
|
|
|
subject
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2019-07-18 09:22:46 +00:00
|
|
|
context 'when job failed to archive trace but did not raise an exception' do
|
|
|
|
before do
|
2019-12-16 12:07:43 +00:00
|
|
|
allow_next_instance_of(Gitlab::Ci::Trace) do |instance|
|
|
|
|
allow(instance).to receive(:archive!) {}
|
|
|
|
end
|
2019-07-18 09:22:46 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'leaves a warning message in sidekiq log' do
|
|
|
|
expect(Sidekiq.logger).to receive(:warn).with(
|
2021-07-13 15:08:38 +00:00
|
|
|
class: Ci::ArchiveTraceWorker.name,
|
2019-07-18 09:22:46 +00:00
|
|
|
message: 'The job does not have archived trace after archiving.',
|
|
|
|
job_id: job.id)
|
|
|
|
|
|
|
|
subject
|
|
|
|
end
|
|
|
|
end
|
2018-11-29 05:34:14 +00:00
|
|
|
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
|
2019-12-16 12:07:43 +00:00
|
|
|
expect(Gitlab::ErrorTracking)
|
2019-12-13 12:07:41 +00:00
|
|
|
.to receive(:track_and_raise_for_dev_exception)
|
2018-11-29 05:34:14 +00:00
|
|
|
.with(::Gitlab::Ci::Trace::ArchiveError,
|
2019-09-18 14:02:45 +00:00
|
|
|
issue_url: 'https://gitlab.com/gitlab-org/gitlab-foss/issues/51502',
|
2019-12-13 12:07:41 +00:00
|
|
|
job_id: job.id).once
|
2018-11-29 05:34:14 +00:00
|
|
|
|
2019-07-18 09:22:46 +00:00
|
|
|
expect(Sidekiq.logger).to receive(:warn).with(
|
2021-07-13 15:08:38 +00:00
|
|
|
class: Ci::ArchiveTraceWorker.name,
|
2019-07-18 09:22:46 +00:00
|
|
|
message: "Failed to archive trace. message: Job is not finished yet.",
|
|
|
|
job_id: job.id).and_call_original
|
2018-11-29 05:34:14 +00:00
|
|
|
|
|
|
|
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
|
2021-09-06 18:11:17 +00:00
|
|
|
expect(job.trace_metadata.archival_attempts).to eq(1)
|
2018-11-29 05:34:14 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|