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
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
|
# frozen_string_literal: true
require 'spec_helper'
RSpec.describe Ci::RunnersFinder, feature_category: :runner_fleet do
context 'admin' do
let_it_be(:admin) { create(:user, :admin) }
describe '#execute' do
shared_examples 'executes as admin' do
context 'with 2 runners' do
let_it_be(:runner1) { create(:ci_runner, active: true) }
let_it_be(:runner2) { create(:ci_runner, active: false) }
context 'with empty params' do
it 'returns all runners' do
expect(Ci::Runner).to receive(:with_tags).and_call_original
expect(described_class.new(current_user: admin, params: {}).execute).to match_array [runner1, runner2]
end
end
context 'with nil group' do
it 'returns all runners' do
expect(Ci::Runner).to receive(:with_tags).and_call_original
expect(described_class.new(current_user: admin, params: { group: nil }).execute).to match_array [runner1, runner2]
end
end
context 'with preload param set to :tag_name true' do
it 'requests tags' do
expect(Ci::Runner).to receive(:with_tags).and_call_original
expect(described_class.new(current_user: admin, params: { preload: { tag_name: true } }).execute).to match_array [runner1, runner2]
end
end
context 'with preload param set to :tag_name false' do
it 'does not request tags' do
expect(Ci::Runner).not_to receive(:with_tags)
expect(described_class.new(current_user: admin, params: { preload: { tag_name: false } }).execute).to match_array [runner1, runner2]
end
end
end
context 'filtering' do
context 'by search term' do
it 'calls Ci::Runner.search' do
expect(Ci::Runner).to receive(:search).with('term').and_call_original
described_class.new(current_user: admin, params: { search: 'term' }).execute
end
end
context 'by upgrade status' do
let(:upgrade_status) {}
let_it_be(:runner1) { create(:ci_runner, version: 'a') }
let_it_be(:runner2) { create(:ci_runner, version: 'b') }
let_it_be(:runner3) { create(:ci_runner, version: 'c') }
let_it_be(:runner_version_recommended) do
create(:ci_runner_version, version: 'a', status: :recommended)
end
let_it_be(:runner_version_unavailable) do
create(:ci_runner_version, version: 'b', status: :unavailable)
end
let_it_be(:runner_version_available) do
create(:ci_runner_version, version: 'c', status: :available)
end
def execute
described_class.new(current_user: admin, params: { upgrade_status: upgrade_status }).execute
end
Ci::RunnerVersion.statuses.keys.map(&:to_sym).each do |status|
context "set to :#{status}" do
let(:upgrade_status) { status }
it "calls with_upgrade_status scope with corresponding :#{status} status" do
if [:available, :unavailable, :recommended].include?(status)
expected_result = Ci::Runner.with_upgrade_status(status)
end
expect(Ci::Runner).to receive(:with_upgrade_status).with(status).and_call_original
result = execute
expect(result).to match_array(expected_result) if expected_result
end
end
end
context 'set to an invalid value' do
let(:upgrade_status) { :some_invalid_status }
it 'raises ArgumentError' do
expect { execute }.to raise_error(ArgumentError)
end
end
context 'set to nil' do
let(:upgrade_status) { nil }
it 'does not call with_upgrade_status' do
expect(Ci::Runner).not_to receive(:with_upgrade_status)
expect(execute).to match_array(Ci::Runner.all)
end
end
end
context 'by status' do
Ci::Runner::AVAILABLE_STATUSES.each do |status|
it "calls the corresponding :#{status} scope on Ci::Runner" do
expect(Ci::Runner).to receive(status.to_sym).and_call_original
described_class.new(current_user: admin, params: { status_status: status }).execute
end
end
end
context 'by active status' do
it 'with active set as false calls the corresponding scope on Ci::Runner with false' do
expect(Ci::Runner).to receive(:active).with(false).and_call_original
described_class.new(current_user: admin, params: { active: false }).execute
end
it 'with active set as true calls the corresponding scope on Ci::Runner with true' do
expect(Ci::Runner).to receive(:active).with(true).and_call_original
described_class.new(current_user: admin, params: { active: true }).execute
end
end
context 'by runner type' do
it 'calls the corresponding scope on Ci::Runner' do
expect(Ci::Runner).to receive(:project_type).and_call_original
described_class.new(current_user: admin, params: { type_type: 'project_type' }).execute
end
end
context 'by tag_name' do
it 'calls the corresponding scope on Ci::Runner' do
expect(Ci::Runner).to receive(:tagged_with).with(%w[tag1 tag2]).and_call_original
described_class.new(current_user: admin, params: { tag_name: %w[tag1 tag2] }).execute
end
end
context 'by creator' do
it 'calls the corresponding scope on Ci::Runner' do
expect(Ci::Runner).to receive(:with_creator_id).with('1').and_call_original
described_class.new(current_user: admin, params: { creator_id: '1' }).execute
end
end
end
context 'sorting' do
let_it_be(:runner1) { create :ci_runner, created_at: '2018-07-12 07:00', contacted_at: 1.minute.ago, token_expires_at: '2022-02-15 07:00' }
let_it_be(:runner2) { create :ci_runner, created_at: '2018-07-12 08:00', contacted_at: 3.minutes.ago, token_expires_at: '2022-02-15 06:00' }
let_it_be(:runner3) { create :ci_runner, created_at: '2018-07-12 09:00', contacted_at: 2.minutes.ago }
subject do
described_class.new(current_user: admin, params: params).execute
end
shared_examples 'sorts by created_at descending' do
it 'sorts by created_at descending' do
is_expected.to eq [runner3, runner2, runner1]
end
end
context 'without sort param' do
let(:params) { {} }
it_behaves_like 'sorts by created_at descending'
end
%w[created_date created_at_desc].each do |sort|
context "with sort param equal to #{sort}" do
let(:params) { { sort: sort } }
it_behaves_like 'sorts by created_at descending'
end
end
context 'with sort param equal to created_at_asc' do
let(:params) { { sort: 'created_at_asc' } }
it 'sorts by created_at ascending' do
is_expected.to eq [runner1, runner2, runner3]
end
end
context 'with sort param equal to contacted_asc' do
let(:params) { { sort: 'contacted_asc' } }
it 'sorts by contacted_at ascending' do
is_expected.to eq [runner2, runner3, runner1]
end
end
context 'with sort param equal to contacted_desc' do
let(:params) { { sort: 'contacted_desc' } }
it 'sorts by contacted_at descending' do
is_expected.to eq [runner1, runner3, runner2]
end
end
context 'with sort param equal to token_expires_at_asc' do
let(:params) { { sort: 'token_expires_at_asc' } }
it 'sorts by contacted_at ascending' do
is_expected.to eq [runner2, runner1, runner3]
end
end
context 'with sort param equal to token_expires_at_desc' do
let(:params) { { sort: 'token_expires_at_desc' } }
it 'sorts by contacted_at descending' do
is_expected.to eq [runner3, runner1, runner2]
end
end
end
end
shared_examples 'executes as normal user' do
it 'raises Gitlab::Access::AccessDeniedError' do
user = create :user
create :ci_runner, active: true
create :ci_runner, active: false
expect do
described_class.new(current_user: user, params: {}).execute
end.to raise_error(Gitlab::Access::AccessDeniedError)
end
end
context 'when admin mode setting is disabled', :do_not_mock_admin_mode_setting do
it_behaves_like 'executes as admin'
end
context 'when admin mode setting is enabled' do
context 'when in admin mode', :enable_admin_mode do
it_behaves_like 'executes as admin'
end
context 'when not in admin mode' do
it_behaves_like 'executes as normal user'
end
end
context 'by non admin user' do
it_behaves_like 'executes as normal user'
end
context 'when user is nil' do
it 'raises Gitlab::Access::AccessDeniedError' do
user = nil
create :ci_runner, active: true
create :ci_runner, active: false
expect do
described_class.new(current_user: user, params: {}).execute
end.to raise_error(Gitlab::Access::AccessDeniedError)
end
end
end
end
context 'group' do
let_it_be(:user) { create(:user) }
let_it_be(:group) { create(:group) }
let_it_be(:sub_group_1) { create(:group, parent: group) }
let_it_be(:sub_group_2) { create(:group, parent: group) }
let_it_be(:sub_group_3) { create(:group, parent: sub_group_1) }
let_it_be(:sub_group_4) { create(:group, parent: sub_group_3) }
let_it_be(:project) { create(:project, group: group) }
let_it_be(:project_2) { create(:project, group: group) }
let_it_be(:project_3) { create(:project, group: sub_group_1) }
let_it_be(:project_4) { create(:project, group: sub_group_2) }
let_it_be(:project_5) { create(:project, group: sub_group_3) }
let_it_be(:project_6) { create(:project, group: sub_group_4) }
let_it_be(:runner_instance) { create(:ci_runner, :instance, contacted_at: 13.minutes.ago) }
let_it_be(:runner_group) { create(:ci_runner, :group, contacted_at: 12.minutes.ago) }
let_it_be(:runner_sub_group_1) { create(:ci_runner, :group, active: false, contacted_at: 11.minutes.ago) }
let_it_be(:runner_sub_group_2) { create(:ci_runner, :group, contacted_at: 10.minutes.ago) }
let_it_be(:runner_sub_group_3) { create(:ci_runner, :group, contacted_at: 9.minutes.ago) }
let_it_be(:runner_sub_group_4) { create(:ci_runner, :group, contacted_at: 8.minutes.ago) }
let_it_be(:runner_project_1) { create(:ci_runner, :project, contacted_at: 7.minutes.ago, projects: [project]) }
let_it_be(:runner_project_2) { create(:ci_runner, :project, contacted_at: 6.minutes.ago, projects: [project_2]) }
let_it_be(:runner_project_3) { create(:ci_runner, :project, contacted_at: 5.minutes.ago, description: 'runner_project_search', projects: [project, project_2]) }
let_it_be(:runner_project_4) { create(:ci_runner, :project, contacted_at: 4.minutes.ago, projects: [project_3]) }
let_it_be(:runner_project_5) { create(:ci_runner, :project, contacted_at: 3.minutes.ago, tag_list: %w[runner_tag], projects: [project_4]) }
let_it_be(:runner_project_6) { create(:ci_runner, :project, contacted_at: 2.minutes.ago, projects: [project_5]) }
let_it_be(:runner_project_7) { create(:ci_runner, :project, contacted_at: 1.minute.ago, projects: [project_6]) }
let(:target_group) { nil }
let(:membership) { nil }
let(:extra_params) { {} }
let(:params) { { group: target_group, membership: membership }.merge(extra_params).reject { |_, v| v.nil? } }
before do
group.runners << runner_group
sub_group_1.runners << runner_sub_group_1
sub_group_2.runners << runner_sub_group_2
sub_group_3.runners << runner_sub_group_3
sub_group_4.runners << runner_sub_group_4
end
describe '#execute' do
subject(:execute) { described_class.new(current_user: user, params: params).execute }
shared_examples 'membership equal to :descendants' do
it 'returns all descendant runners' do
is_expected.to contain_exactly(
runner_project_7, runner_project_6, runner_project_5,
runner_project_4, runner_project_3, runner_project_2,
runner_project_1, runner_sub_group_4, runner_sub_group_3,
runner_sub_group_2, runner_sub_group_1, runner_group)
end
end
context 'with user is group maintainer or owner' do
where(:user_role) do
[GroupMember::OWNER, GroupMember::MAINTAINER]
end
with_them do
before do
group.add_member(user, user_role)
end
context 'with :group as target group' do
let(:target_group) { group }
context 'passing no membership params' do
it_behaves_like 'membership equal to :descendants'
end
context 'with :descendants membership' do
let(:membership) { :descendants }
it_behaves_like 'membership equal to :descendants'
end
context 'with :direct membership' do
let(:membership) { :direct }
it 'returns runners belonging to group' do
is_expected.to contain_exactly(runner_group)
end
end
context 'with :all_available membership' do
let(:membership) { :all_available }
it 'returns runners available to group' do
is_expected.to contain_exactly(
runner_project_7, runner_project_6, runner_project_5,
runner_project_4, runner_project_3, runner_project_2,
runner_project_1, runner_sub_group_4, runner_sub_group_3,
runner_sub_group_2, runner_sub_group_1, runner_group, runner_instance)
end
end
context 'with unknown membership' do
let(:membership) { :unsupported }
it 'raises an error' do
expect { subject }.to raise_error(ArgumentError, 'Invalid membership filter')
end
end
context 'with nil group' do
let(:target_group) { nil }
it 'raises Gitlab::Access::AccessDeniedError' do
# Query should run against all runners, however since user is not admin, we raise an error
expect { execute }.to raise_error(Gitlab::Access::AccessDeniedError)
end
end
context 'with sort param' do
let(:extra_params) { { sort: 'contacted_asc' } }
it 'sorts by specified attribute' do
expect(subject).to eq([runner_group, runner_sub_group_1, runner_sub_group_2,
runner_sub_group_3, runner_sub_group_4, runner_project_1,
runner_project_2, runner_project_3, runner_project_4,
runner_project_5, runner_project_6, runner_project_7])
end
end
context 'filtering' do
context 'by search term' do
let(:extra_params) { { search: 'runner_project_search' } }
it 'returns correct runner' do
expect(subject).to match_array([runner_project_3])
end
end
context 'by active status' do
let(:extra_params) { { active: false } }
it 'returns correct runner' do
expect(subject).to match_array([runner_sub_group_1])
end
end
context 'by status' do
let(:extra_params) { { status_status: 'paused' } }
it 'returns correct runner' do
expect(subject).to match_array([runner_sub_group_1])
end
end
context 'by tag_name' do
let(:extra_params) { { tag_name: %w[runner_tag] } }
it 'returns correct runner' do
expect(subject).to match_array([runner_project_5])
end
end
context 'by runner type' do
let(:extra_params) { { type_type: 'project_type' } }
it 'returns correct runners' do
expect(subject).to eq([runner_project_7, runner_project_6,
runner_project_5, runner_project_4,
runner_project_3, runner_project_2, runner_project_1])
end
end
end
end
end
end
context 'when user is group developer or below' do
where(:user_role) do
[GroupMember::DEVELOPER, GroupMember::REPORTER, GroupMember::GUEST]
end
with_them do
before do
group.add_member(user, user_role)
end
context 'with :sub_group_1 as target group' do
let(:target_group) { sub_group_1 }
it 'raises Gitlab::Access::AccessDeniedError' do
expect { execute }.to raise_error(Gitlab::Access::AccessDeniedError)
end
end
context 'with :group as target group' do
let(:target_group) { group }
it 'raises Gitlab::Access::AccessDeniedError' do
expect { execute }.to raise_error(Gitlab::Access::AccessDeniedError)
end
context 'with :all_available membership' do
let(:membership) { :all_available }
it 'raises Gitlab::Access::AccessDeniedError' do
expect { execute }.to raise_error(Gitlab::Access::AccessDeniedError)
end
end
end
end
end
context 'when user has no access' do
it 'raises Gitlab::Access::AccessDeniedError' do
expect { execute }.to raise_error(Gitlab::Access::AccessDeniedError)
end
end
context 'when user is nil' do
let(:user) { nil }
it 'raises Gitlab::Access::AccessDeniedError' do
expect { execute }.to raise_error(Gitlab::Access::AccessDeniedError)
end
end
end
describe '#sort_key' do
subject(:sort_key) { described_class.new(current_user: user, params: params.merge(group: group)).sort_key }
context 'without params' do
it { is_expected.to eq('created_at_desc') }
end
context 'with params' do
let(:extra_params) { { sort: 'contacted_asc' } }
it { is_expected.to eq('contacted_asc') }
end
end
end
context 'project' do
let_it_be(:user) { create(:user) }
let_it_be(:group) { create(:group) }
let_it_be(:project) { create(:project, group: group) }
let_it_be(:other_project) { create(:project) }
let(:extra_params) { {} }
let(:params) { { project: project }.merge(extra_params).reject { |_, v| v.nil? } }
describe '#execute' do
subject(:execute) { described_class.new(current_user: user, params: params).execute }
context 'with user as project admin' do
before do
project.add_maintainer(user)
end
context 'with project runners' do
let_it_be(:runner_project) { create(:ci_runner, :project, contacted_at: 7.minutes.ago, projects: [project]) }
it 'returns runners available to project' do
is_expected.to match_array([runner_project])
end
end
context 'with ancestor group runners' do
let_it_be(:runner_instance) { create(:ci_runner, contacted_at: 13.minutes.ago) }
let_it_be(:runner_group) { create(:ci_runner, :group, contacted_at: 12.minutes.ago, groups: [group]) }
it 'returns runners available to project' do
is_expected.to match_array([runner_instance, runner_group])
end
end
context 'with allowed shared runners' do
let_it_be(:runner_instance) { create(:ci_runner, :instance, contacted_at: 13.minutes.ago) }
it 'returns runners available to project' do
expect(subject).to match_array([runner_instance])
end
end
context 'with project, ancestor group, and allowed shared runners' do
let_it_be(:runner_project) { create(:ci_runner, :project, contacted_at: 7.minutes.ago, projects: [project]) }
let_it_be(:runner_group) { create(:ci_runner, :group, contacted_at: 12.minutes.ago, groups: [group]) }
let_it_be(:runner_instance) { create(:ci_runner, :instance, contacted_at: 13.minutes.ago) }
it 'returns runners available to project' do
expect(subject).to match_array([runner_project, runner_group, runner_instance])
end
end
context 'filtering' do
let_it_be(:runner_instance_inactive) { create(:ci_runner, :instance, active: false, contacted_at: 13.minutes.ago) }
let_it_be(:runner_instance_active) { create(:ci_runner, :instance, active: true, contacted_at: 13.minutes.ago) }
let_it_be(:runner_project_active) { create(:ci_runner, :project, contacted_at: 5.minutes.ago, active: true, projects: [project]) }
let_it_be(:runner_project_inactive) { create(:ci_runner, :project, contacted_at: 5.minutes.ago, active: false, projects: [project]) }
let_it_be(:runner_other_project_inactive) { create(:ci_runner, :project, contacted_at: 5.minutes.ago, active: false, projects: [other_project]) }
context 'by search term' do
let_it_be(:runner_project_1) { create(:ci_runner, :project, contacted_at: 5.minutes.ago, description: 'runner_project_search', projects: [project]) }
let_it_be(:runner_project_2) { create(:ci_runner, :project, contacted_at: 5.minutes.ago, description: 'runner_project', projects: [project]) }
let_it_be(:runner_another_project) { create(:ci_runner, :project, contacted_at: 5.minutes.ago, description: 'runner_project_search', projects: [other_project]) }
let(:extra_params) { { search: 'runner_project_search' } }
it 'returns the correct runner' do
expect(subject).to match_array([runner_project_1])
end
end
context 'by active status' do
let(:extra_params) { { active: false } }
it 'returns the correct runners' do
expect(subject).to match_array([runner_instance_inactive, runner_project_inactive])
end
end
context 'by status' do
let(:extra_params) { { status_status: 'paused' } }
it 'returns correct runner' do
expect(subject).to match_array([runner_instance_inactive, runner_project_inactive])
end
end
context 'by tag_name' do
let_it_be(:runner_project_1) { create(:ci_runner, :project, contacted_at: 3.minutes.ago, tag_list: %w[runner_tag], projects: [project]) }
let_it_be(:runner_project_2) { create(:ci_runner, :project, contacted_at: 3.minutes.ago, tag_list: %w[other_tag], projects: [project]) }
let_it_be(:runner_other_project) { create(:ci_runner, :project, contacted_at: 3.minutes.ago, tag_list: %w[runner_tag], projects: [other_project]) }
let(:extra_params) { { tag_name: %w[runner_tag] } }
it 'returns correct runner' do
expect(subject).to match_array([runner_project_1])
end
end
context 'by runner type' do
let(:extra_params) { { type_type: 'project_type' } }
it 'returns correct runners' do
expect(subject).to match_array([runner_project_active, runner_project_inactive])
end
end
context 'by creator' do
let_it_be(:runner_creator_1) { create(:ci_runner, creator_id: '1') }
let(:extra_params) { { creator_id: '1' } }
it 'returns correct runners' do
is_expected.to contain_exactly(runner_creator_1)
end
end
end
end
context 'with user as project developer' do
let(:user) { create(:user) }
before do
project.add_developer(user)
end
it 'raises Gitlab::Access::AccessDeniedError' do
expect { execute }.to raise_error(Gitlab::Access::AccessDeniedError)
end
end
context 'when user is nil' do
let_it_be(:user) { nil }
it 'raises Gitlab::Access::AccessDeniedError' do
expect { execute }.to raise_error(Gitlab::Access::AccessDeniedError)
end
end
context 'with nil project_full_path' do
let(:project_full_path) { nil }
it 'raises Gitlab::Access::AccessDeniedError' do
expect { execute }.to raise_error(Gitlab::Access::AccessDeniedError)
end
end
end
end
end
|