-
Notifications
You must be signed in to change notification settings - Fork 369
Expand file tree
/
Copy pathprocesses_sync_spec.rb
More file actions
550 lines (468 loc) · 23.9 KB
/
processes_sync_spec.rb
File metadata and controls
550 lines (468 loc) · 23.9 KB
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
require 'spec_helper'
require 'utils/workpool'
module VCAP::CloudController
module Diego
RSpec.describe ProcessesSync, job_context: :clock do
subject { ProcessesSync.new(config:, statsd_updater:) }
let(:config) { instance_double(Config) }
let(:bbs_apps_client) { instance_double(BbsAppsClient) }
let(:scheduling_infos) { [] }
let(:statsd_updater) { instance_double(VCAP::CloudController::Metrics::StatsdUpdater, update_synced_invalid_lrps: nil) }
before do
CloudController::DependencyLocator.instance.register(:bbs_apps_client, bbs_apps_client)
allow(bbs_apps_client).to receive(:fetch_scheduling_infos).and_return(scheduling_infos)
allow(bbs_apps_client).to receive(:bump_freshness)
end
describe '#sync' do
let(:workpool) { instance_double(WorkPool, submit: nil, exceptions: nil, drain: nil) }
let(:fake_logger) { instance_double(Steno::Logger, debug: nil, info: nil, error: nil) }
before do
allow(Steno).to receive(:logger).and_return(fake_logger)
end
it 'bumps freshness' do
subject.sync
expect(bbs_apps_client).to have_received(:bump_freshness).once
end
context 'when bbs and CC are in sync' do
let(:scheduling_infos) { [good_lrp_scheduling_info] }
let(:good_lrp_scheduling_info) do
::Diego::Bbs::Models::DesiredLRPSchedulingInfo.new(
desired_lrp_key: ::Diego::Bbs::Models::DesiredLRPKey.new({
process_guid: ProcessGuid.from_process(good_process)
}),
annotation: good_process.updated_at.to_f.to_s
)
end
let(:good_lrp) { ::Diego::Bbs::Models::DesiredLRP.new(process_guid: 'good-lrp') }
let!(:good_process) { ProcessModel.make(:diego_runnable) }
it 'does not touch lrps that are up to date and correct' do
allow(bbs_apps_client).to receive(:desire_app)
allow(bbs_apps_client).to receive(:update_app)
allow(bbs_apps_client).to receive(:stop_app)
subject.sync
expect(bbs_apps_client).not_to have_received(:desire_app)
expect(bbs_apps_client).not_to have_received(:update_app)
expect(bbs_apps_client).not_to have_received(:stop_app)
expect(bbs_apps_client).to have_received(:bump_freshness).once
end
context 'when the process is a docker app' do
let(:app) { AppModel.make(:docker, droplet: DropletModel.make(:docker)) }
let(:good_process) { ProcessModel.make(:docker, state: 'STARTED', app: app) }
context 'when diego_docker is enabled' do
before do
FeatureFlag.create(name: 'diego_docker', enabled: true)
end
it 'does not touch lrps that are up to date and correct' do
allow(bbs_apps_client).to receive(:desire_app)
allow(bbs_apps_client).to receive(:update_app)
allow(bbs_apps_client).to receive(:stop_app)
subject.sync
expect(bbs_apps_client).not_to have_received(:desire_app)
expect(bbs_apps_client).not_to have_received(:update_app)
expect(bbs_apps_client).not_to have_received(:stop_app)
expect(bbs_apps_client).to have_received(:bump_freshness).once
end
end
context 'when diego_docker is disabled' do
before do
FeatureFlag.create(name: 'diego_docker', enabled: false)
end
it 'deletes the LRP' do
allow(bbs_apps_client).to receive(:stop_app)
subject.sync
expect(bbs_apps_client).to have_received(:stop_app).with(ProcessGuid.from_process(good_process))
end
end
end
end
context 'when a diego LRP is stale' do
let(:scheduling_infos) { [stale_lrp_scheduling_info] }
let(:stale_lrp_scheduling_info) do
::Diego::Bbs::Models::DesiredLRPSchedulingInfo.new(
desired_lrp_key: ::Diego::Bbs::Models::DesiredLRPKey.new({
process_guid: ProcessGuid.from_process(stale_process)
}),
annotation: 'outdated'
)
end
let(:stale_lrp) { ::Diego::Bbs::Models::DesiredLRP.new(process_guid: 'stale-lrp') }
let(:stale_lrp_update) do
::Diego::Bbs::Models::DesiredLRPUpdate.new(
instances: stale_process.instances,
annotation: stale_process.updated_at.to_f.to_s,
routes: ::Diego::Bbs::Models::ProtoRoutes.new(routes: [])
)
end
let!(:stale_process) { ProcessModel.make(:diego_runnable) }
it 'updates stale lrps' do
allow(bbs_apps_client).to receive(:update_app)
subject.sync
expect(bbs_apps_client).to have_received(:update_app).with(stale_process, stale_lrp_scheduling_info)
expect(bbs_apps_client).to have_received(:bump_freshness).once
end
context 'when the stale process gets a new version between fetching for sync (only guid + version + created_at) and fetching for actual update (full model)' do
before do
allow(subject).to receive(:update_lrps).and_wrap_original do |method, *args|
stale_process.set_new_version
stale_process.save
method.call(*args)
end
end
it 'skips the (obsolete) update' do
allow(bbs_apps_client).to receive(:update_app)
subject.sync
expect(bbs_apps_client).not_to have_received(:update_app)
end
end
context 'when the process is a cnb app' do
let(:app) { AppModel.make(:cnb, droplet: DropletModel.make(:cnb)) }
let!(:stale_process) { ProcessModel.make(:cnb, state: 'STARTED', app: app) }
before do
FeatureFlag.create(name: 'diego_cnb', enabled: true)
end
context 'when diego_docker is disabled' do
before do
FeatureFlag.create(name: 'diego_docker', enabled: false)
end
it 'updates the stale lrp' do
allow(bbs_apps_client).to receive(:update_app)
subject.sync
expect(bbs_apps_client).to have_received(:update_app).with(stale_process, stale_lrp_scheduling_info)
expect(bbs_apps_client).to have_received(:bump_freshness).once
end
end
context 'when diego_docker is enabled' do
before do
FeatureFlag.create(name: 'diego_docker', enabled: true)
end
it 'updates the stale lrp' do
allow(bbs_apps_client).to receive(:update_app)
subject.sync
expect(bbs_apps_client).to have_received(:update_app).with(stale_process, stale_lrp_scheduling_info)
expect(bbs_apps_client).to have_received(:bump_freshness).once
end
end
end
context 'when the process is a docker app' do
let(:app) { AppModel.make(:docker, droplet: DropletModel.make(:docker)) }
let!(:stale_process) { ProcessModel.make(:docker, state: 'STARTED', app: app) }
context 'when diego_docker is enabled' do
before do
FeatureFlag.create(name: 'diego_docker', enabled: true)
end
it 'updates the stale lrp' do
allow(bbs_apps_client).to receive(:update_app)
subject.sync
expect(bbs_apps_client).to have_received(:update_app).with(stale_process, stale_lrp_scheduling_info)
expect(bbs_apps_client).to have_received(:bump_freshness).once
end
end
context 'when diego_docker is disabled' do
before do
FeatureFlag.create(name: 'diego_docker', enabled: false)
end
it 'deletes the lrp' do
allow(bbs_apps_client).to receive(:stop_app)
subject.sync
expect(bbs_apps_client).to have_received(:stop_app).with(ProcessGuid.from_process(stale_process))
expect(bbs_apps_client).to have_received(:bump_freshness).once
end
end
end
context 'when updating app fails' do
# bbs_apps_client will raise ApiErrors as of right now, we should think about factoring that out so that
# the background job doesn't have to deal with API concerns
before do
allow(bbs_apps_client).to receive(:update_app).and_raise(error)
end
context 'when RunnerInvalidRequest is returned' do
let(:error) { CloudController::Errors::ApiError.new_from_details('RunnerInvalidRequest', 'bad request') }
it 'bumps freshness' do
expect { subject.sync }.not_to raise_error
expect(bbs_apps_client).to have_received(:bump_freshness).once
end
end
context 'when the app has been deleted' do
let(:error) { CloudController::Errors::ApiError.new_from_details('RunnerError', 'the requested resource could not be found') }
let(:logger) { double(:logger, info: nil, error: nil) }
before do
allow(Steno).to receive(:logger).and_return(logger)
allow(WorkPool).to receive(:new).and_return(workpool)
allow(workpool).to receive(:submit)
allow(workpool).to receive(:exceptions).and_return([error])
end
it 'bumps freshness, ignoring the error' do
subject.sync
expect(workpool).to have_received(:submit)
expect(bbs_apps_client).to have_received(:bump_freshness)
expect(logger).not_to have_received(:error)
expect(logger).to have_received(:info).with(
'ignore-deleted-resource', error: error.name, error_message: error.message
)
end
end
context 'any other error' do
let(:error) { CloudController::Errors::ApiError.new_from_details('RunnerError', 'some error') }
it 'does not bump freshness' do
expect { subject.sync }.not_to raise_error
expect(fake_logger).to have_received(:error).with(
'error-updating-lrp-state',
error: 'RunnerError',
error_message: 'Runner error: some error',
error_backtrace: anything
)
expect(bbs_apps_client).not_to have_received(:bump_freshness)
end
end
end
end
context 'when diego does not contain the LRP' do
let(:scheduling_infos) { [] }
let!(:missing_process) { ProcessModel.make(:diego_runnable) }
it 'creates missing lrps' do
allow(bbs_apps_client).to receive(:desire_app).with(missing_process)
subject.sync
expect(bbs_apps_client).to have_received(:desire_app).with(missing_process)
expect(bbs_apps_client).to have_received(:bump_freshness).once
end
context 'when the missing process gets a new version between fetching for sync (only guid + version + created_at) and fetching for actual creation (full model)' do
before do
allow(subject).to receive(:desire_lrps).and_wrap_original do |method, *args|
missing_process.set_new_version
missing_process.save
method.call(*args)
end
end
it 'skips the (obsolete) creation' do
allow(bbs_apps_client).to receive(:desire_app)
subject.sync
expect(bbs_apps_client).not_to have_received(:desire_app)
end
end
context 'when desiring app fails' do
# bbs_apps_client will raise ApiErrors as of right now, we should think about factoring that out so that
# the background job doesn't have to deal with API concerns
before do
allow(bbs_apps_client).to receive(:desire_app).and_raise(error)
end
context 'when RunnerInvalidRequest is returned' do
let(:error) { CloudController::Errors::ApiError.new_from_details('RunnerInvalidRequest', 'invalid thing') }
it 'bumps freshness' do
expect { subject.sync }.not_to raise_error
expect(bbs_apps_client).to have_received(:bump_freshness).once
end
end
context 'when any other error is returned' do
let(:error) { CloudController::Errors::ApiError.new_from_details('RunnerError', 'some error') }
it 'does not bump freshness' do
expect { subject.sync }.not_to raise_error
expect(fake_logger).to have_received(:error).with(
'error-updating-lrp-state',
error: 'RunnerError',
error_message: 'Runner error: some error',
error_backtrace: anything
)
expect(bbs_apps_client).not_to have_received(:bump_freshness)
end
end
end
end
context 'when diego already contains the LRP' do
let(:good_process) { ProcessModel.make(:diego_runnable) }
let(:error) { CloudController::Errors::ApiError.new_from_details('RunnerError', 'the requested resource already exists') }
let(:indexed_by_thing) { instance_double(Hash) }
let(:existing_lrp) { ::Diego::Bbs::Models::DesiredLRP.new(process_guid: "#{good_process.guid}-#{good_process.version}") }
let(:logger) { double(:logger, info: nil, error: nil) }
before do
allow(existing_lrp).to receive(:nil?).and_return(true)
allow(bbs_apps_client).to receive(:fetch_scheduling_infos).and_return(indexed_by_thing)
allow(indexed_by_thing).to receive(:index_by).and_return({ existing_lrp.process_guid => existing_lrp })
allow(Steno).to receive(:logger).and_return(logger)
allow(WorkPool).to receive(:new).and_return(workpool)
allow(workpool).to receive(:submit)
allow(workpool).to receive(:exceptions).and_return([error])
end
it 'bumps freshness, ignoring the error' do
subject.sync
expect(workpool).to have_received(:submit)
expect(bbs_apps_client).to have_received(:bump_freshness)
expect(logger).not_to have_received(:error)
expect(logger).to have_received(:info).with(
'ignore-existing-resource', error: error.name, error_message: error.message
)
end
end
context 'when CC does not know about a LRP' do
let(:scheduling_infos) { [deleted_lrp_scheduling_info] }
let(:deleted_lrp_scheduling_info) do
::Diego::Bbs::Models::DesiredLRPSchedulingInfo.new(
desired_lrp_key: ::Diego::Bbs::Models::DesiredLRPKey.new({
process_guid: 'deleted'
})
)
end
it 'deletes deleted lrps' do
allow(bbs_apps_client).to receive(:stop_app)
subject.sync
expect(bbs_apps_client).to have_received(:stop_app).with('deleted')
end
context 'when stopping app fails' do
# bbs_apps_client will raise ApiErrors as of right now, we should think about factoring that out so that
# the background job doesn't have to deal with API concerns
let(:error) { CloudController::Errors::ApiError.new_from_details('RunnerError', 'some error') }
before do
allow(bbs_apps_client).to receive(:stop_app).and_raise(error)
end
it 'does not bump freshness' do
expect { subject.sync }.not_to raise_error
expect(fake_logger).to have_received(:error).with(
'error-updating-lrp-state',
error: 'RunnerError',
error_message: 'Runner error: some error',
error_backtrace: anything
)
expect(bbs_apps_client).not_to have_received(:bump_freshness)
end
end
end
context 'when fetching from diego fails' do
context "when there's a problem with the runner" do
# bbs_apps_client will raise ApiErrors as of right now, we should think about factoring that out so that
# the background job doesn't have to deal with API concerns
let(:error) { CloudController::Errors::ApiError.new_from_details('RunnerError', 'some error') }
before do
allow(bbs_apps_client).to receive(:fetch_scheduling_infos).and_raise(error)
end
it 'does not bump freshness' do
expect { subject.sync }.to raise_error(ProcessesSync::BBSFetchError, error.message)
expect(bbs_apps_client).not_to have_received(:bump_freshness)
end
end
context 'when an error occurs while updating an individual LRP' do
let(:error) { VCAP::CloudController::Diego::LifecycleBundleUriGenerator::InvalidStack.new("no compiler defined for requested stack 'schmidlap'") }
before do
allow_any_instance_of(WorkPool).to receive(:exceptions).and_return([error])
end
it 'does not bump freshness' do
subject.sync
expect(fake_logger).to have_received(:error).with(
'error-updating-lrp-state',
error: 'VCAP::CloudController::Diego::LifecycleBundleUriGenerator::InvalidStack',
error_message: "no compiler defined for requested stack 'schmidlap'",
error_backtrace: ''
)
expect(bbs_apps_client).not_to have_received(:bump_freshness)
end
end
end
context 'when a non-Diego error is raised outside of the workpool' do
let(:error) { Sequel::Error.new('Generic Database Error') }
before do
allow(WorkPool).to receive(:new).and_return(workpool)
allow(ProcessModel).to receive(:table_name).and_raise(error)
end
it 'does not bump freshness' do
expect { subject.sync }.to raise_error(error)
expect(bbs_apps_client).not_to have_received(:bump_freshness)
end
it 'drains the workpool threads to prevent thread leakage' do
expect { subject.sync }.to raise_error(error)
expect(workpool).to have_received(:drain)
end
end
context 'when updating LRP state on diego fails multiple times with ignorable errors' do
let(:scheduling_infos) { [] }
let!(:missing_process1) { ProcessModel.make(:diego_runnable) }
let!(:missing_process2) { ProcessModel.make(:diego_runnable) }
let(:ignorable_error) { CloudController::Errors::ApiError.new_from_details('RunnerInvalidRequest', 'invalid thing') }
let(:fake_app_recipe) { instance_double(AppRecipeBuilder, build_app_lrp: double(:app_lrp_recipe)) }
before do
allow(AppRecipeBuilder).to receive(:new).and_return(fake_app_recipe)
allow(bbs_apps_client).to receive(:desire_app).and_raise(ignorable_error)
end
it 'updates freshness' do
subject.sync
expect(bbs_apps_client).to have_received(:bump_freshness)
end
end
context 'when updating LRP state on diego fails multiple times with some non-ignorable errors' do
let(:scheduling_infos) { [] }
let!(:missing_process1) { ProcessModel.make(:diego_runnable) }
let!(:missing_process2) { ProcessModel.make(:diego_runnable) }
let!(:missing_process3) { ProcessModel.make(:diego_runnable) }
let!(:missing_process4) { ProcessModel.make(:diego_runnable) }
let(:ignorable_error) { CloudController::Errors::ApiError.new_from_details('RunnerInvalidRequest', 'invalid thing') }
let(:non_ignorable_error) { CloudController::Errors::ApiError.new_from_details('RunnerError', 'some error') }
let(:non_api_error) { StandardError.new('something went wrong') }
let(:fake_app_recipe) { instance_double(AppRecipeBuilder, build_app_lrp: double(:app_lrp_recipe)) }
before do
allow(AppRecipeBuilder).to receive(:new).and_return(fake_app_recipe)
calls = 0
allow(bbs_apps_client).to receive(:desire_app) do
raise ignorable_error if calls == 0
raise non_ignorable_error if calls == 1
raise non_api_error if calls == 2
ensure
calls += 1
end
end
it 'does not update freshness' do
expect { subject.sync }.not_to raise_error
expect(fake_logger).to have_received(:error).with(
'error-updating-lrp-state',
error: 'RunnerError',
error_message: 'Runner error: some error',
error_backtrace: anything
)
expect(bbs_apps_client).not_to have_received(:bump_freshness)
end
it 'logs all exceptions' do
expect { subject.sync }.not_to raise_error
expect(fake_logger).to have_received(:info).with(
'synced-invalid-desired-lrps',
error: ignorable_error.name,
error_message: ignorable_error.message
)
expect(fake_logger).to have_received(:error).with(
'error-updating-lrp-state',
error: non_ignorable_error.name,
error_message: non_ignorable_error.message,
error_backtrace: anything
)
expect(fake_logger).to have_received(:error).with(
'error-updating-lrp-state',
error: non_api_error.class.name,
error_message: non_api_error.message,
error_backtrace: anything
)
end
it 'continues to attempt to update all necessary lrps' do
begin
subject.sync
rescue StandardError
nil
end
expect(bbs_apps_client).to have_received(:desire_app).with(missing_process4)
end
end
context 'when logging invalid-lrp-request count to statsd' do
let!(:missing_process) { ProcessModel.make(:diego_runnable) }
let!(:missing_process2) { ProcessModel.make(:diego_runnable) }
let!(:missing_process3) { ProcessModel.make(:diego_runnable) }
let(:invalid_request_error) { CloudController::Errors::ApiError.new_from_details('RunnerInvalidRequest', 'invalid thing') }
let(:other_error) { CloudController::Errors::ApiError.new_from_details('RunnerError', 'bad error!') }
before do
allow(bbs_apps_client).to receive(:desire_app).with(missing_process).and_raise(invalid_request_error)
allow(bbs_apps_client).to receive(:desire_app).with(missing_process2).and_raise(invalid_request_error)
allow(bbs_apps_client).to receive(:desire_app).with(missing_process3).and_raise(other_error)
end
it 'updates invalid-request count even if another error is thrown' do
expect { subject.sync }.not_to raise_error
expect(bbs_apps_client).not_to have_received(:bump_freshness)
expect(statsd_updater).to have_received(:update_synced_invalid_lrps).with(2)
end
end
end
end
end
end