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
|
# frozen_string_literal: true
require 'spec_helper'
RSpec.describe Gitlab::Ci::RunnerReleases, feature_category: :runner_fleet do
subject { described_class.instance }
let(:runner_releases_url) { 'http://testurl.com/runner_public_releases' }
def releases
subject.releases
end
def releases_by_minor
subject.releases_by_minor
end
before do
subject.reset_backoff!
allow(subject).to receive(:runner_releases_url).and_return(runner_releases_url)
end
describe 'caching behavior', :use_clean_rails_memory_store_caching do
before do
allow(Gitlab::HTTP).to receive(:get).with(runner_releases_url, anything).once { mock_http_response(response) }
end
shared_examples 'requests that follow cache status' do |validity_period|
context "almost #{validity_period.inspect} later" do
let(:followup_request_interval) { validity_period - 0.001.seconds }
it 'returns cached releases' do
releases
travel followup_request_interval do
expect(Gitlab::HTTP).not_to receive(:get)
if expected_releases
expected_result_by_minor = expected_releases.group_by(&:without_patch).transform_values(&:max)
end
expect(releases).to eq(expected_releases)
expect(releases_by_minor).to eq(expected_result_by_minor)
end
end
end
context "after #{validity_period.inspect}" do
let(:followup_request_interval) { validity_period + 1.second }
let(:followup_response) { (response || []) + [{ 'name' => 'v14.9.2' }] }
it 'checks new releases' do
releases
travel followup_request_interval do
expect(Gitlab::HTTP).to receive(:get)
.with(runner_releases_url, anything)
.once { mock_http_response(followup_response) }
new_releases = (expected_releases || []) + [Gitlab::VersionInfo.new(14, 9, 2)]
new_releases_by_minor_version = (expected_releases_by_minor || {}).merge(
Gitlab::VersionInfo.new(14, 9, 0) => Gitlab::VersionInfo.new(14, 9, 2)
)
expect(releases).to eq(new_releases)
expect(releases_by_minor).to eq(new_releases_by_minor_version)
end
end
end
end
shared_examples 'a service implementing exponential backoff' do |opts|
it 'performs exponential backoff on requests', :aggregate_failures do
start_time = Time.now.utc.change(usec: 0)
http_call_timestamp_offsets = []
allow(Gitlab::HTTP).to receive(:get).with(runner_releases_url, anything) do
http_call_timestamp_offsets << Time.now.utc - start_time
err_class = opts&.dig(:raise_error)
raise err_class if err_class
mock_http_response(response)
end
# An initial HTTP request fails
travel_to(start_time)
subject.reset_backoff!
expect(releases).to be_nil
expect(releases_by_minor).to be_nil
# Successive failed requests result in HTTP requests only after specific backoff periods
backoff_periods = [5, 10, 20, 40, 80, 160, 320, 640, 1280, 2560, 3600].map(&:seconds)
backoff_periods.each do |period|
travel(period - 1.second)
expect(releases).to be_nil
expect(releases_by_minor).to be_nil
travel 1.second
expect(releases).to be_nil
expect(releases_by_minor).to be_nil
end
expect(http_call_timestamp_offsets).to eq([0, 5, 15, 35, 75, 155, 315, 635, 1275, 2555, 5115, 8715])
# Finally a successful HTTP request results in releases being returned
allow(Gitlab::HTTP).to receive(:get)
.with(runner_releases_url, anything)
.once { mock_http_response([{ 'name' => 'v14.9.1-beta1-ee' }]) }
travel 1.hour
expect(releases).not_to be_nil
expect(releases_by_minor).not_to be_nil
end
end
context 'when request results in timeout' do
let(:response) {}
let(:expected_releases) { nil }
let(:expected_releases_by_minor) { nil }
it_behaves_like 'requests that follow cache status', 5.seconds
it_behaves_like 'a service implementing exponential backoff', raise_error: Net::OpenTimeout
it_behaves_like 'a service implementing exponential backoff', raise_error: Errno::ETIMEDOUT
end
context 'when response is nil' do
let(:response) { nil }
let(:expected_releases) { nil }
let(:expected_releases_by_minor) { nil }
it_behaves_like 'requests that follow cache status', 5.seconds
it_behaves_like 'a service implementing exponential backoff'
end
context 'when response is not nil' do
let(:response) { [{ 'name' => 'v14.9.1-beta1-ee' }, { 'name' => 'v14.9.0' }] }
let(:expected_releases) do
[
Gitlab::VersionInfo.new(14, 9, 0),
Gitlab::VersionInfo.new(14, 9, 1, '-beta1-ee')
]
end
let(:expected_releases_by_minor) do
{
Gitlab::VersionInfo.new(14, 9, 0) => Gitlab::VersionInfo.new(14, 9, 1, '-beta1-ee')
}
end
it_behaves_like 'requests that follow cache status', 1.day
end
end
describe '#releases', :use_clean_rails_memory_store_caching do
before do
allow(Gitlab::HTTP).to receive(:get).with(runner_releases_url, anything).once { mock_http_response(response) }
end
context 'when response is nil' do
let(:response) { nil }
let(:expected_result) { nil }
it 'returns nil' do
expect(releases).to be_nil
end
end
context 'when response is not nil' do
let(:response) { [{ 'name' => 'v14.9.1-beta1-ee' }, { 'name' => 'v14.9.0' }] }
let(:expected_result) do
[
Gitlab::VersionInfo.new(14, 9, 0),
Gitlab::VersionInfo.new(14, 9, 1, '-beta1-ee')
]
end
it 'returns parsed and sorted Gitlab::VersionInfo objects' do
expect(releases).to eq(expected_result)
end
context 'when fetching runner releases is disabled' do
before do
stub_application_setting(update_runner_versions_enabled: false)
end
it 'returns nil' do
expect(releases).to be_nil
end
end
end
context 'when response contains unexpected input type' do
let(:response) { 'error' }
it { expect(releases).to be_nil }
end
context 'when response contains unexpected input array' do
let(:response) { ['error'] }
it { expect(releases).to be_nil }
end
end
describe '#releases_by_minor', :use_clean_rails_memory_store_caching do
before do
allow(Gitlab::HTTP).to receive(:get).with(runner_releases_url, anything).once { mock_http_response(response) }
end
context 'when response is nil' do
let(:response) { nil }
let(:expected_result) { nil }
it 'returns nil' do
expect(releases_by_minor).to be_nil
end
end
context 'when response is not nil' do
let(:response) { [{ 'name' => 'v14.9.1-beta1-ee' }, { 'name' => 'v14.9.0' }, { 'name' => 'v14.8.1' }] }
let(:expected_result) do
{
Gitlab::VersionInfo.new(14, 8, 0) => Gitlab::VersionInfo.new(14, 8, 1),
Gitlab::VersionInfo.new(14, 9, 0) => Gitlab::VersionInfo.new(14, 9, 1, '-beta1-ee')
}
end
it 'returns parsed and grouped Gitlab::VersionInfo objects' do
expect(releases_by_minor).to eq(expected_result)
end
context 'when fetching runner releases is disabled' do
before do
stub_application_setting(update_runner_versions_enabled: false)
end
it 'returns nil' do
expect(releases_by_minor).to be_nil
end
end
end
context 'when response contains unexpected input type' do
let(:response) { 'error' }
it { expect(releases_by_minor).to be_nil }
end
context 'when response contains unexpected input array' do
let(:response) { ['error'] }
it { expect(releases_by_minor).to be_nil }
end
end
describe '#enabled?' do
it { is_expected.to be_enabled }
context 'when fetching runner releases is disabled' do
before do
stub_application_setting(update_runner_versions_enabled: false)
end
it { is_expected.not_to be_enabled }
end
end
def mock_http_response(response)
http_response = instance_double(HTTParty::Response)
allow(http_response).to receive(:success?).and_return(!response.nil?)
allow(http_response).to receive(:parsed_response).and_return(response)
http_response
end
end
|