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

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

require 'spec_helper'

RSpec.describe Gitlab::Ci::Components::InstancePath, feature_category: :pipeline_composition do
  let_it_be(:user) { create(:user) }

  let(:path) { described_class.new(address: address) }
  let(:settings) { GitlabSettings::Options.build({ 'component_fqdn' => current_host }) }
  let(:current_host) { 'acme.com/' }

  before do
    allow(::Settings).to receive(:gitlab_ci).and_return(settings)
  end

  describe 'FQDN path' do
    let(:version) { 'master' }
    let(:project_path) { project.full_path }
    let(:address) { "acme.com/#{project_path}/secret-detection@#{version}" }

    context 'when the project repository contains a templates directory' do
      let_it_be(:project) do
        create(
          :project, :custom_repo,
          files: {
            'templates/secret-detection.yml' => 'image: alpine_1',
            'templates/dast/template.yml' => 'image: alpine_2',
            'templates/dast/another-template.yml' => 'image: alpine_3',
            'templates/dast/another-folder/template.yml' => 'image: alpine_4'
          }
        )
      end

      before do
        project.add_developer(user)
      end

      context 'when user does not have permissions' do
        it 'raises an error when fetching the content' do
          expect { path.fetch_content!(current_user: build(:user)) }
            .to raise_error(Gitlab::Access::AccessDeniedError)
        end
      end

      context 'when the component is simple (single file template)' do
        it 'fetches the component content', :aggregate_failures do
          result = path.fetch_content!(current_user: user)
          expect(result.content).to eq('image: alpine_1')
          expect(result.path).to eq('templates/secret-detection.yml')
          expect(path.host).to eq(current_host)
          expect(path.project).to eq(project)
          expect(path.sha).to eq(project.commit('master').id)
        end
      end

      context 'when the component is complex (directory-based template)' do
        let(:address) { "acme.com/#{project_path}/dast@#{version}" }

        it 'fetches the component content', :aggregate_failures do
          result = path.fetch_content!(current_user: user)
          expect(result.content).to eq('image: alpine_2')
          expect(result.path).to eq('templates/dast/template.yml')
          expect(path.host).to eq(current_host)
          expect(path.project).to eq(project)
          expect(path.sha).to eq(project.commit('master').id)
        end

        context 'when there is an invalid nested component folder' do
          let(:address) { "acme.com/#{project_path}/dast/another-folder@#{version}" }

          it 'returns nil' do
            result = path.fetch_content!(current_user: user)
            expect(result.content).to be_nil
          end
        end

        context 'when there is an invalid nested component path' do
          let(:address) { "acme.com/#{project_path}/dast/another-template@#{version}" }

          it 'returns nil' do
            result = path.fetch_content!(current_user: user)
            expect(result.content).to be_nil
          end
        end
      end

      context 'when fetching the latest version of a component' do
        let_it_be(:project) do
          create(
            :project, :custom_repo,
            files: {
              'templates/secret-detection.yml' => 'image: alpine_1'
            }
          )
        end

        let(:version) { '~latest' }

        let(:latest_sha) do
          project.repository.commit('master').id
        end

        before do
          create(:release, project: project, sha: project.repository.root_ref_sha,
            released_at: Time.zone.now - 1.day)

          project.repository.update_file(
            user, 'templates/secret-detection.yml', 'image: alpine_2',
            message: 'Updates image', branch_name: project.default_branch
          )

          create(:release, project: project, sha: latest_sha,
            released_at: Time.zone.now)
        end

        it 'fetches the component content', :aggregate_failures do
          result = path.fetch_content!(current_user: user)
          expect(result.content).to eq('image: alpine_2')
          expect(result.path).to eq('templates/secret-detection.yml')
          expect(path.host).to eq(current_host)
          expect(path.project).to eq(project)
          expect(path.sha).to eq(latest_sha)
        end
      end

      context 'when version does not exist' do
        let(:version) { 'non-existent' }

        it 'returns nil', :aggregate_failures do
          expect(path.fetch_content!(current_user: user)).to be_nil
          expect(path.host).to eq(current_host)
          expect(path.project).to eq(project)
          expect(path.sha).to be_nil
        end
      end

      context 'when current GitLab instance is installed on a relative URL' do
        let(:address) { "acme.com/gitlab/#{project_path}/secret-detection@#{version}" }
        let(:current_host) { 'acme.com/gitlab/' }

        it 'fetches the component content', :aggregate_failures do
          result = path.fetch_content!(current_user: user)
          expect(result.content).to eq('image: alpine_1')
          expect(result.path).to eq('templates/secret-detection.yml')
          expect(path.host).to eq(current_host)
          expect(path.project).to eq(project)
          expect(path.sha).to eq(project.commit('master').id)
        end
      end
    end

    # All the following tests are for deprecated code and will be removed
    # in https://gitlab.com/gitlab-org/gitlab/-/issues/415855
    context 'when the project does not contain a templates directory' do
      let(:project_path) { project.full_path }
      let(:address) { "acme.com/#{project_path}/component@#{version}" }

      let_it_be(:project) do
        create(
          :project, :custom_repo,
          files: {
            'component/template.yml' => 'image: alpine'
          }
        )
      end

      before do
        project.add_developer(user)
      end

      it 'fetches the component content', :aggregate_failures do
        result = path.fetch_content!(current_user: user)
        expect(result.content).to eq('image: alpine')
        expect(result.path).to eq('component/template.yml')
        expect(path.host).to eq(current_host)
        expect(path.project).to eq(project)
        expect(path.sha).to eq(project.commit('master').id)
      end

      context 'when project path is nested under a subgroup' do
        let_it_be(:group) { create(:group, :nested) }
        let_it_be(:project) do
          create(
            :project, :custom_repo,
            files: {
              'component/template.yml' => 'image: alpine'
            },
            group: group
          )
        end

        it 'fetches the component content', :aggregate_failures do
          result = path.fetch_content!(current_user: user)
          expect(result.content).to eq('image: alpine')
          expect(result.path).to eq('component/template.yml')
          expect(path.host).to eq(current_host)
          expect(path.project).to eq(project)
          expect(path.sha).to eq(project.commit('master').id)
        end
      end

      context 'when current GitLab instance is installed on a relative URL' do
        let(:address) { "acme.com/gitlab/#{project_path}/component@#{version}" }
        let(:current_host) { 'acme.com/gitlab/' }

        it 'fetches the component content', :aggregate_failures do
          result = path.fetch_content!(current_user: user)
          expect(result.content).to eq('image: alpine')
          expect(result.path).to eq('component/template.yml')
          expect(path.host).to eq(current_host)
          expect(path.project).to eq(project)
          expect(path.sha).to eq(project.commit('master').id)
        end
      end

      context 'when version does not exist' do
        let(:version) { 'non-existent' }

        it 'returns nil', :aggregate_failures do
          expect(path.fetch_content!(current_user: user)).to be_nil
          expect(path.host).to eq(current_host)
          expect(path.project).to eq(project)
          expect(path.sha).to be_nil
        end
      end

      context 'when user does not have permissions' do
        it 'raises an error when fetching the content' do
          expect { path.fetch_content!(current_user: build(:user)) }
            .to raise_error(Gitlab::Access::AccessDeniedError)
        end
      end
    end
  end
end