forked from ManageIQ/manageiq-content
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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
- Loading branch information
1 parent
e1690ea
commit b3f636a
Showing
2 changed files
with
89 additions
and
22 deletions.
There are no files selected for viewing
69 changes: 47 additions & 22 deletions
69
...e/ManageIQ/Service/Retirement/StateMachines/Methods.class/__methods__/start_retirement.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |
42 changes: 42 additions & 0 deletions
42
...ageIQ/Service/Retirement/StateMachines/Methods.class/__methods__/start_retirement_spec.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |