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.
135 lines
4.8 KiB
Go
135 lines
4.8 KiB
Go
//go:build libvirt
|
|
|
|
package hypervisor
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"os"
|
|
"path/filepath"
|
|
"strings"
|
|
"testing"
|
|
|
|
"git.luoli.live/root/virt/agent/operation"
|
|
)
|
|
|
|
func TestEveryDomainCreateUsesTheFailClosedPublicGuardGateway(t *testing.T) {
|
|
entries, err := os.ReadDir(".")
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
for _, entry := range entries {
|
|
name := entry.Name()
|
|
if entry.IsDir() || !strings.HasSuffix(name, ".go") || strings.HasSuffix(name, "_test.go") {
|
|
continue
|
|
}
|
|
contents, err := os.ReadFile(filepath.Clean(name))
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
count := strings.Count(string(contents), ".Create()")
|
|
if name == "libvirt_public_guard.go" {
|
|
if count != 1 || !strings.Contains(string(contents), "func (l *Libvirt) startManagedDomainWithLease") {
|
|
t.Fatalf("public guard gateway contains %d raw Domain.Create calls, want exactly one", count)
|
|
}
|
|
continue
|
|
}
|
|
if count != 0 {
|
|
t.Fatalf("%s bypasses the fail-closed public guard gateway with %d raw Domain.Create calls", name, count)
|
|
}
|
|
}
|
|
}
|
|
|
|
func TestPublicLinkRestorePathsAcquireTheSameTransitionLease(t *testing.T) {
|
|
contents, err := os.ReadFile("libvirt_domain_features.go")
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
document := string(contents)
|
|
for _, marker := range []string{
|
|
`if enabled {`,
|
|
`guardLease, err = l.acquirePublicGuardTransition(ctx)`,
|
|
`"public network guard is not ready for network restore"`,
|
|
`"public network guard is not ready for rescue network restore"`,
|
|
} {
|
|
if !strings.Contains(document, marker) {
|
|
t.Fatalf("public link restore contract is missing %q", marker)
|
|
}
|
|
}
|
|
}
|
|
|
|
func TestChangeNetworkRollbackUsesOneDetachedBoundedContext(t *testing.T) {
|
|
contents, err := os.ReadFile("libvirt_domain_features.go")
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
document := string(contents)
|
|
for _, marker := range []string{
|
|
`rollbackCtx, cancelRollback := storageRollbackContext(ctx)`,
|
|
`l.reconcilePublicGuardLease(rollbackCtx, guardLease, nil)`,
|
|
`l.startManagedDomainWithLease(rollbackCtx, guardLease, domain, "restart original domain after public guard failure")`,
|
|
`l.uploadVolume(rollbackCtx, seed, originalSeed)`,
|
|
`l.startManagedDomainWithLease(rollbackCtx, guardLease, original, "restart original domain after rollback")`,
|
|
`l.startManagedDomain(rollbackCtx, original, "restart original domain after rollback")`,
|
|
} {
|
|
if !strings.Contains(document, marker) {
|
|
t.Fatalf("detached network rollback contract is missing %q", marker)
|
|
}
|
|
}
|
|
ctx, cancel := context.WithCancel(context.Background())
|
|
cancel()
|
|
rollbackCtx, cancelRollback := storageRollbackContext(ctx)
|
|
defer cancelRollback()
|
|
if err := rollbackCtx.Err(); err != nil {
|
|
t.Fatalf("rollback context inherited caller cancellation: %v", err)
|
|
}
|
|
}
|
|
|
|
func TestPeerMigrationRevalidatesAndPinsExactVPCRouteAtDefineAndActivation(t *testing.T) {
|
|
adapter, err := os.ReadFile("libvirt_adapter.go")
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
peer, err := os.ReadFile("libvirt_peer_migration.go")
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if !strings.Contains(string(adapter), `l.vpcRouter.AcquireLocalPortLease(`) ||
|
|
!strings.Contains(string(adapter), `defer routeLease.Release()`) {
|
|
t.Fatal("staged migration does not pin the exact VPC route through domain definition")
|
|
}
|
|
activation := string(peer)
|
|
if !strings.Contains(activation, `func (l *Libvirt) ActivatePeerMigration`) ||
|
|
!strings.Contains(activation, `l.vpcRouter.AcquireLocalPortLease(`) ||
|
|
!strings.Contains(activation, `"peer migration target routing no longer authorizes the exact VPC port"`) {
|
|
t.Fatal("peer migration activation does not revalidate and pin the exact VPC route")
|
|
}
|
|
}
|
|
|
|
func TestDomainStartFinalizationPreservesPreDispatchAndPostDispatchSemantics(t *testing.T) {
|
|
preDispatch := operation.MarkNotApplied(errors.New("domain XML read failed before Create"))
|
|
reconcileTimeout := context.DeadlineExceeded
|
|
got := finishManagedDomainStart(preDispatch, reconcileTimeout)
|
|
if !operation.IsNotApplied(got) || operation.IsUncertain(got) || !errors.Is(got, preDispatch) {
|
|
t.Fatalf("pre-dispatch start finalization = %v", got)
|
|
}
|
|
|
|
createResponseLost := operation.MarkUncertain(errors.New("Domain.Create response lost"))
|
|
got = finishManagedDomainStart(createResponseLost, errors.New("guard convergence failed"))
|
|
if !operation.IsUncertain(got) || !errors.Is(got, createResponseLost) {
|
|
t.Fatalf("post-dispatch start finalization = %v", got)
|
|
}
|
|
|
|
got = finishManagedDomainStart(nil, errors.New("guard convergence failed after successful Create"))
|
|
if !operation.IsUncertain(got) || operation.IsNotApplied(got) {
|
|
t.Fatalf("successful Create with failed finalization = %v", got)
|
|
}
|
|
}
|
|
|
|
func TestDomainStartGuardAcquireFailureWasNotDispatched(t *testing.T) {
|
|
err := (&Libvirt{}).startManagedDomain(context.Background(), nil, "test start")
|
|
if !operation.IsNotApplied(err) || operation.IsUncertain(err) {
|
|
t.Fatalf("guard unavailable = %v, want explicit not-applied", err)
|
|
}
|
|
}
|