From 7040201ebe5f1d39f01d9eec7a5647c09fb4a94f Mon Sep 17 00:00:00 2001 From: Joe Rafaniello Date: Mon, 7 May 2018 12:20:18 -0400 Subject: [PATCH] Fix current/active server deletability validation https://bugzilla.redhat.com/show_bug.cgi?id=1575077 Note, the UI will need to check server.errors after attempting to destroy the record to see any errors. --- app/models/miq_server.rb | 11 +++++++---- spec/models/miq_server_spec.rb | 17 +++++++++++++++++ 2 files changed, 24 insertions(+), 4 deletions(-) diff --git a/app/models/miq_server.rb b/app/models/miq_server.rb index d1bf3371889..69a3ded7604 100644 --- a/app/models/miq_server.rb +++ b/app/models/miq_server.rb @@ -249,8 +249,7 @@ def self.check_migrations_up_to_date def validate_is_deleteable unless self.is_deleteable? - _log.error(@error_message) - @error_message = nil + _log.error(@errors.full_messages) throw :abort end end @@ -492,13 +491,17 @@ def is_recently_active? def is_deleteable? if self.is_local? - @error_message = N_("Cannot delete currently used %{log_message}") % {:log_message => format_short_log_msg} + message = N_("Cannot delete currently used %{log_message}") % {:log_message => format_short_log_msg} + @errors ||= ActiveModel::Errors.new(self) + @errors.add(:base, message) return false end return true if self.stopped? if is_recently_active? - @error_message = N_("Cannot delete recently active %{log_message}") % {:log_message => format_short_log_msg} + message = N_("Cannot delete recently active %{log_message}") % {:log_message => format_short_log_msg} + @errors ||= ActiveModel::Errors.new(self) + @errors.add(:base, message) return false end diff --git a/spec/models/miq_server_spec.rb b/spec/models/miq_server_spec.rb index 326e1a1b2d6..046e75ee4db 100644 --- a/spec/models/miq_server_spec.rb +++ b/spec/models/miq_server_spec.rb @@ -156,6 +156,23 @@ end end + context "validate_is_deleteable before destroying" do + it "prevents deleting the current server" do + allow(@miq_server).to receive(:is_local?).and_return(true) + @miq_server.destroy + + expect(@miq_server.errors.full_messages.first).to match(/current/) + end + + it "prevents deleting recently active server" do + allow(@miq_server).to receive(:is_local?).and_return(false) + @miq_server.last_heartbeat = 2.minutes.ago.utc + @miq_server.destroy + + expect(@miq_server.errors.full_messages.first).to match(/recently/) + end + end + context "#ntp_reload_queue" do let(:queue_cond) { {:method_name => 'ntp_reload', :class_name => 'MiqServer', :instance_id => @miq_server.id, :server_guid => @miq_server.guid, :zone => @miq_server.zone.name} } let(:message) { MiqQueue.where(queue_cond).first }