diff --git a/content/automate/ManageIQ/Cloud/VM/Retirement/StateMachines/Methods.class/__methods__/pre_retirement.rb b/content/automate/ManageIQ/Cloud/VM/Retirement/StateMachines/Methods.class/__methods__/pre_retirement.rb index 4c3aa8776..67ffabba8 100644 --- a/content/automate/ManageIQ/Cloud/VM/Retirement/StateMachines/Methods.class/__methods__/pre_retirement.rb +++ b/content/automate/ManageIQ/Cloud/VM/Retirement/StateMachines/Methods.class/__methods__/pre_retirement.rb @@ -1,10 +1,37 @@ # # Description: This method stops a cloud Instance # +module ManageIQ + module Automate + module Cloud + module VM + module Retirement + module StateMachines + module Methods + class PreRetirement + def initialize(handle = $evm) + @handle = handle + end -vm = $evm.root['vm'] -if vm && vm.power_state == 'on' - ems = vm.ext_management_system - $evm.log('info', "Stopping Instance <#{vm.name}> in EMS <#{ems.try(:name)}>") - vm.stop if ems + def main + vm = @handle.root['vm'] + if vm && vm.power_state == 'on' + ems = vm.ext_management_system + if ems + @handle.log('info', "Stopping Instance <#{vm.name}> in EMS <#{ems.name}>") + vm.stop + end + end + end + end + end + end + end + end + end + end +end + +if $PROGRAM_NAME == __FILE__ + ManageIQ::Automate::Cloud::VM::Retirement::StateMachines::Methods::PreRetirement.new.main end diff --git a/spec/automation/unit/method_validation/pre_retirement_spec.rb b/spec/automation/unit/method_validation/pre_retirement_spec.rb deleted file mode 100644 index e142bd06e..000000000 --- a/spec/automation/unit/method_validation/pre_retirement_spec.rb +++ /dev/null @@ -1,30 +0,0 @@ -require File.expand_path(File.join(File.dirname(__FILE__), '..', '..', - '..', 'spec_helper')) -def run_automate_method - MiqAeEngine.instantiate("/Infrastructure/VM/Retirement/StateMachines/Methods/PreRetirement?" \ - "Vm::vm=#{@vm.id}#microsoft", @user) -end - -describe "pre_retirement Method Validation" do - before(:each) do - @zone = FactoryGirl.create(:zone) - @user = FactoryGirl.create(:user_with_group) - @ems = FactoryGirl.create(:ems_microsoft, :zone => @zone) - @host = FactoryGirl.create(:host) - @vm = FactoryGirl.create(:vm_microsoft, :host => @host, - :ems_id => @ems.id, :name => "testVM2", :raw_power_state => "poweredOn") - end - - it "powers off a vm in a 'powered on' state" do - run_automate_method - - expect(MiqQueue.exists?(:method_name => 'stop', :instance_id => @vm.id, :role => 'ems_operations')).to be_truthy - end - - it "does not queue any operation for a vm in 'powered_off' state" do - @vm.update_attribute(:raw_power_state, "PowerOff") - run_automate_method - - expect(MiqQueue.exists?(:method_name => 'stop', :instance_id => @vm.id, :role => 'ems_operations')).to be_falsey - end -end diff --git a/spec/content/automate/ManageIQ/Cloud/VM/Retirement/StateMachines/Methods.class/__methods__/pre_retirement_spec.rb b/spec/content/automate/ManageIQ/Cloud/VM/Retirement/StateMachines/Methods.class/__methods__/pre_retirement_spec.rb new file mode 100644 index 000000000..f36eba065 --- /dev/null +++ b/spec/content/automate/ManageIQ/Cloud/VM/Retirement/StateMachines/Methods.class/__methods__/pre_retirement_spec.rb @@ -0,0 +1,39 @@ +require_domain_file + +describe ManageIQ::Automate::Cloud::VM::Retirement::StateMachines::Methods::PreRetirement do + let(:svc_vm) { MiqAeMethodService::MiqAeServiceVm.find(vm.id) } + let(:ems) { FactoryGirl.create(:ems_microsoft) } + let(:root_object) { Spec::Support::MiqAeMockObject.new(root_hash) } + let(:root_hash) { { 'vm' => svc_vm } } + let(:vm) do + FactoryGirl.create(:vm_microsoft, :ems_id => ems.id) + 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 'powers off a vm in a \'powered_on\' state' do + expect(svc_vm).to receive(:stop) + described_class.new(ae_service).main + end + + it 'does not stop a vm in \'powered_off\' state' do + vm.update_attributes(:raw_power_state => "PowerOff") + expect(svc_vm).to_not receive(:stop) + described_class.new(ae_service).main + end + + context 'nil ems' do + let(:vm) { FactoryGirl.create(:vm_microsoft) } + + it 'does not stop a vm without ems' do + expect(svc_vm).to_not receive(:stop) + described_class.new(ae_service).main + end + end +end