Commit 32f825c6 authored by Shinya Maeda's avatar Shinya Maeda

Add tests for each new code

parent 10acdc30
...@@ -60,7 +60,7 @@ module Ci ...@@ -60,7 +60,7 @@ module Ci
.merge(Ci::Build.finished) .merge(Ci::Build.finished)
.where('ci_builds.finished_at < ?', finished_before) .where('ci_builds.finished_at < ?', finished_before)
.each_batch(column: :build_id) do |chunks| .each_batch(column: :build_id) do |chunks|
build_ids = chunks.map { |chunk| [chunk.build_id] } build_ids = chunks.map { |chunk| chunk.build_id }
yield build_ids yield build_ids
end end
......
require 'spec_helper' require 'spec_helper'
describe Gitlab::Ci::Trace, :clean_gitlab_redis_cache do describe Gitlab::Ci::Trace, :clean_gitlab_redis_shared_state do
let(:build) { create(:ci_build) } let(:build) { create(:ci_build) }
let(:trace) { described_class.new(build) } let(:trace) { described_class.new(build) }
......
...@@ -35,6 +35,46 @@ describe Ci::BuildTraceChunk, :clean_gitlab_redis_shared_state do ...@@ -35,6 +35,46 @@ describe Ci::BuildTraceChunk, :clean_gitlab_redis_shared_state do
end end
end end
describe '.find_stale_in_batches' do
subject { described_class.find_stale_in_batches }
context 'when build status is finished' do
context 'when build finished 2 days ago' do
context 'when build has an archived trace' do
let!(:build) { create(:ci_build, :success, :trace_artifact, finished_at: 2.days.ago) }
it 'does not yield build id' do
expect { |b| described_class.find_stale_in_batches(&b) }.not_to yield_control
end
end
context 'when build has a live trace' do
let!(:build) { create(:ci_build, :success, :trace_live, finished_at: 2.days.ago) }
it 'yields build id' do
expect { |b| described_class.find_stale_in_batches(&b) }.to yield_with_args([build.id])
end
end
end
context 'when build finished 10 minutes ago' do
let!(:build) { create(:ci_build, :success, :trace_live, finished_at: 10.minutes.ago) }
it 'does not yield build id' do
expect { |b| described_class.find_stale_in_batches(&b) }.not_to yield_control
end
end
end
context 'when build status is running' do
let!(:build) { create(:ci_build, :running, :trace_live) }
it 'does not yield build id' do
expect { |b| described_class.find_stale_in_batches(&b) }.not_to yield_control
end
end
end
describe '#data' do describe '#data' do
subject { build_trace_chunk.data } subject { build_trace_chunk.data }
......
...@@ -227,6 +227,31 @@ shared_examples_for 'common trace features' do ...@@ -227,6 +227,31 @@ shared_examples_for 'common trace features' do
end end
end end
end end
describe '#archive!' do
subject { trace.archive! }
context 'when build status is success' do
let!(:build) { create(:ci_build, :success, :trace_live) }
it 'archives a trace' do
subject
expect(build.job_artifacts_trace).to be_exist
end
context 'when anothe process had already been archiving', :clean_gitlab_redis_shared_state do
before do
Gitlab::ExclusiveLease.new("trace:archive:#{trace.job.id}", timeout: 1.hour).try_obtain
end
it 'prevents multiple archiving' do
build.reload
expect(build.job_artifacts_trace).to be_nil
end
end
end
end
end end
shared_examples_for 'trace with disabled live trace feature' do shared_examples_for 'trace with disabled live trace feature' do
......
...@@ -7,19 +7,20 @@ describe Ci::RescueStaleLiveTraceWorker do ...@@ -7,19 +7,20 @@ describe Ci::RescueStaleLiveTraceWorker do
stub_feature_flags(ci_enable_live_trace: true) stub_feature_flags(ci_enable_live_trace: true)
end end
shared_examples_for 'schedules to archive traces' do shared_examples_for 'archives trace' do
it do it do
expect(ArchiveTraceWorker).to receive(:bulk_perform_async).with([[build.id]])
subject subject
expect(build.job_artifacts_trace).to be_exist
end end
end end
shared_examples_for 'does not schedule to archive traces' do shared_examples_for 'does not archive trace' do
it do it do
expect(ArchiveTraceWorker).not_to receive(:bulk_perform_async)
subject subject
build.reload
expect(build.job_artifacts_trace).to be_nil
end end
end end
...@@ -30,7 +31,18 @@ describe Ci::RescueStaleLiveTraceWorker do ...@@ -30,7 +31,18 @@ describe Ci::RescueStaleLiveTraceWorker do
build.update(finished_at: 2.hours.ago) build.update(finished_at: 2.hours.ago)
end end
it_behaves_like 'schedules to archive traces' it_behaves_like 'archives trace'
context 'when build has both archived trace and live trace' do
let!(:build2) { create(:ci_build, :success, :trace_live, finished_at: 2.days.ago) }
it 'archives only available targets' do
subject
build.reload
expect(build.job_artifacts_trace).to be_exist
end
end
end end
context 'when a job was failed 2 hours ago' do context 'when a job was failed 2 hours ago' do
...@@ -40,7 +52,7 @@ describe Ci::RescueStaleLiveTraceWorker do ...@@ -40,7 +52,7 @@ describe Ci::RescueStaleLiveTraceWorker do
build.update(finished_at: 2.hours.ago) build.update(finished_at: 2.hours.ago)
end end
it_behaves_like 'schedules to archive traces' it_behaves_like 'archives trace'
end end
context 'when a job was cancelled 2 hours ago' do context 'when a job was cancelled 2 hours ago' do
...@@ -50,7 +62,7 @@ describe Ci::RescueStaleLiveTraceWorker do ...@@ -50,7 +62,7 @@ describe Ci::RescueStaleLiveTraceWorker do
build.update(finished_at: 2.hours.ago) build.update(finished_at: 2.hours.ago)
end end
it_behaves_like 'schedules to archive traces' it_behaves_like 'archives trace'
end end
context 'when a job has been finished 10 minutes ago' do context 'when a job has been finished 10 minutes ago' do
...@@ -60,12 +72,12 @@ describe Ci::RescueStaleLiveTraceWorker do ...@@ -60,12 +72,12 @@ describe Ci::RescueStaleLiveTraceWorker do
build.update(finished_at: 10.minutes.ago) build.update(finished_at: 10.minutes.ago)
end end
it_behaves_like 'does not schedule to archive traces' it_behaves_like 'does not archive trace'
end end
context 'when a job is running' do context 'when a job is running' do
let!(:build) { create(:ci_build, :running, :trace_live) } let!(:build) { create(:ci_build, :running, :trace_live) }
it_behaves_like 'does not schedule to archive traces' it_behaves_like 'does not archive trace'
end end
end end
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment