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
|
# frozen_string_literal: true
require 'spec_helper'
RSpec.describe API::Ci::Runner, :clean_gitlab_redis_shared_state do
describe '/api/v4/runners' do
describe 'POST /api/v4/runners' do
context 'when no token is provided' do
it 'returns 400 error' do
post api('/runners')
expect(response).to have_gitlab_http_status(:bad_request)
end
end
context 'when invalid token is provided' do
it 'returns 403 error' do
allow_next_instance_of(::Ci::Runners::RegisterRunnerService) do |service|
allow(service).to receive(:execute)
.and_return(ServiceResponse.error(message: 'invalid token supplied', http_status: :forbidden))
end
post api('/runners'), params: { token: 'invalid' }
expect(response).to have_gitlab_http_status(:forbidden)
end
end
context 'when valid parameters are provided' do
def request
post api('/runners'), params: {
token: 'valid token',
description: 'server.hostname',
maintenance_note: 'Some maintainer notes',
run_untagged: false,
tag_list: 'tag1, tag2',
locked: true,
paused: false,
access_level: 'ref_protected',
maximum_timeout: 9000
}
end
let_it_be(:new_runner) { create(:ci_runner) }
before do
allow_next_instance_of(::Ci::Runners::RegisterRunnerService) do |service|
expected_params = {
description: 'server.hostname',
maintenance_note: 'Some maintainer notes',
run_untagged: false,
tag_list: %w(tag1 tag2),
locked: true,
active: true,
access_level: 'ref_protected',
maximum_timeout: 9000
}.stringify_keys
expect(service).to receive(:execute)
.once
.with('valid token', a_hash_including(expected_params))
.and_return(ServiceResponse.success(payload: { runner: new_runner }))
end
end
context 'when token_expires_at is nil' do
it 'creates runner' do
request
expect(response).to have_gitlab_http_status(:created)
expect(json_response).to eq({ 'id' => new_runner.id, 'token' => new_runner.token, 'token_expires_at' => nil })
end
end
context 'when token_expires_at is a valid date' do
before do
new_runner.token_expires_at = DateTime.new(2022, 1, 11, 14, 39, 24)
end
it 'creates runner' do
request
expect(response).to have_gitlab_http_status(:created)
expect(json_response).to eq({ 'id' => new_runner.id, 'token' => new_runner.token, 'token_expires_at' => '2022-01-11T14:39:24.000Z' })
end
end
it_behaves_like 'storing arguments in the application context for the API' do
subject { request }
let(:expected_params) { { client_id: "runner/#{new_runner.id}" } }
end
it_behaves_like 'not executing any extra queries for the application context' do
let(:subject_proc) { proc { request } }
end
end
context 'when deprecated maintainer_note field is provided' do
RSpec::Matchers.define_negated_matcher :excluding, :include
def request
post api('/runners'), params: {
token: 'valid token',
maintainer_note: 'Some maintainer notes'
}
end
let(:new_runner) { create(:ci_runner) }
it 'converts to maintenance_note param' do
allow_next_instance_of(::Ci::Runners::RegisterRunnerService) do |service|
expect(service).to receive(:execute)
.once
.with('valid token', a_hash_including('maintenance_note' => 'Some maintainer notes')
.and(excluding('maintainter_note' => anything)))
.and_return(ServiceResponse.success(payload: { runner: new_runner }))
end
request
expect(response).to have_gitlab_http_status(:created)
end
end
context 'when deprecated active parameter is provided' do
def request
post api('/runners'), params: {
token: 'valid token',
active: false
}
end
let_it_be(:new_runner) { build(:ci_runner) }
it 'uses active value in registration' do
expect_next_instance_of(::Ci::Runners::RegisterRunnerService) do |service|
expected_params = { active: false }.stringify_keys
expect(service).to receive(:execute)
.once
.with('valid token', a_hash_including(expected_params))
.and_return(ServiceResponse.success(payload: { runner: new_runner }))
end
request
expect(response).to have_gitlab_http_status(:created)
end
end
context 'calling actual register service' do
include StubGitlabCalls
let(:registration_token) { 'abcdefg123456' }
before do
stub_gitlab_calls
stub_application_setting(runners_registration_token: registration_token)
allow_any_instance_of(::Ci::Runner).to receive(:cache_attributes)
end
%w(name version revision platform architecture).each do |param|
context "when info parameter '#{param}' info is present" do
let(:value) { "#{param}_value" }
it "updates provided Runner's parameter" do
post api('/runners'), params: {
token: registration_token,
info: { param => value }
}
expect(response).to have_gitlab_http_status(:created)
expect(::Ci::Runner.last.read_attribute(param.to_sym)).to eq(value)
end
end
end
it "sets the runner's ip_address" do
post api('/runners'),
params: { token: registration_token },
headers: { 'X-Forwarded-For' => '123.111.123.111' }
expect(response).to have_gitlab_http_status(:created)
expect(::Ci::Runner.last.ip_address).to eq('123.111.123.111')
end
context 'when tags parameter is provided' do
def request
post api('/runners'), params: {
token: registration_token,
tag_list: tag_list
}
end
context 'with number of tags above limit' do
let(:tag_list) { (1..::Ci::Runner::TAG_LIST_MAX_LENGTH + 1).map { |i| "tag#{i}" } }
it 'uses tag_list value in registration and returns error' do
expect_next_instance_of(::Ci::Runners::RegisterRunnerService) do |service|
expected_params = { tag_list: tag_list }.stringify_keys
expect(service).to receive(:execute)
.once
.with(registration_token, a_hash_including(expected_params))
.and_call_original
end
request
expect(response).to have_gitlab_http_status(:bad_request)
expect(json_response.dig('message', 'tags_list')).to contain_exactly("Too many tags specified. Please limit the number of tags to #{::Ci::Runner::TAG_LIST_MAX_LENGTH}")
end
end
context 'with number of tags below limit' do
let(:tag_list) { (1..20).map { |i| "tag#{i}" } }
it 'uses tag_list value in registration and successfully creates runner' do
expect_next_instance_of(::Ci::Runners::RegisterRunnerService) do |service|
expected_params = { tag_list: tag_list }.stringify_keys
expect(service).to receive(:execute)
.once
.with(registration_token, a_hash_including(expected_params))
.and_call_original
end
request
expect(response).to have_gitlab_http_status(:created)
end
end
end
end
end
end
end
|