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.
100 lines
3.0 KiB
Go
100 lines
3.0 KiB
Go
//go:build libvirt
|
|
|
|
package hypervisor
|
|
|
|
import (
|
|
"errors"
|
|
"strings"
|
|
"testing"
|
|
|
|
"libvirt.org/go/libvirt"
|
|
"libvirt.org/go/libvirtxml"
|
|
)
|
|
|
|
func TestStagedMigrationVerifierAcceptsExactPublicAndVPCLayout(t *testing.T) {
|
|
definition := validDomainDefinition()
|
|
port := validVPCPortMetadata()
|
|
definition.VPCPort = &port
|
|
definition.Network = append(definition.Network, NetworkAttachment{
|
|
Bridge: port.Bridge, MAC: port.MAC, LinkDown: true,
|
|
})
|
|
definition.Network[0].LinkDown = true
|
|
snapshots := []domainSnapshotMetadataXML{{
|
|
Key: strings.Repeat("a", 32), Name: "before-migration", Consistency: "crash",
|
|
SizeBytes: 40 << 30, CreatedAt: 1_700_000_000,
|
|
}}
|
|
definition.Snapshots = snapshots
|
|
document, err := BuildDomainXML(definition)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
request := CreateVMRequest{
|
|
UUID: definition.UUID, DomainName: definition.Name, Name: definition.DisplayName,
|
|
PublicNetwork: definition.PublicNetwork, PlatformNetworkPolicy: definition.PlatformNetworkPolicy,
|
|
}
|
|
if err := validateStagedMigrationDomainDocument(
|
|
document, request, definition.FilterName, definition.Network[0].MAC, true, snapshots, &port,
|
|
); err != nil {
|
|
t.Fatalf("exact dual-interface staged migration was rejected: %v", err)
|
|
}
|
|
|
|
var forged libvirtxml.Domain
|
|
if err := forged.Unmarshal(document); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
forged.Devices.Interfaces = forged.Devices.Interfaces[:1]
|
|
broken, err := forged.Marshal()
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if err := validateStagedMigrationDomainDocument(
|
|
broken, request, definition.FilterName, definition.Network[0].MAC, true, snapshots, &port,
|
|
); err == nil {
|
|
t.Fatal("staged migration accepted metadata for a missing VPC interface")
|
|
}
|
|
}
|
|
|
|
type countingVolumeCleanup struct {
|
|
deletes int
|
|
frees int
|
|
deleteErr error
|
|
}
|
|
|
|
func (volume *countingVolumeCleanup) Delete(libvirt.StorageVolDeleteFlags) error {
|
|
volume.deletes++
|
|
return volume.deleteErr
|
|
}
|
|
|
|
func (volume *countingVolumeCleanup) Free() error {
|
|
volume.frees++
|
|
return nil
|
|
}
|
|
|
|
func TestPreparedMigrationVolumesHaveOneIdempotentOwner(t *testing.T) {
|
|
root := &countingVolumeCleanup{}
|
|
snapshot := &countingVolumeCleanup{}
|
|
owner := &preparedVolumeOwnership{
|
|
root: root, snapshots: []storageVolumeCleanup{snapshot},
|
|
rootOwned: true, snapshotsOwned: true,
|
|
}
|
|
resultErr := errors.New("seed creation failed")
|
|
owner.cleanup(&resultErr)
|
|
owner.cleanup(&resultErr)
|
|
if root.deletes != 1 || root.frees != 1 || snapshot.deletes != 1 || snapshot.frees != 1 {
|
|
t.Fatalf("cleanup counts root=%+v snapshot=%+v", root, snapshot)
|
|
}
|
|
|
|
publishedRoot := &countingVolumeCleanup{}
|
|
publishedSnapshot := &countingVolumeCleanup{}
|
|
published := &preparedVolumeOwnership{
|
|
root: publishedRoot, snapshots: []storageVolumeCleanup{publishedSnapshot},
|
|
rootOwned: true, snapshotsOwned: true,
|
|
}
|
|
published.preserve()
|
|
published.cleanup(&resultErr)
|
|
if publishedRoot.deletes != 0 || publishedRoot.frees != 1 ||
|
|
publishedSnapshot.deletes != 0 || publishedSnapshot.frees != 1 {
|
|
t.Fatalf("published cleanup counts root=%+v snapshot=%+v", publishedRoot, publishedSnapshot)
|
|
}
|
|
}
|