Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add spec test for deleting a VM #255

Merged
merged 4 commits into from
May 10, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -65,8 +65,8 @@ def monitor_updates(vim, property_filter, version, persister, parser)
updated_objects.concat(process_update_set(property_filter, update_set))
end while update_set.truncated

updated_objects.each do |managed_object, props|
parser.parse(managed_object, props)
updated_objects.each do |managed_object, update_kind, props|
parser.parse(managed_object, update_kind, props)
end

save_inventory(persister)
Expand Down Expand Up @@ -151,7 +151,7 @@ def process_object_update(object_update)
process_object_update_leave(managed_object)
end

return managed_object, props
return managed_object, object_update.kind, props
end

def process_object_update_enter(obj, change_set, _missing_set = [])
Expand All @@ -163,7 +163,7 @@ def process_object_update_modify(obj, change_set, _missing_set = [])
end

def process_object_update_leave(obj)
inventory_cache.delete_object(obj.class.wsdl_name, obj._ref)
inventory_cache.delete(obj)
end

def save_inventory(persister)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,12 @@ def customization_specs(extra_attributes = {})

def vms_and_templates(extra_attributes = {})
attributes = {
:model_class => ::VmOrTemplate,
:association => :vms_and_templates,
:attributes_blacklist => %i(parent resource_pool),
:builder_params => {
:model_class => ::VmOrTemplate,
:association => :vms_and_templates,
:delete_method => :disconnect_inv,
:attributes_blacklist => %i(parent resource_pool),
:custom_reconnect_block => ManagerRefresh::InventoryCollectionDefault::INVENTORY_RECONNECT_BLOCK,
:builder_params => {
:ems_id => ->(persister) { persister.manager.id },
},
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,18 +14,18 @@ def initialize(cache, persister)
@persister = persister
end

def parse(object, props)
def parse(object, kind, props)
object_type = object.class.wsdl_name
parse_method = "parse_#{object_type.underscore}"

raise "Missing parser for #{object_type}" unless respond_to?(parse_method)

send(parse_method, object, props)
send(parse_method, object, kind, props)
end

def parse_compute_resource(object, props)
persister.ems_clusters.manager_uuids << object._ref
return if props.nil?
def parse_compute_resource(object, kind, props)
persister.ems_clusters.targeted_scope[object._ref] = nil
return if kind == "leave"

cluster_hash = {
:ems_ref => object._ref,
Expand All @@ -42,9 +42,9 @@ def parse_compute_resource(object, props)
end
alias parse_cluster_compute_resource parse_compute_resource

def parse_datacenter(object, props)
persister.ems_folders.manager_uuids << object._ref
return if props.nil?
def parse_datacenter(object, kind, props)
persister.ems_folders.targeted_scope[object._ref] = nil
return if kind == "leave"

dc_hash = {
:ems_ref => object._ref,
Expand All @@ -57,9 +57,9 @@ def parse_datacenter(object, props)
persister.ems_folders.build(dc_hash)
end

def parse_datastore(object, props)
persister.storages.manager_uuids << object._ref
return if props.nil?
def parse_datastore(object, kind, props)
persister.storages.targeted_scope[object._ref] = nil
return if kind == "leave"

storage_hash = {
:ems_ref => object._ref,
Expand All @@ -74,9 +74,9 @@ def parse_datastore(object, props)
parse_datastore_host_mount(storage, object._ref, props)
end

def parse_distributed_virtual_switch(object, props)
persister.switches.manager_uuids << object._ref
return if props.nil?
def parse_distributed_virtual_switch(object, kind, props)
persister.switches.targeted_scope[object._ref] = nil
return if kind == "leave"

type = ManageIQ::Providers::Vmware::InfraManager::DistributedVirtualSwitch.name

Expand All @@ -95,9 +95,9 @@ def parse_distributed_virtual_switch(object, props)
end
alias parse_vmware_distributed_virtual_switch parse_distributed_virtual_switch

def parse_folder(object, props)
persister.ems_folders.manager_uuids << object._ref
return if props.nil?
def parse_folder(object, kind, props)
persister.ems_folders.targeted_scope[object._ref] = nil
return if kind == "leave"

folder_hash = {
:ems_ref => object._ref,
Expand All @@ -110,9 +110,9 @@ def parse_folder(object, props)
persister.ems_folders.build(folder_hash)
end

def parse_host_system(object, props)
persister.hosts.manager_uuids << object._ref
return if props.nil?
def parse_host_system(object, kind, props)
persister.hosts.targeted_scope[object._ref] = nil
return if kind == "leave"

cluster = lazy_find_managed_object(props[:parent])
host_hash = {
Expand Down Expand Up @@ -142,12 +142,12 @@ def parse_host_system(object, props)
parse_host_system_switches(host, props)
end

def parse_network(object, props)
def parse_network(object, kind, props)
end

def parse_distributed_virtual_portgroup(object, props)
persister.lans.manager_uuids << object._ref
return if props.nil?
def parse_distributed_virtual_portgroup(object, kind, props)
persister.lans.targeted_scope[object._ref] = nil
return if kind == "leave"

name = props.fetch_path(:summary, :name) || props.fetch_path(:config, :name)
name = CGI.unescape(name) unless name.nil?
Expand Down Expand Up @@ -176,9 +176,9 @@ def parse_distributed_virtual_portgroup(object, props)
persister.lans.build(lan_hash)
end

def parse_resource_pool(object, props)
persister.resource_pools.manager_uuids << object._ref
return if props.nil?
def parse_resource_pool(object, kind, props)
persister.resource_pools.targeted_scope[object._ref] = nil
return if kind == "leave"

rp_hash = {
:ems_ref => object._ref,
Expand All @@ -195,9 +195,9 @@ def parse_resource_pool(object, props)
end
alias parse_virtual_app parse_resource_pool

def parse_storage_pod(object, props)
persister.ems_folders.manager_uuids << object._ref
return if props.nil?
def parse_storage_pod(object, kind, props)
persister.ems_folders.targeted_scope[object._ref] = nil
return if kind == "leave"

name = props.fetch_path(:summary, :name)

Expand All @@ -212,9 +212,9 @@ def parse_storage_pod(object, props)
persister.ems_folders.build(pod_hash)
end

def parse_virtual_machine(object, props)
persister.vms_and_templates.manager_uuids << object._ref
return if props.nil?
def parse_virtual_machine(object, kind, props)
persister.vms_and_templates.targeted_scope[object._ref] = nil
return if kind == "leave"

vm_hash = {
:ems_ref => object._ref,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,47 +39,54 @@
context "targeted refresh" do
let(:vim) { RbVmomi::VIM.new(:ns => "urn2", :rev => "6.5") }
let(:property_filter) { RbVmomi::VIM.PropertyFilter(vim, "session[6f2dcefd-41de-6dfb-0160-1ee1cc024553]") }
let(:cache) { collector.send(:inventory_cache) }
let(:persister) { ems.class::Inventory::Persister::Targeted.new(ems) }
let(:parser) { ems.class::Inventory::Parser.new(collector.send(:inventory_cache), persister) }
let(:parser) { ems.class::Inventory::Parser.new(cache, persister) }

before do
# Use the VCR to prime the cache and do the initial save_inventory
run_full_refresh
end

it "doesn't impact unassociated inventory" do
run_targeted_refresh(targeted_update_set(vm_power_on_object_update))
run_targeted_refresh(targeted_update_set([vm_power_on_object_update]))
assert_table_counts
end

it "power on a virtual machine" do
vm = ems.vms.find_by(:ems_ref => 'vm-107')

expect(vm.power_state).to eq("off")
run_targeted_refresh(targeted_update_set(vm_power_on_object_update))
run_targeted_refresh(targeted_update_set([vm_power_on_object_update]))
expect(vm.reload.power_state).to eq("on")
end

it "migrate a virtual machine" do
vm = ems.vms.find_by(:ems_ref => 'vm-107')

expect(vm.host.ems_ref).to eq("host-93")
run_targeted_refresh(targeted_update_set(vm_migrate_object_update))
run_targeted_refresh(targeted_update_set([vm_migrate_object_update]))
expect(vm.reload.host.ems_ref).to eq("host-94")
end

def run_targeted_refresh(update_set)
collector.send(:process_update_set, property_filter, update_set).each do |obj, props|
parser.parse(obj, props)
end
it "deleting a virtual machine" do
vm = ems.vms.find_by(:ems_ref => 'vm-107')

expect(vm.archived?).to be_falsy
run_targeted_refresh(targeted_update_set(vm_delete_object_updates))
expect(vm.reload.archived?).to be_truthy
end

def run_targeted_refresh(update_set)
update_set = collector.send(:process_update_set, property_filter, update_set)
update_set.each { |managed_object, kind, props| parser.parse(managed_object, kind, props) }
collector.send(:save_inventory, persister)
end

def targeted_update_set(object_update)
def targeted_update_set(object_updates)
property_filter_update = RbVmomi::VIM.PropertyFilterUpdate(
:filter => property_filter,
:objectSet => [object_update],
:objectSet => object_updates,
)

RbVmomi::VIM.UpdateSet(
Expand Down Expand Up @@ -116,6 +123,38 @@ def vm_migrate_object_update
:missingSet => [],
)
end

def vm_delete_object_updates
[
RbVmomi::VIM.ObjectUpdate(
:dynamicProperty => [],
:kind => "leave",
:obj => RbVmomi::VIM.VirtualMachine(vim, "vm-107"),
:changeSet => [],
:missingSet => [],
),
RbVmomi::VIM.ObjectUpdate(
:dynamicProperty => [],
:kind => "modify",
:obj => RbVmomi::VIM.ClusterComputeResource(vim, "domain-c91"),
:changeSet => [
RbVmomi::VIM.PropertyChange(
:dynamicProperty => [],
:name => "summary.effectiveCpu",
:op => "assign",
:val => 47_983,
),
RbVmomi::VIM.PropertyChange(
:dynamicProperty => [],
:name => "summary.effectiveMemory",
:op => "assign",
:val => 59_871,
),
],
:missingSet => [],
),
]
end
end

def run_full_refresh
Expand Down