forked from ManageIQ/manageiq
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjob_proxy_dispatcher_spec.rb
417 lines (355 loc) · 16.3 KB
/
job_proxy_dispatcher_spec.rb
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
describe JobProxyDispatcher do
include Spec::Support::JobProxyDispatcherHelper
DISPATCH_ONLY = false
if DISPATCH_ONLY
NUM_VMS = 200
NUM_REPO_VMS = 200
NUM_HOSTS = 10
NUM_SERVERS = 10
NUM_STORAGES = 30
else
NUM_VMS = 3
NUM_REPO_VMS = 3
NUM_HOSTS = 3
NUM_SERVERS = 3
NUM_STORAGES = 3
end
let(:dispatcher) { JobProxyDispatcher.new }
before(:each) do
@server = EvmSpecHelper.local_miq_server(:name => "test_server_main_server")
end
context "With a default zone, server, with hosts with a miq_proxy, vmware vms on storages" do
before(:each) do
(NUM_SERVERS - 1).times do |i|
FactoryGirl.create(:miq_server, :zone => @server.zone, :name => "test_server_#{i}")
end
# TODO: We should be able to set values so we don't need to stub behavior
allow_any_instance_of(MiqServer).to receive_messages(:is_vix_disk? => true)
allow_any_instance_of(MiqServer).to receive_messages(:is_a_proxy? => true)
allow_any_instance_of(MiqServer).to receive_messages(:has_active_role? => true)
allow_any_instance_of(ManageIQ::Providers::Vmware::InfraManager).to receive_messages(:missing_credentials? => false)
allow_any_instance_of(Host).to receive_messages(:missing_credentials? => false)
@hosts, @proxies, @storages, @vms, @repo_vms, @container_providers = build_entities(
:hosts => NUM_HOSTS, :storages => NUM_STORAGES, :vms => NUM_VMS, :repo_vms => NUM_REPO_VMS
)
@container_images = @container_providers.collect(&:container_images).flatten
end
describe "#dispatch" do
# Don't run these tests if we only want to run dispatch for load testing
unless DISPATCH_ONLY
it "should have a server in default zone" do
expect(@server.zone).not_to be_nil
expect(@server).not_to be_nil
end
it "should have #{NUM_HOSTS} hosts" do
expect(NUM_HOSTS).to eq(@hosts.length)
end
it "should have #{NUM_VMS} vms and #{NUM_REPO_VMS} repo vms" do
expect(NUM_VMS).to eq(@vms.length)
end
it "should have #{NUM_REPO_VMS} repo vms" do
expect(NUM_REPO_VMS).to eq(@repo_vms.length)
end
context "with a vm without a storage" do
before(:each) do
# Test a vm without a storage (ie, removed from VC but retained in the VMDB)
allow(MiqVimBrokerWorker).to receive(:available_in_zone?).and_return(true)
@vm = @vms.first
@vm.storage = nil
@vm.save
@vm.raw_scan
end
it "should expect queue_signal and dispatch without errors" do
expect(dispatcher).to receive(:queue_signal)
expect { dispatcher.dispatch }.not_to raise_error
end
end
context "with a Microsoft vm without a storage" do
before(:each) do
# Test a Microsoft vm without a storage
allow(MiqVimBrokerWorker).to receive(:available_in_zone?).and_return(true)
@vm = @vms.first
@vm.storage = nil
@vm.vendor = "microsoft"
@vm.save
@vm.raw_scan
end
it "should run dispatch without calling queue_signal" do
expect(dispatcher).not_to receive(:queue_signal)
end
end
context "with a Microsoft vm with a Microsoft storage" do
before(:each) do
# Test a Microsoft vm without a storage
allow(MiqVimBrokerWorker).to receive(:available_in_zone?).and_return(true)
@vm = @vms.first
@vm.storage.store_type = "CSVFS"
@vm.vendor = "microsoft"
@vm.save
@vm.raw_scan
end
it "should run dispatch without calling queue_signal" do
expect(dispatcher).not_to receive(:queue_signal)
end
end
context "with a Microsoft vm with an invalid storage" do
before(:each) do
# Test a Microsoft vm without a storage
allow(MiqVimBrokerWorker).to receive(:available_in_zone?).and_return(true)
@vm = @vms.first
@vm.storage.store_type = "XFS"
@vm.vendor = "microsoft"
@vm.save
@vm.raw_scan
end
it "should expect queue_signal and dispatch without errors" do
expect(dispatcher).to receive(:queue_signal)
expect { dispatcher.dispatch }.not_to raise_error
end
end
end
context "with jobs, a default smartproxy for repo scanning" do
before(:each) do
allow(MiqVimBrokerWorker).to receive(:available?).and_return(true)
# JobProxyDispatcher.stub(:start_job_on_proxy).and_return(nil)
# MiqProxy.any_instance.stub(:concurrent_job_max).and_return(1)
@repo_proxy = @proxies.last
if @repo_proxy
@repo_proxy.name = "repo_proxy"
@repo_proxy.save
@repo_proxy.host.name = "repo_host"
@repo_proxy.host.save
stub_settings(:repository_scanning => {:defaultsmartproxy => @repo_proxy.id})
end
@jobs = (@vms + @repo_vms).collect(&:raw_scan)
end
# Don't run these tests if we only want to run dispatch for load testing
unless DISPATCH_ONLY
if @repo_proxy
it "should have repository host set" do
expect(@repo_vms.first.myhost.id).to eq(@repo_proxy.host_id)
end
end
it "should have #{NUM_VMS + NUM_REPO_VMS} jobs" do
total = NUM_VMS + NUM_REPO_VMS
expect(@jobs.length).to eq(total)
end
end
it "should run dispatch" do
expect { JobProxyDispatcher.dispatch }.not_to raise_error
end
it "dispatch should handle a job with a deleted target VM" do
@job = Job.first
@job.target_id = 999999
@job.save!
expect { JobProxyDispatcher.dispatch }.not_to raise_error
@job.reload
expect(@job.state).to eq("finished")
expect(@job.status).to eq("warn")
end
end
end
context "with container and vms jobs" do
let (:container_image_classes) { ContainerImage.descendants.collect(&:name).append('ContainerImage') }
before(:each) do
@jobs = (@vms + @repo_vms).collect(&:raw_scan)
User.current_user = FactoryGirl.create(:user)
@jobs += @container_images.map { |img| img.ext_management_system.raw_scan_job_create(img.class, img.id) }
end
describe "#pending_jobs" do
it "returns only vm jobs by default" do
jobs = dispatcher.pending_jobs
expect(jobs.count).to eq(@vms.count + @repo_vms.count)
jobs.each do |x|
expect(x.target_class).to eq 'VmOrTemplate'
end
expect(jobs.count).to be > 0 # in case something unexpected goes wrong
end
it "returns only container images jobs when requested" do
jobs = dispatcher.pending_jobs(dispatcher.container_image_scan_class)
expect(jobs.count).to eq(@container_images.count)
jobs.each do |x|
expect(container_image_classes).to include x.target_class
end
expect(jobs.count).to be > 0 # in case something unexpected goes wrong
end
end
describe "#pending_container_jobs" do
it "returns container jobs by provider" do
jobs_by_ems, = dispatcher.pending_container_jobs
expect(jobs_by_ems.keys).to match_array(@container_providers.map(&:id))
expect(jobs_by_ems[@container_providers.first.id].count).to eq(1 * container_image_classes.count)
expect(jobs_by_ems[@container_providers.second.id].count).to eq(2 * container_image_classes.count)
end
end
describe "#active_container_scans_by_zone_and_ems" do
it "returns active container acans for zone" do
job = @jobs.find { |j| container_image_classes.include?(j.target_class) }
job.update(:dispatch_status => "active")
provider = ExtManagementSystem.find(job.options[:ems_id])
dispatcher.instance_variable_set(:@zone, MiqServer.my_zone) # memoized during pending_jobs call
expect(dispatcher.active_container_scans_by_zone_and_ems).to eq(
job.zone => {provider.id => 1}
)
end
end
describe "#dispatch_container_scan_jobs" do
it "dispatches jobs until reaching limit" do
stub_settings(:container_scanning => {:concurrent_per_ems => 1})
dispatcher.dispatch_container_scan_jobs
expect(Job.where(:target_class => container_image_classes, :dispatch_status => "pending").count).to eq(
(3 * container_image_classes.count) - 2)
# 1 per ems, one ems has 1* job and the other 2*
# initial number of images per ems is multiplied by container_image_classes.count
end
it "does not dispach if limit is already reached" do
stub_settings(:container_scanning => {:concurrent_per_ems => 1})
dispatcher.dispatch_container_scan_jobs
expect(Job.where(:target_class => container_image_classes, :dispatch_status => "pending").count).to eq(
(3 * container_image_classes.count) - 2)
dispatcher.dispatch_container_scan_jobs
expect(Job.where(:target_class => container_image_classes, :dispatch_status => "pending").count).to eq(
(3 * container_image_classes.count) - 2)
end
it "does not apply limit when concurrent_per_ems is 0" do
stub_settings(:container_scanning => {:concurrent_per_ems => 0})
dispatcher.dispatch_container_scan_jobs
expect(Job.where(:target_class => container_image_classes, :dispatch_status => "pending").count).to eq(0)
# 1 per ems, one ems has 1* job and the other 2*
# initial number of images per ems is multiplied by container_image_classes.count
end
it "does not apply limit when concurrent_per_ems is -1" do
stub_settings(:container_scanning => {:concurrent_per_ems => -1})
dispatcher.dispatch_container_scan_jobs
expect(Job.where(:target_class => container_image_classes, :dispatch_status => "pending").count).to eq(0)
# 1 per ems, one ems has 1* job and the other 2*
# initial number of images per ems is multiplied by container_image_classes.count
end
end
end
describe "#active_vm_scans_by_zone" do
it "returns active vm scans for this zone" do
job = @vms.first.raw_scan
dispatcher.instance_variable_set(:@zone, MiqServer.my_zone) # memoized during pending_jobs call
job.update(:dispatch_status => "active")
expect(dispatcher.active_vm_scans_by_zone[job.zone]).to eq(1)
end
it "returns 0 for active vm scan for other zones" do
job = @vms.first.raw_scan
dispatcher.instance_variable_set(:@zone, MiqServer.my_zone) # memoized during pending_jobs call
job.update(:dispatch_status => "active")
expect(dispatcher.active_vm_scans_by_zone['defult']).to eq(0)
end
end
end
context "limiting number of smart state analysis running on one server" do
let(:job) { Job.create_job("VmScan", :miq_server_id => @server.id, :name => "Hello - 1") }
before(:each) do
Job.create_job("VmScan", :miq_server_id => @server.id, :name => "Hello - 2")
.update_attributes(:dispatch_status => "active")
Job.create_job("VmScan", :miq_server_id => @server.id, :name => "Hello - 3")
.update_attributes(:dispatch_status => "active")
end
describe "#busy_proxies" do
it "it returns hash with number of not finished jobs with dispatch status 'active' for each MiqServer" do
expect(dispatcher.busy_proxies).to eq "MiqServer_#{@server.id}" => 2
end
end
describe "#assign_proxy_to_job" do
it "increses by 1 number of jobs (how busy server is) for server" do
expect(dispatcher.busy_proxies).to eq "MiqServer_#{@server.id}" => 2
allow(dispatcher).to receive(:embedded_scan_resource).and_return(nil)
dispatcher.assign_proxy_to_job(@server, job)
expect(dispatcher.busy_proxies).to eq "MiqServer_#{@server.id}" => 3
end
it "links job to instance of MiqServer and updates :started_on and :dispatch_status atributes" do
allow(dispatcher).to receive(:embedded_scan_resource).and_return(nil)
Timecop.freeze do
timestamp = Time.now.utc
dispatcher.assign_proxy_to_job(@server, job)
expect(job.started_on).to eq timestamp
end
expect(job.miq_server_id).to eq @server.id
expect(job.dispatch_status).to eq "active"
end
end
end
describe "#start_job_on_proxy" do
it "creates job options and passing it to `queue_signal'" do
job = Job.create_job("VmScan", :miq_server_id => @server.id, :name => "Hello, World")
dispatcher.instance_variable_set(:@active_vm_scans_by_zone, @server.my_zone => 0)
job_options = {:args => ["start"], :zone => @server.my_zone, :server_guid => @server.guid, :role => "smartproxy"}
expect(dispatcher).to receive(:assign_proxy_to_job)
expect(dispatcher).to receive(:queue_signal).with(job, job_options)
dispatcher.start_job_on_proxy(job, @server)
end
end
describe "#do_dispatch" do
let(:ems_id) { 1 }
let(:job) { Job.create_job("VmScan", :name => "Hello, World") }
before(:each) do
dispatcher.instance_variable_set(:@zone, @server.my_zone)
dispatcher.instance_variable_set(:@active_container_scans_by_zone_and_ems, @server.my_zone => {ems_id => 0})
end
it "updates 'dispatch_status' attribute of job record to 'active'" do
expect(job.dispatch_status).not_to eq "active"
dispatcher.do_dispatch(job, ems_id)
expect(job.dispatch_status).to eq "active"
end
it "updates ':started_on' attribute of job record" do
expect(job.started_on).to be nil
Timecop.freeze do
timestamp = Time.now.utc
dispatcher.do_dispatch(job, ems_id)
expect(job.started_on).to eq timestamp
end
end
it "increases counter of active container scans by zone and ems by 1" do
counter_by_zone_ems = dispatcher.instance_variable_get(:@active_container_scans_by_zone_and_ems)
expect(counter_by_zone_ems[@server.my_zone][ems_id]).to eq 0
dispatcher.do_dispatch(job, ems_id)
counter_by_zone_ems = dispatcher.instance_variable_get(:@active_container_scans_by_zone_and_ems)
expect(counter_by_zone_ems[@server.my_zone][ems_id]).to eq 1
end
it "queues call to Job::StateMachine#signal with argument 'start'" do
expect(MiqQueue.count).to eq 0
dispatcher.do_dispatch(job, ems_id)
queue_record = MiqQueue.where(:instance_id => job.id)[0]
expect(queue_record.method_name).to eq "signal"
expect(queue_record.class_name).to eq "Job"
end
end
describe "#queue_signal" do
let(:job) { Job.create_job("VmScan", :name => "Hello, World") }
it "queues call to Job::StateMachine#signal_abort if signal is 'abort'" do
options = {:args => [:abort]}
dispatcher.queue_signal(job, options)
queue_record = MiqQueue.where(:instance_id => job.id)[0]
expect(queue_record.method_name).to eq "signal_abort"
expect(queue_record.class_name).to eq "Job"
end
it "queues call to Job::StateMachine#signal if signal is not 'abort'" do
options = {:args => [:start_snapshot]}
dispatcher.queue_signal(job, options)
queue_record = MiqQueue.where(:instance_id => job.id)[0]
expect(queue_record.method_name).to eq "signal"
expect(queue_record.class_name).to eq "Job"
expect(queue_record.args[0]).to eq :start_snapshot
end
end
describe "#dispatch_to_ems" do
let(:ems_id) { 1 }
let(:jobs) do
[Job.create_job("VmScan", :name => "Hello, World 1"), Job.create_job("VmScan", :name => "Hello, World 2")]
end
it "dispatches all supplied jobs if supplied concurency limit is 0" do
dispatcher.dispatch_to_ems(ems_id, jobs, 0)
expect(MiqQueue.where(:class_name => "Job", :method_name => "signal").count).to eq 2
end
it "limits dispatching supplied jobs if supplied concurrency limit > 0" do
concurrency_limit = 1
dispatcher.dispatch_to_ems(ems_id, jobs, concurrency_limit)
expect(MiqQueue.where(:class_name => "Job", :method_name => "signal").count).to eq concurrency_limit
end
end
end