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.
1254 lines
47 KiB
Go
1254 lines
47 KiB
Go
//go:build libvirt
|
|
|
|
package hypervisor
|
|
|
|
import (
|
|
"encoding/json"
|
|
"errors"
|
|
"fmt"
|
|
"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
|
|
destroyErr error
|
|
undefineErr error
|
|
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
|
|
}
|
|
|
|
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 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)
|
|
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")); err == nil || operation.IsUncertain(err) {
|
|
t.Fatalf("invalid preflight action error = %v", 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, false},
|
|
{"shutdown-still-running", PowerShutdown, VMStateRunning, false},
|
|
{"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 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) UndefineFlags(flags libvirt.DomainUndefineFlagsValues) error {
|
|
f.undefine = append(f.undefine, flags)
|
|
return f.undefineErr
|
|
}
|
|
|
|
func TestDeleteDomainContinuationUsesObservedTargetState(t *testing.T) {
|
|
t.Run("destroy error but observed inactive continues", func(t *testing.T) {
|
|
domain := &fakeDeleteDomainControl{
|
|
active: []bool{true, false}, destroyErr: errors.New("destroy response lost"),
|
|
}
|
|
if err := driveDeleteDomainToAbsent(domain, func() (bool, error) { return true, nil }); err != nil {
|
|
t.Fatalf("continue delete after observed stop: %v", err)
|
|
}
|
|
if domain.destroyCalls != 1 || len(domain.undefine) != 1 {
|
|
t.Fatalf("delete stages destroy=%d undefine=%d", domain.destroyCalls, len(domain.undefine))
|
|
}
|
|
wantFlags := libvirt.DOMAIN_UNDEFINE_NVRAM |
|
|
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("still active after destroy error remains uncertain", func(t *testing.T) {
|
|
domain := &fakeDeleteDomainControl{
|
|
active: []bool{true, true}, destroyErr: errors.New("destroy failed"),
|
|
}
|
|
err := driveDeleteDomainToAbsent(domain, func() (bool, error) { return false, nil })
|
|
if !operation.IsUncertain(err) || len(domain.undefine) != 0 {
|
|
t.Fatalf("active delete continuation err=%v undefine=%d", err, len(domain.undefine))
|
|
}
|
|
})
|
|
|
|
t.Run("ambiguous undefine with confirmed absence continues", func(t *testing.T) {
|
|
domain := &fakeDeleteDomainControl{
|
|
active: []bool{false}, undefineErr: errors.New("undefine response lost"),
|
|
}
|
|
absenceChecks := 0
|
|
err := driveDeleteDomainToAbsent(domain, func() (bool, error) {
|
|
absenceChecks++
|
|
return true, nil
|
|
})
|
|
if err != nil || domain.destroyCalls != 0 || len(domain.undefine) != 1 || absenceChecks != 1 {
|
|
t.Fatalf("confirmed undefine outcome err=%v destroy=%d undefine=%d checks=%d",
|
|
err, domain.destroyCalls, len(domain.undefine), absenceChecks)
|
|
}
|
|
})
|
|
|
|
t.Run("ambiguous undefine with remaining domain stays uncertain", func(t *testing.T) {
|
|
domain := &fakeDeleteDomainControl{
|
|
active: []bool{false}, undefineErr: errors.New("undefine failed"),
|
|
}
|
|
err := driveDeleteDomainToAbsent(domain, func() (bool, error) { return false, nil })
|
|
if !operation.IsUncertain(err) {
|
|
t.Fatalf("remaining domain delete error = %v", err)
|
|
}
|
|
})
|
|
|
|
t.Run("successful undefine must still prove absence", func(t *testing.T) {
|
|
domain := &fakeDeleteDomainControl{active: []bool{false}}
|
|
err := driveDeleteDomainToAbsent(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))
|
|
}
|
|
})
|
|
}
|
|
|
|
func TestCreateAbsenceCleanupPersistsTrafficStateRemoval(t *testing.T) {
|
|
path := filepath.Join(t.TempDir(), "traffic.json")
|
|
store, err := openTrafficStore(path)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
uuid := "123e4567-e89b-42d3-a456-426614174000"
|
|
if created, err := store.initialize(uuid, time.Unix(1, 0).UTC()); err != nil || !created {
|
|
t.Fatalf("initialize traffic state created=%v err=%v", created, err)
|
|
}
|
|
// Model the post-rename/fsync-error window: the durable file contains the
|
|
// entry while the caller has conservatively restored an older in-memory map.
|
|
delete(store.entries, uuid)
|
|
backend := &Libvirt{traffic: store}
|
|
if err := backend.removeAbsentVMTrafficState(uuid); err != nil {
|
|
t.Fatalf("remove absent VM traffic state: %v", err)
|
|
}
|
|
if _, exists := store.entries[uuid]; exists {
|
|
t.Fatal("traffic state remained in memory after confirmed VM absence")
|
|
}
|
|
reopened, err := openTrafficStore(path)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if _, exists := reopened.entries[uuid]; exists {
|
|
t.Fatal("traffic state removal was not persisted before absence confirmation")
|
|
}
|
|
}
|
|
|
|
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 TestCreateReconciliationUsesDomainMutationSerialization(t *testing.T) {
|
|
for _, action := range []string{
|
|
"create-vm", "delete-vm", "reinstall-vm", "create-snapshot:key", "delete-snapshot:key", "restore-snapshot:key",
|
|
} {
|
|
if !reconciliationNeedsDomainMutationLock(action) {
|
|
t.Fatalf("reconciliation action %q is not serialized", action)
|
|
}
|
|
}
|
|
for _, action := range []string{"power-start", "create-snapshot", "list-vms"} {
|
|
if reconciliationNeedsDomainMutationLock(action) {
|
|
t.Fatalf("read-only or independently safe action %q was unexpectedly serialized", action)
|
|
}
|
|
}
|
|
}
|
|
|
|
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 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 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) {
|
|
if !cleanTrafficFilterXMLValid(`<filter name="clean-traffic" chain="root"/>`) {
|
|
t.Fatal("valid clean-traffic baseline was rejected")
|
|
}
|
|
for _, document := range []string{
|
|
`<filter name="other" chain="root"/>`, `<filter`, ``,
|
|
} {
|
|
if cleanTrafficFilterXMLValid(document) {
|
|
t.Fatalf("invalid clean-traffic baseline accepted: %q", document)
|
|
}
|
|
}
|
|
}
|
|
|
|
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 TestDiskTransitionCommitEvidenceSurvivesJournalCrashWindows(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")
|
|
}
|
|
legacyClean := &domainMetadataXML{RootVolumeName: replacement}
|
|
if !diskTransitionCommitted(legacyClean, replacement, "reinstall", "operation-reinstall-one") {
|
|
t.Fatal("legacy marker-cleared committed root was not accepted")
|
|
}
|
|
legacyClean.PendingCleanupVolume = rootVolumeName("123e4567-e89b-42d3-a456-426614174000")
|
|
if diskTransitionCommitted(legacyClean, replacement, "reinstall", "operation-reinstall-one") {
|
|
t.Fatal("marker-free transition with pending cleanup 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 TestMissingVMResolvesExpectedMutationAsFailureWithoutReplay(t *testing.T) {
|
|
record := operation.Record{
|
|
Action: "service-suspend",
|
|
ExpectedState: &operation.ExpectedState{Kind: operation.ExpectedServiceState, TransitionID: "operation-service-missing"},
|
|
}
|
|
resolution := expectedStateResourceAbsent(record)
|
|
if !resolution.Resolved || resolution.Succeeded || resolution.ErrorCode != "expected_state_resource_absent" {
|
|
t.Fatalf("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"/><source bridge="br0"/></interface>` +
|
|
`<interface type="network"><mac address="52:54:00:00:00:02"/><source network="default"/></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 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 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),
|
|
"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"
|
|
rescuePath := "/var/lib/libvirt/images/rescue-0123456789abcdef0123456789abcdef.iso"
|
|
staged, err := mutateDomainDocument(document, func(domain *libvirtxml.Domain, metadata *domainMetadataXML) error {
|
|
domain.OS.BootDevices = []libvirtxml.DomainBootDevice{{Dev: "cdrom"}, {Dev: "hd"}}
|
|
domain.Devices.Disks = append(domain.Devices.Disks, libvirtxml.DomainDisk{
|
|
Device: "cdrom", Driver: &libvirtxml.DomainDiskDriver{Name: "qemu", Type: "raw"},
|
|
Source: &libvirtxml.DomainDiskSource{File: &libvirtxml.DomainDiskSourceFile{File: rescuePath}},
|
|
Target: &libvirtxml.DomainDiskTarget{Dev: "sdb", Bus: "sata"}, ReadOnly: &libvirtxml.DomainDiskReadOnly{},
|
|
})
|
|
metadata.RescueTemplateID = templateID
|
|
return nil
|
|
})
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if err := validateRescueDefinition(staged, templateID, rescuePath); err != nil {
|
|
t.Fatalf("valid staged rescue rejected: %v", err)
|
|
}
|
|
if err := validateRescueDefinition(staged, templateID, rescuePath+".foreign"); err == nil {
|
|
t.Fatal("foreign rescue volume path was accepted")
|
|
}
|
|
}
|
|
|
|
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),
|
|
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),
|
|
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,
|
|
networks: true, nwfilter: true, q35UEFI: true, liveBackup: true,
|
|
}
|
|
features := all.features()
|
|
if !features.CreateVM || !features.Reinstall || !features.Templates || !features.UEFI || features.SecureBoot {
|
|
t.Fatalf("fully satisfied features = %#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 || features.Templates || features.UserISO || features.Reconciliation {
|
|
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)
|
|
}
|
|
|
|
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 TestNetworkSelectorCapabilityIgnoresUnavailableOptionalSelector(t *testing.T) {
|
|
selectors := map[string]LocalNetworkSource{
|
|
"public": {Type: "bridge", Source: "br0"},
|
|
"private": {Type: "network", Source: "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{"public"}) {
|
|
t.Fatalf("usable selectors = %#v", usable)
|
|
}
|
|
|
|
delete(selectors, "public")
|
|
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 TestNetworkSelectorCapabilitiesAreSortedAndOmitHostSources(t *testing.T) {
|
|
selectors := map[string]LocalNetworkSource{
|
|
"zeta": {Type: "bridge", Source: "br-secret-z"},
|
|
"private": {Type: "network", Source: "inactive-secret"},
|
|
"alpha": {Type: "bridge", Source: "br-secret-a"},
|
|
}
|
|
check, usable := probeNetworkSelectorSet(selectors,
|
|
func(source LocalNetworkSource) CapabilityCheck {
|
|
if source.Source == "inactive-secret" {
|
|
return unavailableCheck(capabilitySelectorUnusable)
|
|
}
|
|
return availableCheck()
|
|
})
|
|
if !check.Available {
|
|
t.Fatalf("usable selector set was rejected: %#v", check)
|
|
}
|
|
if !reflect.DeepEqual(usable, []string{"alpha", "zeta"}) {
|
|
t.Fatalf("usable selectors = %#v", usable)
|
|
}
|
|
for _, selector := range usable {
|
|
if strings.Contains(selector, "secret") {
|
|
t.Fatalf("host network source leaked as selector: %q", selector)
|
|
}
|
|
}
|
|
}
|
|
|
|
func TestNetworkSelectorCapabilitiesAreBounded(t *testing.T) {
|
|
selectors := make(map[string]LocalNetworkSource,
|
|
maxCapabilityNetworkSelectors+1)
|
|
for index := 0; index <= maxCapabilityNetworkSelectors; index++ {
|
|
selectors[fmt.Sprintf("network-%02d", index)] = LocalNetworkSource{
|
|
Type: "bridge", Source: fmt.Sprintf("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)
|
|
}
|
|
}
|
|
|
|
func TestRequestedNetworkSelectorMustBeUsable(t *testing.T) {
|
|
selectors := map[string]LocalNetworkSource{
|
|
"public": {Type: "bridge", Source: "br0"},
|
|
"private": {Type: "network", Source: "default"},
|
|
}
|
|
probe := func(source LocalNetworkSource) CapabilityCheck {
|
|
if source.Source == "br0" {
|
|
return availableCheck()
|
|
}
|
|
return unavailableCheck(capabilitySelectorUnusable)
|
|
}
|
|
selected, err := resolveConfiguredNetworkSelector(
|
|
selectors, "public", probe,
|
|
)
|
|
if err != nil || selected.Source != "br0" {
|
|
t.Fatalf("usable selector = %#v, %v", selected, err)
|
|
}
|
|
if _, err := resolveConfiguredNetworkSelector(
|
|
selectors, "private", probe,
|
|
); !errors.Is(err, ErrUnavailable) {
|
|
t.Fatalf("inactive requested selector error = %v", err)
|
|
}
|
|
if _, err := resolveConfiguredNetworkSelector(
|
|
selectors, "missing", probe,
|
|
); !errors.Is(err, ErrInvalid) {
|
|
t.Fatalf("missing requested selector error = %v", err)
|
|
}
|
|
}
|
|
|
|
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")
|
|
}
|
|
}
|