Skip to content

Commit

Permalink
Test also a relation with redefined foreign_key
Browse files Browse the repository at this point in the history
Test also a relation with redefined foreign_key, the finder
query must build correctly.
  • Loading branch information
Ladas committed Feb 1, 2018
1 parent 625adec commit 19e7b46
Show file tree
Hide file tree
Showing 4 changed files with 250 additions and 3 deletions.
49 changes: 49 additions & 0 deletions spec/models/manager_refresh/helpers/spec_mocked_data.rb
Original file line number Diff line number Diff line change
Expand Up @@ -173,5 +173,54 @@ def initialize_mocked_records
:device => @vm4
)
)

@orchestration_stack_0_1 = FactoryGirl.create(
:orchestration_stack,
orchestration_stack_data("0_1").merge(
:parent => nil
)
)

@orchestration_stack_1_11 = FactoryGirl.create(
:orchestration_stack,
orchestration_stack_data("1_11").merge(
:parent => @orchestration_stack_0_1
)
)

@orchestration_stack_1_12 = FactoryGirl.create(
:orchestration_stack,
orchestration_stack_data("1_12").merge(
:parent => @orchestration_stack_0_1
)
)

@orchestration_stack_resource_1_11_1 = FactoryGirl.create(
:orchestration_stack_resource,
orchestration_stack_resource_data("1_11_1").merge(
:stack => @orchestration_stack_1_11
)
)

@orchestration_stack_resource_1_11_2 = FactoryGirl.create(
:orchestration_stack_resource,
orchestration_stack_resource_data("1_11_2").merge(
:stack => @orchestration_stack_1_11
)
)

@orchestration_stack_resource_1_12_1 = FactoryGirl.create(
:orchestration_stack_resource,
orchestration_stack_resource_data("1_12_1").merge(
:stack => @orchestration_stack_1_12
)
)

@orchestration_stack_resource_1_12_2 = FactoryGirl.create(
:orchestration_stack_resource,
orchestration_stack_resource_data("1_12_2").merge(
:stack => @orchestration_stack_1_12
)
)
end
end
1 change: 1 addition & 0 deletions spec/models/manager_refresh/helpers/spec_parsed_data.rb
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ def flavor_data(i, data = {})
def orchestration_stack_data(i, data = {})
{
:ems_ref => "stack_ems_ref_#{i}",
:type => "ManageIQ::Providers::CloudManager::OrchestrationStack",
:ext_management_system => @ems,
:name => "stack_name_#{i}",
:description => "stack_description_#{i}",
Expand Down
193 changes: 192 additions & 1 deletion spec/models/manager_refresh/persister/local_db_finders_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -138,14 +138,205 @@
)
)

# TODO(lsmola) known weakens, manager_uuid is wrong, but index is correct. So this doesn't affect a functionality
# 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
Expand Down
10 changes: 8 additions & 2 deletions spec/models/manager_refresh/persister/test_persister.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,13 @@ def initialize_inventory_collections
# Child models with references in the Parent InventoryCollections for Cloud
add_inventory_collections(
cloud,
%i(hardwares networks disks vm_and_template_labels orchestration_stacks_resources orchestration_stacks_outputs
%i(orchestration_stacks_resources),
:secondary_refs => {:by_stack_and_ems_ref => %i(stack ems_ref)}
)

add_inventory_collections(
cloud,
%i(hardwares networks disks vm_and_template_labels orchestration_stacks_outputs
orchestration_stacks_parameters)
)

Expand All @@ -40,7 +46,7 @@ def initialize_inventory_collections
:network_ports,
references(:vms) + references(:network_ports) + references(:load_balancers),
:parent => manager.network_manager,
:secondary_refs => {:by_device => [:device], :by_device_and_name => [:device, :name]}
:secondary_refs => {:by_device => [:device], :by_device_and_name => %i(device name)}
)

add_inventory_collection_with_references(
Expand Down

0 comments on commit 19e7b46

Please sign in to comment.