2019-04-11 08:17:24 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2018-09-27 04:50:50 -04:00
|
|
|
require 'spec_helper'
|
|
|
|
|
2020-06-24 02:09:01 -04:00
|
|
|
RSpec.describe Ci::RunScheduledBuildService do
|
2018-09-27 04:50:50 -04:00
|
|
|
let(:user) { create(:user) }
|
|
|
|
let(:project) { create(:project) }
|
|
|
|
let(:pipeline) { create(:ci_pipeline, project: project) }
|
|
|
|
|
|
|
|
subject { described_class.new(project, user).execute(build) }
|
|
|
|
|
|
|
|
context 'when user can update build' do
|
|
|
|
before do
|
|
|
|
project.add_developer(user)
|
|
|
|
|
|
|
|
create(:protected_branch, :developers_can_merge,
|
|
|
|
name: pipeline.ref, project: project)
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when build is scheduled' do
|
|
|
|
context 'when scheduled_at is expired' do
|
|
|
|
let(:build) { create(:ci_build, :expired_scheduled, user: user, project: project, pipeline: pipeline) }
|
|
|
|
|
|
|
|
it 'can run the build' do
|
|
|
|
expect { subject }.not_to raise_error
|
|
|
|
|
|
|
|
expect(build).to be_pending
|
|
|
|
end
|
2019-12-24 07:08:01 -05:00
|
|
|
|
|
|
|
context 'when build requires resource' do
|
|
|
|
let(:resource_group) { create(:ci_resource_group, project: project) }
|
|
|
|
|
|
|
|
before do
|
|
|
|
build.update!(resource_group: resource_group)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'transits to waiting for resource status' do
|
|
|
|
expect { subject }.to change { build.status }.from('scheduled').to('waiting_for_resource')
|
|
|
|
end
|
|
|
|
end
|
2018-09-27 04:50:50 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
context 'when scheduled_at is not expired' do
|
|
|
|
let(:build) { create(:ci_build, :scheduled, user: user, project: project, pipeline: pipeline) }
|
|
|
|
|
2018-10-01 00:57:11 -04:00
|
|
|
it 'can not run the build' do
|
2018-09-27 04:50:50 -04:00
|
|
|
expect { subject }.to raise_error(StateMachines::InvalidTransition)
|
|
|
|
|
|
|
|
expect(build).to be_scheduled
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when build is not scheduled' do
|
|
|
|
let(:build) { create(:ci_build, :created, user: user, project: project, pipeline: pipeline) }
|
|
|
|
|
|
|
|
it 'can not run the build' do
|
|
|
|
expect { subject }.to raise_error(StateMachines::InvalidTransition)
|
|
|
|
|
|
|
|
expect(build).to be_created
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when user can not update build' do
|
|
|
|
context 'when build is scheduled' do
|
|
|
|
let(:build) { create(:ci_build, :scheduled, user: user, project: project, pipeline: pipeline) }
|
|
|
|
|
|
|
|
it 'can not run the build' do
|
|
|
|
expect { subject }.to raise_error(Gitlab::Access::AccessDeniedError)
|
|
|
|
|
|
|
|
expect(build).to be_scheduled
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|