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.
220 lines
9.6 KiB
Go
220 lines
9.6 KiB
Go
//go:build libvirt
|
|
|
|
package hypervisor
|
|
|
|
import (
|
|
"context"
|
|
"encoding/json"
|
|
"errors"
|
|
"reflect"
|
|
"strings"
|
|
"testing"
|
|
"time"
|
|
|
|
"git.luoli.live/root/virt/agent/operation"
|
|
"libvirt.org/go/libvirt"
|
|
)
|
|
|
|
type cleanupJobDomainFake struct {
|
|
active bool
|
|
activeErr error
|
|
job *libvirt.DomainJobInfo
|
|
jobErr error
|
|
document string
|
|
inactiveXML string
|
|
xmlErr error
|
|
blockInfo map[string]*libvirt.DomainBlockJobInfo
|
|
blockErr map[string]error
|
|
blockQueries []string
|
|
}
|
|
|
|
func (f *cleanupJobDomainFake) IsActive() (bool, error) { return f.active, f.activeErr }
|
|
|
|
func (f *cleanupJobDomainFake) GetJobStats(libvirt.DomainGetJobStatsFlags) (*libvirt.DomainJobInfo, error) {
|
|
return f.job, f.jobErr
|
|
}
|
|
|
|
func (f *cleanupJobDomainFake) GetXMLDesc(flags libvirt.DomainXMLFlags) (string, error) {
|
|
if flags == libvirt.DOMAIN_XML_INACTIVE && f.inactiveXML != "" {
|
|
return f.inactiveXML, f.xmlErr
|
|
}
|
|
return f.document, f.xmlErr
|
|
}
|
|
|
|
func (f *cleanupJobDomainFake) GetBlockJobInfo(target string, _ libvirt.DomainBlockJobInfoFlags) (*libvirt.DomainBlockJobInfo, error) {
|
|
f.blockQueries = append(f.blockQueries, target)
|
|
if err := f.blockErr[target]; err != nil {
|
|
return nil, err
|
|
}
|
|
if info, ok := f.blockInfo[target]; ok {
|
|
return info, nil
|
|
}
|
|
return &libvirt.DomainBlockJobInfo{}, nil
|
|
}
|
|
|
|
func TestCleanupJobEvidenceRequiresIdleDomainAndBlockJobs(t *testing.T) {
|
|
document := `<domain><devices><disk device="disk"><target dev="vdb"/></disk><disk device="disk"><target dev="vda"/></disk></devices></domain>`
|
|
idle := &cleanupJobDomainFake{
|
|
active: true, job: &libvirt.DomainJobInfo{Type: libvirt.DOMAIN_JOB_NONE}, document: document,
|
|
}
|
|
evidence, safe := inspectCleanupJobEvidence(idle)
|
|
joinedEvidence := strings.Join(evidence, "\n")
|
|
if !safe || !reflect.DeepEqual(idle.blockQueries, []string{"vda", "vdb"}) ||
|
|
!strings.Contains(joinedEvidence, "domain-runtime:active:job-none") ||
|
|
!strings.Contains(joinedEvidence, "disk-target:vda") ||
|
|
!strings.Contains(joinedEvidence, "disk-target:vdb") ||
|
|
!strings.Contains(joinedEvidence, "persistent-domain-xml:") {
|
|
t.Fatalf("idle job evidence=%v safe=%v queries=%v", evidence, safe, idle.blockQueries)
|
|
}
|
|
|
|
inactive := &cleanupJobDomainFake{document: document}
|
|
evidence, safe = inspectCleanupJobEvidence(inactive)
|
|
if !safe || len(inactive.blockQueries) != 0 || evidence[0] != "domain-runtime:inactive" {
|
|
t.Fatalf("inactive job evidence=%v safe=%v queries=%v", evidence, safe, inactive.blockQueries)
|
|
}
|
|
|
|
for name, domain := range map[string]*cleanupJobDomainFake{
|
|
"active domain job": {
|
|
active: true, job: activeBackupJob(), document: document,
|
|
},
|
|
"active block job": {
|
|
active: true, job: &libvirt.DomainJobInfo{Type: libvirt.DOMAIN_JOB_NONE}, document: document,
|
|
blockInfo: map[string]*libvirt.DomainBlockJobInfo{"vda": {Type: libvirt.DOMAIN_BLOCK_JOB_TYPE_COPY, End: 10}},
|
|
},
|
|
"block state unreadable": {
|
|
active: true, job: &libvirt.DomainJobInfo{Type: libvirt.DOMAIN_JOB_NONE}, document: document,
|
|
blockErr: map[string]error{"vda": errors.New("query failed")},
|
|
},
|
|
"disk evidence incomplete": {
|
|
active: true, job: &libvirt.DomainJobInfo{Type: libvirt.DOMAIN_JOB_NONE},
|
|
document: `<domain><devices><disk device="disk"/></devices></domain>`,
|
|
},
|
|
} {
|
|
t.Run(name, func(t *testing.T) {
|
|
if evidence, safe := inspectCleanupJobEvidence(domain); safe || len(evidence) != 0 {
|
|
t.Fatalf("unsafe job evidence=%v safe=%v", evidence, safe)
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
func TestCleanupPlanIdentifierBindsPrivateExactEvidence(t *testing.T) {
|
|
record := operation.Record{
|
|
OperationID: "operation-exact-cleanup-plan", ResourceID: "123e4567-e89b-42d3-a456-426614174000",
|
|
Action: "delete-vm", Fingerprint: "request-fingerprint", State: operation.StateUncertain,
|
|
}
|
|
first := buildOperationInspection(record, operation.Resolution{}, OperationAbortInspection{},
|
|
newOperationCleanupInspection([]string{CleanupStepDeleteManagedVolumes}, "managed-volume:first"))
|
|
second := buildOperationInspection(record, operation.Resolution{}, OperationAbortInspection{},
|
|
newOperationCleanupInspection([]string{CleanupStepDeleteManagedVolumes}, "managed-volume:second"))
|
|
if first.Cleanup.PlanID == second.Cleanup.PlanID {
|
|
t.Fatal("cleanup plan id did not change with the exact resource set")
|
|
}
|
|
encoded, err := json.Marshal(first)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if string(encoded) == "" || containsAny(string(encoded), "managed-volume:first", "request-fingerprint") {
|
|
t.Fatalf("private cleanup evidence leaked: %s", encoded)
|
|
}
|
|
}
|
|
|
|
func containsAny(value string, candidates ...string) bool {
|
|
for _, candidate := range candidates {
|
|
if len(candidate) != 0 && len(value) >= len(candidate) && strings.Contains(value, candidate) {
|
|
return true
|
|
}
|
|
}
|
|
return false
|
|
}
|
|
|
|
func TestAbsentCreateTrafficResidueRequiresExplicitCleanup(t *testing.T) {
|
|
record := operation.Record{
|
|
OperationID: "operation-create-traffic-residue",
|
|
ResourceID: "123e4567-e89b-42d3-a456-426614174000",
|
|
Action: "create-vm",
|
|
Fingerprint: "request-fingerprint",
|
|
State: operation.StateUncertain,
|
|
}
|
|
resolution, steps := inspectAbsentCreateVM(record.ResourceID, true, true)
|
|
inspection := buildOperationInspection(record, resolution, OperationAbortInspection{}, steps)
|
|
if inspection.Outcome != InspectionOutcomeUncertain || inspection.SummaryCode != InspectionSummaryCleanupRequired ||
|
|
!inspection.Cleanup.Available || len(inspection.Cleanup.Steps) != 1 ||
|
|
inspection.Cleanup.Steps[0] != CleanupStepRemoveTrafficState || !lowerHexDigest(inspection.Cleanup.PlanID) {
|
|
t.Fatalf("create traffic residue inspection = %#v", inspection)
|
|
}
|
|
|
|
resolution, steps = inspectAbsentCreateVM(record.ResourceID, true, false)
|
|
inspection = buildOperationInspection(record, resolution, OperationAbortInspection{}, steps)
|
|
if inspection.Outcome != InspectionOutcomeFailed || inspection.SummaryCode != InspectionSummaryFailed ||
|
|
inspection.ErrorCode != "creation_absent" || inspection.Cleanup.Available {
|
|
t.Fatalf("clean absent create inspection = %#v", inspection)
|
|
}
|
|
|
|
resolution, steps = inspectAbsentCreateVM(record.ResourceID, false, true)
|
|
inspection = buildOperationInspection(record, resolution, OperationAbortInspection{}, steps)
|
|
if inspection.Outcome != InspectionOutcomeUncertain || inspection.SummaryCode != InspectionSummaryIncomplete ||
|
|
inspection.Cleanup.Available {
|
|
t.Fatalf("unproven create absence inspection = %#v", inspection)
|
|
}
|
|
}
|
|
|
|
func TestBackupPushMatchesOnlyExactOwnedTarget(t *testing.T) {
|
|
target := "/var/lib/virt-cloud/managed/snapshot.qcow2"
|
|
matching := `<domainbackup mode="push"><disks><disk name="vda" backup="yes" type="file"><driver type="qcow2"/><target file="` + target + `"/></disk></disks></domainbackup>`
|
|
if !backupPushMatchesTarget(matching, target) {
|
|
t.Fatal("exact push backup target was not recognized")
|
|
}
|
|
for name, document := range map[string]string{
|
|
"foreign target": `<domainbackup mode="push"><disks><disk name="vda" backup="yes" type="file"><target file="/tmp/foreign.qcow2"/></disk></disks></domainbackup>`,
|
|
"wrong disk": `<domainbackup mode="push"><disks><disk name="vdb" backup="yes" type="file"><target file="` + target + `"/></disk></disks></domainbackup>`,
|
|
"pull backup": `<domainbackup mode="pull"><server transport="unix" socket="/tmp/backup.sock"/><disks><disk name="vda" backup="yes" type="file"><scratch file="` + target + `"/></disk></disks></domainbackup>`,
|
|
"multiple disks": `<domainbackup mode="push"><disks><disk name="vda" backup="yes" type="file"><target file="` + target + `"/></disk><disk name="vdb" backup="yes" type="file"><target file="` + target + `"/></disk></disks></domainbackup>`,
|
|
} {
|
|
t.Run(name, func(t *testing.T) {
|
|
if backupPushMatchesTarget(document, target) {
|
|
t.Fatal("non-matching backup was accepted")
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
func TestWaitForAbortTerminalHasIndependentBound(t *testing.T) {
|
|
job := &snapshotBackupJobFake{active: []*libvirt.DomainJobInfo{activeBackupJob()}}
|
|
started := time.Now()
|
|
terminal, err := waitForAbortTerminalWithTiming(context.Background(), job,
|
|
snapshotBackupWaitTiming{pollInterval: time.Millisecond, abortConfirmationTimeout: 8 * time.Millisecond})
|
|
if terminal != libvirt.DOMAIN_JOB_NONE || !errors.Is(err, context.DeadlineExceeded) {
|
|
t.Fatalf("unconfirmed abort terminal=%v err=%v", terminal, err)
|
|
}
|
|
if elapsed := time.Since(started); elapsed > 500*time.Millisecond {
|
|
t.Fatalf("abort confirmation exceeded independent bound: %v", elapsed)
|
|
}
|
|
}
|
|
|
|
func TestWaitForAbortTerminalAcceptsTerminalRaceAtDeadline(t *testing.T) {
|
|
job := &snapshotBackupJobFake{
|
|
active: []*libvirt.DomainJobInfo{activeBackupJob(), noActiveBackupJob()},
|
|
completed: []*libvirt.DomainJobInfo{terminalBackupJob(libvirt.DOMAIN_JOB_CANCELLED)},
|
|
}
|
|
terminal, err := waitForAbortTerminalWithTiming(context.Background(), job,
|
|
snapshotBackupWaitTiming{pollInterval: time.Millisecond, abortConfirmationTimeout: 30 * time.Millisecond})
|
|
if err != nil || terminal != libvirt.DOMAIN_JOB_CANCELLED {
|
|
t.Fatalf("terminal race was not retained: terminal=%v err=%v", terminal, err)
|
|
}
|
|
}
|
|
|
|
func TestAbortTerminalConfirmationContinuesAfterCallerCancellation(t *testing.T) {
|
|
ctx, cancel := context.WithCancel(context.Background())
|
|
cancel()
|
|
job := &snapshotBackupJobFake{
|
|
active: []*libvirt.DomainJobInfo{activeBackupJob(), noActiveBackupJob()},
|
|
completed: []*libvirt.DomainJobInfo{terminalBackupJob(libvirt.DOMAIN_JOB_CANCELLED)},
|
|
}
|
|
terminal, err := waitForAbortTerminalWithTiming(context.WithoutCancel(ctx), job,
|
|
snapshotBackupWaitTiming{pollInterval: time.Millisecond, abortConfirmationTimeout: 30 * time.Millisecond})
|
|
if err != nil || terminal != libvirt.DOMAIN_JOB_CANCELLED {
|
|
t.Fatalf("post-dispatch cancellation stopped terminal confirmation: terminal=%v err=%v", terminal, err)
|
|
}
|
|
}
|