forked from ManageIQ/manageiq
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlocal_db_finders_spec.rb
453 lines (395 loc) · 18.6 KB
/
local_db_finders_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
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
require_relative '../helpers/spec_mocked_data'
require_relative '../helpers/spec_parsed_data'
require_relative 'test_persister'
require_relative 'targeted_refresh_spec_helper'
describe ManagerRefresh::Inventory::Persister do
include SpecMockedData
include SpecParsedData
include TargetedRefreshSpecHelper
######################################################################################################################
# Spec scenarios for making sure the local db index is able to build complex queries using references
######################################################################################################################
#
before :each do
@zone = FactoryGirl.create(:zone)
@ems = FactoryGirl.create(:ems_cloud,
:zone => @zone,
:network_manager => FactoryGirl.create(:ems_network, :zone => @zone))
allow(@ems.class).to receive(:ems_type).and_return(:mock)
allow(Settings.ems_refresh).to receive(:mock).and_return({})
end
before :each do
initialize_mocked_records
end
let(:persister) { create_persister }
context "check we can load network records from the DB" do
it "finds in one batch after the scanning" do
lazy_find_vm1 = persister.vms.lazy_find(:ems_ref => vm_data(1)[:ems_ref])
lazy_find_vm2 = persister.vms.lazy_find(:ems_ref => vm_data(2)[:ems_ref])
lazy_find_vm60 = persister.vms.lazy_find(:ems_ref => vm_data(60)[:ems_ref])
lazy_find_hardware1 = persister.hardwares.lazy_find(:vm_or_template => lazy_find_vm1)
lazy_find_hardware2 = persister.hardwares.lazy_find(:vm_or_template => lazy_find_vm2)
lazy_find_hardware60 = persister.hardwares.lazy_find(:vm_or_template => lazy_find_vm60)
lazy_find_network1 = persister.networks.lazy_find(
{:hardware => lazy_find_hardware1, :description => "public"},
{:key => :hostname,
:default => 'default_value_unknown'}
)
lazy_find_network2 = persister.networks.lazy_find(
{:hardware => lazy_find_hardware2, :description => "public"},
{:key => :hostname,
:default => 'default_value_unknown'}
)
lazy_find_network60 = persister.networks.lazy_find(
{:hardware => lazy_find_hardware60, :description => "public"},
{:key => :hostname,
:default => 'default_value_unknown'}
)
@vm_data101 = vm_data(101).merge(
:flavor => persister.flavors.lazy_find(:ems_ref => flavor_data(1)[:name]),
:genealogy_parent => persister.miq_templates.lazy_find(:ems_ref => image_data(1)[:ems_ref]),
:key_pairs => [persister.key_pairs.lazy_find(:name => key_pair_data(1)[:name])],
:location => lazy_find_network1,
)
@vm_data102 = vm_data(102).merge(
:flavor => persister.flavors.lazy_find(:ems_ref => flavor_data(1)[:name]),
:genealogy_parent => persister.miq_templates.lazy_find(:ems_ref => image_data(1)[:ems_ref]),
:key_pairs => [persister.key_pairs.lazy_find(:name => key_pair_data(1)[:name])],
:location => lazy_find_network2,
)
@vm_data160 = vm_data(160).merge(
:flavor => persister.flavors.lazy_find(:ems_ref => flavor_data(1)[:name]),
:genealogy_parent => persister.miq_templates.lazy_find(:ems_ref => image_data(1)[:ems_ref]),
:key_pairs => [persister.key_pairs.lazy_find(:name => key_pair_data(1)[:name])],
:location => lazy_find_network60,
)
persister.vms.build(@vm_data101)
persister.vms.build(@vm_data102)
persister.vms.build(@vm_data160)
ManagerRefresh::InventoryCollection::Scanner.scan!(persister.inventory_collections)
# Assert the local db index is empty if we do not load the reference
expect(persister.networks.index_proxy.send(:local_db_indexes)[:manager_ref].send(:index)).to be_nil
lazy_find_network1.load
# Assert all references are loaded at once
expect(persister.networks.index_proxy.send(:local_db_indexes)[:manager_ref].send(:index).keys).to(
match_array(
[
"vm_ems_ref_1__public",
"vm_ems_ref_2__public"
]
)
)
expect(lazy_find_network1.load).to eq "host_10_10_10_1.com"
expect(lazy_find_network2.load).to eq "host_10_10_10_2.com"
expect(lazy_find_network60.load).to eq "default_value_unknown"
end
it "finds one by one before we scan" do
lazy_find_vm1 = persister.vms.lazy_find(:ems_ref => vm_data(1)[:ems_ref])
lazy_find_vm2 = persister.vms.lazy_find(:ems_ref => vm_data(2)[:ems_ref])
lazy_find_vm60 = persister.vms.lazy_find(:ems_ref => vm_data(60)[:ems_ref])
lazy_find_hardware1 = persister.hardwares.lazy_find(:vm_or_template => lazy_find_vm1)
lazy_find_hardware2 = persister.hardwares.lazy_find(:vm_or_template => lazy_find_vm2)
lazy_find_hardware60 = persister.hardwares.lazy_find(:vm_or_template => lazy_find_vm60)
# Assert the local db index is empty if we do not load the reference
expect(persister.networks.index_proxy.send(:local_db_indexes)[:manager_ref].send(:index)).to be_nil
network1 = persister.networks.lazy_find(:hardware => lazy_find_hardware1, :description => "public").load
# Assert all references are one by one
expect(persister.networks.index_proxy.send(:local_db_indexes)[:manager_ref].send(:index).keys).to(
match_array(
[
"vm_ems_ref_1__public",
]
)
)
network2 = persister.networks.find(:hardware => lazy_find_hardware2, :description => "public")
expect(persister.networks.index_proxy.send(:local_db_indexes)[:manager_ref].send(:index).keys).to(
match_array(
[
"vm_ems_ref_1__public",
"vm_ems_ref_2__public"
]
)
)
network60 = persister.networks.find(:hardware => lazy_find_hardware60, :description => "public")
expect(persister.networks.index_proxy.send(:local_db_indexes)[:manager_ref].send(:index).keys).to(
match_array(
[
"vm_ems_ref_1__public",
"vm_ems_ref_2__public"
]
)
)
# TODO(lsmola) known weakness, manager_uuid is wrong, but index is correct. So this doesn't affect a functionality
# now, but it can be confusing
expect(network1.manager_uuid).to eq "__public"
expect(network2.manager_uuid).to eq "__public"
expect(network60).to be_nil
end
end
context "check we can load stack resource records from the DB" do
it "finds in one batch after the scanning" do
lazy_find_stack_1_11 = persister.orchestration_stacks.lazy_find(
:ems_ref => orchestration_stack_data("1_11")[:ems_ref]
)
lazy_find_stack_1_12 = persister.orchestration_stacks.lazy_find(
:ems_ref => orchestration_stack_data("1_12")[:ems_ref]
)
# Assert the local db index is empty if we do not load the reference
expect(persister.orchestration_stacks_resources.index_proxy.send(:local_db_indexes)[:by_stack_and_ems_ref].send(:index)).to be_nil
stack_resource_1_11_1 = persister.orchestration_stacks_resources.lazy_find(
{
:stack => lazy_find_stack_1_11,
:ems_ref => orchestration_stack_resource_data("1_11_1")[:ems_ref]
},
{:ref => :by_stack_and_ems_ref}
)
stack_resource_1_11_2 = persister.orchestration_stacks_resources.lazy_find(
{
:stack => lazy_find_stack_1_11,
:ems_ref => orchestration_stack_resource_data("1_11_2")[:ems_ref]
},
{:ref => :by_stack_and_ems_ref}
)
stack_resource_1_11_3 = persister.orchestration_stacks_resources.lazy_find(
{
:stack => lazy_find_stack_1_11,
:ems_ref => orchestration_stack_resource_data("1_11_3")[:ems_ref]
},
{:ref => :by_stack_and_ems_ref}
)
stack_1_12 = persister.orchestration_stacks_resources.lazy_find(
{
:stack => lazy_find_stack_1_12,
:ems_ref => orchestration_stack_resource_data("1_12_1")[:ems_ref]
},
{:ref => :by_stack_and_ems_ref,
:key => :stack},
)
@network_port1 = network_port_data(1).merge(
:device => stack_resource_1_11_1
)
@network_port2 = network_port_data(2).merge(
:device => stack_resource_1_11_2
)
@network_port3 = network_port_data(3).merge(
:device => stack_resource_1_11_3
)
@network_port4 = network_port_data(4).merge(
:device => stack_1_12
)
persister.network_ports.build(@network_port1)
persister.network_ports.build(@network_port2)
persister.network_ports.build(@network_port3)
persister.network_ports.build(@network_port4)
# Save the collections, which invokes scanner
persister.persist!
# Loading 1 should load all scanned
stack_resource_1_11_3.load
expect(persister.orchestration_stacks_resources.index_proxy.send(:local_db_indexes)[:by_stack_and_ems_ref].send(:index).keys).to(
match_array(
[
"stack_ems_ref_1_11__stack_resource_physical_resource_1_11_1",
"stack_ems_ref_1_11__stack_resource_physical_resource_1_11_2",
"stack_ems_ref_1_12__stack_resource_physical_resource_1_12_1",
]
)
)
# Getting already loaded resource is taking it from cache
persister.orchestration_stacks_resources.lazy_find(
{
:stack => lazy_find_stack_1_11,
:ems_ref => orchestration_stack_resource_data("1_11_3")[:ems_ref]
},
{:ref => :by_stack_and_ems_ref}
).load
expect(persister.orchestration_stacks_resources.index_proxy.send(:local_db_indexes)[:by_stack_and_ems_ref].send(:index).keys).to(
match_array(
[
"stack_ems_ref_1_11__stack_resource_physical_resource_1_11_1",
"stack_ems_ref_1_11__stack_resource_physical_resource_1_11_2",
"stack_ems_ref_1_12__stack_resource_physical_resource_1_12_1"
]
)
)
expect(NetworkPort.find_by(:ems_ref => network_port_data(1)[:ems_ref]).device.ems_ref).to eq "stack_resource_physical_resource_1_11_1"
expect(NetworkPort.find_by(:ems_ref => network_port_data(2)[:ems_ref]).device.ems_ref).to eq "stack_resource_physical_resource_1_11_2"
expect(NetworkPort.find_by(:ems_ref => network_port_data(3)[:ems_ref]).device).to be_nil
expect(NetworkPort.find_by(:ems_ref => network_port_data(4)[:ems_ref]).device.ems_ref).to eq "stack_ems_ref_1_12"
end
it "finds one by one before we scan" do
lazy_find_stack_1_11 = persister.orchestration_stacks.lazy_find(
:ems_ref => orchestration_stack_data("1_11")[:ems_ref]
)
lazy_find_stack_1_12 = persister.orchestration_stacks.lazy_find(
:ems_ref => orchestration_stack_data("1_12")[:ems_ref]
)
# Assert the local db index is empty if we do not load the reference
expect(persister.orchestration_stacks_resources.index_proxy.send(:local_db_indexes)[:by_stack_and_ems_ref].send(:index)).to be_nil
stack_resource_1_11_1 = persister.orchestration_stacks_resources.find(
{
:stack => lazy_find_stack_1_11,
:ems_ref => orchestration_stack_resource_data("1_11_1")[:ems_ref]
},
{:ref => :by_stack_and_ems_ref}
)
# Assert all references are one by one
expect(persister.orchestration_stacks_resources.index_proxy.send(:local_db_indexes)[:by_stack_and_ems_ref].send(:index).keys).to(
match_array(
[
"stack_ems_ref_1_11__stack_resource_physical_resource_1_11_1",
]
)
)
stack_resource_1_11_2 = persister.orchestration_stacks_resources.lazy_find(
{
:stack => lazy_find_stack_1_11,
:ems_ref => orchestration_stack_resource_data("1_11_2")[:ems_ref]
},
{:ref => :by_stack_and_ems_ref}
).load
expect(persister.orchestration_stacks_resources.index_proxy.send(:local_db_indexes)[:by_stack_and_ems_ref].send(:index).keys).to(
match_array(
[
"stack_ems_ref_1_11__stack_resource_physical_resource_1_11_1",
"stack_ems_ref_1_11__stack_resource_physical_resource_1_11_2"
]
)
)
stack_resource_1_11_3 = persister.orchestration_stacks_resources.lazy_find(
{
:stack => lazy_find_stack_1_11,
:ems_ref => orchestration_stack_resource_data("1_11_3")[:ems_ref]
},
{:ref => :by_stack_and_ems_ref}
).load
expect(persister.orchestration_stacks_resources.index_proxy.send(:local_db_indexes)[:by_stack_and_ems_ref].send(:index).keys).to(
match_array(
[
"stack_ems_ref_1_11__stack_resource_physical_resource_1_11_1",
"stack_ems_ref_1_11__stack_resource_physical_resource_1_11_2"
]
)
)
stack_1_12 = persister.orchestration_stacks_resources.lazy_find(
{
:stack => lazy_find_stack_1_12,
:ems_ref => orchestration_stack_resource_data("1_12_1")[:ems_ref]
},
{:ref => :by_stack_and_ems_ref,
:key => :stack},
).load
expect(persister.orchestration_stacks_resources.index_proxy.send(:local_db_indexes)[:by_stack_and_ems_ref].send(:index).keys).to(
match_array(
[
"stack_ems_ref_1_11__stack_resource_physical_resource_1_11_1",
"stack_ems_ref_1_11__stack_resource_physical_resource_1_11_2",
"stack_ems_ref_1_12__stack_resource_physical_resource_1_12_1",
]
)
)
expect(stack_resource_1_11_1.manager_uuid).to eq "stack_resource_physical_resource_1_11_1"
expect(stack_resource_1_11_2.manager_uuid).to eq "stack_resource_physical_resource_1_11_2"
expect(stack_resource_1_11_3).to be_nil
# TODO(lsmola) should we preload the relation even before the scanner found it it's referenced?
# :key pointing to relation is not loaded when scanner is not invoked
expect(stack_1_12).to be_nil
end
end
context "check secondary indexes on Vms" do
it "finds Vm by name" do
vm1 = persister.vms.lazy_find({:name => vm_data(1)[:name]}, {:ref => :by_name}).load
expect(vm1[:ems_ref]).to eq "vm_ems_ref_1"
expect(persister.vms.index_proxy.send(:local_db_indexes)[:by_name].send(:index).keys).to(
match_array(
[
"vm_name_1",
]
)
)
vm1 = persister.vms.find({:name => vm_data(1)[:name]}, {:ref => :by_name})
expect(vm1[:ems_ref]).to eq "vm_ems_ref_1"
expect(persister.vms.index_proxy.send(:local_db_indexes)[:by_name].send(:index).keys).to(
match_array(
[
"vm_name_1",
]
)
)
end
it "finds Vm by uid_ems and name" do
vm1 = persister.vms.lazy_find({:name => vm_data(1)[:name], :uid_ems => vm_data(1)[:uid_ems]}, {:ref => :by_uid_ems_and_name}).load
expect(vm1[:ems_ref]).to eq "vm_ems_ref_1"
expect(persister.vms.index_proxy.send(:local_db_indexes)[:by_uid_ems_and_name].send(:index).keys).to(
match_array(
[
"vm_uid_ems_1__vm_name_1",
]
)
)
vm1 = persister.vms.find({:uid_ems => vm_data(1)[:uid_ems], :name => vm_data(1)[:name]}, {:ref => :by_uid_ems_and_name})
expect(vm1[:ems_ref]).to eq "vm_ems_ref_1"
expect(persister.vms.index_proxy.send(:local_db_indexes)[:by_uid_ems_and_name].send(:index).keys).to(
match_array(
[
"vm_uid_ems_1__vm_name_1",
]
)
)
end
end
context "check secondary index with polymorphic relation inside" do
it "will fail trying to build query using polymorphic column as index" do
lazy_find_vm1 = persister.vms.lazy_find(:ems_ref => vm_data(1)[:ems_ref])
# TODO(lsmola) Will we need to search by polymorphic columns? We do not do that now in any refresh. By design,
# polymoprhic columns can't do join (they can, only for 1 table). Maybe union of 1 table joins using polymorphic
# relations?
# TODO(lsmola) We should probably assert this sooner? Now we are getting a failure trying to add :device in
# .includes
expect { persister.network_ports.lazy_find({:device => lazy_find_vm1}, {:ref => :by_device}).load }.to(
raise_error(ActiveRecord::EagerLoadPolymorphicError,
"Cannot eagerly load the polymorphic association :device")
)
end
end
context "check validity of defined index" do
it "checks primary index attributes exist" do
expect do
persister.send(:add_inventory_collection,
:model_class => ::Vm,
:association => :vms,
:manager_ref => %i(ems_ref ems_gref))
end.to raise_error("Invalid definition of index :manager_ref, there is no attribute :ems_gref on model Vm")
end
it "checks secondary index attributes exist" do
expect do
persister.send(:add_inventory_collection,
:model_class => ::Vm,
:association => :vms,
:secondary_refs => {:by_uid_ems_and_name => %i(uid_emsa name)})
end.to raise_error("Invalid definition of index :by_uid_ems_and_name, there is no attribute :uid_emsa on model Vm")
end
it "checks relation is allowed in index" do
persister.send(:add_inventory_collection,
:model_class => ::ManageIQ::Providers::CloudManager::Vm,
:association => :vms,
:secondary_refs => {:by_availability_zone_and_name => %i(availability_zone name)})
expect(persister.vms.index_proxy.send(:data_indexes).keys).to match_array(%i(manager_ref by_availability_zone_and_name))
end
it "checks relation is on model class" do
expect do
persister.send(:add_inventory_collection,
:model_class => ::Vm,
:association => :vms,
:secondary_refs => {:by_availability_zone_and_name => %i(availability_zone name)})
end.to raise_error("Invalid definition of index :by_availability_zone_and_name, there is no attribute :availability_zone on model Vm")
end
it "checks we allow any index attributes when we use custom_saving block" do
persister.send(:add_inventory_collection,
:model_class => ::Vm,
:association => :vms,
:custom_save_block => ->(ems, _ic) { ems },
:manager_ref => %i(a b c))
expect(persister.vms.index_proxy.send(:data_indexes).keys).to match_array([:manager_ref])
end
end
end