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
|
# frozen_string_literal: true
require 'spec_helper'
RSpec.describe ::Ci::Runners::CreateRunnerService, "#execute", feature_category: :runner_fleet do
subject(:execute) { described_class.new(user: current_user, params: params).execute }
let(:runner) { execute.payload[:runner] }
let_it_be(:admin) { create(:admin) }
let_it_be(:non_admin_user) { create(:user) }
let_it_be(:anonymous) { nil }
let_it_be(:group_owner) { create(:user) }
let_it_be(:group) { create(:group) }
shared_examples 'it can create a runner' do
it 'creates a runner of the specified type', :aggregate_failures do
is_expected.to be_success
expect(runner.runner_type).to eq expected_type
end
context 'with default params provided' do
let(:args) do
{}
end
before do
params.merge!(args)
end
it { is_expected.to be_success }
it 'uses default values when none are provided' do
expect(runner).to be_an_instance_of(::Ci::Runner)
expect(runner.persisted?).to be_truthy
expect(runner.run_untagged).to be true
expect(runner.active).to be true
expect(runner.creator).to be current_user
expect(runner.authenticated_user_registration_type?).to be_truthy
expect(runner.runner_type).to eq expected_type
end
end
context 'with non-default params provided' do
let(:args) do
{
description: 'some description',
maintenance_note: 'a note',
paused: true,
tag_list: %w[tag1 tag2],
access_level: 'ref_protected',
locked: true,
maximum_timeout: 600,
run_untagged: false
}
end
before do
params.merge!(args)
end
it { is_expected.to be_success }
it 'creates runner with specified values', :aggregate_failures do
expect(runner).to be_an_instance_of(::Ci::Runner)
expect(runner.description).to eq 'some description'
expect(runner.maintenance_note).to eq 'a note'
expect(runner.active).to eq !args[:paused]
expect(runner.locked).to eq args[:locked]
expect(runner.run_untagged).to eq args[:run_untagged]
expect(runner.tags).to contain_exactly(
an_object_having_attributes(name: 'tag1'),
an_object_having_attributes(name: 'tag2')
)
expect(runner.access_level).to eq args[:access_level]
expect(runner.maximum_timeout).to eq args[:maximum_timeout]
expect(runner.authenticated_user_registration_type?).to be_truthy
expect(runner.runner_type).to eq expected_type
end
context 'with a nil paused value' do
let(:args) do
{
paused: nil,
description: 'some description',
maintenance_note: 'a note',
tag_list: %w[tag1 tag2],
access_level: 'ref_protected',
locked: true,
maximum_timeout: 600,
run_untagged: false
}
end
it { is_expected.to be_success }
it 'creates runner with active set to true' do
expect(runner).to be_an_instance_of(::Ci::Runner)
expect(runner.active).to eq true
end
end
context 'with no paused value given' do
let(:args) do
{
description: 'some description',
maintenance_note: 'a note',
tag_list: %w[tag1 tag2],
access_level: 'ref_protected',
locked: true,
maximum_timeout: 600,
run_untagged: false
}
end
it { is_expected.to be_success }
it 'creates runner with active set to true' do
expect(runner).to be_an_instance_of(::Ci::Runner)
expect(runner.active).to eq true
end
end
end
end
shared_examples 'it cannot create a runner' do
it 'runner payload is nil' do
expect(runner).to be nil
end
it { is_expected.to be_error }
end
shared_examples 'it can return an error' do
let(:runner_double) { Ci::Runner.new }
context 'when the runner fails to save' do
before do
allow(Ci::Runner).to receive(:new).and_return runner_double
end
it_behaves_like 'it cannot create a runner'
it 'returns error message' do
expect(execute.errors).not_to be_empty
end
end
end
context 'with :runner_type param set to instance_type' do
let(:expected_type) { 'instance_type' }
let(:params) { { runner_type: 'instance_type' } }
context 'when anonymous user' do
let(:current_user) { anonymous }
it_behaves_like 'it cannot create a runner'
end
context 'when non-admin user' do
let(:current_user) { non_admin_user }
it_behaves_like 'it cannot create a runner'
end
context 'when admin user' do
let(:current_user) { admin }
it_behaves_like 'it cannot create a runner'
context 'when admin mode is enabled', :enable_admin_mode do
it_behaves_like 'it can create a runner'
it_behaves_like 'it can return an error'
context 'with unexpected scope param specified' do
let(:params) { { runner_type: 'instance_type', scope: group } }
it_behaves_like 'it cannot create a runner'
end
end
end
end
context 'with :runner_type param set to group_type' do
let(:expected_type) { 'group_type' }
let(:params) { { runner_type: 'group_type', scope: group } }
before do
group.add_developer(non_admin_user)
group.add_owner(group_owner)
end
context 'when anonymous user' do
let(:current_user) { anonymous }
it_behaves_like 'it cannot create a runner'
end
context 'when non-admin user' do
let(:current_user) { non_admin_user }
it_behaves_like 'it cannot create a runner'
end
context 'when group owner' do
let(:current_user) { group_owner }
it_behaves_like 'it can create a runner'
context 'with missing scope param' do
let(:params) { { runner_type: 'group_type' } }
it_behaves_like 'it cannot create a runner'
end
end
context 'when admin user' do
let(:current_user) { admin }
it_behaves_like 'it cannot create a runner'
context 'when admin mode is enabled', :enable_admin_mode do
it_behaves_like 'it can create a runner'
it_behaves_like 'it can return an error'
end
end
end
context 'with :runner_type param set to project_type' do
let_it_be(:project) { create(:project, namespace: group) }
let(:expected_type) { 'project_type' }
let(:params) { { runner_type: 'project_type', scope: project } }
before do
group.add_developer(non_admin_user)
group.add_owner(group_owner)
end
context 'when anonymous user' do
let(:current_user) { anonymous }
it_behaves_like 'it cannot create a runner'
end
context 'when group owner' do
let(:current_user) { group_owner }
it_behaves_like 'it can create a runner'
context 'with missing scope param' do
let(:params) { { runner_type: 'project_type' } }
it_behaves_like 'it cannot create a runner'
end
end
context 'when non-admin user' do
let(:current_user) { non_admin_user }
it_behaves_like 'it cannot create a runner'
context 'with project permissions to create runner' do
before do
project.add_maintainer(current_user)
end
it_behaves_like 'it can create a runner'
end
end
context 'when admin user' do
let(:current_user) { admin }
it_behaves_like 'it cannot create a runner'
context 'when admin mode is enabled', :enable_admin_mode do
it_behaves_like 'it can create a runner'
it_behaves_like 'it can return an error'
end
end
end
end
|