Skip to content

Commit

Permalink
Merge pull request #897 from agrare/fix_vm_destroy_allowed_when_running
Browse files Browse the repository at this point in the history
Fix VM terminate showing supported when running

(cherry picked from commit 74fc1b0)
  • Loading branch information
Fryguy committed Jan 31, 2024
1 parent 6669115 commit 7d1f834
Show file tree
Hide file tree
Showing 4 changed files with 103 additions and 11 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
module ManageIQ::Providers::Vmware::InfraManager::Template::Operations
extend ActiveSupport::Concern

included do
supports :terminate do
if retired?
_('The VM is retired')
elsif terminated?
_('The VM is terminated')
elsif disconnected?
_('The VM does not have a valid connection state')
elsif !has_active_ems?
_("The VM is not connected to an active Provider")
end
end
end
end
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,14 @@ module ManageIQ::Providers::Vmware::InfraManager::Vm::Operations

include_concern 'Guest'
include_concern 'Snapshot'

included do
supports :terminate do
if !supports?(:control)
unsupported_reason(:control)
elsif power_state != "off"
_('The VM is not powered off')
end
end
end
end
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,6 @@ def raw_set_custom_field(attribute, value)
run_command_via_parent(:vm_set_custom_field, :attribute => attribute, :value => value)
end

included do
supports :terminate do
unsupported_reason_add(:terminate, unsupported_reason(:control)) unless supports?(:control)
end
end

def raw_clone(name, folder, pool = nil, host = nil, datastore = nil, powerOn = false, template_flag = false, transform = nil, config = nil, customization = nil, disk = nil)
folder_mor = folder.ems_ref_obj if folder.respond_to?(:ems_ref_obj)
pool_mor = pool.ems_ref_obj if pool.respond_to?(:ems_ref_obj)
Expand Down
81 changes: 76 additions & 5 deletions spec/models/manageiq/providers/vmware/infra_manager/vm_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@
let(:host) { FactoryBot.create(:host_vmware_esx, :ext_management_system => ems) }
let(:vm) { FactoryBot.create(:vm_vmware, :ext_management_system => ems, :host => host) }
let(:power_state_on) { "poweredOn" }
let(:power_state_suspended) { "poweredOff" }
let(:power_state_off) { "poweredOff" }
let(:power_state_suspended) { "suspended" }

context "#supports?" do
context("with :start") do
Expand Down Expand Up @@ -49,7 +50,25 @@
let(:op) { 'shutdown_guest' }

context "when powered off" do
let(:vm_status) { {:raw_power_state => power_state_suspended} }
let(:vm_status) { {:raw_power_state => power_state_off} }

it 'is not available if tools is not installed' do
vm_status[:tools_status] = 'toolsNotInstalled'
vm.update(vm_status)
expect(vm.supports?(op)).to be false
expect(vm.unsupported_reason(op)).to include("power")
end

it 'is not available even if tools is installed' do
vm_status[:tools_status] = nil
vm.update(vm_status)
expect(vm.supports?(op)).to be false
expect(vm.unsupported_reason(op)).to include("power")
end
end

context "when suspended" do
let(:vm_status) { {:raw_power_state => power_state_off} }

it 'is not available if tools is not installed' do
vm_status[:tools_status] = 'toolsNotInstalled'
Expand Down Expand Up @@ -87,6 +106,24 @@
let(:op) { 'reboot_guest' }

context "when powered off" do
let(:vm_status) { {:raw_power_state => power_state_off} }

it 'is not available if tools is not installed' do
vm_status[:tools_status] = 'toolsNotInstalled'
vm.update(vm_status)
expect(vm.supports?(op)).to be false
expect(vm.unsupported_reason(op)).to include("power")
end

it 'is not available even if tools is installed' do
vm_status[:tools_status] = nil
vm.update(vm_status)
expect(vm.supports?(op)).to be false
expect(vm.unsupported_reason(op)).to include("power")
end
end

context "when suspended" do
let(:vm_status) { {:raw_power_state => power_state_suspended} }

it 'is not available if tools is not installed' do
Expand Down Expand Up @@ -132,7 +169,17 @@
end

context "when powered off" do
let(:power_state) {{:raw_power_state => power_state_off}}

it "is not allowed" do
vm.update(power_state)
expect(vm.snapshotting_memory_allowed?).to be_falsy
end
end

context "when suspended" do
let(:power_state) {{:raw_power_state => power_state_suspended}}

it "is not allowed" do
vm.update(power_state)
expect(vm.snapshotting_memory_allowed?).to be_falsy
Expand All @@ -141,9 +188,33 @@
end

describe "#supports?(:terminate)" do
context "when connected to a provider" do
it "returns true" do
expect(vm.supports?(:terminate)).to be_truthy
context "when powered on" do
let(:power_state) { {:raw_power_state => power_state_on} }

it "returns false" do
vm.update(power_state)
expect(vm.supports?(:terminate)).to be_falsey
expect(vm.unsupported_reason(:terminate)).to eq("The VM is not powered off")
end
end

context "when suspended" do
let(:power_state) { {:raw_power_state => power_state_suspended} }

it "returns false" do
vm.update(power_state)
expect(vm.supports?(:terminate)).to be_falsey
expect(vm.unsupported_reason(:terminate)).to eq("The VM is not powered off")
end
end

context "when powered off" do
let(:power_state) { {:raw_power_state => power_state_off} }
context "when connected to a provider" do
it "returns true" do
vm.update(power_state)
expect(vm.supports?(:terminate)).to be_truthy
end
end
end

Expand Down

0 comments on commit 7d1f834

Please sign in to comment.