Skip to content

Commit

Permalink
Refactor infra vm check_remove_from_provider method and test.
Browse files Browse the repository at this point in the history
Refactored the check_remove_from_provider method for Infrastructure Vm retirement.
Updated tests.

This PR is based on the issue below.
ManageIQ/manageiq#12038

@miq-bot add_label refactoring
  • Loading branch information
billfitzgerald0120 committed May 11, 2017
1 parent 5330cb9 commit f3d305d
Show file tree
Hide file tree
Showing 2 changed files with 92 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,44 @@
# Description: This method checks to see if the VM has been removed from the provider
#

vm = $evm.root['vm']
module ManageIQ
module Automate
module Infrastructure
module VM
module Retirement
module StateMachines
class CheckRemovedFromProvider
def initialize(handle = $evm)
@handle = handle
end

$evm.root['ae_result'] = 'ok'
def main
check_removed_from_provider(vm)
end

if vm && $evm.get_state_var('vm_removed_from_provider')
if vm.ext_management_system
vm.refresh
$evm.root['ae_result'] = 'retry'
$evm.root['ae_retry_interval'] = '60.seconds'
private

def vm
raise "ERROR - vm object not passed in" unless @handle.root['vm']
@handle.root['vm']
end

def check_removed_from_provider(vm)
@handle.root['ae_result'] = 'ok'
if vm.ext_management_system && @handle.get_state_var('vm_removed_from_provider')
vm.refresh
@handle.root['ae_result'] = 'retry'
@handle.root['ae_retry_interval'] = '60.seconds'
end
end
end
end
end
end
end
end
end

if __FILE__ == $PROGRAM_NAME
ManageIQ::Automate::Infrastructure::VM::Retirement::StateMachines::CheckRemovedFromProvider.new.main
end
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
require_domain_file

describe ManageIQ::Automate::Infrastructure::VM::Retirement::StateMachines::CheckRemovedFromProvider do
let(:user) { FactoryGirl.create(:user_with_group) }
let(:zone) { FactoryGirl.create(:zone) }
let(:ems) { FactoryGirl.create(:ems_vmware, :zone => zone, :last_refresh_date => Time.now.getlocal - 100) }
let(:host) { FactoryGirl.create(:host) }
let(:vm) do
FactoryGirl.create(:vm_vmware,
:raw_power_state => "poweredOff",
:registered => false,
:ems_id => ems.id)
end

# let(:ae_state) {'vm_removed_from_provider' => true}

let(:svc_model_vmware_ems) do
MiqAeMethodService::MiqAeServiceExtManagementSystem.find(ems.id)
end

let(:svc_model_vm) do
MiqAeMethodService::MiqAeServiceVm.find(vm.id)
end

let(:root_hash) do
{'vm' => svc_model_vm }
end

let(:root_object) do
Spec::Support::MiqAeMockObject.new(root_hash)
end

let(:ae_service) do
Spec::Support::MiqAeMockService.new(root_object).tap do |service|
current_object = Spec::Support::MiqAeMockObject.new
current_object.parent = root_object
service.object = current_object
end
end

it "returns 'ok' if the vm is not connected to a ems" do
described_class.new(ae_service).main
expect(ae_service.root['vm'].registered).to eq(false)
expect(ae_service.root['ae_result']).to eq('ok')
end

it "returns 'retry' if the vm is still connected to ems" do
ae_service.set_state_var('vm_removed_from_provider', true)
vm.update_attributes(:host => host, :registered => true)
allow_any_instance_of(Vm).to receive(:refresh_ems)
described_class.new(ae_service).main
vm.reload
expect(ae_service.root['ae_result']).to eq('retry')
end
end

0 comments on commit f3d305d

Please sign in to comment.