summaryrefslogtreecommitdiff
path: root/spec/controllers/projects/settings/ci_cd_controller_spec.rb
blob: d50f1aa1dd8bb3c8e0acf919b829cf12547fb6af (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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
# frozen_string_literal: true

require('spec_helper')

RSpec.describe Projects::Settings::CiCdController do
  let_it_be(:user) { create(:user) }
  let_it_be(:project_auto_devops) { create(:project_auto_devops) }

  let(:project) { project_auto_devops.project }

  before do
    project.add_maintainer(user)
    sign_in(user)
  end

  describe 'GET show' do
    let_it_be(:parent_group) { create(:group) }
    let_it_be(:group) { create(:group, parent: parent_group) }
    let_it_be(:other_project) { create(:project, group: group) }

    it 'renders show with 200 status code' do
      get :show, params: { namespace_id: project.namespace, project_id: project }

      expect(response).to have_gitlab_http_status(:ok)
      expect(response).to render_template(:show)
    end

    context 'with CI/CD disabled' do
      before do
        project.project_feature.update_attribute(:builds_access_level, ProjectFeature::DISABLED)
      end

      it 'renders show with 404 status code' do
        get :show, params: { namespace_id: project.namespace, project_id: project }
        expect(response).to have_gitlab_http_status(:not_found)
      end
    end

    context 'with group runners' do
      let_it_be(:group_runner) { create(:ci_runner, :group, groups: [group]) }
      let_it_be(:project_runner) { create(:ci_runner, :project, projects: [other_project]) }
      let_it_be(:shared_runner) { create(:ci_runner, :instance) }

      it 'sets assignable project runners only' do
        group.add_maintainer(user)

        get :show, params: { namespace_id: project.namespace, project_id: project }

        expect(assigns(:assignable_runners)).to contain_exactly(project_runner)
      end
    end

    context 'prevents N+1 queries for tags' do
      render_views

      def show
        get :show, params: { namespace_id: project.namespace, project_id: project }
      end

      it 'has the same number of queries with one tag or with many tags', :request_store do
        group.add_maintainer(user)

        show # warmup

        # with one tag
        create(:ci_runner, :instance, tag_list: %w(shared_runner))
        create(:ci_runner, :project, projects: [other_project], tag_list: %w(project_runner))
        create(:ci_runner, :group, groups: [group], tag_list: %w(group_runner))
        control = ActiveRecord::QueryRecorder.new { show }

        # with several tags
        create(:ci_runner, :instance, tag_list: %w(shared_runner tag2 tag3))
        create(:ci_runner, :project, projects: [other_project], tag_list: %w(project_runner tag2 tag3))
        create(:ci_runner, :group, groups: [group], tag_list: %w(group_runner tag2 tag3))

        expect { show }.not_to exceed_query_limit(control)
      end
    end
  end

  describe '#reset_cache' do
    before do
      sign_in(user)

      project.add_maintainer(user)

      allow(ResetProjectCacheService).to receive_message_chain(:new, :execute).and_return(true)
    end

    subject { post :reset_cache, params: { namespace_id: project.namespace, project_id: project }, format: :json }

    it 'calls reset project cache service' do
      expect(ResetProjectCacheService).to receive_message_chain(:new, :execute)

      subject
    end

    context 'when service returns successfully' do
      it 'returns a success header' do
        subject

        expect(response).to have_gitlab_http_status(:ok)
      end
    end

    context 'when service does not return successfully' do
      before do
        allow(ResetProjectCacheService).to receive_message_chain(:new, :execute).and_return(false)
      end

      it 'returns an error header' do
        subject

        expect(response).to have_gitlab_http_status(:bad_request)
      end
    end
  end

  describe 'PUT #reset_registration_token' do
    subject { put :reset_registration_token, params: { namespace_id: project.namespace, project_id: project } }

    it 'resets runner registration token' do
      expect { subject }.to change { project.reload.runners_token }
      expect(flash[:toast]).to eq('New runners registration token has been generated!')
    end

    it 'redirects the user to admin runners page' do
      subject

      expect(response).to redirect_to(namespace_project_settings_ci_cd_path)
    end
  end

  describe 'PATCH update' do
    let(:params) { { ci_config_path: '' } }

    subject do
      patch :update,
            params: {
              namespace_id: project.namespace.to_param,
              project_id: project,
              project: params
            }
    end

    it 'redirects to the settings page' do
      subject

      expect(response).to have_gitlab_http_status(:found)
      expect(flash[:toast]).to eq("Pipelines settings for '#{project.name}' were successfully updated.")
    end

    context 'when updating the auto_devops settings' do
      let(:params) { { auto_devops_attributes: { enabled: '' } } }

      context 'following the instance default' do
        let(:params) { { auto_devops_attributes: { enabled: '' } } }

        it 'allows enabled to be set to nil' do
          subject
          project_auto_devops.reload

          expect(project_auto_devops.enabled).to be_nil
        end
      end

      context 'when run_auto_devops_pipeline is true' do
        before do
          expect_next_instance_of(Projects::UpdateService) do |instance|
            expect(instance).to receive(:run_auto_devops_pipeline?).and_return(true)
          end
        end

        context 'when the project repository is empty' do
          it 'sets a notice flash' do
            subject

            expect(controller).to set_flash[:notice]
          end

          it 'does not queue a CreatePipelineWorker' do
            expect(CreatePipelineWorker).not_to receive(:perform_async).with(project.id, user.id, project.default_branch, :web, any_args)

            subject
          end
        end

        context 'when the project repository is not empty' do
          let(:project) { create(:project, :repository) }

          it 'displays a toast message' do
            allow(CreatePipelineWorker).to receive(:perform_async).with(project.id, user.id, project.default_branch, :web, any_args)

            subject

            expect(controller).to set_flash[:toast]
          end

          it 'queues a CreatePipelineWorker' do
            expect(CreatePipelineWorker).to receive(:perform_async).with(project.id, user.id, project.default_branch, :web, any_args)

            subject
          end

          it 'creates a pipeline', :sidekiq_inline do
            project.repository.create_file(user, 'Gemfile', 'Gemfile contents',
                                           message: 'Add Gemfile',
                                           branch_name: 'master')

            expect { subject }.to change { Ci::Pipeline.count }.by(1)
          end
        end
      end

      context 'when run_auto_devops_pipeline is not true' do
        before do
          expect_next_instance_of(Projects::UpdateService) do |instance|
            expect(instance).to receive(:run_auto_devops_pipeline?).and_return(false)
          end
        end

        it 'does not queue a CreatePipelineWorker' do
          expect(CreatePipelineWorker).not_to receive(:perform_async).with(project.id, user.id, :web, any_args)

          subject
        end
      end
    end

    context 'when updating general settings' do
      context 'when build_timeout_human_readable is not specified' do
        let(:params) { { build_timeout_human_readable: '' } }

        it 'set default timeout' do
          subject

          project.reload
          expect(project.build_timeout).to eq(3600)
        end
      end

      context 'when build_timeout_human_readable is specified' do
        let(:params) { { build_timeout_human_readable: '1h 30m' } }

        it 'set specified timeout' do
          subject

          project.reload
          expect(project.build_timeout).to eq(5400)
        end
      end

      context 'when build_timeout_human_readable is invalid' do
        let(:params) { { build_timeout_human_readable: '5m' } }

        it 'set specified timeout' do
          subject

          expect(controller).to set_flash[:alert]
          expect(response).to redirect_to(namespace_project_settings_ci_cd_path)
        end
      end

      context 'when default_git_depth is not specified' do
        let(:params) { { ci_cd_settings_attributes: { default_git_depth: 10 } } }

        before do
          project.ci_cd_settings.update!(default_git_depth: nil)
        end

        it 'set specified git depth' do
          subject

          project.reload
          expect(project.ci_default_git_depth).to eq(10)
        end
      end

      context 'when forward_deployment_enabled is not specified' do
        let(:params) { { ci_cd_settings_attributes: { forward_deployment_enabled: false } } }

        before do
          project.ci_cd_settings.update!(forward_deployment_enabled: nil)
        end

        it 'sets forward deployment enabled' do
          subject

          project.reload
          expect(project.ci_forward_deployment_enabled).to eq(false)
        end
      end

      context 'when max_artifacts_size is specified' do
        let(:params) { { max_artifacts_size: 10 } }

        context 'and user is not an admin' do
          it 'does not set max_artifacts_size' do
            subject

            project.reload
            expect(project.max_artifacts_size).to be_nil
          end
        end

        context 'and user is an admin' do
          let(:user) { create(:admin)  }

          context 'with admin mode disabled' do
            it 'does not set max_artifacts_size' do
              subject

              project.reload
              expect(project.max_artifacts_size).to be_nil
            end
          end

          context 'with admin mode enabled', :enable_admin_mode do
            it 'sets max_artifacts_size' do
              subject

              project.reload
              expect(project.max_artifacts_size).to eq(10)
            end
          end
        end
      end
    end
  end

  describe 'GET #runner_setup_scripts' do
    it 'renders the setup scripts' do
      get :runner_setup_scripts, params: { os: 'linux', arch: 'amd64', namespace_id: project.namespace, project_id: project }

      expect(response).to have_gitlab_http_status(:ok)
      expect(json_response).to have_key("install")
      expect(json_response).to have_key("register")
    end

    it 'renders errors if they occur' do
      get :runner_setup_scripts, params: { os: 'foo', arch: 'bar', namespace_id: project.namespace, project_id: project }

      expect(response).to have_gitlab_http_status(:bad_request)
      expect(json_response).to have_key("errors")
    end
  end
end