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
|
# frozen_string_literal: true
require 'spec_helper'
RSpec.describe ::Ci::Runners::BulkDeleteRunnersService, '#execute' do
subject(:execute) { described_class.new(**service_args).execute }
let_it_be(:admin_user) { create(:user, :admin) }
let_it_be_with_refind(:owner_user) { create(:user) } # discard memoized ci_owned_runners
let_it_be(:group) { create(:group) }
let_it_be(:project) { create(:project, group: group) }
let(:user) {}
let(:service_args) { { runners: runners_arg, current_user: user } }
let(:runners_arg) {}
context 'with runners specified' do
let!(:instance_runner) { create(:ci_runner) }
let!(:group_runner) { create(:ci_runner, :group, groups: [group]) }
let!(:project_runner) { create(:ci_runner, :project, projects: [project]) }
shared_examples 'a service deleting runners in bulk' do
let!(:expected_deleted_ids) { expected_deleted_runners.map(&:id) }
it 'destroys runners', :aggregate_failures do
expect { execute }.to change { Ci::Runner.count }.by(-expected_deleted_ids.count)
is_expected.to be_success
expect(execute.payload).to eq(
{
deleted_count: expected_deleted_ids.count,
deleted_ids: expected_deleted_ids,
errors: []
})
expect { project_runner.runner_projects.first.reload }.to raise_error(ActiveRecord::RecordNotFound)
expected_deleted_runners.each do |deleted_runner|
expect(deleted_runner[:errors]).to be_nil
expect { deleted_runner.reload }.to raise_error(ActiveRecord::RecordNotFound)
end
end
context 'with too many runners specified' do
before do
stub_const("#{described_class}::RUNNER_LIMIT", 1)
end
it 'deletes only first RUNNER_LIMIT runners', :aggregate_failures do
expect { execute }.to change { Ci::Runner.count }.by(-1)
is_expected.to be_success
expect(execute.payload).to eq(
{
deleted_count: 1,
deleted_ids: expected_deleted_ids.take(1),
errors: ["Can only delete up to 1 runners per call. Ignored the remaining runner(s)."]
})
end
end
end
context 'when the user cannot delete runners' do
let(:runners_arg) { Ci::Runner.all }
context 'when user is not group owner' do
before do
group.add_developer(user)
end
let(:user) { create(:user) }
it 'does not delete any runner and returns error', :aggregate_failures do
expect { execute }.not_to change { Ci::Runner.count }
expect(execute[:errors]).to match_array("User does not have permission to delete any of the runners")
end
end
context 'when user is not part of the group' do
let(:user) { create(:user) }
it 'does not delete any runner and returns error', :aggregate_failures do
expect { execute }.not_to change { Ci::Runner.count }
expect(execute[:errors]).to match_array("User does not have permission to delete any of the runners")
end
end
end
context 'when the user can delete runners' do
context 'when user is an admin', :enable_admin_mode do
include_examples 'a service deleting runners in bulk' do
let(:runners_arg) { Ci::Runner.all }
let!(:expected_deleted_runners) { [instance_runner, group_runner, project_runner] }
let(:user) { admin_user }
end
context 'with a runner already deleted' do
before do
group_runner.destroy!
end
include_examples 'a service deleting runners in bulk' do
let(:runners_arg) { Ci::Runner.all }
let!(:expected_deleted_runners) { [instance_runner, project_runner] }
let(:user) { admin_user }
end
end
context 'when deleting a single runner' do
let(:runners_arg) { Ci::Runner.all }
it 'avoids N+1 cached queries', :use_sql_query_cache, :request_store do
# Run this once to establish a baseline
control_count = ActiveRecord::QueryRecorder.new(skip_cached: false) do
execute
end
additional_runners = 1
create_list(:ci_runner, 1 + additional_runners, :instance)
create_list(:ci_runner, 1 + additional_runners, :group, groups: [group])
create_list(:ci_runner, 1 + additional_runners, :project, projects: [project])
service = described_class.new(runners: runners_arg, current_user: user)
# Base cost per runner is:
# - 1 `SELECT * FROM "taggings"` query
# - 1 `SAVEPOINT` query
# - 1 `DELETE FROM "ci_runners"` query
# - 1 `RELEASE SAVEPOINT` query
# Project runners have an additional query:
# - 1 `DELETE FROM "ci_runner_projects"` query, given the call to `destroy_all`
instance_runner_cost = 4
group_runner_cost = 4
project_runner_cost = 5
expect { service.execute }
.not_to exceed_all_query_limit(control_count)
.with_threshold(additional_runners * (instance_runner_cost + group_runner_cost + project_runner_cost))
end
end
end
context 'when user is group owner' do
before do
group.add_owner(user)
end
include_examples 'a service deleting runners in bulk' do
let(:runners_arg) { Ci::Runner.not_instance_type }
let!(:expected_deleted_runners) { [group_runner, project_runner] }
let(:user) { owner_user }
end
context 'with a runner non-authorised to be deleted' do
let(:runners_arg) { Ci::Runner.all }
let!(:expected_deleted_runners) { [project_runner] }
let(:user) { owner_user }
it 'destroys only authorised runners', :aggregate_failures do
allow(Ability).to receive(:allowed?).and_call_original
expect(Ability).to receive(:allowed?).with(user, :delete_runner, instance_runner).and_return(false)
expect { execute }.to change { Ci::Runner.count }.by(-2)
is_expected.to be_success
expect(execute.payload).to eq(
{
deleted_count: 2,
deleted_ids: [group_runner.id, project_runner.id],
errors: ["User does not have permission to delete runner(s) ##{instance_runner.id}"]
})
end
end
end
end
context 'with no arguments specified' do
let(:runners_arg) { nil }
let(:user) { owner_user }
it 'returns 0 deleted runners' do
is_expected.to be_success
expect(execute.payload).to eq({ deleted_count: 0, deleted_ids: [], errors: [] })
end
end
end
end
|