Welcome to mirror list, hosted at ThFree Co, Russian Federation.

content_spec.rb « config « chain « pipeline « ci « gitlab « lib « spec - gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: f84f10bdc46ae1519d847dcf35e5097501c52e66 (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
# frozen_string_literal: true

require 'spec_helper'

describe Gitlab::Ci::Pipeline::Chain::Config::Content do
  let(:project) { create(:project, ci_config_path: ci_config_path) }
  let(:pipeline) { build(:ci_pipeline, project: project) }
  let(:command) { Gitlab::Ci::Pipeline::Chain::Command.new(project: project) }

  subject { described_class.new(pipeline, command) }

  describe '#perform!' do
    context 'when feature flag is disabled' do
      before do
        stub_feature_flags(ci_root_config_content: false)
      end

      context 'when config is defined in a custom path in the repository' do
        let(:ci_config_path) { 'path/to/config.yml' }

        before do
          expect(project.repository)
            .to receive(:gitlab_ci_yml_for)
            .with(pipeline.sha, ci_config_path)
            .and_return('the-content')
        end

        it 'returns the content of the YAML file' do
          subject.perform!

          expect(pipeline.config_source).to eq 'repository_source'
          expect(command.config_content).to eq('the-content')
        end
      end

      context 'when config is defined remotely' do
        let(:ci_config_path) { 'http://example.com/path/to/ci/config.yml' }

        it 'does not support URLs and default to AutoDevops' do
          subject.perform!

          expect(pipeline.config_source).to eq 'auto_devops_source'
          template = Gitlab::Template::GitlabCiYmlTemplate.find('Beta/Auto-DevOps')
          expect(command.config_content).to eq(template.content)
        end
      end

      context 'when config is defined in a separate repository' do
        let(:ci_config_path) { 'path/to/.gitlab-ci.yml@another-group/another-repo' }

        it 'does not support YAML from external repository and default to AutoDevops' do
          subject.perform!

          expect(pipeline.config_source).to eq 'auto_devops_source'
          template = Gitlab::Template::GitlabCiYmlTemplate.find('Beta/Auto-DevOps')
          expect(command.config_content).to eq(template.content)
        end
      end

      context 'when config is defined in the default .gitlab-ci.yml' do
        let(:ci_config_path) { nil }

        before do
          expect(project.repository)
            .to receive(:gitlab_ci_yml_for)
            .with(pipeline.sha, '.gitlab-ci.yml')
            .and_return('the-content')
        end

        it 'returns the content of the canonical config file' do
          subject.perform!

          expect(pipeline.config_source).to eq 'repository_source'
          expect(command.config_content).to eq('the-content')
        end
      end

      context 'when config is the Auto-Devops template' do
        let(:ci_config_path) { nil }

        before do
          expect(project).to receive(:auto_devops_enabled?).and_return(true)
        end

        context 'when beta is enabled' do
          before do
            stub_feature_flags(auto_devops_beta: true)
          end

          it 'returns the content of AutoDevops template' do
            subject.perform!

            expect(pipeline.config_source).to eq 'auto_devops_source'
            template = Gitlab::Template::GitlabCiYmlTemplate.find('Beta/Auto-DevOps')
            expect(command.config_content).to eq(template.content)
          end
        end

        context 'when beta is disabled' do
          before do
            stub_feature_flags(auto_devops_beta: false)
          end

          it 'returns the content of AutoDevops template' do
            subject.perform!

            expect(pipeline.config_source).to eq 'auto_devops_source'
            template = Gitlab::Template::GitlabCiYmlTemplate.find('Auto-DevOps')
            expect(command.config_content).to eq(template.content)
          end
        end
      end

      context 'when config is not defined anywhere' do
        let(:ci_config_path) { nil }

        before do
          expect(project).to receive(:auto_devops_enabled?).and_return(false)
        end

        it 'builds root config including the auto-devops template' do
          subject.perform!

          expect(pipeline.config_source).to eq('unknown_source')
          expect(command.config_content).to be_nil
          expect(pipeline.errors.full_messages).to include('Missing CI config file')
        end
      end
    end

    context 'when config is defined in a custom path in the repository' do
      let(:ci_config_path) { 'path/to/config.yml' }

      before do
        expect(project.repository)
          .to receive(:gitlab_ci_yml_for)
          .with(pipeline.sha, ci_config_path)
          .and_return('the-content')
      end

      it 'builds root config including the local custom file' do
        subject.perform!

        expect(pipeline.config_source).to eq 'repository_source'
        expect(command.config_content).to eq(<<~EOY)
          ---
          include:
          - local: #{ci_config_path}
        EOY
      end
    end

    context 'when config is defined remotely' do
      let(:ci_config_path) { 'http://example.com/path/to/ci/config.yml' }

      it 'builds root config including the remote config' do
        subject.perform!

        expect(pipeline.config_source).to eq 'remote_source'
        expect(command.config_content).to eq(<<~EOY)
          ---
          include:
          - remote: #{ci_config_path}
        EOY
      end
    end

    context 'when config is defined in a separate repository' do
      let(:ci_config_path) { 'path/to/.gitlab-ci.yml@another-group/another-repo' }

      it 'builds root config including the path to another repository' do
        subject.perform!

        expect(pipeline.config_source).to eq 'external_project_source'
        expect(command.config_content).to eq(<<~EOY)
          ---
          include:
          - project: another-group/another-repo
            file: path/to/.gitlab-ci.yml
        EOY
      end
    end

    context 'when config is defined in the default .gitlab-ci.yml' do
      let(:ci_config_path) { nil }

      before do
        expect(project.repository)
          .to receive(:gitlab_ci_yml_for)
          .with(pipeline.sha, '.gitlab-ci.yml')
          .and_return('the-content')
      end

      it 'builds root config including the canonical CI config file' do
        subject.perform!

        expect(pipeline.config_source).to eq 'repository_source'
        expect(command.config_content).to eq(<<~EOY)
          ---
          include:
          - local: ".gitlab-ci.yml"
        EOY
      end
    end

    context 'when config is the Auto-Devops template' do
      let(:ci_config_path) { nil }

      before do
        expect(project).to receive(:auto_devops_enabled?).and_return(true)
      end

      context 'when beta is enabled' do
        before do
          stub_feature_flags(auto_devops_beta: true)
        end

        it 'builds root config including the auto-devops template' do
          subject.perform!

          expect(pipeline.config_source).to eq 'auto_devops_source'
          expect(command.config_content).to eq(<<~EOY)
            ---
            include:
            - template: Beta/Auto-DevOps.gitlab-ci.yml
          EOY
        end
      end

      context 'when beta is disabled' do
        before do
          stub_feature_flags(auto_devops_beta: false)
        end

        it 'builds root config including the auto-devops template' do
          subject.perform!

          expect(pipeline.config_source).to eq 'auto_devops_source'
          expect(command.config_content).to eq(<<~EOY)
            ---
            include:
            - template: Auto-DevOps.gitlab-ci.yml
          EOY
        end
      end
    end

    context 'when config is not defined anywhere' do
      let(:ci_config_path) { nil }

      before do
        expect(project).to receive(:auto_devops_enabled?).and_return(false)
      end

      it 'builds root config including the auto-devops template' do
        subject.perform!

        expect(pipeline.config_source).to eq('unknown_source')
        expect(command.config_content).to be_nil
        expect(pipeline.errors.full_messages).to include('Missing CI config file')
      end
    end
  end
end