Skip to content

Commit

Permalink
Add basic specs for persister serialization
Browse files Browse the repository at this point in the history
Add basic specs for persister serialization
  • Loading branch information
Ladas committed Apr 27, 2018
1 parent b4155b2 commit e0efeab
Show file tree
Hide file tree
Showing 4 changed files with 164 additions and 39 deletions.
71 changes: 36 additions & 35 deletions spec/models/manager_refresh/helpers/spec_parsed_data.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,37 +3,38 @@
module SpecParsedData
def vm_data(i, data = {})
{
:type => ManageIQ::Providers::CloudManager::Vm.name,
:ext_management_system => @ems,
:uid_ems => "vm_uid_ems_#{i}",
:ems_ref => "vm_ems_ref_#{i}",
:name => "vm_name_#{i}",
:vendor => "amazon",
:raw_power_state => "unknown",
:location => "vm_location_#{i}",
:type => ManageIQ::Providers::CloudManager::Vm.name,
:ems_id => @ems.id,
:uid_ems => "vm_uid_ems_#{i}",
:ems_ref => "vm_ems_ref_#{i}",
:name => "vm_name_#{i}",
:vendor => "amazon",
:raw_power_state => "unknown",
:location => "vm_location_#{i}",
}.merge(data)
end

def key_pair_data(i, data = {})
{
:type => ManageIQ::Providers::CloudManager::AuthKeyPair.name,
:resource => @ems,
:name => "key_pair_name_#{i}",
:type => ManageIQ::Providers::CloudManager::AuthKeyPair.name,
:resource_id => @ems.id,
:resource_type => "ExtManagementSystem",
:name => "key_pair_name_#{i}",
}.merge(data)
end

def image_data(i, data = {})
{
:type => ManageIQ::Providers::CloudManager::Template.name,
:ext_management_system => @ems,
:uid_ems => "image_uid_ems_#{i}",
:ems_ref => "image_ems_ref_#{i}",
:name => "image_name_#{i}",
:location => "image_location_#{i}",
:vendor => "amazon",
:raw_power_state => "never",
:template => true,
:publicly_available => false,
:type => ManageIQ::Providers::CloudManager::Template.name,
:ems_id => @ems.id,
:uid_ems => "image_uid_ems_#{i}",
:ems_ref => "image_ems_ref_#{i}",
:name => "image_name_#{i}",
:location => "image_location_#{i}",
:vendor => "amazon",
:raw_power_state => "never",
:template => true,
:publicly_available => false,
}.merge(data)
end

Expand Down Expand Up @@ -67,20 +68,20 @@ def public_network_data(i, data = {})

def flavor_data(i, data = {})
{
:name => "t#{i}.nano",
:ext_management_system => @ems,
:name => "t#{i}.nano",
:ems_id => @ems.id,
}.merge(data)
end

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}",
:status => "stack_status_#{i}",
:status_reason => "stack_status_reason_#{i}",
:ems_ref => "stack_ems_ref_#{i}",
:type => "ManageIQ::Providers::CloudManager::OrchestrationStack",
:ems_id => @ems.id,
:name => "stack_name_#{i}",
:description => "stack_description_#{i}",
:status => "stack_status_#{i}",
:status_reason => "stack_status_reason_#{i}",
}.merge(data)
end

Expand All @@ -95,11 +96,11 @@ def orchestration_stack_resource_data(i, data = {})

def network_port_data(i, data = {})
{
:name => "network_port_name_#{i}",
:ext_management_system => @ems.network_manager,
:ems_ref => "network_port_ems_ref_#{i}",
:status => "network_port_status#{i}",
:mac_address => "network_port_mac_#{i}",
:name => "network_port_name_#{i}",
:ems_id => @ems.network_manager.try(:id),
:ems_ref => "network_port_ems_ref_#{i}",
:status => "network_port_status#{i}",
:mac_address => "network_port_mac_#{i}",
}.merge(data)
end

Expand Down
124 changes: 124 additions & 0 deletions spec/models/manager_refresh/persister/serializing_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@
require_relative '../helpers/spec_parsed_data'
require_relative 'test_persister'
require_relative 'targeted_refresh_spec_helper'

describe ManagerRefresh::Inventory::Persister do
include SpecParsedData
include TargetedRefreshSpecHelper

######################################################################################################################
# Spec scenarios testing Perister can serialize/deserialize, with having complex nested lazy_find links
######################################################################################################################
#
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

let(:persister) { create_persister }

it "tests we can serialize inventory object with nested lazy references" do
persister = create_persister
populate_test_data(persister)

ManagerRefresh::Inventory::Persister.from_json(persister.to_json).persist!

counts = {
:disk => 2,
:flavor => 0,
:hardware => 3,
:miq_template => 1,
:network => 2,
:vm => 2,
:vm_or_template => 3
}

assert_counts(counts)

vm = Vm.find_by(:ems_ref => "vm_ems_ref_1")
expect(vm.location).to eq("host_10_10_10_1.com")
expect(vm.hardware.guest_os).to eq("linux_generic_1")
expect(vm.hardware.guest_os_full_name).to eq("amazon")
expect(vm.hardware.networks.first.ipaddress).to eq("10.10.10.1")
expect(vm.hardware.disks.first.device_name).to eq("disk_name_1")

