You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
virt/agent/hypervisor/libvirt_resources_test.go

151 lines
6.0 KiB
Go

//go:build libvirt
package hypervisor
import (
"encoding/json"
"testing"
"git.luoli.live/root/virt/agent/operation"
"libvirt.org/go/libvirtxml"
)
func TestResizeManagedDomainDocumentUpdatesStoppedResourcesAndMarker(t *testing.T) {
document, err := BuildDomainXML(validDomainDefinition())
if err != nil {
t.Fatal(err)
}
normalized, _, err := parseManagedDomain(document)
if err != nil {
t.Fatal(err)
}
normalized.VCPU.Current = normalized.VCPU.Value
normalized.CurrentMemory = &libvirtxml.DomainCurrentMemory{
Value: normalized.Memory.Value * 1024,
Unit: "KiB",
}
document, err = normalized.Marshal()
if err != nil {
t.Fatal(err)
}
target := resizeResourceValues{VCPUs: 4, MemoryMiB: 4096, DiskGiB: 80, BandwidthMbps: 250}
updated, err := resizeManagedDomainDocument(document, target, "operation-resize-resources")
if err != nil {
t.Fatal(err)
}
definition, metadata, err := parseManagedDomain(updated)
if err != nil {
t.Fatal(err)
}
if metadata.ResourcesTransitionID != "operation-resize-resources" ||
!managedDefinitionMatchesResources(definition, metadata, target.DiskGiB*gib, target) {
t.Fatalf("resized definition did not match target: metadata=%#v xml=%s", metadata, updated)
}
if definition.VCPU.Current != target.VCPUs || definition.CurrentMemory == nil ||
!memoryValueMatchesMiB(definition.CurrentMemory.Value, definition.CurrentMemory.Unit, target.MemoryMiB) {
t.Fatalf("normalized current CPU/memory were not moved to the target: %#v", definition)
}
if managedDefinitionMatchesResources(definition, metadata, 40*gib, target) {
t.Fatal("metadata-only target was accepted without the actual volume capacity")
}
}
func TestReconcileResizeObservationDistinguishesAppliedNotAppliedAndPartial(t *testing.T) {
document, err := BuildDomainXML(validDomainDefinition())
if err != nil {
t.Fatal(err)
}
fromDefinition, fromMetadata, err := parseManagedDomain(document)
if err != nil {
t.Fatal(err)
}
expected := &operation.ExpectedState{
Kind: operation.ExpectedResizeResources, TransitionID: "operation-resize-reconcile",
FromVCPUs: 2, TargetVCPUs: 4,
FromMemoryMiB: 2048, TargetMemoryMiB: 4096,
FromDiskGiB: 40, TargetDiskGiB: 80,
FromBandwidthMbps: 100, TargetBandwidthMbps: 200,
}
for name, marker := range map[string]string{
"no marker": "",
"prior transition": "operation-prior-resize",
} {
notApplied := reconcileResizeObservation(expected, marker, fromDefinition, fromMetadata, 40*gib)
if !notApplied.Resolved || notApplied.Succeeded || notApplied.ErrorCode != "resources_not_committed" {
t.Fatalf("%s not-applied observation = %#v", name, notApplied)
}
}
updated, err := resizeManagedDomainDocument(document, expectedResizeTargetValues(expected), expected.TransitionID)
if err != nil {
t.Fatal(err)
}
targetDefinition, targetMetadata, err := parseManagedDomain(updated)
if err != nil {
t.Fatal(err)
}
applied := reconcileResizeObservation(expected, expected.TransitionID, targetDefinition, targetMetadata, 80*gib)
if !applied.Resolved || !applied.Succeeded {
t.Fatalf("applied observation = %#v", applied)
}
for name, partial := range map[string]operation.Resolution{
"volume only": reconcileResizeObservation(expected, "", fromDefinition, fromMetadata, 80*gib),
"definition only": reconcileResizeObservation(expected, expected.TransitionID, targetDefinition, targetMetadata, 40*gib),
"target without marker": reconcileResizeObservation(expected, "operation-other-resize", targetDefinition, targetMetadata, 80*gib),
} {
if !partial.Resolved || partial.Succeeded || partial.ErrorCode != "resources_partially_resized" {
t.Fatalf("%s partial observation = %#v", name, partial)
}
}
}
func TestResizeInspectionPublishesOnlyBoundedProjectionAndKeepsPartialFenced(t *testing.T) {
const uuid = "123e4567-e89b-42d3-a456-426614174000"
record := operation.Record{
OperationID: "operation-resize-inspection", ResourceID: uuid,
Action: "resize-vm", State: operation.StateFailed, EffectMayHaveApplied: true,
ExpectedState: &operation.ExpectedState{
Kind: operation.ExpectedResizeResources, TransitionID: "operation-resize-inspection",
FromVCPUs: 2, TargetVCPUs: 4,
FromMemoryMiB: 2048, TargetMemoryMiB: 4096,
FromDiskGiB: 40, TargetDiskGiB: 80,
FromBandwidthMbps: 100, TargetBandwidthMbps: 200,
},
}
result := ResizeResourcesResult{
UUID: uuid,
FromVCPUs: 2, TargetVCPUs: 4, AppliedVCPUs: 4,
FromMemoryMiB: 2048, TargetMemoryMiB: 4096, AppliedMemoryMiB: 4096,
FromDiskGiB: 40, TargetDiskGiB: 80, AppliedDiskGiB: 80,
FromBandwidthMbps: 100, TargetBandwidthMbps: 200, AppliedBandwidthMbps: 200,
Status: VMStateShutoff, ResourcesResized: true,
}
raw, err := json.Marshal(result)
if err != nil {
t.Fatal(err)
}
applied := buildOperationInspection(record, operation.Resolution{Resolved: true, Succeeded: true, Result: raw}, OperationAbortInspection{}, operationCleanupInspection{})
if applied.EffectResolution != InspectionEffectApplied || len(applied.EffectProjection) == 0 {
t.Fatalf("applied resize inspection = %#v", applied)
}
var projected ResizeResourcesResult
if err := json.Unmarshal(applied.EffectProjection, &projected); err != nil || !projected.ResourcesResized || projected.AppliedDiskGiB != 80 {
t.Fatalf("resize projection = %s err=%v", applied.EffectProjection, err)
}
notApplied := buildOperationInspection(record, operation.Resolution{
Resolved: true, Succeeded: false, ErrorCode: "resources_not_committed",
}, OperationAbortInspection{}, operationCleanupInspection{})
if notApplied.EffectResolution != InspectionEffectNotApplied {
t.Fatalf("not-applied resize inspection = %#v", notApplied)
}
partial := buildOperationInspection(record, operation.Resolution{
Resolved: true, Succeeded: false, ErrorCode: "resources_partially_resized",
}, OperationAbortInspection{}, operationCleanupInspection{})
if partial.EffectResolution != InspectionEffectPartial || partial.Outcome != InspectionOutcomeUncertain || len(partial.EffectProjection) != 0 {
t.Fatalf("partial resize inspection = %#v", partial)
}
}