summaryrefslogtreecommitdiff
path: root/spec/services/groups/update_shared_runners_service_spec.rb
blob: a29f73a71c2d6737163f74ae7fb3428ccc688f55 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
# 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) }

      let(:params) { { shared_runners_setting: 'enabled' } }

      before do
        group.add_maintainer(user)
      end

      it 'results error and does not call any method' do
        expect(group).not_to receive(:update_shared_runners_setting!)

        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
        let(:params) { { shared_runners_setting: 'enabled' } }

        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) }

          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')
          end
        end

        context 'root group with shared runners disabled' do
          let_it_be(:group) { create(:group, :shared_runners_disabled) }

          it 'receives correct method and succeeds' do
            expect(group).to receive(:update_shared_runners_setting!).with('enabled')

            expect(subject[:status]).to eq(:success)
          end
        end

        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
            expect(::Ci::UpdatePendingBuildService).to receive(:new).and_call_original

            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
              expect(::Ci::UpdatePendingBuildService).not_to receive(:new).and_call_original

              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
      end

      context 'disable shared Runners' do
        let_it_be(:group) { create(:group) }

        let(:params) { { shared_runners_setting: Namespace::SR_DISABLED_AND_UNOVERRIDABLE } }

        it 'receives correct method and succeeds' do
          expect(group).to receive(:update_shared_runners_setting!).with(Namespace::SR_DISABLED_AND_UNOVERRIDABLE)

          expect(subject[:status]).to eq(:success)
        end

        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
            expect(::Ci::UpdatePendingBuildService).to receive(:new).and_call_original

            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

      context 'allow descendants to override' do
        let(:params) { { shared_runners_setting: Namespace::SR_DISABLED_AND_OVERRIDABLE } }

        context 'top level group' do
          let_it_be(:group) { create(:group, :shared_runners_disabled) }

          it 'receives correct method and succeeds' do
            expect(group).to receive(:update_shared_runners_setting!).with(Namespace::SR_DISABLED_AND_OVERRIDABLE)

            expect(subject[:status]).to eq(:success)
          end
        end

        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) }

          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')
          end
        end

        context 'when using DISABLED_WITH_OVERRIDE (deprecated)' do
          let(:params) { { shared_runners_setting: Namespace::SR_DISABLED_WITH_OVERRIDE } }

          context 'top level group' do
            let_it_be(:group) { create(:group, :shared_runners_disabled) }

            it 'receives correct method and succeeds' do
              expect(group).to receive(:update_shared_runners_setting!).with(Namespace::SR_DISABLED_WITH_OVERRIDE)

              expect(subject[:status]).to eq(:success)
            end
          end

          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) }

            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')
            end
          end
        end
      end
    end
  end
end