vm2 = Vm.find_by(:ems_ref => "vm_ems_ref_2")
expect(vm2.hardware.networks.first.ipaddress).to eq("10.10.10.2")
expect(vm2.hardware.disks.first.device_name).to eq("disk_name_2")

# TODO(lsmola) interestingly persister.hardwares.lazy_find({:vm_or_template => lazy_find_image_sec}) will not work
# because we build the reference only from the passed data. To make it work, we would have to do internally
# persister.hardwares.lazy_find({:vm_or_template => lazy_find_image_sec.load}), which will allow the nested lazy
# find to reach its :manager_ref data. That would mean the stringified reference would have to be build when
# we try to evaluate the lazy object. We should be able to do that.
expect(vm.hardware.model).to eq(nil)
expect(vm.hardware.manufacturer).to eq(nil)
end

def populate_test_data(persister)
@image_data_1 = image_data(1)
@image_hardware_data_1 = image_hardware_data(1).merge(
:guest_os => "linux_generic_1",
:vm_or_template => persister.miq_templates.lazy_find(:ems_ref => image_data(1)[:ems_ref]),
:model => "test1",
:manufacturer => "test2"
)

# Nested lazy find
lazy_find_image = persister.miq_templates.lazy_find(:ems_ref => image_data(1)[:ems_ref])
lazy_find_image_sec = persister.miq_templates.lazy_find({:name => image_data(1)[:name]}, {:ref => :by_name})
lazy_find_image_sec1 = persister.miq_templates.lazy_find(
{:name => image_data(1)[:name], :uid_ems => image_data(1)[:uid_ems]}, {:ref => :by_uid_ems_and_name}
)
lazy_find_image_sec2 = persister.miq_templates.lazy_find(
{:name => image_data(1)[:name], :uid_ems => image_data(1)[:uid_ems]}, {:ref => :by_uid_ems_and_name, :key => :vendor}
)
lazy_find_vm = persister.vms.lazy_find(:ems_ref => vm_data(1)[:ems_ref])
lazy_find_hardware = persister.hardwares.lazy_find(:vm_or_template => lazy_find_vm)

@vm_data_1 = vm_data(1).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 => persister.networks.lazy_find(
{:hardware => lazy_find_hardware, :description => "public"},
{:key => :hostname,
:default => 'default_value_unknown'}
),
)

@hardware_data_1 = hardware_data(1).merge(
:guest_os => persister.hardwares.lazy_find({:vm_or_template => lazy_find_image}, {:key => :guest_os}),
:model => persister.hardwares.lazy_find({:vm_or_template => lazy_find_image_sec}, {:key => :model}),
:manufacturer => persister.hardwares.lazy_find({:vm_or_template => lazy_find_image_sec1}, {:key => :manufacturer}),
:guest_os_full_name => lazy_find_image_sec2,
:vm_or_template => persister.vms.lazy_find(:ems_ref => vm_data(1)[:ems_ref])
)

@public_network_data_1 = public_network_data(1).merge(
:hardware => persister.hardwares.lazy_find(:vm_or_template => lazy_find_vm),
)

@disk_data_1 = disk_data(1).merge(
:hardware => persister.hardwares.lazy_find(:vm_or_template => lazy_find_vm),
)

persister.miq_templates.build(@image_data_1)
persister.hardwares.build(@image_hardware_data_1)
persister.vms.build(@vm_data_1)
persister.hardwares.build(@hardware_data_1)
persister.networks.build(@public_network_data_1)
persister.disks.build(@disk_data_1)

# Try also data with relations without lazy link
vm = persister.vms.build(vm_data(2))
hardware = persister.hardwares.build(hardware_data(2).merge(:vm_or_template => vm))
persister.networks.build(public_network_data(2).merge(:hardware => hardware))
persister.disks.build(disk_data(2).merge(:hardware => hardware))
end
end
4 changes: 2 additions & 2 deletions spec/models/manager_refresh/persister/test_persister.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,12 @@ def initialize_inventory_collections
# Top level models with direct references for Cloud
add_inventory_collections_with_references(
cloud,
%i(vms),
%i(vms miq_templates),
:secondary_refs => {:by_name => [:name], :by_uid_ems_and_name => %i(uid_ems name)}
)
add_inventory_collections_with_references(
cloud,
%i(miq_templates availability_zones orchestration_stacks)
%i(availability_zones orchestration_stacks)
)

add_inventory_collection_with_references(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -452,7 +452,7 @@
:parent => @ems,
:association => :vms,
# TODO(lsmola) vendor is not getting caught by fixed attributes
:attributes_whitelist => [:uid_ems, :vendor, :ext_management_system]
:attributes_whitelist => [:uid_ems, :vendor, :ext_management_system, :ems_id]
)

# Fill the InventoryCollections with data, that have a modified name, new VM and a missing VM
Expand Down Expand Up @@ -493,7 +493,7 @@
:parent => @ems,
:association => :vms,
# TODO(lsmola) vendor is not getting caught by fixed attributes
:attributes_whitelist => [:uid_ems, :raw_power_state, :vendor, :ext_management_system],
:attributes_whitelist => [:uid_ems, :raw_power_state, :vendor, :ems_id, :ext_management_system],
:attributes_blacklist => [:name, :ems_ref, :raw_power_state]
)

Expand Down

0 comments on commit e0efeab

Please sign in to comment.