Skip to content

Commit

Permalink
Refactor start_retirement method for Infra VM Retirement.
Browse files Browse the repository at this point in the history
This PR is based on the issue below.
ManageIQ/manageiq#12038

@miq-bot add_label refactoring
@miq-bot assign @tinaafitz
Added test for retirement_start eq retiring as requested
  • Loading branch information
billfitzgerald0120 committed Nov 12, 2019
1 parent e1690ea commit 79d3e6b
Show file tree
Hide file tree
Showing 2 changed files with 90 additions and 20 deletions.
Original file line number Diff line number Diff line change
@@ -1,30 +1,62 @@
#
# Description: This method sets the retirement_state to retiring
#
module ManageIQ
module Automate
module Infrastructure
module VM
module Retirement
module StateMachines
module Methods
class StartRetirement
def initialize(handle = $evm)
@handle = handle
end

$evm.log("info", "Listing Root Object Attributes:")
$evm.root.attributes.sort.each { |k, v| $evm.log("info", "\t#{k}: #{v}") }
$evm.log("info", "===========================================")
def main
log_info
@vm = @handle.root['vm']
vm_validation
start_retirement
end

vm = $evm.root['vm']
if vm.nil?
$evm.log('error', "VM Object not found")
exit MIQ_ABORT
end
private

if vm.retired?
$evm.log('error', "VM is already retired. Aborting current State Machine.")
exit MIQ_ABORT
end
def log_info
@handle.log("info", "Listing Root Object Attributes:")
@handle.root.attributes.sort.each { |k, v| @handle.log("info", "\t#{k}: #{v}") }
@handle.log("info", "===========================================")
end

if vm.retiring?
$evm.log('error', "VM is in the process of being retired. Aborting current State Machine.")
exit MIQ_ABORT
end
def vm_validation
if @vm.nil?
raise 'VM Object not found'
end

if @vm.retired?
raise 'VM is already retired'
end

$evm.log('info', "VM before start_retirement: #{vm.inspect} ")
if @vm.retiring?
raise 'VM is already in the process of being retired'
end
end

$evm.create_notification(:type => :vm_retiring, :subject => vm)
vm.start_retirement
def start_retirement
@handle.log('info', "VM before start_retirement: #{@vm.inspect} ")
@handle.create_notification(:type => :vm_retiring, :subject => @vm)

@vm.start_retirement

@handle.log('info', "VM after start_retirement: #{@vm.inspect} ")
end
end
end
end
end
end
end
end
end

$evm.log('info', "VM after start_retirement: #{vm.inspect} ")
ManageIQ::Automate::Infrastructure::VM::Retirement::StateMachines::Methods::StartRetirement.new.main
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
require_domain_file

describe ManageIQ::Automate::Infrastructure::VM::Retirement::StateMachines::Methods::StartRetirement do
let(:svc_vm) { MiqAeMethodService::MiqAeServiceVm.find(vm.id) }
let(:ems) { FactoryBot.create(:ems_vmware) }
let(:vm) { FactoryBot.create(:vm_vmware, :ems_id => ems.id) }
let(:root_object) { Spec::Support::MiqAeMockObject.new(root_hash) }
let(:root_hash) { { 'vm' => svc_vm } }

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 "without vm" do
ae_service.root['vm'] = nil
expect { described_class.new(ae_service).main }.to raise_error('VM Object not found')
end

it "with retired vm" do
svc_vm.finish_retirement
expect { described_class.new(ae_service).main }.to raise_error('VM is already retired')
end

it "with retiring vm" do
svc_vm.start_retirement
expect { described_class.new(ae_service).main }.to raise_error('VM is already in the process of being retired')
end

it "starts retirement" do
expect(ae_service).to receive(:create_notification).with(:type => :vm_retiring, :subject => svc_vm)
described_class.new(ae_service).main
expect(svc_vm.retirement_state).to eq('retiring')
end
end

0 comments on commit 79d3e6b

Please sign in to comment.