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.
185 lines
6.6 KiB
Go
185 lines
6.6 KiB
Go
//go:build libvirt
|
|
|
|
package hypervisor
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"testing"
|
|
|
|
"git.luoli.live/root/virt/agent/objecttransfer"
|
|
"libvirt.org/go/libvirt"
|
|
"libvirt.org/go/libvirtxml"
|
|
)
|
|
|
|
type backupVolumeInspector struct {
|
|
document string
|
|
capacity uint64
|
|
}
|
|
|
|
func (v backupVolumeInspector) GetXMLDesc(uint32) (string, error) { return v.document, nil }
|
|
func (v backupVolumeInspector) GetInfo() (*libvirt.StorageVolInfo, error) {
|
|
return &libvirt.StorageVolInfo{Capacity: v.capacity}, nil
|
|
}
|
|
|
|
func TestValidateSingleWritableRootRejectsAdditionalDataDisk(t *testing.T) {
|
|
root := libvirtxml.DomainDisk{
|
|
Device: "disk", Target: &libvirtxml.DomainDiskTarget{Dev: "vda"},
|
|
}
|
|
seed := libvirtxml.DomainDisk{
|
|
Device: "cdrom", Target: &libvirtxml.DomainDiskTarget{Dev: "sda"},
|
|
ReadOnly: &libvirtxml.DomainDiskReadOnly{},
|
|
}
|
|
domain := &libvirtxml.Domain{Devices: &libvirtxml.DomainDeviceList{Disks: []libvirtxml.DomainDisk{root, seed}}}
|
|
if err := validateSingleWritableRoot(domain); err != nil {
|
|
t.Fatalf("one root plus read-only seed rejected: %v", err)
|
|
}
|
|
domain.Devices.Disks = append(domain.Devices.Disks, libvirtxml.DomainDisk{
|
|
Device: "disk", Target: &libvirtxml.DomainDiskTarget{Dev: "vdb"},
|
|
})
|
|
if err := validateSingleWritableRoot(domain); !errors.Is(err, ErrConflict) {
|
|
t.Fatalf("extra writable disk error = %v", err)
|
|
}
|
|
domain.Devices.Disks = []libvirtxml.DomainDisk{root, {
|
|
Device: "lun", Target: &libvirtxml.DomainDiskTarget{Dev: "sdb"},
|
|
}}
|
|
if err := validateSingleWritableRoot(domain); !errors.Is(err, ErrConflict) {
|
|
t.Fatalf("extra writable LUN error = %v", err)
|
|
}
|
|
}
|
|
|
|
func TestValidateLiveBackupDefinitionRejectsLiveOnlyDiskTopology(t *testing.T) {
|
|
const managedPath = "/var/lib/virt-cloud/managed/root.qcow2"
|
|
root := libvirtxml.DomainDisk{
|
|
Device: "disk", Driver: &libvirtxml.DomainDiskDriver{Type: "qcow2"},
|
|
Source: &libvirtxml.DomainDiskSource{File: &libvirtxml.DomainDiskSourceFile{File: managedPath}},
|
|
Target: &libvirtxml.DomainDiskTarget{Dev: "vda"},
|
|
}
|
|
domain := &libvirtxml.Domain{Devices: &libvirtxml.DomainDeviceList{Disks: []libvirtxml.DomainDisk{root}}}
|
|
if err := validateLiveBackupDefinition(domain, managedPath); err != nil {
|
|
t.Fatalf("matching live root rejected: %v", err)
|
|
}
|
|
domain.Devices.Disks[0].BackingStore = &libvirtxml.DomainDiskBackingStore{}
|
|
if err := validateLiveBackupDefinition(domain, managedPath); err != nil {
|
|
t.Fatalf("empty libvirt backing-store sentinel rejected: %v", err)
|
|
}
|
|
domain.Devices.Disks[0].BackingStore = nil
|
|
|
|
domain.Devices.Disks = append(domain.Devices.Disks, libvirtxml.DomainDisk{
|
|
Device: "disk", Target: &libvirtxml.DomainDiskTarget{Dev: "vdb"},
|
|
})
|
|
if err := validateLiveBackupDefinition(domain, managedPath); !errors.Is(err, ErrConflict) {
|
|
t.Fatalf("live-only writable disk error = %v", err)
|
|
}
|
|
|
|
domain.Devices.Disks = []libvirtxml.DomainDisk{root}
|
|
domain.Devices.Disks[0].Source.File.File = "/var/lib/virt-cloud/managed/pivoted.qcow2"
|
|
if err := validateLiveBackupDefinition(domain, managedPath); !errors.Is(err, ErrConflict) {
|
|
t.Fatalf("live pivoted root error = %v", err)
|
|
}
|
|
|
|
domain.Devices.Disks = []libvirtxml.DomainDisk{root}
|
|
domain.Devices.Disks[0].BackingStore = &libvirtxml.DomainDiskBackingStore{
|
|
Source: &libvirtxml.DomainDiskSource{File: &libvirtxml.DomainDiskSourceFile{File: "/external/base.qcow2"}},
|
|
}
|
|
if err := validateLiveBackupDefinition(domain, managedPath); !errors.Is(err, ErrConflict) {
|
|
t.Fatalf("live backing chain error = %v", err)
|
|
}
|
|
}
|
|
|
|
func TestValidateLiveBackupDefinitionAcceptsSerializedEmptyBackingStoreSentinel(t *testing.T) {
|
|
const managedPath = "/var/lib/virt-cloud/managed/root.qcow2"
|
|
var domain libvirtxml.Domain
|
|
if err := domain.Unmarshal(`<domain><devices>
|
|
<disk type='file' device='disk'>
|
|
<driver name='qemu' type='qcow2'/>
|
|
<source file='` + managedPath + `' index='2'/>
|
|
<backingStore/>
|
|
<target dev='vda' bus='virtio'/>
|
|
</disk>
|
|
</devices></domain>`); err != nil {
|
|
t.Fatalf("unmarshal live libvirt XML: %v", err)
|
|
}
|
|
if err := validateLiveBackupDefinition(&domain, managedPath); err != nil {
|
|
t.Fatalf("serialized empty libvirt backing-store sentinel rejected: %v", err)
|
|
}
|
|
}
|
|
|
|
func TestValidateStandaloneQCOW2RejectsBackingChain(t *testing.T) {
|
|
const capacity = 40 << 30
|
|
standalone := backupVolumeInspector{
|
|
capacity: capacity,
|
|
document: `<volume><name>root.qcow2</name><capacity unit="bytes">42949672960</capacity><target><format type="qcow2"/></target></volume>`,
|
|
}
|
|
if _, err := validateStandaloneQCOW2Volume(standalone, capacity); err != nil {
|
|
t.Fatalf("standalone qcow2 rejected: %v", err)
|
|
}
|
|
standalone.document = `<volume><name>root.qcow2</name><capacity unit="bytes">42949672960</capacity><target><format type="qcow2"/></target><backingStore><path>/template/base.qcow2</path><format type="qcow2"/></backingStore></volume>`
|
|
if _, err := validateStandaloneQCOW2Volume(standalone, capacity); !errors.Is(err, ErrConflict) {
|
|
t.Fatalf("backing-chain error = %v", err)
|
|
}
|
|
}
|
|
|
|
func TestValidateBackupTransitionStateAllowsCompletedDiskTransitions(t *testing.T) {
|
|
tests := []struct {
|
|
name string
|
|
metadata *domainMetadataXML
|
|
wantError bool
|
|
}{
|
|
{
|
|
name: "reinstall cleanup pending",
|
|
metadata: &domainMetadataXML{
|
|
PendingCleanupVolume: "123e4567-e89b-42d3-a456-426614174000-root.qcow2",
|
|
DiskTransitionKind: "reinstall",
|
|
DiskTransitionID: "operation-reinstall-one",
|
|
},
|
|
wantError: true,
|
|
},
|
|
{
|
|
name: "completed reinstall marker",
|
|
metadata: &domainMetadataXML{
|
|
DiskTransitionKind: "reinstall",
|
|
DiskTransitionID: "operation-reinstall-one",
|
|
},
|
|
},
|
|
{
|
|
name: "completed snapshot restore marker",
|
|
metadata: &domainMetadataXML{
|
|
DiskTransitionKind: "snapshot_restore",
|
|
DiskTransitionID: "operation-snapshot-restore-one",
|
|
},
|
|
},
|
|
{name: "no transition", metadata: &domainMetadataXML{}},
|
|
}
|
|
|
|
for _, test := range tests {
|
|
t.Run(test.name, func(t *testing.T) {
|
|
err := validateBackupTransitionState(test.metadata)
|
|
if test.wantError && !errors.Is(err, ErrConflict) {
|
|
t.Fatalf("pending transition error = %v", err)
|
|
}
|
|
if !test.wantError && err != nil {
|
|
t.Fatalf("completed transition marker rejected: %v", err)
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
func TestAbortRejectedBackupPlanPreservesPreflightCause(t *testing.T) {
|
|
rejected := NewError(ErrConflict, "backup_disk_transition_active",
|
|
"VM backup is unavailable during a disk transition", nil)
|
|
called := false
|
|
err := abortRejectedBackupPlan(
|
|
context.Background(),
|
|
func(context.Context, objecttransfer.MultipartPlan) error {
|
|
called = true
|
|
return nil
|
|
},
|
|
objecttransfer.MultipartPlan{},
|
|
rejected,
|
|
)
|
|
if !called || !errors.Is(err, rejected) {
|
|
t.Fatalf("called=%v err=%v", called, err)
|
|
}
|
|
}
|