Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Validate CPU and Memory Hot-Plug settings in reconfigure #12275

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,31 @@ def max_memory_mb
end
end

def validate_config_spec(options)
# Check hot-plug settings if the VM is on
if power_state == "on"
if options[:number_of_cpus]
number_of_cpus = options[:number_of_cpus].to_i
cores_per_socket = options[:cores_per_socket].to_i

raise MiqException::MiqVmError, "CPU Hot-Add not enabled" if number_of_cpus > cpu_total_cores && !cpu_hot_add_enabled
raise MiqException::MiqVmError, "Cannot remove CPUs from a running VM" if number_of_cpus < cpu_total_cores && !cpu_hot_remove_enabled
raise MiqException::MiqVmError, "Cannot change CPU cores per socket on a running VM" if cores_per_socket != cpu_cores_per_socket
end

if options[:vm_memory]
vm_memory = options[:vm_memory].to_i

raise MiqException::MiqVmError, "Memory Hot-Add not enabled" if vm_memory > ram_size && !memory_hot_add_enabled
raise MiqException::MiqVmError, "Cannot add more than #{memory_hot_add_limit}MB to this VM" if vm_memory > ram_size && vm_memory > memory_hot_add_limit
raise MiqException::MiqVmError, "Cannot remove memory from a running VM" if vm_memory < ram_size
end
end
end

def build_config_spec(options)
validate_config_spec(options)

VimHash.new("VirtualMachineConfigSpec") do |vmcs|
case hardware.virtual_hw_version
when "07"
Expand Down
6 changes: 6 additions & 0 deletions spec/factories/hardware.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,12 @@
cpu_total_cores 2
end

trait(:cpu4x2) do
cpu_sockets 4
cpu_cores_per_socket 2
cpu_total_cores 8
end

trait(:ram1GB) { memory_mb 1024 }
end
end
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@
let(:vm) do
FactoryGirl.create(
:vm_vmware,
:name => 'test_vm',
:storage => FactoryGirl.create(:storage, :name => 'storage'),
:hardware => FactoryGirl.create(:hardware, :virtual_hw_version => "07")
:name => 'test_vm',
:raw_power_state => 'poweredOff',
:storage => FactoryGirl.create(:storage, :name => 'storage'),
:hardware => FactoryGirl.create(:hardware, :cpu4x2, :ram1GB, :virtual_hw_version => "07")
)
end

Expand Down Expand Up @@ -82,6 +83,77 @@
expect(subject["numCoresPerSocket"]).to eq(2)
end
end

context "Running VM" do
before do
vm.update_attributes(:raw_power_state => 'poweredOn')
end

context "with CPU Hot-Add disabled" do
it "raises an exception when adding CPUs" do
options[:number_of_cpus] = '16'
expect { subject }.to raise_error(MiqException::MiqVmError, "CPU Hot-Add not enabled")
end
end

context "with CPU Hot-Add enabled" do
before do
vm.update_attributes(:cpu_hot_add_enabled => true,
:cpu_hot_remove_enabled => false)
end

it "raises an exception when removing CPUs" do
options[:number_of_cpus] = '2'

expect { subject }.to raise_error(MiqException::MiqVmError, "Cannot remove CPUs from a running VM")
end

it "raises an exception when changing numCoresPerSocket" do
options[:cores_per_socket] = 4

expect { subject }.to raise_error(MiqException::MiqVmError, "Cannot change CPU cores per socket on a running VM")
end

it "sets numCPUs correctly" do
options[:number_of_cpus] = '16'

expect(subject["numCPUs"]).to eq(16)
end
end

context "with Memory Hot-Add disabled" do
it "raises an exception when adding RAM" do
options[:vm_memory] = '2048'

expect { subject }.to raise_error(MiqException::MiqVmError, "Memory Hot-Add not enabled")
end
end

context "with Memory Hot-Add enabled" do
before do
vm.update_attributes(:memory_hot_add_enabled => true,
:memory_hot_add_limit => 2048)
end

it "raises an exception when removing memory" do
options[:vm_memory] = '512'

expect { subject }.to raise_error(MiqException::MiqVmError, "Cannot remove memory from a running VM")
end

it "raises an exception if adding more than the memory limit" do
options[:vm_memory] = '4096'

expect { subject }.to raise_error(MiqException::MiqVmError, "Cannot add more than 2048MB to this VM")
end

it "sets memoryMB correctly" do
options[:vm_memory] = '1536'

expect(subject["memoryMB"]).to eq(1536)
end
end
end
end

context "#add_disk_config_spec" do
Expand Down