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.
virt/agent/hypervisor/libvirt_safety_test.go

2205 lines
88 KiB
Go

//go:build libvirt
package hypervisor
import (
"context"
"encoding/json"
"errors"
"fmt"
"net"
"os"
"path/filepath"
"reflect"
"sort"
"strings"
"testing"
"time"
"git.luoli.live/root/virt/agent/operation"
"libvirt.org/go/libvirt"
"libvirt.org/go/libvirtxml"
)
type fakeDeleteDomainControl struct {
active []bool
activeErrors []error
shutdownErr error
destroyErr error
undefineErr error
shutdownCalls int
destroyCalls int
undefine []libvirt.DomainUndefineFlagsValues
}
type fakePowerDomainControl struct {
called string
err error
}
func (f *fakePowerDomainControl) Create() error {
f.called = "start"
return f.err
}
func (f *fakePowerDomainControl) ShutdownFlags(libvirt.DomainShutdownFlags) error {
f.called = "shutdown"
return f.err
}
func (f *fakePowerDomainControl) Reboot(libvirt.DomainRebootFlagValues) error {
f.called = "reboot"
return f.err
}
func (f *fakePowerDomainControl) Reset(uint32) error {
f.called = "reset"
return f.err
}
func (f *fakePowerDomainControl) DestroyFlags(libvirt.DomainDestroyFlags) error {
f.called = "destroy"
return f.err
}
type fakeServiceStateDomainControl struct {
setValue bool
setErr error
autostart bool
autostartErr error
active bool
activeErr error
createCalled bool
createErr error
}
func (f *fakeServiceStateDomainControl) Create() error {
f.createCalled = true
return f.createErr
}
func (f *fakeServiceStateDomainControl) SetAutostart(value bool) error {
f.setValue = value
return f.setErr
}
func (f *fakeServiceStateDomainControl) GetAutostart() (bool, error) {
return f.autostart, f.autostartErr
}
func (f *fakeServiceStateDomainControl) IsActive() (bool, error) {
return f.active, f.activeErr
}
func TestMaintenanceMigrationRollbackRestoresSourceState(t *testing.T) {
adapter := &Libvirt{}
domain := &fakeServiceStateDomainControl{active: false}
err := adapter.restoreMaintenanceMigrationDomain(domain,
maintenanceMigrationSourceState{wasRunning: false, autostart: false}, domain.Create)
if err != nil || domain.setValue || domain.createCalled {
t.Fatalf("expected powered-off unmanaged source state restoration, err=%v state=%+v", err, domain)
}
domain = &fakeServiceStateDomainControl{active: false}
err = adapter.restoreMaintenanceMigrationDomain(domain,
maintenanceMigrationSourceState{wasRunning: true, autostart: true}, domain.Create)
if err != nil || !domain.setValue || !domain.createCalled {
t.Fatalf("expected source state restoration, err=%v state=%+v", err, domain)
}
// Runtime and autostart are independent libvirt states. A VM may be
// manually started while autostart remains disabled, and migration must
// preserve that exact combination.
domain = &fakeServiceStateDomainControl{active: false}
err = adapter.restoreMaintenanceMigrationDomain(domain,
maintenanceMigrationSourceState{wasRunning: true, autostart: false}, domain.Create)
if err != nil || domain.setValue || !domain.createCalled {
t.Fatalf("expected running-without-autostart restoration, err=%v state=%+v", err, domain)
}
domain = &fakeServiceStateDomainControl{active: false, createErr: errors.New("lost")}
err = adapter.restoreMaintenanceMigrationDomain(domain,
maintenanceMigrationSourceState{wasRunning: true, autostart: true}, domain.Create)
if !operation.IsUncertain(err) {
t.Fatalf("restart failure must be uncertain, got %v", err)
}
}
func TestPowerMutationErrorsRemainReconciliable(t *testing.T) {
for _, action := range []PowerAction{
PowerStart, PowerShutdown, PowerReboot, PowerReset, PowerDestroy,
} {
t.Run(string(action), func(t *testing.T) {
cause := errors.New("libvirt response lost")
domain := &fakePowerDomainControl{err: cause}
err := dispatchPowerMutation(domain, action, domain.Create)
if domain.called != string(action) || !operation.IsUncertain(err) ||
!errors.Is(err, cause) {
t.Fatalf("power mutation action=%q called=%q err=%v", action, domain.called, err)
}
})
}
if err := dispatchPowerMutation(&fakePowerDomainControl{}, PowerAction("invalid"), nil); err == nil || operation.IsUncertain(err) {
t.Fatalf("invalid preflight action error = %v", err)
}
preCreate := operation.MarkNotApplied(errors.New("public guard unavailable"))
domain := &fakePowerDomainControl{}
if err := dispatchPowerMutation(domain, PowerStart, func() error { return preCreate }); !operation.IsNotApplied(err) || operation.IsUncertain(err) || domain.called != "" {
t.Fatalf("pre-Create failure semantics called=%q err=%v", domain.called, err)
}
for _, test := range []struct {
name string
action PowerAction
state VMStateName
wantOK bool
}{
{"start-running", PowerStart, VMStateRunning, true},
{"start-blocked", PowerStart, VMStateBlocked, true},
{"start-still-off", PowerStart, VMStateShutoff, false},
{"shutdown-off", PowerShutdown, VMStateShutoff, true},
{"shutdown-in-progress", PowerShutdown, VMStateShutdown, true},
{"shutdown-still-running", PowerShutdown, VMStateRunning, true},
{"destroy-off", PowerDestroy, VMStateShutoff, true},
{"destroy-still-running", PowerDestroy, VMStateRunning, false},
{"reboot-observed", PowerReboot, VMStateRunning, true},
{"reset-observed", PowerReset, VMStateRunning, true},
} {
t.Run(test.name, func(t *testing.T) {
err := confirmPowerTargetState(test.action, test.state)
if test.wantOK && err != nil {
t.Fatalf("confirmed target rejected: %v", err)
}
if !test.wantOK && !operation.IsUncertain(err) {
t.Fatalf("unconfirmed target error = %v", err)
}
})
}
}
func TestPowerActionsThatCanExposeNetworkingRequirePublicGuard(t *testing.T) {
for _, action := range []PowerAction{PowerStart, PowerReboot, PowerReset} {
if !powerActionRequiresPublicGuard(action) {
t.Fatalf("%s can make a public interface active without requiring the guard", action)
}
}
for _, action := range []PowerAction{PowerShutdown, PowerDestroy} {
if powerActionRequiresPublicGuard(action) {
t.Fatalf("%s was unnecessarily blocked by public guard readiness", action)
}
}
}
func TestExitRescueForceStopsWithoutGracefulShutdown(t *testing.T) {
t.Run("force stop succeeds", func(t *testing.T) {
domain := &fakeDeleteDomainControl{active: []bool{false}}
if err := forceStopRescueForExit(domain); err != nil {
t.Fatalf("force stop rescue before exit: %v", err)
}
if domain.destroyCalls != 1 || domain.shutdownCalls != 0 {
t.Fatalf("exit rescue shutdown=%d destroy=%d, want shutdown=0 destroy=1",
domain.shutdownCalls, domain.destroyCalls)
}
})
t.Run("lost destroy response confirmed inactive succeeds", func(t *testing.T) {
cause := errors.New("destroy response lost")
domain := &fakeDeleteDomainControl{
active: []bool{false},
destroyErr: cause,
}
if err := forceStopRescueForExit(domain); err != nil {
t.Fatalf("confirmed force stop should succeed: %v", err)
}
})
t.Run("still active remains uncertain", func(t *testing.T) {
domain := &fakeDeleteDomainControl{active: []bool{true}}
err := forceStopRescueForExit(domain)
if !operation.IsUncertain(err) || domain.destroyCalls != 1 || domain.shutdownCalls != 0 {
t.Fatalf("unconfirmed force stop err=%v shutdown=%d destroy=%d",
err, domain.shutdownCalls, domain.destroyCalls)
}
})
}
func TestServiceMutationAndTargetObservationRemainReconciliable(t *testing.T) {
cause := errors.New("libvirt response lost")
for _, enabled := range []bool{false, true} {
domain := &fakeServiceStateDomainControl{setErr: cause}
err := setServiceAutostart(domain, enabled)
if domain.setValue != enabled || !operation.IsUncertain(err) ||
!errors.Is(err, cause) {
t.Fatalf("set autostart enabled=%v err=%v", enabled, err)
}
}
for _, test := range []struct {
name, wantCode string
domain fakeServiceStateDomainControl
enabled, startIfDown bool
wantActive bool
}{
{name: "enabled-running", domain: fakeServiceStateDomainControl{autostart: true, active: true}, enabled: true, startIfDown: true, wantActive: true},
{name: "enabled-stopped-allowed", domain: fakeServiceStateDomainControl{autostart: true}, enabled: true},
{name: "disabled-stopped", domain: fakeServiceStateDomainControl{}, enabled: false},
{name: "autostart-mismatch", wantCode: "uncertain", domain: fakeServiceStateDomainControl{}, enabled: true},
{name: "required-start-missing", wantCode: "uncertain", domain: fakeServiceStateDomainControl{autostart: true}, enabled: true, startIfDown: true},
{name: "disabled-still-active", wantCode: "uncertain", domain: fakeServiceStateDomainControl{active: true}},
{name: "autostart-read-error", wantCode: "uncertain", domain: fakeServiceStateDomainControl{autostartErr: cause}},
{name: "activity-read-error", wantCode: "uncertain", domain: fakeServiceStateDomainControl{activeErr: cause}},
} {
t.Run(test.name, func(t *testing.T) {
active, err := confirmServiceTargetState(
&test.domain, test.enabled, test.startIfDown,
)
if test.wantCode == "" {
if err != nil || active != test.wantActive {
t.Fatalf("target state active=%v err=%v", active, err)
}
return
}
if !operation.IsUncertain(err) {
t.Fatalf("unconfirmed target state error = %v", err)
}
})
}
}
func (f *fakeDeleteDomainControl) IsActive() (bool, error) {
var active bool
if len(f.active) != 0 {
active = f.active[0]
f.active = f.active[1:]
}
var err error
if len(f.activeErrors) != 0 {
err = f.activeErrors[0]
f.activeErrors = f.activeErrors[1:]
}
return active, err
}
func (f *fakeDeleteDomainControl) DestroyFlags(libvirt.DomainDestroyFlags) error {
f.destroyCalls++
return f.destroyErr
}
func (f *fakeDeleteDomainControl) ShutdownFlags(libvirt.DomainShutdownFlags) error {
f.shutdownCalls++
return f.shutdownErr
}
func (f *fakeDeleteDomainControl) UndefineFlags(flags libvirt.DomainUndefineFlagsValues) error {
f.undefine = append(f.undefine, flags)
return f.undefineErr
}
func TestMissingDeleteDomainUsesSharedCleanupAndAbsenceResult(t *testing.T) {
for _, test := range []struct {
name string
lookupErr error
result DeleteVMResult
cleanupErr error
wantCalled bool
wantHandle bool
}{
{
name: "residual artifacts are cleaned", lookupErr: NewError(ErrNotFound, "vm_not_found", "virtual machine was not found", nil),
result: DeleteVMResult{Deleted: true, ManagedArtifactsAbsent: true}, wantCalled: true, wantHandle: true,
},
{
name: "fully absent delete is idempotent", lookupErr: ErrNotFound,
result: DeleteVMResult{Deleted: true, ManagedArtifactsAbsent: true, Forced: true}, wantCalled: true, wantHandle: true,
},
{
name: "cleanup failure remains uncertain", lookupErr: ErrNotFound,
cleanupErr: operation.MarkUncertain(errors.New("cleanup incomplete")), wantCalled: true, wantHandle: true,
},
{
name: "other lookup error is preserved by caller", lookupErr: NewError(ErrUnavailable, "libvirt_unavailable", "libvirt unavailable", nil),
},
} {
t.Run(test.name, func(t *testing.T) {
called := false
result, handled, err := completeDeleteForMissingDomain(test.lookupErr,
func() (DeleteVMResult, error) {
called = true
return test.result, test.cleanupErr
})
if called != test.wantCalled || handled != test.wantHandle ||
!reflect.DeepEqual(result, test.result) || !errors.Is(err, test.cleanupErr) {
t.Fatalf("missing delete result=%#v handled=%v called=%v err=%v", result, handled, called, err)
}
if test.cleanupErr != nil && !operation.IsUncertain(err) {
t.Fatalf("cleanup failure lost uncertainty: %v", err)
}
})
}
}
func TestDeleteDomainStagesNeverUpgradeGracefulRequestToForce(t *testing.T) {
t.Run("graceful shutdown observed inactive continues without destroy", func(t *testing.T) {
domain := &fakeDeleteDomainControl{
active: []bool{true, false, false},
}
if err := driveGracefulDeleteDomainToAbsent(context.Background(), domain, time.Second,
nil,
func(context.Context) (bool, error) { return true, nil }); err != nil {
t.Fatalf("continue delete after graceful stop: %v", err)
}
if domain.shutdownCalls != 1 || domain.destroyCalls != 0 || len(domain.undefine) != 1 {
t.Fatalf("delete stages shutdown=%d destroy=%d undefine=%d",
domain.shutdownCalls, domain.destroyCalls, len(domain.undefine))
}
wantFlags := libvirt.DOMAIN_UNDEFINE_NVRAM |
libvirt.DOMAIN_UNDEFINE_MANAGED_SAVE |
libvirt.DOMAIN_UNDEFINE_SNAPSHOTS_METADATA |
libvirt.DOMAIN_UNDEFINE_CHECKPOINTS_METADATA
if domain.undefine[0] != wantFlags {
t.Fatalf("undefine flags = %v, want %v", domain.undefine[0], wantFlags)
}
})
t.Run("graceful timeout requires explicit force and never destroys", func(t *testing.T) {
domain := &fakeDeleteDomainControl{
active: []bool{true, true, true},
}
err := driveGracefulDeleteDomainToAbsent(context.Background(), domain, time.Nanosecond,
nil,
func(context.Context) (bool, error) { return false, nil })
var backend *Error
if !errors.As(err, &backend) || backend.Code != "force_required" || domain.destroyCalls != 0 || len(domain.undefine) != 0 {
t.Fatalf("graceful timeout err=%v destroy=%d undefine=%d", err, domain.destroyCalls, len(domain.undefine))
}
})
t.Run("service suspension uses the shared graceful-only stop", func(t *testing.T) {
domain := &fakeDeleteDomainControl{active: []bool{true, true, true}}
err := requestGracefulDomainShutoff(context.Background(), domain, time.Nanosecond)
var backend *Error
if !errors.As(err, &backend) || backend.Code != "force_required" ||
domain.shutdownCalls != 1 || domain.destroyCalls != 0 || len(domain.undefine) != 0 {
t.Fatalf("graceful service stop err=%v shutdown=%d destroy=%d undefine=%d",
err, domain.shutdownCalls, domain.destroyCalls, len(domain.undefine))
}
})
t.Run("ambiguous graceful request stays uncertain and never destroys", func(t *testing.T) {
domain := &fakeDeleteDomainControl{
active: []bool{true, true},
activeErrors: []error{nil, errors.New("activity response lost")},
shutdownErr: errors.New("shutdown response lost"),
}
err := driveGracefulDeleteDomainToAbsent(context.Background(), domain, time.Second,
nil,
func(context.Context) (bool, error) { return false, nil })
var backend *Error
if !operation.IsUncertain(err) || !errors.As(err, &backend) ||
backend.Code != "graceful_shutdown_ambiguous" ||
domain.destroyCalls != 0 || len(domain.undefine) != 0 {
t.Fatalf("ambiguous graceful request err=%v destroy=%d undefine=%d", err, domain.destroyCalls, len(domain.undefine))
}
})
t.Run("failed graceful response still observes a guest that stops", func(t *testing.T) {
domain := &fakeDeleteDomainControl{
active: []bool{true, true, false, false}, shutdownErr: errors.New("shutdown response lost"),
}
err := driveGracefulDeleteDomainToAbsent(context.Background(), domain, time.Second,
nil, func(context.Context) (bool, error) { return true, nil })
if err != nil || domain.shutdownCalls != 1 || domain.destroyCalls != 0 || len(domain.undefine) != 1 {
t.Fatalf("late graceful stop err=%v shutdown=%d destroy=%d undefine=%d",
err, domain.shutdownCalls, domain.destroyCalls, len(domain.undefine))
}
})
t.Run("failed graceful response never authorizes force", func(t *testing.T) {
domain := &fakeDeleteDomainControl{
active: []bool{true, true, true, true}, shutdownErr: errors.New("shutdown response lost"),
}
err := driveGracefulDeleteDomainToAbsent(context.Background(), domain, time.Nanosecond,
nil, func(context.Context) (bool, error) { return false, nil })
var backend *Error
if operation.IsUncertain(err) || !errors.As(err, &backend) ||
backend.Code != "graceful_shutdown_request_failed" ||
domain.destroyCalls != 0 || len(domain.undefine) != 0 {
t.Fatalf("failed graceful request err=%v destroy=%d undefine=%d",
err, domain.destroyCalls, len(domain.undefine))
}
})
t.Run("cancelled request still records the bounded graceful result", func(t *testing.T) {
ctx, cancel := context.WithCancel(context.Background())
cancel()
domain := &fakeDeleteDomainControl{active: []bool{true, true, false}}
err := requestGracefulDomainShutoff(ctx, domain, 10*time.Millisecond)
if err != nil || domain.shutdownCalls != 1 || domain.destroyCalls != 0 {
t.Fatalf("late graceful result err=%v shutdown=%d destroy=%d",
err, domain.shutdownCalls, domain.destroyCalls)
}
})
t.Run("cancelled request cannot abort evidence and undefine tail", func(t *testing.T) {
ctx, cancel := context.WithCancel(context.Background())
cancel()
domain := &fakeDeleteDomainControl{active: []bool{true, false, false}}
beforeCalled := false
absenceCalled := false
err := driveGracefulDeleteDomainToAbsent(ctx, domain, time.Second,
func(completionCtx context.Context) error {
beforeCalled = true
if completionCtx.Err() != nil {
t.Fatalf("completion evidence inherited request cancellation: %v", completionCtx.Err())
}
return nil
}, func(completionCtx context.Context) (bool, error) {
absenceCalled = true
if completionCtx.Err() != nil {
t.Fatalf("absence check inherited request cancellation: %v", completionCtx.Err())
}
return true, nil
})
if err != nil || !beforeCalled || !absenceCalled ||
domain.shutdownCalls != 1 || len(domain.undefine) != 1 {
t.Fatalf("detached graceful tail err=%v before=%v absent=%v shutdown=%d undefine=%d",
err, beforeCalled, absenceCalled, domain.shutdownCalls, len(domain.undefine))
}
})
t.Run("initially inactive restart receives a fresh graceful request", func(t *testing.T) {
domain := &fakeDeleteDomainControl{
active: []bool{false, true, true, false, false},
}
err := driveGracefulDeleteDomainToAbsent(context.Background(), domain,
time.Second, nil, func(context.Context) (bool, error) { return true, nil })
if err != nil || domain.shutdownCalls != 1 || domain.destroyCalls != 0 ||
len(domain.undefine) != 1 {
t.Fatalf("reactivated graceful delete err=%v shutdown=%d destroy=%d undefine=%d",
err, domain.shutdownCalls, domain.destroyCalls, len(domain.undefine))
}
})
t.Run("restart after graceful stop never fabricates force authorization", func(t *testing.T) {
domain := &fakeDeleteDomainControl{
active: []bool{false, true, true, false, true},
}
err := driveGracefulDeleteDomainToAbsent(context.Background(), domain,
time.Second, nil, func(context.Context) (bool, error) { return false, nil })
var backend *Error
if !operation.IsUncertain(err) || !errors.As(err, &backend) ||
backend.Code != "graceful_delete_domain_reactivated" ||
domain.shutdownCalls != 1 || domain.destroyCalls != 0 || len(domain.undefine) != 0 {
t.Fatalf("second restart race err=%v shutdown=%d destroy=%d undefine=%d",
err, domain.shutdownCalls, domain.destroyCalls, len(domain.undefine))
}
})
t.Run("explicit force error but observed inactive continues", func(t *testing.T) {
domain := &fakeDeleteDomainControl{
active: []bool{true, false, false}, destroyErr: errors.New("destroy response lost"),
}
if err := driveForceDeleteDomainToAbsent(domain, nil, func() (bool, error) { return true, nil }); err != nil {
t.Fatalf("continue explicit force after observed stop: %v", err)
}
if domain.shutdownCalls != 0 || domain.destroyCalls != 1 || len(domain.undefine) != 1 {
t.Fatalf("force stages shutdown=%d destroy=%d undefine=%d",
domain.shutdownCalls, domain.destroyCalls, len(domain.undefine))
}
})
t.Run("restart between force and undefine retains the explicit force operation", func(t *testing.T) {
domain := &fakeDeleteDomainControl{active: []bool{true, false, true}}
err := driveForceDeleteDomainToAbsent(domain, nil, func() (bool, error) { return false, nil })
var backend *Error
if !operation.IsUncertain(err) || !errors.As(err, &backend) || backend.Code != "force_delete_domain_reactivated" ||
domain.destroyCalls != 1 || len(domain.undefine) != 0 {
t.Fatalf("force restart race err=%v destroy=%d undefine=%d", err, domain.destroyCalls, len(domain.undefine))
}
})
t.Run("post-destroy evidence failure remains uncertain", func(t *testing.T) {
domain := &fakeDeleteDomainControl{active: []bool{true, false}}
err := driveForceDeleteDomainToAbsent(domain, func() error {
return NewError(ErrUnavailable, "delete_evidence_unavailable", "delete evidence is unavailable", nil)
}, func() (bool, error) { return false, nil })
if !operation.IsUncertain(err) || domain.destroyCalls != 1 || len(domain.undefine) != 0 {
t.Fatalf("post-destroy evidence error=%v destroy=%d undefine=%d", err, domain.destroyCalls, len(domain.undefine))
}
})
t.Run("automatic force reconciliation observes active without replay", func(t *testing.T) {
domain := &fakeDeleteDomainControl{active: []bool{true}}
completed, err := continueForceDeleteReconciliation(domain, nil, func() (bool, error) { return false, nil })
if err != nil || completed || domain.destroyCalls != 0 || domain.shutdownCalls != 0 || len(domain.undefine) != 0 {
t.Fatalf("active force reconcile completed=%v err=%v shutdown=%d destroy=%d undefine=%d",
completed, err, domain.shutdownCalls, domain.destroyCalls, len(domain.undefine))
}
})
t.Run("automatic force reconciliation may continue only when inactive", func(t *testing.T) {
domain := &fakeDeleteDomainControl{active: []bool{false, false}}
completed, err := continueForceDeleteReconciliation(domain, nil, func() (bool, error) { return true, nil })
if err != nil || !completed || domain.destroyCalls != 0 || domain.shutdownCalls != 0 || len(domain.undefine) != 1 {
t.Fatalf("inactive force reconcile completed=%v err=%v shutdown=%d destroy=%d undefine=%d",
completed, err, domain.shutdownCalls, domain.destroyCalls, len(domain.undefine))
}
})
t.Run("automatic force reconciliation race remains uncertain without replay", func(t *testing.T) {
domain := &fakeDeleteDomainControl{active: []bool{false, true}}
completed, err := continueForceDeleteReconciliation(domain, nil, func() (bool, error) { return false, nil })
var backend *Error
if completed || !operation.IsUncertain(err) || !errors.As(err, &backend) || backend.Code != "force_delete_domain_reactivated" ||
domain.destroyCalls != 0 || len(domain.undefine) != 0 {
t.Fatalf("force reconcile race completed=%v err=%v destroy=%d undefine=%d",
completed, err, domain.destroyCalls, len(domain.undefine))
}
})
t.Run("undefine error stays uncertain despite domain absence", func(t *testing.T) {
domain := &fakeDeleteDomainControl{
active: []bool{false}, undefineErr: errors.New("undefine failed"),
}
err := driveInactiveDeleteDomainToAbsent(domain, func() (bool, error) { return true, nil })
if !operation.IsUncertain(err) || domain.destroyCalls != 0 || domain.shutdownCalls != 0 || len(domain.undefine) != 1 {
t.Fatalf("ambiguous undefine outcome err=%v shutdown=%d destroy=%d undefine=%d",
err, domain.shutdownCalls, domain.destroyCalls, len(domain.undefine))
}
})
t.Run("successful undefine must still prove absence", func(t *testing.T) {
domain := &fakeDeleteDomainControl{active: []bool{false}}
err := driveInactiveDeleteDomainToAbsent(domain, func() (bool, error) { return false, nil })
if !operation.IsUncertain(err) || len(domain.undefine) != 1 {
t.Fatalf("unconfirmed successful undefine err=%v undefine=%d", err, len(domain.undefine))
}
})
t.Run("reconcile continuation refuses an active domain without force", func(t *testing.T) {
domain := &fakeDeleteDomainControl{active: []bool{true}}
err := driveInactiveDeleteDomainToAbsent(domain, func() (bool, error) { return false, nil })
var backend *Error
if !errors.As(err, &backend) || backend.Code != "force_required" ||
domain.shutdownCalls != 0 || domain.destroyCalls != 0 || len(domain.undefine) != 0 {
t.Fatalf("active reconcile continuation err=%v shutdown=%d destroy=%d undefine=%d",
err, domain.shutdownCalls, domain.destroyCalls, len(domain.undefine))
}
})
}
func TestManagedDeleteFilterEvidenceRejectsForeignDefinitions(t *testing.T) {
name := "virt-123e4567-e89b-42d3-a456-426614174000"
document, err := BuildPlatformFilterXML(name, dualStackPublicNetwork(), testPlatformPolicy())
if err != nil {
t.Fatal(err)
}
if !managedFilterDocument(document, name) {
t.Fatal("managed filter evidence was rejected")
}
for label, document := range map[string]string{
"wrong name": strings.Replace(document, `name="`+name+`"`, `name="virt-foreign"`, 1),
"wrong chain": strings.Replace(document, `chain="root"`, `chain="ipv4"`, 1),
"missing baseline": strings.Replace(document,
`<mac match="no" srcmacaddr="$MAC" comment="block-spoofed-mac"></mac>`, `<mac comment="block-spoofed-mac"></mac>`, 1),
"foreign reference": strings.Replace(document, "</filter>", `<filterref filter="foreign"></filterref></filter>`, 1),
} {
t.Run(label, func(t *testing.T) {
if managedFilterDocument(document, name) {
t.Fatal("foreign filter definition was accepted as panel managed")
}
})
}
}
func TestCreateTrafficBaselineCleanupFailureRemainsUncertain(t *testing.T) {
createErr := errors.New("domain definition failed")
cleanupErr := errors.New("traffic state persistence failed")
err := markTrafficCleanupUncertain(createErr, cleanupErr)
if !operation.IsUncertain(err) || !errors.Is(err, createErr) ||
!errors.Is(err, cleanupErr) {
t.Fatalf("traffic cleanup error lost uncertainty or causes: %v", err)
}
}
func TestDomainAbsenceEvidenceRequiresNoDomainCode(t *testing.T) {
if !isNoDomain(libvirt.Error{Code: libvirt.ERR_NO_DOMAIN}) {
t.Fatal("ERR_NO_DOMAIN was not accepted as absence evidence")
}
if isNoDomain(libvirt.Error{Code: libvirt.ERR_RPC}) || isNoDomain(errors.New("lookup failed")) {
t.Fatal("ambiguous domain lookup error was accepted as absence evidence")
}
if !domainMayExistAfterDefine(nil) ||
!domainMayExistAfterDefine(libvirt.Error{Code: libvirt.ERR_RPC}) ||
domainMayExistAfterDefine(libvirt.Error{Code: libvirt.ERR_NO_DOMAIN}) {
t.Fatal("domain definition crash-window evidence was classified unsafely")
}
}
func TestCreateMutationLibvirtErrorsOnlyMarkAmbiguousTransportFailuresUncertain(t *testing.T) {
for _, test := range []struct {
name string
cause error
uncertain bool
wantCode string
}{
{
name: "deterministic-invalid-request", cause: libvirt.Error{Code: libvirt.ERR_INVALID_ARG},
wantCode: "invalid_libvirt_request",
},
{
name: "deterministic-operation-denied", cause: libvirt.Error{Code: libvirt.ERR_OPERATION_DENIED},
wantCode: "libvirt_conflict",
},
{
name: "rpc-result-unknown", cause: libvirt.Error{Code: libvirt.ERR_RPC},
uncertain: true, wantCode: "libvirt_error",
},
{
name: "system-result-unknown", cause: libvirt.Error{Code: libvirt.ERR_SYSTEM_ERROR},
uncertain: true, wantCode: "libvirt_error",
},
} {
t.Run(test.name, func(t *testing.T) {
err := mutationLibvirtError("create VM artifact", test.cause)
if got := operation.IsUncertain(err); got != test.uncertain {
t.Fatalf("uncertain = %v, want %v: %v", got, test.uncertain, err)
}
if got := errorPublicCode(err); got != test.wantCode {
t.Fatalf("public code = %q, want %q: %v", got, test.wantCode, err)
}
})
}
}
func TestCreateVMStageErrorsExposeStableSafeCodes(t *testing.T) {
stages := []struct {
stage createVMFailureStage
code string
}{
{createVMTemplateLookupStage, "template_lookup_failed"},
{createVMTemplateCapacityStage, "template_capacity_failed"},
{createVMStorageCloneStage, "storage_clone_failed"},
{createVMSeedCreateStage, "seed_create_failed"},
{createVMSeedUploadStage, "seed_upload_failed"},
{createVMNWFilterDefineStage, "nwfilter_define_failed"},
{createVMDomainDefineStage, "domain_define_failed"},
{createVMDomainAutostartStage, "domain_autostart_failed"},
{createVMDomainStartStage, "domain_start_failed"},
}
seen := make(map[string]struct{}, len(stages))
for _, test := range stages {
t.Run(test.code, func(t *testing.T) {
cause := NewError(ErrInvalid, "internal_detail", "secret path /srv/private/image.qcow2", nil)
err := test.stage.wrap(cause)
if got := errorPublicCode(err); got != test.code {
t.Fatalf("public code = %q, want %q", got, test.code)
}
var public interface{ PublicMessage() string }
if !errors.As(err, &public) {
t.Fatalf("stage error has no public message: %v", err)
}
if got := public.PublicMessage(); got != test.stage.message {
t.Fatalf("public message = %q, want %q", got, test.stage.message)
}
if strings.Contains(err.Error(), "/srv/private") || strings.Contains(public.PublicMessage(), "/srv/private") {
t.Fatalf("stage error exposed its private cause: %v", err)
}
if !errors.Is(err, ErrInvalid) {
t.Fatalf("stage error lost backend classification: %v", err)
}
if _, duplicate := seen[test.code]; duplicate {
t.Fatalf("duplicate create stage code %q", test.code)
}
seen[test.code] = struct{}{}
})
}
}
func TestCreateVMStageErrorsPreserveArtifactUncertainty(t *testing.T) {
deterministic := createVMStorageCloneStage.wrap(deterministicArtifactCreateFailure(
"clone template volume", libvirt.Error{Code: libvirt.ERR_INVALID_ARG}, nil, false,
))
if operation.IsUncertain(deterministic) || errorPublicCode(deterministic) != "storage_clone_failed" {
t.Fatalf("confirmed-absent clone failure = %v", deterministic)
}
for _, test := range []struct {
name string
mutationErr error
inspectErr error
artifactFound bool
}{
{name: "artifact-observed", mutationErr: libvirt.Error{Code: libvirt.ERR_INVALID_ARG}, artifactFound: true},
{name: "absence-unknown", mutationErr: libvirt.Error{Code: libvirt.ERR_INVALID_ARG}, inspectErr: errors.New("inspection unavailable")},
{name: "transport-result-unknown", mutationErr: libvirt.Error{Code: libvirt.ERR_RPC}},
} {
t.Run(test.name, func(t *testing.T) {
err := createVMStorageCloneStage.wrap(deterministicArtifactCreateFailure(
"clone template volume", test.mutationErr, test.inspectErr, test.artifactFound,
))
if !operation.IsUncertain(err) {
t.Fatalf("ambiguous create failure lost uncertainty: %v", err)
}
if got := errorPublicCode(err); got != "storage_clone_failed" {
t.Fatalf("uncertain public code = %q, want storage_clone_failed", got)
}
})
}
}
func TestDirPoolVolumePathRequiresDirectSafeFilename(t *testing.T) {
valid := `<pool type="dir"><target><path>/var/lib/libvirt/images</path></target></pool>`
path, err := dirPoolVolumePath(valid, "vm-root.qcow2")
if err != nil || path != "/var/lib/libvirt/images/vm-root.qcow2" {
t.Fatalf("valid dir pool path = %q, %v", path, err)
}
for _, test := range []struct {
name string
document string
volume string
}{
{name: "non-dir", document: `<pool type="fs"><target><path>/mnt/images</path></target></pool>`, volume: "vm.qcow2"},
{name: "relative-target", document: `<pool type="dir"><target><path>images</path></target></pool>`, volume: "vm.qcow2"},
{name: "root-target", document: `<pool type="dir"><target><path>/</path></target></pool>`, volume: "vm.qcow2"},
{name: "nested-name", document: valid, volume: "nested/vm.qcow2"},
{name: "parent-name", document: valid, volume: "../vm.qcow2"},
{name: "malformed-xml", document: `<pool`, volume: "vm.qcow2"},
} {
t.Run(test.name, func(t *testing.T) {
if path, err := dirPoolVolumePath(test.document, test.volume); err == nil {
t.Fatalf("unsafe pool path was accepted: %q", path)
}
})
}
}
func TestProveDirPoolVolumeAbsentRequiresRefreshLookupAndLstat(t *testing.T) {
valid := `<pool type="dir"><target><path>/var/lib/libvirt/images</path></target></pool>`
var calls []string
probe := storagePoolVolumeAbsenceProbe{
refresh: func() error {
calls = append(calls, "refresh")
return nil
},
lookup: func() (bool, error) {
calls = append(calls, "lookup")
return false, nil
},
poolXML: func() (string, error) {
calls = append(calls, "xml")
return valid, nil
},
lstat: func(path string) (os.FileInfo, error) {
calls = append(calls, "lstat:"+path)
return nil, os.ErrNotExist
},
}
absent, err := proveDirPoolVolumeAbsent("vm-root.qcow2", probe)
if err != nil || !absent {
t.Fatalf("complete absence evidence = %v, %v", absent, err)
}
wantCalls := []string{"refresh", "lookup", "xml", "lstat:/var/lib/libvirt/images/vm-root.qcow2"}
if !reflect.DeepEqual(calls, wantCalls) {
t.Fatalf("absence evidence calls = %v, want %v", calls, wantCalls)
}
t.Run("refresh-failure-stops-inspection", func(t *testing.T) {
refreshErr := errors.New("refresh failed")
lookupCalled := false
_, err := proveDirPoolVolumeAbsent("vm-root.qcow2", storagePoolVolumeAbsenceProbe{
refresh: func() error { return refreshErr },
lookup: func() (bool, error) {
lookupCalled = true
return false, nil
},
poolXML: func() (string, error) { return valid, nil },
lstat: os.Lstat,
})
if !errors.Is(err, refreshErr) || lookupCalled {
t.Fatalf("refresh failure = %v, lookup called = %v", err, lookupCalled)
}
})
t.Run("refreshed-libvirt-object-is-present", func(t *testing.T) {
xmlCalled := false
absent, err := proveDirPoolVolumeAbsent("vm-root.qcow2", storagePoolVolumeAbsenceProbe{
refresh: func() error { return nil },
lookup: func() (bool, error) { return true, nil },
poolXML: func() (string, error) {
xmlCalled = true
return valid, nil
},
lstat: os.Lstat,
})
if err != nil || absent || xmlCalled {
t.Fatalf("present libvirt object = %v, %v; xml called = %v", absent, err, xmlCalled)
}
})
t.Run("lookup-failure-is-not-absence", func(t *testing.T) {
lookupErr := errors.New("lookup failed")
absent, err := proveDirPoolVolumeAbsent("vm-root.qcow2", storagePoolVolumeAbsenceProbe{
refresh: func() error { return nil },
lookup: func() (bool, error) { return false, lookupErr },
poolXML: func() (string, error) { return valid, nil },
lstat: os.Lstat,
})
if !errors.Is(err, lookupErr) || absent {
t.Fatalf("failed libvirt lookup = %v, %v", absent, err)
}
})
t.Run("pool-definition-failure-is-not-absence", func(t *testing.T) {
xmlErr := errors.New("pool XML unavailable")
absent, err := proveDirPoolVolumeAbsent("vm-root.qcow2", storagePoolVolumeAbsenceProbe{
refresh: func() error { return nil },
lookup: func() (bool, error) { return false, nil },
poolXML: func() (string, error) { return "", xmlErr },
lstat: os.Lstat,
})
if !errors.Is(err, xmlErr) || absent {
t.Fatalf("failed pool definition read = %v, %v", absent, err)
}
})
t.Run("lookup-miss-with-file-is-not-absence", func(t *testing.T) {
absent, err := proveDirPoolVolumeAbsent("vm-root.qcow2", storagePoolVolumeAbsenceProbe{
refresh: func() error { return nil },
lookup: func() (bool, error) { return false, nil },
poolXML: func() (string, error) { return valid, nil },
lstat: func(string) (os.FileInfo, error) { return nil, nil },
})
if err != nil || absent {
t.Fatalf("unlisted on-disk volume was treated absent: %v, %v", absent, err)
}
})
for _, test := range []struct {
name string
poolXML string
lstat func(string) (os.FileInfo, error)
}{
{
name: "unsupported-pool", poolXML: `<pool type="fs"><target><path>/mnt/images</path></target></pool>`,
lstat: os.Lstat,
},
{
name: "filesystem-inspection-failed", poolXML: valid,
lstat: func(string) (os.FileInfo, error) { return nil, os.ErrPermission },
},
} {
t.Run(test.name, func(t *testing.T) {
absent, err := proveDirPoolVolumeAbsent("vm-root.qcow2", storagePoolVolumeAbsenceProbe{
refresh: func() error { return nil },
lookup: func() (bool, error) { return false, nil },
poolXML: func() (string, error) { return test.poolXML, nil },
lstat: test.lstat,
})
if err == nil || absent {
t.Fatalf("incomplete absence evidence = %v, %v", absent, err)
}
})
}
}
func TestDeterministicVolumeCreateFailureRequiresReliableAbsence(t *testing.T) {
cause := errors.New("clone rejected")
if err := deterministicVolumeCreateFailure("clone", cause, nil, false); operation.IsUncertain(err) {
t.Fatalf("reliably absent clone was left uncertain: %v", err)
}
for _, test := range []struct {
name string
mutationErr error
inspectErr error
artifactFound bool
}{
{name: "artifact-present", mutationErr: cause, artifactFound: true},
{name: "inspection-failed", mutationErr: cause, inspectErr: errors.New("lookup failed")},
{name: "rpc-result-lost", mutationErr: libvirt.Error{Code: libvirt.ERR_RPC}},
} {
t.Run(test.name, func(t *testing.T) {
err := deterministicVolumeCreateFailure(
"clone", test.mutationErr, test.inspectErr, test.artifactFound,
)
if !operation.IsUncertain(err) {
t.Fatalf("ambiguous clone failure = %v", err)
}
})
}
}
func TestSnapshotDeleteNotFoundRequiresMetadataAndVolumeAbsent(t *testing.T) {
absent := validateSnapshotDeletePresence(false, false)
if !errors.Is(absent, ErrNotFound) || errorPublicCode(absent) != "snapshot_not_found" {
t.Fatalf("double-absence snapshot error = %v", absent)
}
orphaned := validateSnapshotDeletePresence(false, true)
if !errors.Is(orphaned, ErrConflict) || errors.Is(orphaned, ErrNotFound) ||
errorPublicCode(orphaned) != "snapshot_artifacts_inconsistent" {
t.Fatalf("orphaned snapshot error = %v", orphaned)
}
for _, presence := range [][2]bool{{true, false}, {true, true}} {
if err := validateSnapshotDeletePresence(presence[0], presence[1]); err != nil {
t.Fatalf("deletable snapshot presence %v rejected: %v", presence, err)
}
}
}
func TestSnapshotPreconditionFailureIsExplicitlyNoEffect(t *testing.T) {
err := snapshotInUsePrecondition("snapshot volume is referenced by a domain")
if operation.IsUncertain(err) {
t.Fatalf("precondition failure became uncertain: %v", err)
}
var semantics interface{ FailureSemantics() (bool, bool) }
if !errors.As(err, &semantics) {
t.Fatalf("precondition failure has no explicit semantics: %T", err)
}
effect, safe := semantics.FailureSemantics()
if effect || !safe || errorPublicCode(err) != "snapshot_in_use" {
t.Fatalf("precondition semantics effect=%v safe=%v code=%q", effect, safe, errorPublicCode(err))
}
uncertain := operation.MarkUncertain(errors.New("mutation result unknown"))
if got := snapshotPreconditionFailure(uncertain); !operation.IsUncertain(got) {
t.Fatalf("uncertain failure was downgraded: %v", got)
}
}
func TestSnapshotPreconditionReleasesJournalAdmissionButPostMutationUnknownBlocks(t *testing.T) {
newJournal := func(t *testing.T) *operation.Journal {
t.Helper()
journal, err := operation.Open(filepath.Join(t.TempDir(), "operations.db"))
if err != nil {
t.Fatal(err)
}
t.Cleanup(func() { _ = journal.Close() })
return journal
}
newSpec := func(id, resource string) operation.Spec {
return operation.Spec{
OperationID: id, ResourceID: resource,
Action: "delete-snapshot:0123456789abcdef0123456789abcdef",
Fingerprint: operation.Fingerprint("DELETE", "/v1/vms/"+resource+"/snapshots/snapshot-a", []byte(`{}`)),
Destructive: true,
}
}
t.Run("precondition", func(t *testing.T) {
journal := newJournal(t)
resource := "123e4567-e89b-42d3-a456-426614174000"
first := newSpec("operation-snapshot-in-use", resource)
record, replayed, err := journal.Execute(context.Background(), first, func(context.Context) (json.RawMessage, error) {
return nil, snapshotInUsePrecondition("snapshot volume is referenced by a domain")
})
if err == nil || replayed || record.EffectMayHaveApplied || !record.SafeToResubmit {
t.Fatalf("snapshot precondition record = %#v replayed=%v err=%v", record, replayed, err)
}
second := newSpec("operation-snapshot-after-precondition", resource)
if next, replayed, err := journal.Execute(context.Background(), second, func(context.Context) (json.RawMessage, error) {
return json.RawMessage(`{"deleted":true}`), nil
}); err != nil || replayed || next.State != operation.StateSucceeded {
t.Fatalf("successor after precondition = %#v replayed=%v err=%v", next, replayed, err)
}
})
t.Run("post-mutation-unknown", func(t *testing.T) {
journal := newJournal(t)
resource := "123e4567-e89b-42d3-a456-426614174001"
first := newSpec("operation-snapshot-mutated", resource)
record, _, err := journal.Execute(context.Background(), first, func(context.Context) (json.RawMessage, error) {
return nil, operation.MarkUncertain(errors.New("volume deletion result unknown"))
})
if err == nil || !record.EffectMayHaveApplied || record.SafeToResubmit {
t.Fatalf("post-mutation record = %#v err=%v", record, err)
}
second := newSpec("operation-snapshot-after-unknown", resource)
called := false
if _, _, err := journal.Execute(context.Background(), second, func(context.Context) (json.RawMessage, error) {
called = true
return nil, nil
}); !errors.Is(err, operation.ErrResourceBlocked) || called {
t.Fatalf("successor after unknown called=%v err=%v", called, err)
}
})
}
func TestNVRAMCapabilityChecksExistenceWithoutAgentWriteProbe(t *testing.T) {
directory := filepath.Join(t.TempDir(), "nvram")
if err := os.Mkdir(directory, 0o500); err != nil {
t.Fatal(err)
}
if check := probeExistingDirectory(directory); !check.Available {
t.Fatalf("existing NVRAM directory rejected: %#v", check)
}
regular := filepath.Join(t.TempDir(), "not-a-directory")
if err := os.WriteFile(regular, []byte("x"), 0o600); err != nil {
t.Fatal(err)
}
if check := probeExistingDirectory(regular); check.Available || check.Reason != capabilityInvalid {
t.Fatalf("regular file accepted as NVRAM directory: %#v", check)
}
}
func TestDeleteNVRAMEvidenceRequiresExactUUIDOwnedPath(t *testing.T) {
uuid := "123e4567-e89b-42d3-a456-426614174000"
directory := "/var/lib/libvirt/qemu/nvram"
definition := &libvirtxml.Domain{OS: &libvirtxml.DomainOS{NVRam: &libvirtxml.DomainNVRam{
NVRam: filepath.Join(directory, uuid+"_VARS.fd"),
}}}
paths, err := validateManagedDomainNVRAMPaths(definition, directory, uuid)
if err != nil || len(paths) != 1 || paths[0] != filepath.Join(directory, uuid+"_VARS.fd") {
t.Fatalf("valid managed NVRAM evidence paths=%v err=%v", paths, err)
}
definition.OS.NVRam.NVRam = filepath.Join(directory, "another-domain_VARS.fd")
if _, err := validateManagedDomainNVRAMPaths(definition, directory, uuid); err == nil {
t.Fatal("foreign NVRAM path was accepted for delete")
}
definition.OS.NVRam = nil
if _, err := validateManagedDomainNVRAMPaths(definition, directory, uuid); err == nil {
t.Fatal("missing NVRAM path was accepted for delete")
}
}
func TestDeleteSuccessRequiresDomainArtifactsAndTrafficAbsence(t *testing.T) {
for _, test := range []struct {
name string
domainAbsent, artifactsAbsent bool
trafficPresent, forced, wantResult bool
}{
{name: "graceful-complete", domainAbsent: true, artifactsAbsent: true, wantResult: true},
{name: "force-complete", domainAbsent: true, artifactsAbsent: true, forced: true, wantResult: true},
{name: "domain-remains", artifactsAbsent: true},
{name: "artifact-remains", domainAbsent: true},
{name: "traffic-remains", domainAbsent: true, artifactsAbsent: true, trafficPresent: true},
} {
t.Run(test.name, func(t *testing.T) {
result, err := confirmDeletedVMAbsence(test.domainAbsent, test.artifactsAbsent, test.trafficPresent, test.forced)
if test.wantResult {
if err != nil || !result.Deleted || !result.ManagedArtifactsAbsent || result.Forced != test.forced {
t.Fatalf("confirmed delete result=%#v err=%v", result, err)
}
return
}
if !operation.IsUncertain(err) || result.Deleted || result.ManagedArtifactsAbsent {
t.Fatalf("incomplete delete result=%#v err=%v", result, err)
}
})
}
}
func TestDomainFilterReferenceEvidenceIncludesInactiveDefinitions(t *testing.T) {
filterName := "virt-cloud-123e4567-e89b-42d3-a456-426614174000"
definition := &libvirtxml.Domain{Devices: &libvirtxml.DomainDeviceList{
Interfaces: []libvirtxml.DomainInterface{{FilterRef: &libvirtxml.DomainInterfaceFilterRef{Filter: filterName}}},
}}
if !domainReferencesFilter(definition, filterName) {
t.Fatal("matching network filter reference was not detected")
}
if domainReferencesFilter(definition, "another-filter") {
t.Fatal("unrelated network filter reference was reported")
}
historical := &libvirtxml.DomainSnapshot{
InactiveDomain: &libvirtxml.DomainSnapshotInactiveDomain{Domain: *definition},
}
if !domainSnapshotReferencesFilter(historical, filterName) {
t.Fatal("historical snapshot network filter reference was ignored")
}
if domainSnapshotReferencesFilter(historical, "another-filter") {
t.Fatal("unrelated historical snapshot network filter reference was reported")
}
managedSave, err := parseHistoricalDomainXML(`<domain><devices><interface type="bridge">`+
`<filterref filter="`+filterName+`"/><source bridge="br0"/>`+
`</interface><disk type="file"><source file="/srv/managed/shared.qcow2"/>`+
`</disk></devices></domain>`, "managed_save")
if err != nil || !domainReferencesFilter(managedSave, filterName) ||
!domainReferencesPath(managedSave, "/srv/managed/shared.qcow2") {
t.Fatalf("managed-save resource references were ignored: %v", err)
}
}
func TestNetworkFilterDependencyEvidenceProtectsReferencedChild(t *testing.T) {
target := "virt-cloud-123e4567-e89b-42d3-a456-426614174000"
parent := "tenant-parent-filter"
var definition libvirtxml.NWFilter
if err := definition.Unmarshal(`<filter name="` + parent +
`"><filterref filter="clean-traffic"/><filterref filter="` + target +
`"/></filter>`); err != nil {
t.Fatal(err)
}
if !networkFilterReferencesFilter(&definition, target) {
t.Fatal("parent network filter dependency on managed child was ignored")
}
if networkFilterReferencesFilter(&definition, "unrelated-filter") {
t.Fatal("unrelated network filter dependency was reported")
}
domain := &libvirtxml.Domain{Devices: &libvirtxml.DomainDeviceList{
Interfaces: []libvirtxml.DomainInterface{{
FilterRef: &libvirtxml.DomainInterfaceFilterRef{Filter: parent},
}},
}}
if !domainReferencesFilter(domain, parent) {
t.Fatal("domain reference to the parent network filter was ignored")
}
}
func TestSnapshotCapabilityRequiresDirectFileBackedPool(t *testing.T) {
for _, test := range []struct {
name, document string
want bool
}{
{"directory", `<pool type="dir"><target><path>/var/lib/libvirt/images</path></target></pool>`, true},
{"filesystem", `<pool type="fs"><target><path>/srv/vms</path></target></pool>`, true},
{"network-filesystem", `<pool type="netfs"><target><path>/mnt/vms</path></target></pool>`, true},
{"logical", `<pool type="logical"><target><path>/dev/vg0</path></target></pool>`, false},
{"rbd", `<pool type="rbd"><source><name>vms</name></source></pool>`, false},
{"relative", `<pool type="dir"><target><path>images</path></target></pool>`, false},
{"root", `<pool type="dir"><target><path>/</path></target></pool>`, false},
{"broken", `<pool`, false},
} {
t.Run(test.name, func(t *testing.T) {
if got := snapshotStoragePoolXMLSupported(test.document); got != test.want {
t.Fatalf("snapshot pool support = %v, want %v", got, test.want)
}
})
}
}
func TestNWFilterCapabilityRequiresReadableCleanTrafficBaseline(t *testing.T) {
document, err := BuildManagedVPCAntiSpoofFilterXML()
if err != nil {
t.Fatal(err)
}
if !cleanTrafficFilterXMLValid(document) {
t.Fatal("valid project-owned VPC anti-spoof filter was rejected")
}
for _, document := range []string{
`<filter name="virt-cloud-vpc-antispoof-v1" chain="root"/>`,
`<filter name="virt-cloud-vpc-antispoof-v1" chain="root"><filterref filter="clean-traffic"/></filter>`,
`<filter name="virt-cloud-vpc-antispoof-v1" chain="root"><rule action="accept" direction="out" priority="-999"><all/></rule></filter>`,
`<filter name="other" chain="root"/>`, `<filter`, ``,
} {
if cleanTrafficFilterXMLValid(document) {
t.Fatalf("invalid VPC anti-spoof filter accepted: %q", document)
}
}
var exact libvirtxml.NWFilter
if err := exact.Unmarshal(document); err != nil {
t.Fatal(err)
}
missing := exact
missing.Entries = append([]libvirtxml.NWFilterEntry(nil), exact.Entries[1:]...)
missingDocument, err := missing.Marshal()
if err != nil {
t.Fatal(err)
}
if cleanTrafficFilterXMLValid(missingDocument) {
t.Fatal("VPC anti-spoof filter with a missing MAC guard was accepted")
}
earlyAccept := exact
earlyAccept.Entries = append([]libvirtxml.NWFilterEntry{{Rule: &libvirtxml.NWFilterRule{
Action: "accept", Direction: "out", Priority: -999, All: &libvirtxml.NWFilterRuleAll{},
}}}, exact.Entries...)
earlyDocument, err := earlyAccept.Marshal()
if err != nil {
t.Fatal(err)
}
if cleanTrafficFilterXMLValid(earlyDocument) {
t.Fatal("VPC anti-spoof filter with an early accept was accepted")
}
}
func TestVPCCapabilityRequiresIndependentCleanTrafficEvidence(t *testing.T) {
ready := &VPCL3Capability{Version: 3, ApplySupported: true, PortSupported: true}
if !applyVPCCleanTrafficGate(ready, availableCheck()) {
t.Fatal("valid VPC clean-traffic evidence did not preserve readiness")
}
missing := &VPCL3Capability{Version: 3, ApplySupported: true, PortSupported: true}
if applyVPCCleanTrafficGate(missing, unavailableCheck(capabilityMissing)) ||
missing.ApplySupported || missing.PortSupported {
t.Fatalf("VPC readiness escaped without clean-traffic evidence: %#v", missing)
}
}
func TestManagedNVRAMArtifactMustBeAbsentBeforeCreateFailureIsConfirmed(t *testing.T) {
directory := t.TempDir()
uuid := "123e4567-e89b-42d3-a456-426614174000"
absent, err := managedNVRAMArtifactAbsent(directory, uuid)
if err != nil || !absent {
t.Fatalf("empty NVRAM directory absent=%v err=%v", absent, err)
}
path := filepath.Join(directory, uuid+"_VARS.fd")
if err := os.WriteFile(path, []byte("partial firmware state"), 0o600); err != nil {
t.Fatal(err)
}
absent, err = managedNVRAMArtifactAbsent(directory, strings.ToUpper(uuid))
if err != nil || absent {
t.Fatalf("remaining NVRAM artifact absent=%v err=%v", absent, err)
}
if err := removeManagedNVRAMArtifacts(directory, strings.ToUpper(uuid)); err != nil {
t.Fatalf("remove managed NVRAM artifact: %v", err)
}
absent, err = managedNVRAMArtifactAbsent(directory, uuid)
if err != nil || !absent {
t.Fatalf("cleaned NVRAM artifact absent=%v err=%v", absent, err)
}
symlink := filepath.Join(directory, uuid+"_VARS.fd")
if err := os.Symlink(filepath.Join(directory, "outside"), symlink); err != nil {
t.Fatal(err)
}
if err := removeManagedNVRAMArtifacts(directory, uuid); err == nil {
t.Fatal("symlink NVRAM cleanup target was accepted")
}
}
func TestDomainPathReferenceIncludesFirmwareState(t *testing.T) {
path := "/var/lib/libvirt/qemu/nvram/shared_VARS.fd"
for _, document := range []string{
`<domain><os><nvram>` + path + `</nvram></os></domain>`,
`<domain><os><varstore path="` + path + `"/></os></domain>`,
} {
var definition libvirtxml.Domain
if err := definition.Unmarshal(document); err != nil {
t.Fatal(err)
}
if !domainReferencesPath(&definition, path) {
t.Fatalf("firmware or disk path reference was ignored: %s", document)
}
}
sourceBacked := libvirtxml.Domain{OS: &libvirtxml.DomainOS{
NVRam: &libvirtxml.DomainNVRam{Source: &libvirtxml.DomainDiskSource{
File: &libvirtxml.DomainDiskSourceFile{File: path},
}},
}}
if !domainReferencesPath(&sourceBacked, path) {
t.Fatal("source-backed NVRAM path reference was ignored")
}
diskBacked := libvirtxml.Domain{Devices: &libvirtxml.DomainDeviceList{
Disks: []libvirtxml.DomainDisk{{Source: &libvirtxml.DomainDiskSource{
File: &libvirtxml.DomainDiskSourceFile{File: path},
}}},
}}
if !domainReferencesPath(&diskBacked, path) {
t.Fatal("disk path reference was ignored")
}
var unrelated libvirtxml.Domain
if err := unrelated.Unmarshal(`<domain><os><nvram>/var/lib/libvirt/qemu/nvram/other_VARS.fd</nvram></os></domain>`); err != nil {
t.Fatal(err)
}
if domainReferencesPath(&unrelated, path) {
t.Fatal("unrelated firmware path was treated as a reference")
}
}
func TestDomainStorageReferenceIncludesBackingChainsAndPoolVolumes(t *testing.T) {
path := "/var/lib/libvirt/images/managed-base.qcow2"
target := domainStorageReference{
Path: path, Pool: "virt-cloud-managed", Volume: "managed-base.qcow2",
}
fileSource := func(candidate string) *libvirtxml.DomainDiskSource {
return &libvirtxml.DomainDiskSource{
File: &libvirtxml.DomainDiskSourceFile{File: candidate},
}
}
volumeSource := func(pool, volume string) *libvirtxml.DomainDiskSource {
return &libvirtxml.DomainDiskSource{
Volume: &libvirtxml.DomainDiskSourceVolume{Pool: pool, Volume: volume},
}
}
cases := map[string]*libvirtxml.Domain{
"nested backing file": {Devices: &libvirtxml.DomainDeviceList{
Disks: []libvirtxml.DomainDisk{{
Source: fileSource("/var/lib/libvirt/images/overlay.qcow2"),
BackingStore: &libvirtxml.DomainDiskBackingStore{
Source: fileSource("/var/lib/libvirt/images/intermediate.qcow2"),
BackingStore: &libvirtxml.DomainDiskBackingStore{Source: fileSource(path)},
},
}},
}},
"mirror backing volume": {Devices: &libvirtxml.DomainDeviceList{
Disks: []libvirtxml.DomainDisk{{Mirror: &libvirtxml.DomainDiskMirror{
Source: fileSource("/var/lib/libvirt/images/mirror.qcow2"),
BackingStore: &libvirtxml.DomainDiskBackingStore{
Source: volumeSource(target.Pool, target.Volume),
},
}}},
}},
"data store file": {Devices: &libvirtxml.DomainDeviceList{
Disks: []libvirtxml.DomainDisk{{Source: &libvirtxml.DomainDiskSource{
File: &libvirtxml.DomainDiskSourceFile{File: "/var/lib/libvirt/images/data.qcow2"},
DataStore: &libvirtxml.DomainDiskDataStore{Source: fileSource(path)},
}}},
}},
"direct pool volume": {Devices: &libvirtxml.DomainDeviceList{
Disks: []libvirtxml.DomainDisk{{Source: volumeSource(target.Pool, target.Volume)}},
}},
"filesystem pool volume": {Devices: &libvirtxml.DomainDeviceList{
Filesystems: []libvirtxml.DomainFilesystem{{Source: &libvirtxml.DomainFilesystemSource{
Volume: &libvirtxml.DomainFilesystemSourceVolume{
Pool: target.Pool, Volume: target.Volume,
},
}}},
}},
}
for name, definition := range cases {
t.Run(name, func(t *testing.T) {
if !domainReferencesStorage(definition, target) {
t.Fatal("managed storage reference was ignored")
}
})
}
realPath := filepath.Join(t.TempDir(), "real-managed-base.qcow2")
if err := os.WriteFile(realPath, []byte("qcow2-placeholder"), 0o600); err != nil {
t.Fatal(err)
}
aliasPath := realPath + ".alias"
if err := os.Symlink(realPath, aliasPath); err != nil {
t.Fatal(err)
}
aliasDefinition := &libvirtxml.Domain{Devices: &libvirtxml.DomainDeviceList{
Disks: []libvirtxml.DomainDisk{{Source: fileSource(aliasPath)}},
}}
if !domainReferencesStorage(aliasDefinition, domainStorageReference{Path: realPath}) {
t.Fatal("symlink alias to a managed volume was ignored")
}
hardlinkPath := realPath + ".hardlink"
if err := os.Link(realPath, hardlinkPath); err != nil {
t.Fatal(err)
}
hardlinkDefinition := &libvirtxml.Domain{Devices: &libvirtxml.DomainDeviceList{
Disks: []libvirtxml.DomainDisk{{Source: fileSource(hardlinkPath)}},
}}
if !domainReferencesStorage(hardlinkDefinition, domainStorageReference{Path: realPath}) {
t.Fatal("hardlink alias to a managed volume was ignored")
}
unrelated := &libvirtxml.Domain{Devices: &libvirtxml.DomainDeviceList{
Disks: []libvirtxml.DomainDisk{{
Source: volumeSource(target.Pool, "different-volume.qcow2"),
BackingStore: &libvirtxml.DomainDiskBackingStore{
Source: fileSource("/var/lib/libvirt/images/different-base.qcow2"),
},
}},
}}
if domainReferencesStorage(unrelated, target) {
t.Fatal("unrelated backing or pool volume was treated as a reference")
}
}
func TestNativeSnapshotDeleteSafetyRejectsExternalAndManualContent(t *testing.T) {
for name, document := range map[string]string{
"external memory": `<domainsnapshot><memory snapshot="external" file="/srv/snapshots/tenant.mem"/></domainsnapshot>`,
"manual memory": `<domainsnapshot><memory snapshot="manual"/></domainsnapshot>`,
"external disk": `<domainsnapshot><disks><disk name="vda" snapshot="external">` +
`<source file="/srv/snapshots/tenant-overlay.qcow2"/></disk></disks></domainsnapshot>`,
"manual disk": `<domainsnapshot><disks><disk name="vda" snapshot="manual"/>` +
`</disks></domainsnapshot>`,
"delete in progress": `<domainsnapshot><disks><disk name="vda">` +
`<snapshotDeleteInProgress/></disk></disks></domainsnapshot>`,
"revert disk cleanup pending": `<domainsnapshot><revertDisks>` +
`<disk name="vda"><source file="/srv/snapshots/revert.qcow2"/>` +
`</disk></revertDisks></domainsnapshot>`,
"malformed": `<domainsnapshot><memory`,
} {
t.Run(name, func(t *testing.T) {
if err := validateNativeSnapshotDeleteSafety(document); err == nil {
t.Fatal("unsafe native snapshot content was accepted for VM deletion")
}
})
}
for name, document := range map[string]string{
"internal memory": `<domainsnapshot><memory snapshot="internal"/></domainsnapshot>`,
"internal disk": `<domainsnapshot><disks><disk name="vda" snapshot="internal"/>` +
`</disks></domainsnapshot>`,
"no memory": `<domainsnapshot><memory snapshot="no"/>` +
`<disks><disk name="vda" snapshot="no"/></disks></domainsnapshot>`,
"cookie extension is not top-level state": `<domainsnapshot><cookie>` +
`<snapshotDeleteInProgress/></cookie></domainsnapshot>`,
"unknown top-level extension is not disk state": `<domainsnapshot>` +
`<snapshotDeleteInProgress/></domainsnapshot>`,
} {
t.Run(name, func(t *testing.T) {
if err := validateNativeSnapshotDeleteSafety(document); err != nil {
t.Fatalf("metadata-only native snapshot was rejected: %v", err)
}
})
}
}
func TestHistoricalDomainMetadataParticipatesInStorageReferenceChecks(t *testing.T) {
target := domainStorageReference{
Path: "/srv/managed/base.qcow2",
Pool: "virt-cloud-managed", Volume: "base.qcow2",
}
for name, snapshot := range map[string]*libvirtxml.DomainSnapshot{
"external disk": {Disks: &libvirtxml.DomainSnapshotDisks{
Disks: []libvirtxml.DomainSnapshotDisk{{Source: &libvirtxml.DomainDiskSource{
File: &libvirtxml.DomainDiskSourceFile{File: target.Path},
}}},
}},
"external memory": {Memory: &libvirtxml.DomainSnapshotMemory{File: target.Path}},
"embedded domain volume": {Domain: &libvirtxml.Domain{Devices: &libvirtxml.DomainDeviceList{
Disks: []libvirtxml.DomainDisk{{Source: &libvirtxml.DomainDiskSource{
Volume: &libvirtxml.DomainDiskSourceVolume{Pool: target.Pool, Volume: target.Volume},
}}},
}}},
} {
t.Run(name, func(t *testing.T) {
if !domainSnapshotReferencesStorage(snapshot, target) {
t.Fatal("historical snapshot storage reference was ignored")
}
})
}
checkpoint := &libvirtxml.DomainCheckpoint{Domain: &libvirtxml.Domain{
Devices: &libvirtxml.DomainDeviceList{Disks: []libvirtxml.DomainDisk{{
Source: &libvirtxml.DomainDiskSource{
File: &libvirtxml.DomainDiskSourceFile{File: target.Path},
},
}}},
}}
if checkpoint.Domain == nil || !domainReferencesStorage(checkpoint.Domain, target) {
t.Fatal("historical checkpoint domain storage reference was ignored")
}
privateState, err := parseNativeSnapshotPrivateXML(`<domainsnapshot><revertDisks>` +
`<disk name="vda" type="file"><source file="/srv/managed/base.qcow2"/>` +
`</disk></revertDisks></domainsnapshot>`)
if err != nil || !nativeSnapshotPrivateReferencesStorage(privateState, target) {
t.Fatalf("historical revert-disk storage reference was ignored: %v", err)
}
}
func TestDiskTransitionCommitEvidenceRequiresCurrentOperationMarker(t *testing.T) {
replacement := "vreinstall-123e4567e89b42d3a456426614174000-0123456789abcdef.qcow2"
marked := &domainMetadataXML{
RootVolumeName: replacement, PendingCleanupVolume: rootVolumeName("123e4567-e89b-42d3-a456-426614174000"),
DiskTransitionKind: "reinstall", DiskTransitionID: "operation-reinstall-one",
}
if !diskTransitionCommitted(marked, replacement, "reinstall", "operation-reinstall-one") {
t.Fatal("committed transition marker was not accepted")
}
markerFree := &domainMetadataXML{RootVolumeName: replacement}
if diskTransitionCommitted(markerFree, replacement, "reinstall", "operation-reinstall-one") {
t.Fatal("marker-free transition was accepted")
}
wrongOperation := *marked
wrongOperation.DiskTransitionID = "operation-reinstall-other"
if diskTransitionCommitted(&wrongOperation, replacement, "reinstall", "operation-reinstall-one") {
t.Fatal("another operation's transition marker was accepted")
}
marked.RootVolumeName = rootVolumeName("123e4567-e89b-42d3-a456-426614174000")
if diskTransitionCommitted(marked, replacement, "reinstall", "operation-reinstall-one") {
t.Fatal("transition marker without the deterministic active root was accepted")
}
}
func TestPMSuspendedStateUsesPanelProtocolValue(t *testing.T) {
got := mapDomainState(libvirt.DOMAIN_PMSUSPENDED)
if got != VMStateSuspended {
t.Fatalf("PM suspended state = %q", got)
}
if string(got) != "pmsuspended" {
t.Fatalf("PM suspended state = %q", got)
}
}
func TestExpectedTransitionMarkerClassificationNeverRequestsReplay(t *testing.T) {
for _, test := range []struct {
name string
primary, secondary bool
wantCode string
wantResolved bool
}{
{"absent", false, false, "settings_not_committed", true},
{"primary-only", true, false, "", false},
{"secondary-only", false, true, "", false},
} {
t.Run(test.name, func(t *testing.T) {
resolution := classifyTransitionMarkers("settings", test.primary, test.secondary)
if resolution.Resolved != test.wantResolved || resolution.Succeeded || resolution.ErrorCode != test.wantCode {
t.Fatalf("resolution = %#v", resolution)
}
})
}
resolved, err := resolvedExpectedSuccess(map[string]bool{"network_enabled": true})
if err != nil || !resolved.Resolved || !resolved.Succeeded {
t.Fatalf("successful read-only resolution = %#v err=%v", resolved, err)
}
var body map[string]bool
if err := json.Unmarshal(resolved.Result, &body); err != nil || !body["network_enabled"] {
t.Fatalf("resolution body=%s err=%v", resolved.Result, err)
}
}
func TestMissingVMResolvesExpectedMutationWithoutReplay(t *testing.T) {
suspend := operation.Record{
ResourceID: "123e4567-e89b-42d3-a456-426614174000",
Action: "service-suspend",
ExpectedState: &operation.ExpectedState{
Kind: operation.ExpectedServiceState, TransitionID: "operation-service-missing",
},
}
resolution := expectedStateResourceAbsent(suspend)
var state VMState
if err := json.Unmarshal(resolution.Result, &state); err != nil ||
!resolution.Resolved || !resolution.Succeeded || state.UUID != suspend.ResourceID || state.State != VMStateShutoff {
t.Fatalf("absent service suspend resolution = %#v state=%#v err=%v", resolution, state, err)
}
resume := suspend
resume.Action = "service-resume"
resume.ExpectedState = &operation.ExpectedState{
Kind: operation.ExpectedServiceState, Enabled: true, TransitionID: "operation-service-resume-missing",
}
resolution = expectedStateResourceAbsent(resume)
if !resolution.Resolved || resolution.Succeeded || resolution.ErrorCode != "expected_state_resource_absent" {
t.Fatalf("absent service resume resolution = %#v", resolution)
}
transient, err := expectedEvidenceError("settings", NewError(ErrUnavailable, "libvirt_unavailable", "temporarily unavailable", nil))
if err == nil || transient.Resolved {
t.Fatalf("transient evidence read was finalized: resolution=%#v err=%v", transient, err)
}
for _, kind := range []string{"network", "settings", "service", "firewall"} {
t.Run(kind+"-invalid-evidence", func(t *testing.T) {
invalid, err := expectedEvidenceError(kind, NewError(ErrConflict,
"managed_metadata_invalid", "invalid metadata", nil))
if err != nil || invalid.Resolved || invalid.Succeeded || invalid.ErrorCode != "" {
t.Fatalf("invalid %s evidence was terminalized: resolution=%#v err=%v", kind, invalid, err)
}
})
t.Run(kind+"-missing-evidence", func(t *testing.T) {
missing, err := expectedEvidenceError(kind, NewError(ErrNotFound,
"seed_not_found", "missing evidence", nil))
if err != nil || missing.Resolved || missing.Succeeded || missing.ErrorCode != "" {
t.Fatalf("missing %s evidence was terminalized: resolution=%#v err=%v", kind, missing, err)
}
})
}
}
func TestNetworkAndFirewallReadOnlyEvidenceMatchers(t *testing.T) {
up := `<domain><devices><interface type="bridge"><link state="up"/></interface></devices></domain>`
down := `<domain><devices><interface type="bridge"><link state="down"/></interface></devices></domain>`
if !domainNetworkLinksMatch(up, true) || domainNetworkLinksMatch(up, false) || !domainNetworkLinksMatch(down, false) || domainNetworkLinksMatch(down, true) {
t.Fatal("network link evidence matcher accepted an incorrect state")
}
var bridgeDefinition libvirtxml.Domain
if err := bridgeDefinition.Unmarshal(`<domain><devices><interface type="bridge"><source bridge="br0"/></interface></devices></domain>`); err != nil {
t.Fatal(err)
}
iface := bridgeDefinition.Devices.Interfaces[0]
if !domainNetworkSourceMatches(iface, "bridge", "br0") || domainNetworkSourceMatches(iface, "bridge", "br-old") || domainNetworkSourceMatches(iface, "network", "br0") {
t.Fatal("network source evidence matcher accepted a stale or wrong source")
}
policy, err := validateFirewallPolicy(FirewallPolicy{
Revision: 3, DefaultIn: "drop", DefaultOut: "accept",
Rules: []FirewallRule{{Direction: "ingress", Action: "accept", Protocol: "tcp", CIDR: "8.8.8.0/24", PortFrom: 22, PortTo: 22}},
})
if err != nil {
t.Fatal(err)
}
document, err := buildUserFirewallXML(`<filter name="virt-test" chain="root"><filterref filter="clean-traffic"/></filter>`, policy)
if err != nil {
t.Fatal(err)
}
if !firewallDocumentMatchesPolicy(document, policy) {
t.Fatal("matching firewall evidence was rejected")
}
other := policy
other.DefaultOut = "drop"
other.PolicyDigest = ""
other, err = validateFirewallPolicy(other)
if err != nil {
t.Fatal(err)
}
if firewallDocumentMatchesPolicy(document, other) {
t.Fatal("mismatched firewall evidence was accepted")
}
if !firewallDocumentMatchesStoredState(document, firewallToMetadata(policy)) {
t.Fatal("filter matching stored firewall metadata was rejected")
}
if firewallDocumentMatchesStoredState(document, firewallToMetadata(other)) {
t.Fatal("filter mismatching stored firewall metadata was accepted")
}
baseline := `<filter name="virt-test" chain="root"><filterref filter="clean-traffic"/><rule action="accept" direction="inout" priority="900"><all/></rule></filter>`
if !firewallDocumentMatchesStoredState(baseline, nil) {
t.Fatal("untouched platform filter was rejected as the pre-policy state")
}
if firewallDocumentMatchesStoredState(document, nil) {
t.Fatal("applied user firewall was accepted as an untouched platform filter")
}
}
func TestNetworkInterfaceLinkDocumentsBuildAllUpdatesBeforeMutation(t *testing.T) {
document := `<domain><devices>` +
`<interface type="bridge"><mac address="52:54:00:00:00:01"/><filterref filter="virt-test"><parameter name="IP" value="8.8.8.10"/><parameter name="IPV6" value="2404:c140:156:3a::1000"/><parameter name="IPV6_LINK_LOCAL" value="fe80::5054:ff:fe00:1"/></filterref><source bridge="br0"/></interface>` +
`<interface type="bridge"><mac address="02:00:00:00:00:02"/><source bridge="vpc-a1b2c3d4e5"/></interface>` +
`</devices></domain>`
for _, enabled := range []bool{false, true} {
documents, err := networkInterfaceLinkDocuments(document, enabled)
if err != nil || len(documents) != 2 {
t.Fatalf("enabled=%v documents=%d err=%v", enabled, len(documents), err)
}
for _, update := range documents {
var iface libvirtxml.DomainInterface
if err := iface.Unmarshal(update); err != nil {
t.Fatalf("decode interface update: %v", err)
}
want := "down"
if enabled {
want = "up"
}
if iface.Link == nil || iface.Link.State != want {
t.Fatalf("enabled=%v update=%s", enabled, update)
}
if iface.MAC != nil && iface.MAC.Address == "52:54:00:00:00:01" &&
!managedInterfaceFilterIPMatches(&iface, "virt-test", dualStackPublicNetwork()) {
t.Fatalf("network state update dropped managed IP binding: %s", update)
}
if iface.MAC != nil && iface.MAC.Address == "02:00:00:00:00:02" &&
(iface.Source == nil || iface.Source.Bridge == nil ||
iface.Source.Bridge.Bridge != "vpc-a1b2c3d4e5") {
t.Fatalf("network state update changed the VPC bridge: %s", update)
}
}
}
if documents, err := networkInterfaceLinkDocuments(`<domain><devices/></domain>`, false); err == nil || len(documents) != 0 {
t.Fatalf("interface-free document updates=%v err=%v", documents, err)
}
}
func TestCatalogDeleteNotFoundRequiresBothManagedArtifactsAbsent(t *testing.T) {
for _, test := range []struct {
name string
evidence catalogDeleteEvidence
}{
{"template", catalogDeleteEvidence{"template", "template_not_found", "template_artifacts_inconsistent"}},
{"rescue-template", catalogDeleteEvidence{"rescue template", "rescue_template_not_found", "rescue_template_artifacts_inconsistent"}},
{"user-iso", catalogDeleteEvidence{"user ISO", "user_iso_not_found", "user_iso_artifacts_inconsistent"}},
} {
t.Run(test.name, func(t *testing.T) {
absent := validateCatalogDeletePresence(false, false, test.evidence)
if !errors.Is(absent, ErrNotFound) || errorPublicCode(absent) != test.evidence.notFoundCode {
t.Fatalf("double-absence error = %v", absent)
}
for _, presence := range [][2]bool{{true, false}, {false, true}} {
err := validateCatalogDeletePresence(presence[0], presence[1], test.evidence)
if !errors.Is(err, ErrConflict) || errors.Is(err, ErrNotFound) || errorPublicCode(err) != test.evidence.inconsistentCode {
t.Fatalf("inconsistent presence %v error = %v", presence, err)
}
}
if err := validateCatalogDeletePresence(true, true, test.evidence); err != nil {
t.Fatalf("complete presence rejected: %v", err)
}
disappeared := catalogDeleteEvidenceChanged(
NewError(ErrNotFound, "catalog_entry_not_found", "missing", nil),
test.evidence,
)
if !errors.Is(disappeared, ErrConflict) || errors.Is(disappeared, ErrNotFound) || errorPublicCode(disappeared) != test.evidence.inconsistentCode {
t.Fatalf("post-presence disappearance error = %v", disappeared)
}
})
}
}
func TestStoragePoolNamesAreFixedByRole(t *testing.T) {
l := &Libvirt{}
for role, want := range map[string]string{
StorageRoleManaged: managedStoragePoolName,
StorageRoleTemplate: templateStoragePoolName,
StorageRoleSeed: seedStoragePoolName,
StorageRoleUserISO: userISOStoragePoolName,
StorageRoleRescue: rescueStoragePoolName,
} {
if got := l.storagePoolName(role); got != want {
t.Fatalf("storagePoolName(%q) = %q, want %q", role, got, want)
}
}
}
func TestStoragePoolTargetMatchRequiresExactCleanDirectory(t *testing.T) {
definition := libvirtxml.StoragePool{
Type: "dir", Target: &libvirtxml.StoragePoolTarget{Path: "/srv/virt/images"},
}
if !storagePoolTargetMatches(definition, "/srv/virt/images") {
t.Fatal("exact clean directory target did not match")
}
definition.Target.Path = "/srv/virt/templates/../images"
if storagePoolTargetMatches(definition, "/srv/virt/images") {
t.Fatal("non-clean storage pool target was accepted")
}
}
func errorPublicCode(err error) string {
var public interface{ PublicCode() string }
if !errors.As(err, &public) {
return ""
}
return public.PublicCode()
}
func TestVMManagedVolumeOwnershipIsUUIDScoped(t *testing.T) {
uuid := "123e4567-e89b-42d3-a456-426614174000"
otherUUID := "223e4567-e89b-42d3-a456-426614174000"
snapshotKey := "0123456789abcdef0123456789abcdef"
owned := []string{
rootVolumeName(uuid),
reinstallVolumeName(uuid, "operation-reinstall"),
snapshotRestoreVolumeName(uuid, "operation-restore"),
snapshotVolumeName(uuid, snapshotKey),
}
for _, name := range owned {
if !isVMOwnedManagedVolumeName(uuid, name) {
t.Errorf("owned volume %q was rejected", name)
}
}
for _, name := range []string{
rootVolumeName(otherUUID),
reinstallVolumeName(otherUUID, "operation-reinstall"),
snapshotRestoreVolumeName(otherUUID, "operation-restore"),
snapshotVolumeName(otherUUID, snapshotKey),
rescueImageVolumeName(snapshotKey),
"template-debian-12.qcow2",
"vsnap-123e4567e89b42d3a456426614174000-not-a-key.qcow2",
"VREINSTALL-123e4567e89b42d3a456426614174000-0123456789abcdef.qcow2",
} {
if isVMOwnedManagedVolumeName(uuid, name) {
t.Errorf("foreign volume %q was accepted", name)
}
}
}
func TestValidateRescueDefinitionRequiresManagedReadOnlyBootDisk(t *testing.T) {
definition := validDomainDefinition()
document, err := BuildDomainXML(definition)
if err != nil {
t.Fatal(err)
}
templateID := "0123456789abcdef0123456789abcdef"
revision := uint64(7)
generation := "fedcba9876543210fedcba9876543210"
rescuePath := "/var/lib/libvirt/images/rescue-0123456789abcdef0123456789abcdef.iso"
staged, err := mutateDomainDocument(document, func(domain *libvirtxml.Domain, metadata *domainMetadataXML) error {
if err := stageRescueBoot(domain, rescuePath); err != nil {
return err
}
metadata.RescueTemplateID = templateID
metadata.RescueTemplateRevision = revision
metadata.RescueTemplateGeneration = generation
return nil
})
if err != nil {
t.Fatal(err)
}
if err := validateRescueDefinition(staged, templateID, revision, generation, rescuePath); err != nil {
t.Fatalf("valid staged rescue rejected: %v", err)
}
if err := validateRescueDefinition(staged, templateID, revision+1, generation, rescuePath); err == nil {
t.Fatal("rescue definition with a different catalog revision was accepted")
}
if err := validateRescueDefinition(staged, templateID, revision, templateID, rescuePath); err == nil {
t.Fatal("rescue definition with a different generation token was accepted")
}
if err := validateRescueDefinition(staged, templateID, revision, generation, rescuePath+".foreign"); err == nil {
t.Fatal("foreign rescue volume path was accepted")
}
incomplete, err := mutateDomainDocument(document, func(_ *libvirtxml.Domain, metadata *domainMetadataXML) error {
metadata.RescueTemplateID = templateID
return nil
})
if err != nil {
t.Fatal(err)
}
if _, _, managed, err := inspectManagedDomain(incomplete); err == nil || !managed || errorPublicCode(err) != "managed_metadata_invalid" {
t.Fatalf("incomplete rescue identity was accepted: managed=%v err=%v", managed, err)
}
parsed, _, err := parseManagedDomain(staged)
if err != nil {
t.Fatal(err)
}
if len(parsed.OS.BootDevices) != 0 {
t.Fatalf("global boot order remains staged: %#v", parsed.OS.BootDevices)
}
for _, disk := range parsed.Devices.Disks {
if disk.Target == nil {
continue
}
switch disk.Target.Dev {
case "vda":
if disk.Boot == nil || disk.Boot.Order != 2 {
t.Fatalf("root disk boot order = %#v, want 2", disk.Boot)
}
case "sda":
if disk.Boot != nil {
t.Fatalf("seed ISO unexpectedly participates in rescue boot: %#v", disk.Boot)
}
case "sdb":
if disk.Boot == nil || disk.Boot.Order != 1 {
t.Fatalf("rescue ISO boot order = %#v, want 1", disk.Boot)
}
}
}
legacy, err := mutateDomainDocument(staged, func(domain *libvirtxml.Domain, _ *domainMetadataXML) error {
domain.OS.BootDevices = []libvirtxml.DomainBootDevice{{Dev: "cdrom"}, {Dev: "hd"}}
for index := range domain.Devices.Disks {
domain.Devices.Disks[index].Boot = nil
}
return nil
})
if err != nil {
t.Fatal(err)
}
if err := validateRescueDefinition(legacy, templateID, revision, generation, rescuePath); err != nil {
t.Fatalf("legacy rescue definition cannot be exited after upgrade: %v", err)
}
restored, _, err := parseManagedDomain(staged)
if err != nil {
t.Fatal(err)
}
restoreNormalBoot(restored)
if len(restored.OS.BootDevices) != 1 || restored.OS.BootDevices[0].Dev != "hd" {
t.Fatalf("normal global boot order = %#v, want hd", restored.OS.BootDevices)
}
for _, disk := range restored.Devices.Disks {
if disk.Boot != nil {
t.Fatalf("per-device boot order remained after rescue exit: %#v", disk.Boot)
}
if disk.Target != nil && disk.Target.Dev == "sdb" {
t.Fatal("managed rescue disk remained after rescue exit")
}
}
}
func TestCollectVMManagedVolumeNamesIncludesTransitionsSnapshotsAndOrphans(t *testing.T) {
uuid := "123e4567-e89b-42d3-a456-426614174000"
otherUUID := "223e4567-e89b-42d3-a456-426614174000"
registeredKey := "0123456789abcdef0123456789abcdef"
orphanKey := "fedcba9876543210fedcba9876543210"
root := snapshotRestoreVolumeName(uuid, "operation-current-root")
pending := reinstallVolumeName(uuid, "operation-old-root")
orphanRestore := snapshotRestoreVolumeName(uuid, "operation-orphan-restore")
orphanReinstall := reinstallVolumeName(uuid, "operation-orphan-reinstall")
metadata := &domainMetadataXML{
RootVolumeName: root,
PendingCleanupVolume: pending,
Snapshots: []domainSnapshotMetadataXML{{Key: registeredKey}},
}
inventory := []string{
root, pending, snapshotVolumeName(uuid, registeredKey), snapshotVolumeName(uuid, orphanKey),
backupWorkspaceVolumeName(uuid, orphanKey),
orphanRestore, orphanReinstall, rootVolumeName(otherUUID), "template-debian-12.qcow2",
}
got, err := collectVMManagedVolumeNames(uuid, metadata, inventory)
if err != nil {
t.Fatal(err)
}
want := []string{
root, pending, snapshotVolumeName(uuid, registeredKey), snapshotVolumeName(uuid, orphanKey),
backupWorkspaceVolumeName(uuid, orphanKey),
orphanRestore, orphanReinstall,
}
sort.Strings(want)
if !reflect.DeepEqual(got, want) {
t.Fatalf("collected volumes = %#v, want %#v", got, want)
}
metadata.RootVolumeName = rootVolumeName(otherUUID)
if _, err := collectVMManagedVolumeNames(uuid, metadata, inventory); err == nil {
t.Fatal("foreign metadata root volume was accepted")
}
}
func TestCapabilitiesAreDerivedFromRuntimePrerequisites(t *testing.T) {
all := capabilityPrerequisites{
firmwareLoader: true, nvramTemplate: true, nvramDirectory: true,
managedPool: true, snapshotPool: true, templatePool: true, seedPool: true,
userISOPool: true, rescuePool: true, storageConfig: true,
networks: true, nwfilter: true, publicGuard: true, q35UEFI: true, liveBackup: true,
}
features := all.features()
if !features.CreateVM || !features.Resources || !features.Reinstall || !features.Templates || !features.UEFI || features.SecureBoot {
t.Fatalf("fully satisfied features = %#v", features)
}
missingPublicGuard := all
missingPublicGuard.publicGuard = false
features = missingPublicGuard.features()
if features.CreateVM || features.Network || features.Reinstall || features.Backups {
t.Fatalf("public-network mutations were advertised without the per-port NDP guard: %#v", features)
}
if !features.DeleteVM || !features.Power || !features.Firewall || !features.Metrics {
t.Fatalf("guard-independent features were disabled with the per-port NDP guard: %#v", features)
}
missingStorageConfiguration := all
missingStorageConfiguration.storageConfig = false
features = missingStorageConfiguration.features()
if features.CreateVM || features.DeleteVM || features.Resources || features.Settings || features.Network || features.Reinstall ||
features.Snapshots || features.Templates || features.Rescue || features.UserISO {
t.Fatalf("storage-dependent features were advertised before storage enrollment: %#v", features)
}
if !features.Power || !features.Metrics || !features.Console {
t.Fatalf("storage-independent features were disabled before storage enrollment: %#v", features)
}
missingFirmware := all
missingFirmware.firmwareLoader = false
features = missingFirmware.features()
if features.CreateVM || features.Reinstall || features.UEFI {
t.Fatalf("firmware-dependent features were advertised without a loader: %#v", features)
}
if !features.Templates || !features.Power || !features.Metrics {
t.Fatalf("independent features were disabled with the loader: %#v", features)
}
missingSeedPool := all
missingSeedPool.seedPool = false
features = missingSeedPool.features()
if features.CreateVM || features.DeleteVM || features.Settings || features.Network {
t.Fatalf("seed-dependent features were advertised without their pool: %#v", features)
}
if !features.Snapshots || !features.UEFI {
t.Fatalf("independent features were disabled with the seed pool: %#v", features)
}
if !features.Templates || features.Rescue || !features.UserISO {
t.Fatalf("only seed-dependent rescue should be disabled without the seed pool: %#v", features)
}
missingBackup := all
missingBackup.liveBackup = false
features = missingBackup.features()
if features.Snapshots {
t.Fatalf("snapshots were advertised without qemu live backup support: %#v", features)
}
unsupportedSnapshotPool := all
unsupportedSnapshotPool.snapshotPool = false
features = unsupportedSnapshotPool.features()
if features.Snapshots || !features.CreateVM || !features.Reinstall {
t.Fatalf("non-file pool did not disable only snapshots: %#v", features)
}
}
func TestPortableRestoreCapabilityRequiresEveryRuntimeProof(t *testing.T) {
required := []string{
"firmware_loader", "firmware_nvram_template", "nvram_directory",
"managed_storage_pool", "seed_storage_pool", "storage_configuration",
"network_selectors", "nwfilter_api", "public_ndp_guard", "q35_uefi",
}
checks := make(map[string]CapabilityCheck, len(required))
for _, name := range required {
checks[name] = availableCheck()
}
public := PublicNetworkCapability{
Contract: 1, DualStackRequired: true, ProjectOwnedFilter: true, NDPGuard: true,
}
if got := portableRestoreCapability(checks, public); got == nil || *got != fixedPortableRestoreContract() {
t.Fatalf("ready portable restore capability = %#v", got)
}
for _, missing := range required {
t.Run(missing, func(t *testing.T) {
broken := make(map[string]CapabilityCheck, len(checks))
for name, check := range checks {
broken[name] = check
}
broken[missing] = unavailableCheck(capabilityUnavailable)
if got := portableRestoreCapability(broken, public); got != nil {
t.Fatalf("capability advertised without %s: %#v", missing, got)
}
})
}
brokenPublic := public
brokenPublic.NDPGuard = false
if got := portableRestoreCapability(checks, brokenPublic); got != nil {
t.Fatalf("capability advertised without public network guard: %#v", got)
}
}
func TestPublicNetworkCapabilityRequiresBothRuntimeGuards(t *testing.T) {
checks := map[string]CapabilityCheck{
"nwfilter_api": availableCheck(),
"public_ndp_guard": availableCheck(),
}
capability := publicNetworkCapability(checks)
if !capability.ProjectOwnedFilter || !capability.NDPGuard {
t.Fatalf("ready public network capability = %#v", capability)
}
checks["nwfilter_api"] = unavailableCheck(capabilityUnavailable)
capability = publicNetworkCapability(checks)
if capability.ProjectOwnedFilter || !capability.NDPGuard {
t.Fatalf("missing nwfilter readiness was advertised: %#v", capability)
}
checks["nwfilter_api"] = availableCheck()
checks["public_ndp_guard"] = unavailableCheck(capabilityUnavailable)
capability = publicNetworkCapability(checks)
if !capability.ProjectOwnedFilter || capability.NDPGuard {
t.Fatalf("missing NDP guard readiness was advertised: %#v", capability)
}
}
func TestNetworkSelectorCapabilityIgnoresUnavailableOptionalSelector(t *testing.T) {
selectors := []string{"bridge:br0", "network:default"}
probe := func(source LocalNetworkSource) CapabilityCheck {
if source.Source == "br0" {
return availableCheck()
}
return unavailableCheck(capabilitySelectorUnusable)
}
check, usable := probeNetworkSelectorSet(selectors, probe)
if !check.Available {
t.Fatalf("one usable selector did not satisfy network capability: %#v", check)
}
if !reflect.DeepEqual(usable, []string{"bridge:br0"}) {
t.Fatalf("usable selectors = %#v", usable)
}
selectors = selectors[1:]
if check, usable = probeNetworkSelectorSet(selectors, probe); check.Available || check.Reason != capabilitySelectorUnusable || len(usable) != 0 {
t.Fatalf("unusable selector set = %#v", check)
}
if check, usable = probeNetworkSelectorSet(nil, probe); check.Available || check.Reason != capabilitySelectorMissing || len(usable) != 0 {
t.Fatalf("empty selector set = %#v", check)
}
}
func TestNetworkSelectorCapabilitiesAreCanonicalSortedAndUnique(t *testing.T) {
selectors := []string{
"network:default", "bridge:br-z", "network:inactive", "bridge:br-a",
"bridge:br-a", "public", "bridge:br0:extra",
}
check, usable := probeNetworkSelectorSet(selectors,
func(source LocalNetworkSource) CapabilityCheck {
if source.Source == "inactive" {
return unavailableCheck(capabilitySelectorUnusable)
}
return availableCheck()
})
if !check.Available {
t.Fatalf("usable selector set was rejected: %#v", check)
}
if !reflect.DeepEqual(usable, []string{"bridge:br-a", "bridge:br-z", "network:default"}) {
t.Fatalf("usable selectors = %#v", usable)
}
}
func TestNetworkSelectorCapabilitiesAreBounded(t *testing.T) {
var selectors []string
for index := 0; index <= maxCapabilityNetworkSelectors; index++ {
selectors = append(selectors, fmt.Sprintf("bridge:br%d", index))
}
check, usable := probeNetworkSelectorSet(selectors,
func(LocalNetworkSource) CapabilityCheck { return availableCheck() })
if check.Available || check.Reason != capabilityInvalid || len(usable) != 0 {
t.Fatalf("oversized selector capabilities = %#v, %#v", check, usable)
}
selectors = append(selectors[:maxCapabilityNetworkSelectors], "public", "network:inactive", "bridge:br0")
check, usable = probeNetworkSelectorSet(selectors, func(source LocalNetworkSource) CapabilityCheck {
return CapabilityCheck{Available: source.Source != "inactive"}
})
if !check.Available || len(usable) != maxCapabilityNetworkSelectors {
t.Fatalf("invalid, inactive, or duplicate candidates consumed the limit: %#v, %d", check, len(usable))
}
}
func TestRequestedNetworkSelectorMustBeUsable(t *testing.T) {
active := true
probe := func(source LocalNetworkSource) CapabilityCheck {
if source.Source == "br0" && active {
return availableCheck()
}
return unavailableCheck(capabilitySelectorUnusable)
}
selected, err := resolveNetworkSelector("bridge:br0", probe)
if err != nil || selected.Source != "br0" {
t.Fatalf("usable selector = %#v, %v", selected, err)
}
if _, err := resolveNetworkSelector("network:default", probe); !errors.Is(err, ErrUnavailable) {
t.Fatalf("inactive requested selector error = %v", err)
}
active = false
if _, err := resolveNetworkSelector("bridge:br0", probe); !errors.Is(err, ErrUnavailable) {
t.Fatalf("selector was not checked again before use: %v", err)
}
if _, err := resolveNetworkSelector("public", probe); !errors.Is(err, ErrInvalid) {
t.Fatalf("legacy alias selector error = %v", err)
}
}
func TestBridgeProbeRequiresRealUpBridge(t *testing.T) {
root := t.TempDir()
if err := os.MkdirAll(filepath.Join(root, "br0", "bridge"), 0o700); err != nil {
t.Fatal(err)
}
for _, test := range []struct {
name string
flags net.Flags
want bool
}{
{"eth0", net.FlagUp, false},
{"br0", 0, false},
{"br0", net.FlagUp, true},
} {
check := probeBridgeInterface(net.Interface{Name: test.name, Flags: test.flags}, root)
if check.Available != test.want {
t.Errorf("bridge %s flags %v = %#v", test.name, test.flags, check)
}
}
}
func TestDomainCapabilitiesRequireConfiguredQ35UEFISecureBootOffCombination(t *testing.T) {
loader := "/usr/share/edk2/ovmf/OVMF_CODE.fd"
document := `<domainCapabilities>
<path>/usr/bin/qemu-system-x86_64</path>
<domain>kvm</domain>
<machine>pc-q35-9.2</machine>
<arch>x86_64</arch>
<os supported="yes">
<loader supported="yes">
<value>` + loader + `</value>
<enum name="type"><value>pflash</value></enum>
<enum name="readonly"><value>yes</value></enum>
<enum name="secure"><value>yes</value><value>no</value></enum>
</loader>
</os>
<cpu><mode name="host-passthrough" supported="yes"/></cpu>
<features><backup supported="yes"/></features>
</domainCapabilities>`
if !domainCapsSupportQ35UEFI(document, loader) {
t.Fatal("valid q35 UEFI domain capabilities were rejected")
}
if !domainCapsSupportLiveBackup(document) {
t.Fatal("advertised live backup capability was rejected")
}
for name, invalid := range map[string]string{
"wrong loader": strings.Replace(document, loader, loader+".other", 1),
"secure off missing": strings.Replace(document, `<value>yes</value><value>no</value>`, `<value>yes</value>`, 1),
"wrong machine": strings.Replace(document, "pc-q35-9.2", "pc-i440fx-9.2", 1),
"CPU unsupported": strings.Replace(document, `name="host-passthrough" supported="yes"`, `name="host-passthrough" supported="no"`, 1),
} {
t.Run(name, func(t *testing.T) {
if domainCapsSupportQ35UEFI(invalid, loader) {
t.Fatal("unsafe domain capabilities were accepted")
}
})
}
if domainCapsSupportLiveBackup(strings.Replace(document, `<backup supported="yes"/>`, `<backup supported="no"/>`, 1)) {
t.Fatal("missing live backup capability was accepted")
}
}
func TestReinstallMetadataNameKeepsDisplayNameForInternalDomain(t *testing.T) {
document, err := BuildDomainXML(validDomainDefinition())
if err != nil {
t.Fatal(err)
}
if got := metadataName(document); got != "vm-test" {
t.Fatalf("reinstall cloud-init name = %q, want vm-test", got)
}
legacy := strings.Replace(document, "<name>3:vm-123e4567e89b</name>", "<name>legacy-vm</name>", 1)
legacy = strings.Replace(legacy, "<display-name>vm-test</display-name>", "", 1)
if got := metadataName(legacy); got != "legacy-vm" {
t.Fatalf("legacy reinstall cloud-init name = %q, want legacy-vm", got)
}
}