Skip to content

Commit

Permalink
Move miq_preloader_spec tree building logic to shared_context
Browse files Browse the repository at this point in the history
This makes a new rspec shared_context that can be used to replicate the
tree building functionality in the specs that test the method
`MiqPreloader.polymorphic_preload_for_child_classes`.  The same data can
then be used in higher level abstraction tests to confirm the
functionality works as expected there as well.
  • Loading branch information
NickLaMuro committed May 22, 2018
1 parent 2f971d5 commit 4980f77
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 22 deletions.
27 changes: 5 additions & 22 deletions spec/lib/miq_preloader_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -114,29 +114,12 @@ def preload_and_scope(*args)
end

describe ".polymorphic_preload_for_child_classes" do
include_context "simple ems_metadata tree" do
before { init_full_tree }
end

it "preloads polymorphic relationships that are defined" do
ems = FactoryGirl.create(:ems_infra)
clusters = FactoryGirl.create_list(:ems_cluster, 2,
:ext_management_system => ems)
host_group1 = FactoryGirl.create_list(:host, 2,
:ext_management_system => ems,
:ems_cluster => clusters.first)
host_group2 = FactoryGirl.create_list(:host, 2,
:ext_management_system => ems,
:ems_cluster => clusters.last)

ems_rel = ems.init_relationship
cluster_rels = clusters.map { |cluster| cluster.init_relationship(ems_rel) }
host_rels1 = host_group1.map { |host| [host, host.init_relationship(cluster_rels.first)] }
host_rels2 = host_group2.map { |host| [host, host.init_relationship(cluster_rels.last)] }

(host_rels1 + host_rels2).each do |(host, host_rel)|
FactoryGirl.create_list(:vm, 2, :ext_management_system => ems, :host => host).each do |vm|
vm.init_relationship(host_rel)
end
end

tree = ExtManagementSystem.last.fulltree_rels_arranged(:except_type => "VmOrTemplate")
tree = ems.fulltree_rels_arranged(:except_type => "VmOrTemplate")
records = Relationship.flatten_arranged_rels(tree)

hosts_scope = Host.select(Host.arel_table[Arel.star], :v_total_vms)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
shared_context "simple ems_metadata tree" do
#####################
# Context Variables #
#####################

# These can be updated to increase the amount of data in the tree

let(:cluster_count) { 2 }
let(:hosts_per_cluster) { 2 }
let(:vms_per_host) { 2 }

###############
# Base models #
###############

let(:ems) { FactoryGirl.create(:ems_infra) }
let(:clusters) { FactoryGirl.create_list(:ems_cluster, cluster_count, :ext_management_system => ems) }

let(:hosts) do
hosts = []
clusters.each do |cluster|
hosts += FactoryGirl.create_list(:host, hosts_per_cluster,
:ext_management_system => ems,
:ems_cluster => cluster)
end
hosts
end

let(:vms) do
vms = []
hosts.each do |host|
vms += FactoryGirl.create_list(:vm, vms_per_host,
:ext_management_system => ems,
:host => host)
end
vms
end

#################
# Relationships #
#################

let(:ems_rel) { ems.init_relationship }
let(:cluster_rels) { clusters.map { |cluster| cluster.init_relationship(ems_rel) } }

# The next to use a integer division trick to map the proper parent_rel to
# the record being created (the `[index / child_per_parent]` part)

let(:host_rels) do
hosts.map.with_index do |host, index|
host.init_relationship(cluster_rels[index / hosts_per_cluster])
end
end

let(:vm_rels) do
vms.map.with_index do |vm, index|
vm.init_relationship(host_rels[index / vms_per_host])
end
end

###########
# Helpers #
###########

# Convenience statement for initializing the tree (there is no included
# `before` to do this automatically
let(:init_full_tree) { vm_rels }
end

0 comments on commit 4980f77

Please sign in to comment.