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.
97 lines
3.0 KiB
Go
97 lines
3.0 KiB
Go
//go:build libvirt
|
|
|
|
package hypervisor
|
|
|
|
import (
|
|
"errors"
|
|
"reflect"
|
|
"testing"
|
|
|
|
"libvirt.org/go/libvirt"
|
|
)
|
|
|
|
type migrationBundleXMLDomainFake struct {
|
|
liveXML string
|
|
inactiveXML string
|
|
flags []libvirt.DomainXMLFlags
|
|
}
|
|
|
|
func (f *migrationBundleXMLDomainFake) GetXMLDesc(flags libvirt.DomainXMLFlags) (string, error) {
|
|
f.flags = append(f.flags, flags)
|
|
if flags == libvirt.DOMAIN_XML_INACTIVE {
|
|
return f.inactiveXML, nil
|
|
}
|
|
return f.liveXML, nil
|
|
}
|
|
|
|
func TestMigrationBundleSnapshotCatalogReadsPersistentMetadata(t *testing.T) {
|
|
const snapshotKey = "fedcba9876543210fedcba9876543210"
|
|
liveDefinition := validDomainDefinition()
|
|
liveXML, err := BuildDomainXML(liveDefinition)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
persistentDefinition := validDomainDefinition()
|
|
persistentDefinition.Snapshots = []domainSnapshotMetadataXML{{
|
|
Key: snapshotKey, Name: "before-maintenance", Consistency: "crash",
|
|
SizeBytes: 40 << 30, CreatedAt: 1700000000,
|
|
}}
|
|
persistentXML, err := BuildDomainXML(persistentDefinition)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
domain := &migrationBundleXMLDomainFake{
|
|
liveXML: liveXML, inactiveXML: persistentXML,
|
|
}
|
|
|
|
stored, err := validateMigrationBundleSnapshotCatalog(
|
|
domain, []MigrationBundleSnapshotUpload{{SnapshotKey: snapshotKey}},
|
|
)
|
|
if err != nil {
|
|
t.Fatalf("persistent snapshot catalog rejected: %v", err)
|
|
}
|
|
if len(stored) != 1 || stored[0].Key != snapshotKey {
|
|
t.Fatalf("snapshot catalog = %#v, want %q", stored, snapshotKey)
|
|
}
|
|
if !reflect.DeepEqual(domain.flags, []libvirt.DomainXMLFlags{libvirt.DOMAIN_XML_INACTIVE}) {
|
|
t.Fatalf("GetXMLDesc flags = %#v, want DOMAIN_XML_INACTIVE", domain.flags)
|
|
}
|
|
}
|
|
|
|
func TestMigrationBundleSnapshotCatalogRejectsPersistentChanges(t *testing.T) {
|
|
const requestedKey = "fedcba9876543210fedcba9876543210"
|
|
const persistentKey = "0123456789abcdef0123456789abcdef"
|
|
liveDefinition := validDomainDefinition()
|
|
liveDefinition.Snapshots = []domainSnapshotMetadataXML{{
|
|
Key: requestedKey, Name: "stale-live", Consistency: "crash",
|
|
SizeBytes: 40 << 30, CreatedAt: 1700000000,
|
|
}}
|
|
liveXML, err := BuildDomainXML(liveDefinition)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
persistentDefinition := validDomainDefinition()
|
|
persistentDefinition.Snapshots = []domainSnapshotMetadataXML{{
|
|
Key: persistentKey, Name: "current-persistent", Consistency: "crash",
|
|
SizeBytes: 40 << 30, CreatedAt: 1700000001,
|
|
}}
|
|
persistentXML, err := BuildDomainXML(persistentDefinition)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
domain := &migrationBundleXMLDomainFake{
|
|
liveXML: liveXML, inactiveXML: persistentXML,
|
|
}
|
|
|
|
_, err = validateMigrationBundleSnapshotCatalog(
|
|
domain, []MigrationBundleSnapshotUpload{{SnapshotKey: requestedKey}},
|
|
)
|
|
var public *Error
|
|
if !errors.As(err, &public) || public.Code != "migration_snapshot_catalog_changed" {
|
|
t.Fatalf("changed persistent catalog error = %v, want migration_snapshot_catalog_changed", err)
|
|
}
|
|
if !reflect.DeepEqual(domain.flags, []libvirt.DomainXMLFlags{libvirt.DOMAIN_XML_INACTIVE}) {
|
|
t.Fatalf("GetXMLDesc flags = %#v, want DOMAIN_XML_INACTIVE", domain.flags)
|
|
}
|
|
}
|