diff --git a/agent/hypervisor/libvirt_domain_features.go b/agent/hypervisor/libvirt_domain_features.go index fae33e6b..fe6dedf8 100644 --- a/agent/hypervisor/libvirt_domain_features.go +++ b/agent/hypervisor/libvirt_domain_features.go @@ -1369,7 +1369,11 @@ func inspectManagedDomain(document string) (*libvirtxml.Domain, *domainMetadataX return &definition, &metadata, true, nil } -func requireManagedDomain(domain *libvirt.Domain) (string, *domainMetadataXML, error) { +type managedDomainXMLReader interface { + GetXMLDesc(libvirt.DomainXMLFlags) (string, error) +} + +func requireManagedDomain(domain managedDomainXMLReader) (string, *domainMetadataXML, error) { document, err := domain.GetXMLDesc(libvirt.DOMAIN_XML_INACTIVE) if err != nil { return "", nil, translateLibvirtError("read managed domain XML", err) diff --git a/agent/hypervisor/libvirt_migration_bundle.go b/agent/hypervisor/libvirt_migration_bundle.go index 232d900e..098d49b9 100644 --- a/agent/hypervisor/libvirt_migration_bundle.go +++ b/agent/hypervisor/libvirt_migration_bundle.go @@ -165,15 +165,18 @@ func (l *Libvirt) validateMigrationBundleSource(ctx context.Context, uuid string return nil, err } defer domain.Free() - document, err := domain.GetXMLDesc(0) - if err != nil { - return nil, translateLibvirtError("read migration source metadata", err) - } - _, metadata, err := parseManagedDomain(document) + return validateMigrationBundleSnapshotCatalog( + domain, request.MigrationBundle.Snapshots, + ) +} + +func validateMigrationBundleSnapshotCatalog(domain managedDomainXMLReader, + requested []MigrationBundleSnapshotUpload) ([]domainSnapshotMetadataXML, error) { + _, metadata, err := requireManagedDomain(domain) if err != nil { return nil, err } - if len(metadata.Snapshots) != len(request.MigrationBundle.Snapshots) { + if len(metadata.Snapshots) != len(requested) { return nil, NewError(ErrConflict, "migration_snapshot_catalog_changed", "migration bundle must include every current snapshot", nil) } @@ -182,8 +185,8 @@ func (l *Libvirt) validateMigrationBundleSource(ctx context.Context, uuid string byKey[snapshot.Key] = snapshot } ordered := make([]domainSnapshotMetadataXML, 0, len(metadata.Snapshots)) - for _, requested := range request.MigrationBundle.Snapshots { - snapshot, ok := byKey[requested.SnapshotKey] + for _, expected := range requested { + snapshot, ok := byKey[expected.SnapshotKey] if !ok { return nil, NewError(ErrConflict, "migration_snapshot_catalog_changed", "migration snapshot catalog changed before export", nil) diff --git a/agent/hypervisor/libvirt_migration_bundle_test.go b/agent/hypervisor/libvirt_migration_bundle_test.go new file mode 100644 index 00000000..053cc9ea --- /dev/null +++ b/agent/hypervisor/libvirt_migration_bundle_test.go @@ -0,0 +1,96 @@ +//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) + } +}