2019-07-31 22:56:46 +05:30
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2017-08-17 22:00:37 +05:30
|
|
|
require 'spec_helper'
|
|
|
|
|
2023-05-27 22:25:52 +05:30
|
|
|
RSpec.describe Ci::PlayBuildService, '#execute', feature_category: :continuous_integration do
|
2019-09-04 21:01:54 +05:30
|
|
|
let(:user) { create(:user, developer_projects: [project]) }
|
2017-09-10 17:25:29 +05:30
|
|
|
let(:project) { create(:project) }
|
2017-08-17 22:00:37 +05:30
|
|
|
let(:pipeline) { create(:ci_pipeline, project: project) }
|
|
|
|
let(:build) { create(:ci_build, :manual, pipeline: pipeline) }
|
|
|
|
|
|
|
|
let(:service) do
|
|
|
|
described_class.new(project, user)
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when project does not have repository yet' do
|
2017-09-10 17:25:29 +05:30
|
|
|
let(:project) { create(:project) }
|
2017-08-17 22:00:37 +05:30
|
|
|
|
|
|
|
it 'allows user to play build if protected branch rules are met' do
|
|
|
|
create(:protected_branch, :developers_can_merge,
|
|
|
|
name: build.ref, project: project)
|
|
|
|
|
|
|
|
service.execute(build)
|
|
|
|
|
|
|
|
expect(build.reload).to be_pending
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'does not allow user with developer role to play build' do
|
|
|
|
expect { service.execute(build) }
|
|
|
|
.to raise_error Gitlab::Access::AccessDeniedError
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when project has repository' do
|
2017-09-10 17:25:29 +05:30
|
|
|
let(:project) { create(:project, :repository) }
|
2017-08-17 22:00:37 +05:30
|
|
|
|
|
|
|
it 'allows user with developer role to play a build' do
|
|
|
|
service.execute(build)
|
|
|
|
|
|
|
|
expect(build.reload).to be_pending
|
|
|
|
end
|
2019-09-04 21:01:54 +05:30
|
|
|
|
|
|
|
it 'prevents a blocked developer from playing a build' do
|
|
|
|
user.block!
|
|
|
|
|
|
|
|
expect { service.execute(build) }.to raise_error(Gitlab::Access::AccessDeniedError)
|
|
|
|
end
|
2017-08-17 22:00:37 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
context 'when build is a playable manual action' do
|
|
|
|
let(:build) { create(:ci_build, :manual, pipeline: pipeline) }
|
2019-09-04 21:01:54 +05:30
|
|
|
let!(:branch) { create(:protected_branch, :developers_can_merge, name: build.ref, project: project) }
|
2017-08-17 22:00:37 +05:30
|
|
|
|
|
|
|
it 'enqueues the build' do
|
|
|
|
expect(service.execute(build)).to eq build
|
|
|
|
expect(build.reload).to be_pending
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'reassignes build user correctly' do
|
|
|
|
service.execute(build)
|
|
|
|
|
|
|
|
expect(build.reload.user).to eq user
|
|
|
|
end
|
2019-10-12 21:52:04 +05:30
|
|
|
|
2021-04-29 21:17:54 +05:30
|
|
|
context 'when a subsequent job is skipped' do
|
|
|
|
let!(:job) { create(:ci_build, :skipped, pipeline: pipeline, stage_idx: build.stage_idx + 1) }
|
|
|
|
|
|
|
|
before do
|
|
|
|
create(:ci_build_need, build: job, name: build.name)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'marks the subsequent job as processable' do
|
|
|
|
expect { service.execute(build) }.to change { job.reload.status }.from('skipped').to('created')
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2019-10-12 21:52:04 +05:30
|
|
|
context 'when variables are supplied' do
|
|
|
|
let(:job_variables) do
|
|
|
|
[{ key: 'first', secret_value: 'first' },
|
|
|
|
{ key: 'second', secret_value: 'second' }]
|
|
|
|
end
|
|
|
|
|
2022-01-26 12:08:38 +05:30
|
|
|
subject { service.execute(build, job_variables) }
|
|
|
|
|
2019-10-12 21:52:04 +05:30
|
|
|
it 'assigns the variables to the build' do
|
2022-01-26 12:08:38 +05:30
|
|
|
subject
|
2019-10-12 21:52:04 +05:30
|
|
|
|
|
|
|
expect(build.reload.job_variables.map(&:key)).to contain_exactly('first', 'second')
|
|
|
|
end
|
2021-03-08 18:12:59 +05:30
|
|
|
|
2023-01-13 00:05:48 +05:30
|
|
|
context 'and variables are invalid' do
|
2022-01-26 12:08:38 +05:30
|
|
|
let(:job_variables) { [{}] }
|
|
|
|
|
2023-01-13 00:05:48 +05:30
|
|
|
it 'resets the attributes of the build' do
|
|
|
|
build.update!(job_variables_attributes: [{ key: 'old', value: 'old variable' }])
|
|
|
|
|
|
|
|
subject
|
|
|
|
|
|
|
|
expect(build.job_variables.map(&:key)).to contain_exactly('old')
|
2022-01-26 12:08:38 +05:30
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2021-03-08 18:12:59 +05:30
|
|
|
context 'when user defined variables are restricted' do
|
|
|
|
before do
|
|
|
|
project.update!(restrict_user_defined_variables: true)
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when user is maintainer' do
|
|
|
|
before do
|
|
|
|
project.add_maintainer(user)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'assigns the variables to the build' do
|
2022-01-26 12:08:38 +05:30
|
|
|
subject
|
2021-03-08 18:12:59 +05:30
|
|
|
|
|
|
|
expect(build.reload.job_variables.map(&:key)).to contain_exactly('first', 'second')
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when user is developer' do
|
|
|
|
it 'raises an error' do
|
2022-01-26 12:08:38 +05:30
|
|
|
expect { subject }.to raise_error Gitlab::Access::AccessDeniedError
|
2021-03-08 18:12:59 +05:30
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2019-10-12 21:52:04 +05:30
|
|
|
end
|
2017-08-17 22:00:37 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
context 'when build is not a playable manual action' do
|
2022-03-02 08:16:31 +05:30
|
|
|
let(:build) { create(:ci_build, :success, pipeline: pipeline) }
|
2019-09-04 21:01:54 +05:30
|
|
|
let!(:branch) { create(:protected_branch, :developers_can_merge, name: build.ref, project: project) }
|
2017-08-17 22:00:37 +05:30
|
|
|
|
|
|
|
it 'duplicates the build' do
|
|
|
|
duplicate = service.execute(build)
|
|
|
|
|
|
|
|
expect(duplicate).not_to eq build
|
|
|
|
expect(duplicate).to be_pending
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'assigns users correctly' do
|
|
|
|
duplicate = service.execute(build)
|
|
|
|
|
|
|
|
expect(build.user).not_to eq user
|
|
|
|
expect(duplicate.user).to eq user
|
|
|
|
end
|
2022-03-02 08:16:31 +05:30
|
|
|
|
|
|
|
context 'and is not retryable' do
|
|
|
|
let(:build) { create(:ci_build, :deployment_rejected, pipeline: pipeline) }
|
|
|
|
|
|
|
|
it 'does not duplicate the build' do
|
|
|
|
expect { service.execute(build) }.not_to change { Ci::Build.count }
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'does not enqueue the build' do
|
|
|
|
expect { service.execute(build) }.not_to change { build.status }
|
|
|
|
end
|
|
|
|
end
|
2017-08-17 22:00:37 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
context 'when build is not action' do
|
2019-09-04 21:01:54 +05:30
|
|
|
let(:user) { create(:user) }
|
2017-08-17 22:00:37 +05:30
|
|
|
let(:build) { create(:ci_build, :success, pipeline: pipeline) }
|
|
|
|
|
|
|
|
it 'raises an error' do
|
|
|
|
expect { service.execute(build) }
|
|
|
|
.to raise_error Gitlab::Access::AccessDeniedError
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when user does not have ability to trigger action' do
|
2019-09-04 21:01:54 +05:30
|
|
|
let(:user) { create(:user) }
|
|
|
|
let!(:branch) { create(:protected_branch, :developers_can_merge, name: build.ref, project: project) }
|
2017-08-17 22:00:37 +05:30
|
|
|
|
|
|
|
it 'raises an error' do
|
|
|
|
expect { service.execute(build) }
|
|
|
|
.to raise_error Gitlab::Access::AccessDeniedError
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|