summaryrefslogtreecommitdiff
path: root/spec/presenters/ci/build_runner_presenter_spec.rb
blob: ace65307321482328ee3998e37231fd7062972cc (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
348
349
350
351
352
353
354
# frozen_string_literal: true

require 'spec_helper'

RSpec.describe Ci::BuildRunnerPresenter do
  let(:presenter) { described_class.new(build) }
  let(:archive) { { paths: ['sample.txt'] } }

  let(:archive_expectation) do
    {
      artifact_type: :archive,
      artifact_format: :zip,
      paths: archive[:paths],
      untracked: archive[:untracked]
    }
  end

  describe '#artifacts' do
    context "when option contains archive-type artifacts" do
      let(:build) { create(:ci_build, options: { artifacts: archive }) }

      it 'presents correct hash' do
        expect(presenter.artifacts.first).to include(archive_expectation)
      end

      context "when untracked is specified" do
        let(:archive) { { untracked: true } }

        it 'presents correct hash' do
          expect(presenter.artifacts.first).to include(archive_expectation)
        end
      end

      context "when untracked and paths are missing" do
        let(:archive) { { when: 'always' } }

        it 'does not present hash' do
          expect(presenter.artifacts).to be_empty
        end
      end

      context 'when artifacts exclude is defined' do
        let(:build) do
          create(:ci_build, options: { artifacts: { paths: %w[abc], exclude: %w[cde] } })
        end

        it 'includes the list of excluded paths' do
          expect(presenter.artifacts.first).to include(
            artifact_type: :archive,
            artifact_format: :zip,
            paths: %w[abc],
            exclude: %w[cde]
          )
        end
      end

      context 'when artifacts exclude is not defined' do
        let(:build) do
          create(:ci_build, options: { artifacts: { paths: %w[abc] } })
        end

        it 'does not include an empty list of excluded paths' do
          expect(presenter.artifacts.first).not_to have_key(:exclude)
        end
      end
    end

    context "with reports" do
      Ci::JobArtifact::DEFAULT_FILE_NAMES.each do |file_type, filename|
        context file_type.to_s do
          let(:report) { { "#{file_type}": [filename] } }
          let(:build) { create(:ci_build, options: { artifacts: { reports: report } }) }

          let(:report_expectation) do
            {
              name: filename,
              artifact_type: :"#{file_type}",
              artifact_format: Ci::JobArtifact::TYPE_AND_FORMAT_PAIRS.fetch(file_type),
              paths: [filename],
              when: 'always'
            }.compact
          end

          it 'presents correct hash' do
            expect(presenter.artifacts).to contain_exactly(report_expectation)
          end
        end
      end
    end

    context 'when a specific coverage_report type is given' do
      let(:coverage_format) { :cobertura }
      let(:filename) { 'cobertura-coverage.xml' }
      let(:coverage_report) { { path: filename, coverage_format: coverage_format } }
      let(:report) { { coverage_report: coverage_report } }
      let(:build) { create(:ci_build, options: { artifacts: { reports: report } }) }

      let(:expected_coverage_report) do
        {
          name: filename,
          artifact_type: coverage_format,
          artifact_format: Ci::JobArtifact::TYPE_AND_FORMAT_PAIRS.fetch(coverage_format),
          paths: [filename],
          when: 'always'
        }
      end

      it 'presents the coverage report hash with the coverage format' do
        expect(presenter.artifacts).to contain_exactly(expected_coverage_report)
      end
    end

    context 'when a specific coverage_report type is given with another report type' do
      let(:coverage_format) { :cobertura }
      let(:coverage_filename) { 'cobertura-coverage.xml' }
      let(:coverage_report) { { path: coverage_filename, coverage_format: coverage_format } }
      let(:ds_filename) { 'gl-dependency-scanning-report.json' }

      let(:report) { { coverage_report: coverage_report, dependency_scanning: [ds_filename] } }
      let(:build) { create(:ci_build, options: { artifacts: { reports: report } }) }

      let(:expected_coverage_report) do
        {
          name: coverage_filename,
          artifact_type: coverage_format,
          artifact_format: Ci::JobArtifact::TYPE_AND_FORMAT_PAIRS.fetch(coverage_format),
          paths: [coverage_filename],
          when: 'always'
        }
      end

      let(:expected_ds_report) do
        {
          name: ds_filename,
          artifact_type: :dependency_scanning,
          artifact_format: Ci::JobArtifact::TYPE_AND_FORMAT_PAIRS.fetch(:dependency_scanning),
          paths: [ds_filename],
          when: 'always'
        }
      end

      it 'presents both reports' do
        expect(presenter.artifacts).to contain_exactly(expected_coverage_report, expected_ds_report)
      end
    end

    context "when option has both archive and reports specification" do
      let(:report) { { junit: ['junit.xml'] } }
      let(:build) { create(:ci_build, options: { script: 'echo', artifacts: { **archive, reports: report } }) }

      let(:report_expectation) do
        {
          name: 'junit.xml',
          artifact_type: :junit,
          artifact_format: :gzip,
          paths: ['junit.xml'],
          when: 'always'
        }
      end

      it 'presents correct hash' do
        expect(presenter.artifacts.first).to include(archive_expectation)
        expect(presenter.artifacts.second).to include(report_expectation)
      end

      context "when archive specifies 'expire_in' keyword" do
        let(:archive) { { paths: ['sample.txt'], expire_in: '3 mins 4 sec' } }

        it 'inherits expire_in from archive' do
          expect(presenter.artifacts.first).to include({ **archive_expectation, expire_in: '3 mins 4 sec' })
          expect(presenter.artifacts.second).to include({ **report_expectation, expire_in: '3 mins 4 sec' })
        end
      end
    end

    context "when option has no artifact keywords" do
      let(:build) { create(:ci_build, :no_options) }

      it 'does not present hash' do
        expect(presenter.artifacts).to be_nil
      end
    end
  end

  describe '#ref_type' do
    subject { presenter.ref_type }

    let(:build) { create(:ci_build, tag: tag) }
    let(:tag) { true }

    it 'returns the correct ref type' do
      is_expected.to eq('tag')
    end

    context 'when tag is false' do
      let(:tag) { false }

      it 'returns the correct ref type' do
        is_expected.to eq('branch')
      end
    end
  end

  describe '#git_depth' do
    let(:build) { create(:ci_build) }

    subject(:git_depth) { presenter.git_depth }

    context 'when GIT_DEPTH variable is specified' do
      before do
        create(:ci_pipeline_variable, key: 'GIT_DEPTH', value: 1, pipeline: build.pipeline)
      end

      it 'returns its value' do
        expect(git_depth).to eq(1)
      end
    end

    it 'defaults to git depth setting for the project' do
      expect(git_depth).to eq(build.project.ci_default_git_depth)
    end
  end

  describe '#refspecs' do
    subject { presenter.refspecs }

    let(:build) { create(:ci_build) }
    let(:pipeline) { build.pipeline }

    it 'returns the correct refspecs' do
      is_expected.to contain_exactly("+refs/heads/#{build.ref}:refs/remotes/origin/#{build.ref}",
                                     "+refs/pipelines/#{pipeline.id}:refs/pipelines/#{pipeline.id}")
    end

    context 'when ref is tag' do
      let(:build) { create(:ci_build, :tag) }

      it 'returns the correct refspecs' do
        is_expected.to contain_exactly("+refs/tags/#{build.ref}:refs/tags/#{build.ref}",
                                       "+refs/pipelines/#{pipeline.id}:refs/pipelines/#{pipeline.id}")
      end

      context 'when GIT_DEPTH is zero' do
        before do
          create(:ci_pipeline_variable, key: 'GIT_DEPTH', value: 0, pipeline: build.pipeline)
        end

        it 'returns the correct refspecs' do
          is_expected.to contain_exactly('+refs/tags/*:refs/tags/*',
                                         '+refs/heads/*:refs/remotes/origin/*',
                                         "+refs/pipelines/#{pipeline.id}:refs/pipelines/#{pipeline.id}")
        end
      end
    end

    context 'when pipeline is detached merge request pipeline' do
      let(:merge_request) { create(:merge_request, :with_detached_merge_request_pipeline) }
      let(:pipeline) { merge_request.all_pipelines.first }
      let(:build) { create(:ci_build, ref: pipeline.ref, pipeline: pipeline) }

      before do
        pipeline.persistent_ref.create # rubocop:disable Rails/SaveBang
      end

      it 'returns the correct refspecs' do
        is_expected
          .to contain_exactly("+refs/pipelines/#{pipeline.id}:refs/pipelines/#{pipeline.id}")
      end

      context 'when GIT_DEPTH is zero' do
        before do
          create(:ci_pipeline_variable, key: 'GIT_DEPTH', value: 0, pipeline: build.pipeline)
        end

        it 'returns the correct refspecs' do
          is_expected
            .to contain_exactly("+refs/pipelines/#{pipeline.id}:refs/pipelines/#{pipeline.id}",
                                '+refs/heads/*:refs/remotes/origin/*',
                                '+refs/tags/*:refs/tags/*')
        end
      end

      context 'when pipeline is legacy detached merge request pipeline' do
        let(:merge_request) { create(:merge_request, :with_legacy_detached_merge_request_pipeline) }

        it 'returns the correct refspecs' do
          is_expected.to contain_exactly("+refs/pipelines/#{pipeline.id}:refs/pipelines/#{pipeline.id}",
                                         "+refs/heads/#{build.ref}:refs/remotes/origin/#{build.ref}")
        end
      end
    end

    context 'when persistent pipeline ref exists' do
      let(:project) { create(:project, :repository) }
      let(:sha) { project.repository.commit.sha }
      let(:pipeline) { create(:ci_pipeline, sha: sha, project: project) }
      let(:build) { create(:ci_build, pipeline: pipeline) }

      before do
        pipeline.persistent_ref.create # rubocop:disable Rails/SaveBang
      end

      it 'exposes the persistent pipeline ref' do
        is_expected
          .to contain_exactly("+refs/pipelines/#{pipeline.id}:refs/pipelines/#{pipeline.id}",
                              "+refs/heads/#{build.ref}:refs/remotes/origin/#{build.ref}")
      end
    end
  end

  describe '#runner_variables' do
    subject { presenter.runner_variables }

    let_it_be(:project) { create(:project, :repository) }

    shared_examples 'returns an array with the expected variables' do
      it 'returns an array' do
        is_expected.to be_an_instance_of(Array)
      end

      it 'returns the expected variables' do
        is_expected.to eq(presenter.variables.to_runner_variables)
      end
    end

    let(:sha) { project.repository.commit.sha }
    let(:pipeline) { create(:ci_pipeline, sha: sha, project: project) }
    let(:build) { create(:ci_build, pipeline: pipeline) }

    it_behaves_like 'returns an array with the expected variables'
  end

  describe '#runner_variables subset' do
    subject { presenter.runner_variables.select { |v| %w[A B C].include?(v.fetch(:key)) } }

    let(:build) { create(:ci_build) }

    context 'with references in pipeline variables' do
      before do
        create(:ci_pipeline_variable, key: 'A', value: 'refA-$B', pipeline: build.pipeline)
        create(:ci_pipeline_variable, key: 'B', value: 'refB-$C-$D', pipeline: build.pipeline)
        create(:ci_pipeline_variable, key: 'C', value: 'value', pipeline: build.pipeline)
      end

      it 'returns expanded and sorted variables' do
        is_expected.to eq [
                            { key: 'C', value: 'value', public: false, masked: false },
                            { key: 'B', value: 'refB-value-$D', public: false, masked: false },
                            { key: 'A', value: 'refA-refB-value-$D', public: false, masked: false }
                          ]
      end
    end
  end
end