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.
129 lines
4.9 KiB
Go
129 lines
4.9 KiB
Go
//go:build libvirt
|
|
|
|
package hypervisor
|
|
|
|
import (
|
|
"errors"
|
|
"testing"
|
|
|
|
"git.luoli.live/root/virt/agent/operation"
|
|
"libvirt.org/go/libvirt"
|
|
)
|
|
|
|
type snapshotGuestAgentFake struct {
|
|
probeErr error
|
|
freezeErr error
|
|
thawErr error
|
|
probeCalls int
|
|
freezeCalls int
|
|
thawCalls int
|
|
command string
|
|
timeout libvirt.DomainQemuAgentCommandTimeout
|
|
}
|
|
|
|
func (f *snapshotGuestAgentFake) QemuAgentCommand(command string,
|
|
timeout libvirt.DomainQemuAgentCommandTimeout, _ uint32) (string, error) {
|
|
f.probeCalls++
|
|
f.command = command
|
|
f.timeout = timeout
|
|
return `{"return":{}}`, f.probeErr
|
|
}
|
|
|
|
func (f *snapshotGuestAgentFake) FSFreeze(_ []string, _ uint32) error {
|
|
f.freezeCalls++
|
|
return f.freezeErr
|
|
}
|
|
|
|
func (f *snapshotGuestAgentFake) FSThaw(_ []string, _ uint32) error {
|
|
f.thawCalls++
|
|
return f.thawErr
|
|
}
|
|
|
|
func TestSnapshotConsistencyMatchesRequest(t *testing.T) {
|
|
tests := []struct {
|
|
requested string
|
|
actual string
|
|
allowed bool
|
|
}{
|
|
{requested: "filesystem", actual: "filesystem", allowed: true},
|
|
{requested: "filesystem", actual: "crash", allowed: true},
|
|
{requested: "crash", actual: "crash", allowed: true},
|
|
{requested: "crash", actual: "filesystem", allowed: false},
|
|
{requested: "filesystem", actual: "unknown", allowed: false},
|
|
}
|
|
for _, test := range tests {
|
|
if got := snapshotConsistencyMatchesRequest(test.requested, test.actual); got != test.allowed {
|
|
t.Fatalf("snapshot consistency %s -> %s allowed=%v, want %v", test.requested, test.actual, got, test.allowed)
|
|
}
|
|
}
|
|
}
|
|
|
|
func TestPrepareSnapshotConsistencySkipsProbeForExplicitCrash(t *testing.T) {
|
|
agent := &snapshotGuestAgentFake{}
|
|
actual, frozen, err := prepareSnapshotConsistency(agent, "crash")
|
|
if err != nil || actual != "crash" || frozen || agent.probeCalls != 0 || agent.freezeCalls != 0 {
|
|
t.Fatalf("explicit crash mode unexpectedly used guest agent: actual=%q frozen=%v err=%v calls=%+v", actual, frozen, err, agent)
|
|
}
|
|
}
|
|
|
|
func TestPrepareSnapshotConsistencyFallsBackWhenGuestAgentIsUnavailable(t *testing.T) {
|
|
for _, test := range []struct {
|
|
name string
|
|
code libvirt.ErrorNumber
|
|
}{
|
|
{name: "unresponsive", code: libvirt.ERR_AGENT_UNRESPONSIVE},
|
|
{name: "not-configured", code: libvirt.ERR_ARGUMENT_UNSUPPORTED},
|
|
{name: "probe-timeout", code: libvirt.ERR_AGENT_COMMAND_TIMEOUT},
|
|
} {
|
|
t.Run(test.name, func(t *testing.T) {
|
|
agent := &snapshotGuestAgentFake{probeErr: libvirt.Error{Code: test.code}}
|
|
actual, frozen, err := prepareSnapshotConsistency(agent, "filesystem")
|
|
if err != nil || actual != "crash" || frozen {
|
|
t.Fatalf("unavailable guest agent did not fall back to crash consistency: actual=%q frozen=%v err=%v", actual, frozen, err)
|
|
}
|
|
if agent.probeCalls != 1 || agent.freezeCalls != 0 || agent.thawCalls != 0 || agent.command != snapshotGuestAgentPing || agent.timeout != 5 {
|
|
t.Fatalf("unexpected guest agent fallback calls: %+v", agent)
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
func TestPrepareSnapshotConsistencyRejectsInfrastructureProbeFailure(t *testing.T) {
|
|
agent := &snapshotGuestAgentFake{probeErr: libvirt.Error{Code: libvirt.ERR_RPC}}
|
|
actual, frozen, err := prepareSnapshotConsistency(agent, "filesystem")
|
|
if err == nil || actual != "" || frozen || agent.freezeCalls != 0 || agent.thawCalls != 0 {
|
|
t.Fatalf("infrastructure failure was incorrectly downgraded: actual=%q frozen=%v err=%v calls=%+v", actual, frozen, err, agent)
|
|
}
|
|
}
|
|
|
|
func TestPrepareSnapshotConsistencyFreezesResponsiveGuest(t *testing.T) {
|
|
agent := &snapshotGuestAgentFake{}
|
|
actual, frozen, err := prepareSnapshotConsistency(agent, "filesystem")
|
|
if err != nil || actual != "filesystem" || !frozen || agent.probeCalls != 1 || agent.freezeCalls != 1 || agent.thawCalls != 0 {
|
|
t.Fatalf("responsive guest was not frozen: actual=%q frozen=%v err=%v calls=%+v", actual, frozen, err, agent)
|
|
}
|
|
}
|
|
|
|
func TestPrepareSnapshotConsistencyKeepsFreezeFailureDeterministicAfterThaw(t *testing.T) {
|
|
agent := &snapshotGuestAgentFake{freezeErr: libvirt.Error{Code: libvirt.ERR_AGENT_COMMAND_FAILED}}
|
|
actual, frozen, err := prepareSnapshotConsistency(agent, "filesystem")
|
|
var public *Error
|
|
if err == nil || actual != "" || frozen || !errors.As(err, &public) || public.Code != "snapshot_filesystem_freeze_failed" || operation.IsUncertain(err) {
|
|
t.Fatalf("freeze failure was not preserved as a deterministic error: actual=%q frozen=%v err=%v", actual, frozen, err)
|
|
}
|
|
if agent.thawCalls != 1 {
|
|
t.Fatalf("freeze failure did not attempt a compensating thaw: %+v", agent)
|
|
}
|
|
}
|
|
|
|
func TestPrepareSnapshotConsistencyMarksFailedThawUncertain(t *testing.T) {
|
|
agent := &snapshotGuestAgentFake{
|
|
freezeErr: libvirt.Error{Code: libvirt.ERR_AGENT_COMMAND_TIMEOUT},
|
|
thawErr: libvirt.Error{Code: libvirt.ERR_AGENT_UNRESPONSIVE},
|
|
}
|
|
_, _, err := prepareSnapshotConsistency(agent, "filesystem")
|
|
if err == nil || !operation.IsUncertain(err) || agent.thawCalls != 1 {
|
|
t.Fatalf("ambiguous freeze without confirmed thaw was not uncertain: err=%v calls=%+v", err, agent)
|
|
}
|
|
}
|