From b3f636a2eaa57c0d44194efb3b36b1a9cf9fc8fa Mon Sep 17 00:00:00 2001 From: William Fitzgerald Date: Tue, 5 Nov 2019 12:11:42 -0500 Subject: [PATCH] Refactor start_retirement method for Service Retirement. This PR is based on the issue below. ManageIQ/manageiq#12038 @miq-bot add_label refactoring @miq-bot assign @tinaafitz Added test for service_retire_task = nil, was missing this in coverage Added test for retirement_state = 'retiring' as requested Modified method as requested Deleted comment and blank line Removed extra log messages because Raise doesn't need them Fixed rob error Fixed raise logic to 1 line --- .../__methods__/start_retirement.rb | 69 +++++++++++++------ .../__methods__/start_retirement_spec.rb | 42 +++++++++++ 2 files changed, 89 insertions(+), 22 deletions(-) create mode 100644 spec/content/automate/ManageIQ/Service/Retirement/StateMachines/Methods.class/__methods__/start_retirement_spec.rb diff --git a/content/automate/ManageIQ/Service/Retirement/StateMachines/Methods.class/__methods__/start_retirement.rb b/content/automate/ManageIQ/Service/Retirement/StateMachines/Methods.class/__methods__/start_retirement.rb index 33ad4ff32..e1dfe0c4f 100644 --- a/content/automate/ManageIQ/Service/Retirement/StateMachines/Methods.class/__methods__/start_retirement.rb +++ b/content/automate/ManageIQ/Service/Retirement/StateMachines/Methods.class/__methods__/start_retirement.rb @@ -1,32 +1,57 @@ # # Description: This method sets the retirement_state to retiring # +module ManageIQ + module Automate + module Service + module Retirement + module StateMachines + module Methods + class StartRetirement + def initialize(handle = $evm) + @handle = handle + end -service = $evm.root['service'] -if service.nil? - $evm.log('error', "Service Object not found") - exit MIQ_ABORT -end + def main + @handle.log('info', "Service before start_retirement: #{service.inspect} ") + start_retirement(service) + @handle.log('info', "Service after start_retirement: #{service.inspect} ") + end -$evm.log('info', "Service before start_retirement: #{service.inspect} ") + private -if service.retired? - $evm.log('error', "Service is already retired. Aborting current State Machine.") - exit MIQ_ABORT -end + def service + @handle.root["service"].tap do |service| + raise 'Service Object not found' if service.nil? + end + end -if service.retiring? - $evm.log('error', "Service is in the process of being retired. Aborting current State Machine.") - exit MIQ_ABORT -end + def start_retirement(service) + service_validation(service) + @handle.create_notification(:type => :service_retiring, :subject => service) -unless $evm.root['service_retire_task'] - $evm.log(:error, "Service retire task not found") - $evm.log(:error, "The old style retirement is incompatible with the new retirement state machine.") - exit(MIQ_ABORT) -end + service.start_retirement + end -$evm.create_notification(:type => :service_retiring, :subject => service) -service.start_retirement + def service_validation(service) + if service.retired? + raise 'Service is already retired' + end + + if service.retiring? + raise 'Service is already in the process of being retired' + end + + unless @handle.root['service_retire_task'] + raise 'Service retire task not found, The old style retirement is incompatible with the new retirement state machine.' + end + end + end + end + end + end + end + end +end -$evm.log('info', "Service after start_retirement: #{service.inspect} ") +ManageIQ::Automate::Service::Retirement::StateMachines::Methods::StartRetirement.new.main diff --git a/spec/content/automate/ManageIQ/Service/Retirement/StateMachines/Methods.class/__methods__/start_retirement_spec.rb b/spec/content/automate/ManageIQ/Service/Retirement/StateMachines/Methods.class/__methods__/start_retirement_spec.rb new file mode 100644 index 000000000..ab09fd391 --- /dev/null +++ b/spec/content/automate/ManageIQ/Service/Retirement/StateMachines/Methods.class/__methods__/start_retirement_spec.rb @@ -0,0 +1,42 @@ +require_domain_file + +describe ManageIQ::Automate::Service::Retirement::StateMachines::Methods::StartRetirement do + let(:admin) { FactoryBot.create(:user_admin) } + let(:request) { FactoryBot.create(:service_retire_request, :requester => admin) } + let(:service) { FactoryBot.create(:service) } + let(:task) { FactoryBot.create(:service_retire_task, :destination => service, :miq_request => request) } + let(:svc_task) { MiqAeMethodService::MiqAeServiceServiceRetireTask.find(task.id) } + let(:svc_service) { MiqAeMethodService::MiqAeServiceService.find(service.id) } + let(:root_object) do + Spec::Support::MiqAeMockObject.new('service' => svc_service, + 'service_retire_task' => svc_task, + 'service_action' => 'Retirement') + end + let(:ae_service) { Spec::Support::MiqAeMockService.new(root_object) } + + it "without service" do + ae_service.root['service'] = nil + expect { described_class.new(ae_service).main }.to raise_error('Service Object not found') + end + + it "without task" do + ae_service.root['service_retire_task'] = nil + expect { described_class.new(ae_service).main }.to raise_error('Service retire task not found, The old style retirement is incompatible with the new retirement state machine.') + end + + it "with retired service" do + svc_service.finish_retirement + expect { described_class.new(ae_service).main }.to raise_error('Service is already retired') + end + + it "with retiring service" do + svc_service.start_retirement + expect { described_class.new(ae_service).main }.to raise_error('Service is already in the process of being retired') + end + + it "starts retirement" do + expect(ae_service).to receive(:create_notification).with(:type => :service_retiring, :subject => svc_service) + described_class.new(ae_service).main + expect(svc_service.retirement_state).to eq('retiring') + end +end