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

Implemented max memory #206

Merged
1 commit merged into from
May 24, 2022
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
29 changes: 29 additions & 0 deletions vm.go
Original file line number Diff line number Diff line change
Expand Up @@ -1119,6 +1119,7 @@ func (v vmPlacementPolicyParameters) MustWithHostIDs(hostIDs []HostID) Buildable
// MemoryPolicyParameters contain the parameters for the memory policy setting on the VM.
type MemoryPolicyParameters interface {
Guaranteed() *int64
Max() *int64
}

// BuildableMemoryPolicyParameters is a buildable version of MemoryPolicyParameters.
Expand All @@ -1127,6 +1128,9 @@ type BuildableMemoryPolicyParameters interface {

WithGuaranteed(guaranteed int64) (BuildableMemoryPolicyParameters, error)
MustWithGuaranteed(guaranteed int64) BuildableMemoryPolicyParameters

WithMax(max int64) (BuildableMemoryPolicyParameters, error)
MustWithMax(max int64) BuildableMemoryPolicyParameters
}

// NewMemoryPolicyParameters creates a new instance of BuildableMemoryPolicyParameters.
Expand All @@ -1136,6 +1140,7 @@ func NewMemoryPolicyParameters() BuildableMemoryPolicyParameters {

type memoryPolicyParameters struct {
guaranteed *int64
max *int64
}

func (m *memoryPolicyParameters) MustWithGuaranteed(guaranteed int64) BuildableMemoryPolicyParameters {
Expand All @@ -1155,14 +1160,38 @@ func (m *memoryPolicyParameters) WithGuaranteed(guaranteed int64) (BuildableMemo
return m, nil
}

func (m *memoryPolicyParameters) MustWithMax(max int64) BuildableMemoryPolicyParameters {
builder, err := m.WithMax(max)
if err != nil {
panic(err)
}
return builder
}

func (m *memoryPolicyParameters) Max() *int64 {
return m.max
}

func (m *memoryPolicyParameters) WithMax(max int64) (BuildableMemoryPolicyParameters, error) {
m.max = &max
return m, nil
}

// MemoryPolicy is the memory policy set on the VM.
type MemoryPolicy interface {
// Guaranteed returns the number of guaranteed bytes to the VM.
Guaranteed() *int64
// Max returns the maximum amount of memory given to the VM.
Max() *int64
}

type memoryPolicy struct {
guaranteed *int64
max *int64
}

func (m memoryPolicy) Max() *int64 {
return m.max
}

func (m memoryPolicy) Guaranteed() *int64 {
Expand Down
8 changes: 8 additions & 0 deletions vm_create.go
Original file line number Diff line number Diff line change
Expand Up @@ -223,6 +223,9 @@ func vmBuilderMemoryPolicy(params OptionalVMParameters, builder *ovirtsdk.VmBuil
if guaranteed := (*memPolicyParams).Guaranteed(); guaranteed != nil {
memoryPolicyBuilder.Guaranteed(*guaranteed)
}
if max := (*memPolicyParams).Max(); max != nil {
memoryPolicyBuilder.Max(*max)
}
builder.MemoryPolicyBuilder(memoryPolicyBuilder)
}
}
Expand Down Expand Up @@ -415,8 +418,13 @@ func (m *mockClient) createVMMemoryPolicy(params OptionalVMParameters) *memoryPo
if guaranteedMemory := (*memoryPolicyParams).Guaranteed(); guaranteedMemory != nil {
guaranteed = guaranteedMemory
}
var max *int64
if maxMemory := (*memoryPolicyParams).Max(); maxMemory != nil {
max = maxMemory
}
memPolicy = &memoryPolicy{
guaranteed,
max,
}
}
return memPolicy
Expand Down
28 changes: 28 additions & 0 deletions vm_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -499,6 +499,34 @@ func TestGuaranteedMemory(t *testing.T) {
}
}

func TestMaxMemory(t *testing.T) {
helper := getHelper(t)
expectedMax := int64(2 * 1024 * 1024 * 1024)
vm := assertCanCreateVM(
t,
helper,
fmt.Sprintf("%s-%s", t.Name(), helper.GenerateRandomID(5)),
ovirtclient.
NewCreateVMParams().
WithMemoryPolicy(
ovirtclient.
NewMemoryPolicyParameters().
MustWithMax(expectedMax),
).MustWithMemory(expectedMax),
)
memoryPolicy, ok := vm.MemoryPolicy()
if !ok {
t.Fatalf("Memory policy is not set on VM.")
}
max := memoryPolicy.Max()
if max == nil {
t.Fatalf("Guaranteed memory is not set on VM.")
}
if *max != expectedMax {
t.Fatalf("Incorrect max memory value (expected: %d, got: %d)", expectedMax, *max)
}
}

func checkVMDiskSparseness(t *testing.T, checkVM ovirtclient.VM, sparse bool, message string) {
t.Helper()
diskAttachments, err := checkVM.ListDiskAttachments()
Expand Down