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

Refactoring cloud vm retirement openstack_pre_retirement method #286

Merged
merged 2 commits into from
May 16, 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
@@ -1,10 +1,35 @@
#
# Description: This method suspends the Openstack Instance
#
module ManageIQ
module Automate
module Cloud
module VM
module Retirement
module StateMachines
module Methods
class OpenstackPreRetirement
def initialize(handle = $evm)
@handle = handle
end

vm = $evm.root['vm']
unless vm.nil? || vm.attributes['power_state'] == 'off'
ems = vm.ext_management_system
$evm.log('info', "Suspending Openstack Instance <#{vm.name}> in EMS <#{ems.try(:name)}")
vm.suspend if ems
def main
vm = @handle.root['vm']
unless vm.nil? || vm.attributes['power_state'] == 'off'
ems = vm.ext_management_system
@handle.log('info', "Suspending Openstack Instance <#{vm.name}> in EMS <#{ems.try(:name)}")
vm.suspend if ems
end
end
end
end
end
end
end
end
end
end

if $PROGRAM_NAME == __FILE__
ManageIQ::Automate::Cloud::VM::Retirement::StateMachines::Methods::OpenstackPreRetirement.new.main
end

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
require_domain_file

describe ManageIQ::Automate::Cloud::VM::Retirement::StateMachines::Methods::OpenstackPreRetirement do
let(:svc_vm) { MiqAeMethodService::MiqAeServiceVm.find(vm.id) }
let(:ems) { FactoryGirl.create(:ems_vmware) }
let(:vm) { FactoryGirl.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 'call suspend for running instances' do
expect(svc_vm).to receive(:suspend)
described_class.new(ae_service).main
end

context "Doesn't call suspend for " do
shared_examples_for 'Not calling suspend' do
it 'call check' do
expect(svc_vm).not_to receive(:suspend)
described_class.new(ae_service).main
end
end

context 'poweredOff vm' do
let(:vm) { FactoryGirl.create(:vm_vmware, :ems_id => ems.id, :raw_power_state => 'poweredOff') }
it_behaves_like 'Not calling suspend'
end

context 'nil vm' do
let(:root_hash) { {} }
it_behaves_like 'Not calling suspend'
end

context 'nil ems' do
let(:vm) { FactoryGirl.create(:vm_vmware) }
it_behaves_like 'Not calling suspend'
end
end
end