debian-mirror-gitlab/spec/models/ci/processable_spec.rb

281 lines
8.6 KiB
Ruby
Raw Normal View History

2020-03-13 15:44:24 +05:30
# frozen_string_literal: true
require 'spec_helper'
2020-07-28 23:09:34 +05:30
RSpec.describe Ci::Processable do
2020-03-13 15:44:24 +05:30
let_it_be(:project) { create(:project) }
let_it_be(:pipeline) { create(:ci_pipeline, project: project) }
2020-05-24 23:13:21 +05:30
describe 'delegations' do
subject { Ci::Processable.new }
2020-04-22 19:07:51 +05:30
2020-05-24 23:13:21 +05:30
it { is_expected.to delegate_method(:merge_request?).to(:pipeline) }
it { is_expected.to delegate_method(:merge_request_ref?).to(:pipeline) }
it { is_expected.to delegate_method(:legacy_detached_merge_request_pipeline?).to(:pipeline) }
2020-04-22 19:07:51 +05:30
end
2022-06-21 17:19:12 +05:30
describe '#retryable' do
shared_examples_for 'retryable processable' do
context 'when processable is successful' do
before do
processable.success!
end
it { is_expected.to be_retryable }
end
context 'when processable is failed' do
before do
processable.drop!
end
it { is_expected.to be_retryable }
end
context 'when processable is canceled' do
before do
processable.cancel!
end
it { is_expected.to be_retryable }
end
end
shared_examples_for 'non-retryable processable' do
context 'when processable is skipped' do
before do
processable.skip!
end
it { is_expected.not_to be_retryable }
end
context 'when processable is degenerated' do
before do
processable.degenerate!
end
it { is_expected.not_to be_retryable }
end
context 'when a canceled processable has been retried already' do
before do
project.add_developer(create(:user))
processable.cancel!
processable.update!(retried: true)
end
it { is_expected.not_to be_retryable }
end
end
context 'when the processable is a build' do
subject(:processable) { create(:ci_build, pipeline: pipeline) }
context 'when the processable is retryable' do
it_behaves_like 'retryable processable'
context 'when deployment is rejected' do
before do
processable.drop!(:deployment_rejected)
end
it { is_expected.not_to be_retryable }
end
context 'when build is waiting for deployment approval' do
subject { build_stubbed(:ci_build, :manual, environment: 'production') }
before do
create(:deployment, :blocked, deployable: subject)
end
it { is_expected.not_to be_retryable }
end
end
context 'when the processable is non-retryable' do
it_behaves_like 'non-retryable processable'
context 'when processable is running' do
before do
processable.run!
end
it { is_expected.not_to be_retryable }
end
end
end
end
2020-03-13 15:44:24 +05:30
describe '#aggregated_needs_names' do
let(:with_aggregated_needs) { pipeline.processables.select_with_aggregated_needs(project) }
context 'with created status' do
let!(:processable) { create(:ci_build, :created, project: project, pipeline: pipeline) }
context 'with needs' do
before do
create(:ci_build_need, build: processable, name: 'test1')
create(:ci_build_need, build: processable, name: 'test2')
end
it 'returns all processables' do
expect(with_aggregated_needs).to contain_exactly(processable)
end
it 'returns all needs' do
expect(with_aggregated_needs.first.aggregated_needs_names).to contain_exactly('test1', 'test2')
end
end
context 'without needs' do
it 'returns all processables' do
expect(with_aggregated_needs).to contain_exactly(processable)
end
it 'returns empty needs' do
expect(with_aggregated_needs.first.aggregated_needs_names).to be_nil
end
end
end
end
describe 'validate presence of scheduling_type' do
2020-05-24 23:13:21 +05:30
using RSpec::Parameterized::TableSyntax
2020-03-13 15:44:24 +05:30
2020-05-24 23:13:21 +05:30
subject { build(:ci_build, project: project, pipeline: pipeline, importing: importing) }
2020-03-13 15:44:24 +05:30
2020-05-24 23:13:21 +05:30
where(:importing, :should_validate) do
false | true
true | false
end
2020-03-13 15:44:24 +05:30
2020-05-24 23:13:21 +05:30
with_them do
context 'on create' do
it 'validates presence' do
if should_validate
is_expected.to validate_presence_of(:scheduling_type).on(:create)
else
is_expected.not_to validate_presence_of(:scheduling_type).on(:create)
2020-03-13 15:44:24 +05:30
end
end
end
2020-05-24 23:13:21 +05:30
context 'on update' do
it { is_expected.not_to validate_presence_of(:scheduling_type).on(:update) }
2020-03-13 15:44:24 +05:30
end
end
end
describe '.populate_scheduling_type!' do
let!(:build_without_needs) { create(:ci_build, project: project, pipeline: pipeline) }
let!(:build_with_needs) { create(:ci_build, project: project, pipeline: pipeline) }
let!(:needs_relation) { create(:ci_build_need, build: build_with_needs) }
let!(:another_build) { create(:ci_build, project: project) }
before do
Ci::Processable.update_all(scheduling_type: nil)
end
it 'populates scheduling_type of processables' do
expect do
pipeline.processables.populate_scheduling_type!
end.to change(pipeline.processables.where(scheduling_type: nil), :count).from(2).to(0)
expect(build_without_needs.reload.scheduling_type).to eq('stage')
expect(build_with_needs.reload.scheduling_type).to eq('dag')
end
it 'does not affect processables from other pipelines' do
pipeline.processables.populate_scheduling_type!
expect(another_build.reload.scheduling_type).to be_nil
end
end
2020-04-08 14:13:33 +05:30
describe '#needs_attributes' do
let(:build) { create(:ci_build, :created, project: project, pipeline: pipeline) }
2020-05-24 23:13:21 +05:30
subject { build.needs_attributes }
2020-04-08 14:13:33 +05:30
context 'with needs' do
before do
create(:ci_build_need, build: build, name: 'test1')
create(:ci_build_need, build: build, name: 'test2')
end
it 'returns all needs attributes' do
2020-05-24 23:13:21 +05:30
is_expected.to contain_exactly(
2021-04-17 20:07:23 +05:30
{ 'artifacts' => true, 'name' => 'test1', 'optional' => false },
{ 'artifacts' => true, 'name' => 'test2', 'optional' => false }
2020-04-08 14:13:33 +05:30
)
end
end
context 'without needs' do
2020-05-24 23:13:21 +05:30
it { is_expected.to be_empty }
2020-04-22 19:07:51 +05:30
end
end
2021-03-11 19:13:27 +05:30
describe 'state transition with resource group' do
let(:resource_group) { create(:ci_resource_group, project: project) }
context 'when build status is created' do
let(:build) { create(:ci_build, :created, project: project, resource_group: resource_group) }
it 'is waiting for resource when build is enqueued' do
expect(Ci::ResourceGroups::AssignResourceFromResourceGroupWorker).to receive(:perform_async).with(resource_group.id)
expect { build.enqueue! }.to change { build.status }.from('created').to('waiting_for_resource')
expect(build.waiting_for_resource_at).not_to be_nil
end
context 'when build is waiting for resource' do
before do
build.update_column(:status, 'waiting_for_resource')
end
it 'is enqueued when build requests resource' do
expect { build.enqueue_waiting_for_resource! }.to change { build.status }.from('waiting_for_resource').to('pending')
end
it 'releases a resource when build finished' do
2021-11-18 22:05:49 +05:30
expect(build.resource_group).to receive(:release_resource_from).with(build).and_return(true).and_call_original
2021-03-11 19:13:27 +05:30
expect(Ci::ResourceGroups::AssignResourceFromResourceGroupWorker).to receive(:perform_async).with(build.resource_group_id)
build.enqueue_waiting_for_resource!
build.success!
end
2021-11-18 22:05:49 +05:30
it 're-checks the resource group even if the processable does not retain a resource' do
expect(build.resource_group).to receive(:release_resource_from).with(build).and_return(false).and_call_original
expect(Ci::ResourceGroups::AssignResourceFromResourceGroupWorker).to receive(:perform_async).with(build.resource_group_id)
build.success!
end
2021-03-11 19:13:27 +05:30
context 'when build has prerequisites' do
before do
allow(build).to receive(:any_unmet_prerequisites?) { true }
end
it 'is preparing when build is enqueued' do
expect { build.enqueue_waiting_for_resource! }.to change { build.status }.from('waiting_for_resource').to('preparing')
end
end
context 'when there are no available resources' do
before do
resource_group.assign_resource_to(create(:ci_build))
end
it 'stays as waiting for resource when build requests resource' do
expect { build.enqueue_waiting_for_resource }.not_to change { build.status }
end
end
end
end
end
2020-03-13 15:44:24 +05:30
end