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.
102 lines
4.1 KiB
Go
102 lines
4.1 KiB
Go
//go:build libvirt
|
|
|
|
package hypervisor
|
|
|
|
import (
|
|
"errors"
|
|
"testing"
|
|
|
|
"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 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)
|
|
}
|
|
}
|