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
|
# frozen_string_literal: true
require 'spec_helper'
RSpec.describe Gitlab::Ci::Config::External::File::Component, feature_category: :pipeline_composition do
let_it_be(:context_project) { create(:project, :repository) }
let_it_be(:project) { create(:project, :repository) }
let_it_be(:user) { create(:user) }
let_it_be(:project_variables) { project.predefined_variables }
let(:context) { Gitlab::Ci::Config::External::Context.new(**context_params) }
let(:external_resource) { described_class.new(params, context) }
let(:params) { { component: 'gitlab.com/acme/components/my-component@1.0' } }
let(:fetch_service) { instance_double(::Ci::Components::FetchService) }
let(:response) { ServiceResponse.error(message: 'some error message') }
let(:context_params) do
{
project: context_project,
sha: '12345',
user: user,
variables: project_variables
}
end
before do
allow(::Ci::Components::FetchService)
.to receive(:new)
.with(
address: params[:component],
current_user: context.user
).and_return(fetch_service)
allow(fetch_service).to receive(:execute).and_return(response)
end
describe '#matching?' do
subject(:matching) { external_resource.matching? }
context 'when component is specified' do
let(:params) { { component: 'some-value' } }
it { is_expected.to be_truthy }
end
context 'when component is not specified' do
let(:params) { { local: 'some-value' } }
it { is_expected.to be_falsy }
end
end
describe '#valid?' do
subject(:valid?) do
Gitlab::Ci::Config::External::Mapper::Verifier.new(context).process([external_resource])
external_resource.valid?
end
context 'when the context project does not have a repository' do
before do
allow(context_project).to receive(:repository).and_return(nil)
end
it 'is invalid' do
expect(subject).to be_falsy
expect(external_resource.error_message).to eq('Unable to use components outside of a project context')
end
end
context 'when location is not provided' do
let(:params) { { component: 123 } }
it 'is invalid' do
expect(subject).to be_falsy
expect(external_resource.error_message).to eq('Included file `123` needs to be a string')
end
end
context 'when component path is provided' do
context 'when component is not found' do
let(:response) do
ServiceResponse.error(message: 'Content not found')
end
it 'is invalid' do
expect(subject).to be_falsy
expect(external_resource.error_message).to eq('Content not found')
end
end
context 'when component is found' do
let(:content) do
<<~COMPONENT
job:
script: echo
COMPONENT
end
let(:response) do
ServiceResponse.success(payload: {
content: content,
path: instance_double(::Gitlab::Ci::Components::InstancePath, project: project, sha: '12345')
})
end
it 'is valid' do
expect(subject).to be_truthy
expect(external_resource.content).to eq(content)
end
context 'when content is not a valid YAML' do
let(:content) { 'the-content' }
it 'is invalid' do
expect(subject).to be_falsy
expect(external_resource.error_message).to match(/Invalid configuration format/)
end
end
end
end
end
describe '#content' do
context 'when component is valid' do
let(:content) do
<<~COMPONENT
job:
script: echo
COMPONENT
end
let(:response) do
ServiceResponse.success(payload: {
content: content,
path: instance_double(::Gitlab::Ci::Components::InstancePath, project: project, sha: '12345')
})
end
it 'tracks the event' do
expect(::Gitlab::UsageDataCounters::HLLRedisCounter).to receive(:track_event).with('cicd_component_usage',
values: external_resource.context.user.id)
external_resource.content
end
end
context 'when component is invalid' do
let(:content) { 'the-content' }
it 'does not track the event' do
expect(::Gitlab::UsageDataCounters::HLLRedisCounter).not_to receive(:track_event)
external_resource.content
end
end
end
describe '#metadata' do
subject(:metadata) { external_resource.metadata }
let(:component_path) do
instance_double(::Gitlab::Ci::Components::InstancePath,
project: project,
sha: '12345',
project_file_path: 'my-component/template.yml')
end
let(:response) do
ServiceResponse.success(payload: { path: component_path })
end
it 'returns the metadata' do
is_expected.to include(
context_project: context_project.full_path,
context_sha: context.sha,
type: :component,
location: 'gitlab.com/acme/components/my-component@1.0',
blob: a_string_ending_with("#{project.full_path}/-/blob/12345/my-component/template.yml"),
raw: nil,
extra: {}
)
end
end
describe '#expand_context' do
let(:component_path) do
instance_double(::Gitlab::Ci::Components::InstancePath,
project: project,
sha: '12345')
end
let(:response) do
ServiceResponse.success(payload: { path: component_path })
end
subject { external_resource.send(:expand_context_attrs) }
it 'inherits user and variables while changes project and sha' do
is_expected.to include(
project: project,
sha: '12345',
user: context.user,
variables: context.variables)
end
end
describe '#to_hash' do
context 'when interpolation is being used' do
let(:response) do
ServiceResponse.success(payload: { content: content, path: path })
end
let(:path) do
instance_double(::Gitlab::Ci::Components::InstancePath, project: project, sha: '12345')
end
let(:content) do
<<~YAML
spec:
inputs:
env:
---
deploy:
script: deploy $[[ inputs.env ]]
YAML
end
let(:params) do
{ component: 'gitlab.com/acme/components/my-component@1.0', with: { env: 'production' } }
end
it 'correctly interpolates the content' do
expect(external_resource.to_hash).to eq({ deploy: { script: 'deploy production' } })
end
end
end
end
|