From a384e2844bbed40f6e13d2c58315b40b18509858 Mon Sep 17 00:00:00 2001 From: d-m-u Date: Mon, 21 Jan 2019 10:22:47 -0500 Subject: [PATCH] Add mark_retired method to not reset retirement_requester after end of retirement --- app/models/mixins/retirement_mixin.rb | 7 ++++++- .../retirement_management_spec.rb | 12 +++++++++++- spec/models/service/retirement_management_spec.rb | 11 ++++++++++- spec/models/vm/retirement_management_spec.rb | 12 +++++++++++- 4 files changed, 38 insertions(+), 4 deletions(-) diff --git a/app/models/mixins/retirement_mixin.rb b/app/models/mixins/retirement_mixin.rb index 55fdb94e70a..f3df747e585 100644 --- a/app/models/mixins/retirement_mixin.rb +++ b/app/models/mixins/retirement_mixin.rb @@ -150,13 +150,18 @@ def finish_retirement raise _("%{name} already retired") % {:name => name} if retired? $log.info("Finishing Retirement for [#{name}]") requester = retirement_requester - update_attributes(:retires_on => Time.zone.now, :retired => true, :retirement_state => "retired") + mark_retired message = "#{self.class.base_model.name}: [#{name}], Retires On: [#{retires_on.strftime("%x %R %Z")}], has been retired" $log.info("Calling audit event for: #{message} ") raise_audit_event(retired_event_name, message, requester) $log.info("Called audit event for: #{message} ") end + def mark_retired + self[:retires_on] = Time.zone.now + update_attributes(:retired => true, :retirement_state => "retired") + end + def start_retirement return if retired? || retiring? $log.info("Starting Retirement for [#{name}]") diff --git a/spec/models/orchestration_stack/retirement_management_spec.rb b/spec/models/orchestration_stack/retirement_management_spec.rb index a42392c942d..f2a3cfbe78d 100644 --- a/spec/models/orchestration_stack/retirement_management_spec.rb +++ b/spec/models/orchestration_stack/retirement_management_spec.rb @@ -1,5 +1,6 @@ describe "Service Retirement Management" do let(:user) { FactoryGirl.create(:user) } + let(:orchestration_stack) { FactoryBot.create(:orchestration_stack) } before(:each) do @miq_server = EvmSpecHelper.local_miq_server @stack = FactoryGirl.create(:orchestration_stack) @@ -71,8 +72,17 @@ end it "#finish_retirement" do + message = "OrchestrationStack: [#{orchestration_stack.name}], Retires On: [#{Time.zone.now.strftime("%x %R %Z")}], has been retired" + expect(orchestration_stack).to receive(:raise_audit_event).with("orchestration_stack_retired", message, nil) + + orchestration_stack.finish_retirement + + expect(orchestration_stack.retirement_state).to eq("retired") + end + + it "#mark_retired" do expect(@stack.retirement_state).to be_nil - @stack.finish_retirement + @stack.mark_retired @stack.reload expect(@stack.retired).to be_truthy expect(@stack.retires_on).to be_between(Time.zone.now - 1.hour, Time.zone.now + 1.second) diff --git a/spec/models/service/retirement_management_spec.rb b/spec/models/service/retirement_management_spec.rb index a299d324c66..05114fa758b 100644 --- a/spec/models/service/retirement_management_spec.rb +++ b/spec/models/service/retirement_management_spec.rb @@ -143,8 +143,17 @@ end it "#finish_retirement" do - expect(@service.retirement_state).to be_nil + message = "Service: [#{@service.name}], Retires On: [#{Time.zone.now.strftime("%x %R %Z")}], has been retired" + expect(@service).to receive(:raise_audit_event).with("service_retired", message, nil) + @service.finish_retirement + + expect(@service.retirement_state).to eq("retired") + end + + it "#mark_retired" do + expect(@service.retirement_state).to be_nil + @service.mark_retired @service.reload expect(@service.retired).to be_truthy expect(@service.retires_on).to be_between(Time.zone.now - 1.hour, Time.zone.now + 1.second) diff --git a/spec/models/vm/retirement_management_spec.rb b/spec/models/vm/retirement_management_spec.rb index 390cbfbafab..5be605843a0 100644 --- a/spec/models/vm/retirement_management_spec.rb +++ b/spec/models/vm/retirement_management_spec.rb @@ -1,5 +1,6 @@ describe "VM Retirement Management" do let(:user) { FactoryGirl.create(:user) } + let(:vm) { FactoryGirl.create(:vm) } before(:each) do miq_server = EvmSpecHelper.local_miq_server @zone = miq_server.zone @@ -90,8 +91,17 @@ end it "#finish_retirement" do + message = "Vm: [#{vm.name}], Retires On: [#{Time.zone.now.strftime("%x %R %Z")}], has been retired" + expect(vm).to receive(:raise_audit_event).with("vm_retired", message, nil) + + vm.finish_retirement + + expect(vm.retirement_state).to eq("retired") + end + + it "#mark_retired" do expect(@vm.retirement_state).to be_nil - @vm.finish_retirement + @vm.mark_retired @vm.reload expect(@vm.retired).to eq(true)