debian-mirror-gitlab/spec/services/groups/update_shared_runners_service_spec.rb

142 lines
5.4 KiB
Ruby
Raw Normal View History

2020-07-28 23:09:34 +05:30
# frozen_string_literal: true
require 'spec_helper'
RSpec.describe Groups::UpdateSharedRunnersService do
let(:user) { create(:user) }
let(:group) { create(:group) }
let(:params) { {} }
describe '#execute' do
subject { described_class.new(group, user, params).execute }
context 'when current_user is not the group owner' do
let_it_be(:group) { create(:group) }
2021-01-03 14:25:43 +05:30
let(:params) { { shared_runners_setting: 'enabled' } }
2020-07-28 23:09:34 +05:30
before do
group.add_maintainer(user)
end
it 'results error and does not call any method' do
2021-01-03 14:25:43 +05:30
expect(group).not_to receive(:update_shared_runners_setting!)
2020-07-28 23:09:34 +05:30
expect(subject[:status]).to eq(:error)
expect(subject[:message]).to eq('Operation not allowed')
expect(subject[:http_status]).to eq(403)
end
end
context 'when current_user is the group owner' do
before do
group.add_owner(user)
end
context 'enable shared Runners' do
2021-01-03 14:25:43 +05:30
let(:params) { { shared_runners_setting: 'enabled' } }
2020-07-28 23:09:34 +05:30
2021-01-03 14:25:43 +05:30
context 'group that its ancestors have shared runners disabled' do
let_it_be(:parent) { create(:group, :shared_runners_disabled) }
let_it_be(:group) { create(:group, :shared_runners_disabled, parent: parent) }
2020-07-28 23:09:34 +05:30
2021-01-03 14:25:43 +05:30
it 'results error' do
expect(subject[:status]).to eq(:error)
expect(subject[:message]).to eq('Validation failed: Shared runners enabled cannot be enabled because parent group has shared Runners disabled')
2020-07-28 23:09:34 +05:30
end
end
2021-01-03 14:25:43 +05:30
context 'root group with shared runners disabled' do
let_it_be(:group) { create(:group, :shared_runners_disabled) }
2020-07-28 23:09:34 +05:30
it 'receives correct method and succeeds' do
2021-01-03 14:25:43 +05:30
expect(group).to receive(:update_shared_runners_setting!).with('enabled')
2020-07-28 23:09:34 +05:30
expect(subject[:status]).to eq(:success)
end
end
2021-11-11 11:23:49 +05:30
context 'when group has pending builds' do
let_it_be(:group) { create(:group, :shared_runners_disabled) }
let_it_be(:project) { create(:project, namespace: group, shared_runners_enabled: false) }
let_it_be(:pending_build_1) { create(:ci_pending_build, project: project, instance_runners_enabled: false) }
let_it_be(:pending_build_2) { create(:ci_pending_build, project: project, instance_runners_enabled: false) }
it 'updates pending builds for the group' do
2022-01-26 12:08:38 +05:30
expect(::Ci::UpdatePendingBuildService).to receive(:new).and_call_original
2021-11-11 11:23:49 +05:30
subject
expect(pending_build_1.reload.instance_runners_enabled).to be_truthy
expect(pending_build_2.reload.instance_runners_enabled).to be_truthy
end
context 'when shared runners is not toggled' do
let(:params) { { shared_runners_setting: 'invalid_enabled' } }
it 'does not update pending builds for the group' do
2022-01-26 12:08:38 +05:30
expect(::Ci::UpdatePendingBuildService).not_to receive(:new).and_call_original
2021-11-11 11:23:49 +05:30
subject
expect(pending_build_1.reload.instance_runners_enabled).to be_falsey
expect(pending_build_2.reload.instance_runners_enabled).to be_falsey
end
end
end
2020-07-28 23:09:34 +05:30
end
2021-01-03 14:25:43 +05:30
context 'disable shared Runners' do
let_it_be(:group) { create(:group) }
2021-04-29 21:17:54 +05:30
2021-11-18 22:05:49 +05:30
let(:params) { { shared_runners_setting: Namespace::SR_DISABLED_AND_UNOVERRIDABLE } }
2020-07-28 23:09:34 +05:30
2021-01-03 14:25:43 +05:30
it 'receives correct method and succeeds' do
2021-11-18 22:05:49 +05:30
expect(group).to receive(:update_shared_runners_setting!).with(Namespace::SR_DISABLED_AND_UNOVERRIDABLE)
2020-07-28 23:09:34 +05:30
2021-01-03 14:25:43 +05:30
expect(subject[:status]).to eq(:success)
2020-07-28 23:09:34 +05:30
end
2021-11-11 11:23:49 +05:30
context 'when group has pending builds' do
let_it_be(:project) { create(:project, namespace: group) }
let_it_be(:pending_build_1) { create(:ci_pending_build, project: project, instance_runners_enabled: true) }
let_it_be(:pending_build_2) { create(:ci_pending_build, project: project, instance_runners_enabled: true) }
it 'updates pending builds for the group' do
2022-01-26 12:08:38 +05:30
expect(::Ci::UpdatePendingBuildService).to receive(:new).and_call_original
2021-11-11 11:23:49 +05:30
subject
expect(pending_build_1.reload.instance_runners_enabled).to be_falsey
expect(pending_build_2.reload.instance_runners_enabled).to be_falsey
end
end
2020-07-28 23:09:34 +05:30
end
2021-01-03 14:25:43 +05:30
context 'allow descendants to override' do
2021-11-18 22:05:49 +05:30
let(:params) { { shared_runners_setting: Namespace::SR_DISABLED_WITH_OVERRIDE } }
2020-07-28 23:09:34 +05:30
2021-01-03 14:25:43 +05:30
context 'top level group' do
2020-07-28 23:09:34 +05:30
let_it_be(:group) { create(:group, :shared_runners_disabled) }
2021-01-03 14:25:43 +05:30
it 'receives correct method and succeeds' do
2021-11-18 22:05:49 +05:30
expect(group).to receive(:update_shared_runners_setting!).with(Namespace::SR_DISABLED_WITH_OVERRIDE)
2020-07-28 23:09:34 +05:30
2021-01-03 14:25:43 +05:30
expect(subject[:status]).to eq(:success)
2020-07-28 23:09:34 +05:30
end
end
2021-01-03 14:25:43 +05:30
context 'when parent does not allow' do
let_it_be(:parent) { create(:group, :shared_runners_disabled, allow_descendants_override_disabled_shared_runners: false ) }
let_it_be(:group) { create(:group, :shared_runners_disabled, allow_descendants_override_disabled_shared_runners: false, parent: parent) }
2020-07-28 23:09:34 +05:30
2021-01-03 14:25:43 +05:30
it 'results error' do
expect(subject[:status]).to eq(:error)
expect(subject[:message]).to eq('Validation failed: Allow descendants override disabled shared runners cannot be enabled because parent group does not allow it')
2020-07-28 23:09:34 +05:30
end
end
end
end
end
end