From a3002d23bc1365d079c6d09c808836f73e645f6c Mon Sep 17 00:00:00 2001 From: Michael Kuzmin Date: Tue, 16 Oct 2018 18:59:51 +0300 Subject: [PATCH] Fix default CPU limit (#163) --- driver/vm.go | 4 +++- iso/builder_acc_test.go | 34 ++++++++++++++++++++++++++++++++++ 2 files changed, 37 insertions(+), 1 deletion(-) diff --git a/driver/vm.go b/driver/vm.go index e896a65cc..5e253311f 100644 --- a/driver/vm.go +++ b/driver/vm.go @@ -255,7 +255,9 @@ func (vm *VirtualMachine) Configure(config *HardwareConfig) error { var cpuSpec types.ResourceAllocationInfo cpuSpec.Reservation = &config.CPUReservation - cpuSpec.Limit = &config.CPULimit + if config.CPULimit != 0 { + cpuSpec.Limit = &config.CPULimit + } confSpec.CpuAllocation = &cpuSpec var ramSpec types.ResourceAllocationInfo diff --git a/iso/builder_acc_test.go b/iso/builder_acc_test.go index 288780a09..5f4c6de89 100644 --- a/iso/builder_acc_test.go +++ b/iso/builder_acc_test.go @@ -159,6 +159,40 @@ func checkHardware(t *testing.T) builderT.TestCheckFunc { } } +func TestISOBuilderAcc_limit(t *testing.T) { + builderT.Test(t, builderT.TestCase{ + Builder: &Builder{}, + Template: limitConfig(), + Check: checkLimit(t), + }) +} + +func limitConfig() string { + config := defaultConfig() + config["CPUs"] = 1 // hardware is customized, but CPU limit is not specified explicitly + + return commonT.RenderConfig(config) +} + +func checkLimit(t *testing.T) builderT.TestCheckFunc { + return func(artifacts []packer.Artifact) error { + d := commonT.TestConn(t) + + vm := commonT.GetVM(t, d, artifacts) + vmInfo, err := vm.Info("config.cpuAllocation") + if err != nil { + t.Fatalf("Cannot read VM properties: %v", err) + } + + limit := *vmInfo.Config.CpuAllocation.Limit + if limit != -1 { // must be unlimited + t.Errorf("Invalid CPU limit: expected '%v', got '%v'", -1, limit) + } + + return nil + } +} + func TestISOBuilderAcc_cdrom(t *testing.T) { builderT.Test(t, builderT.TestCase{ Builder: &Builder{},