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/vpc_capability_test.go

115 lines
3.7 KiB
Go

package hypervisor
import (
"errors"
"net"
"os"
"reflect"
"strconv"
"testing"
)
type vpcCapabilityProbeError struct{ code string }
func (e vpcCapabilityProbeError) Error() string { return e.code }
func (e vpcCapabilityProbeError) PublicCode() string { return e.code }
type vpcCapabilityProbeRouter struct {
err error
calls int
}
func (r *vpcCapabilityProbeRouter) Available() error {
r.calls++
return r.err
}
func TestNormalizeVPCTransportCandidates(t *testing.T) {
candidates, check := normalizeVPCTransportCandidates([]vpcInterfaceSnapshot{
{Name: "lo", Flags: net.FlagUp | net.FlagLoopback, MTU: 65536,
Addresses: []string{"127.0.0.1/8"}},
{Name: "br-private", Flags: net.FlagUp | net.FlagBroadcast, MTU: 1500,
Addresses: []string{
"172.16.0.11/24", "172.16.0.11/24", "203.0.113.5/24", "fe80::1/64",
}, Bridge: true},
{Name: "bond0", Flags: net.FlagUp, MTU: 9000,
Addresses: []string{"10.20.30.40/16"}},
{Name: "down0", Flags: 0, MTU: 1500,
Addresses: []string{"192.168.1.2/24"}},
})
if !check.Available {
t.Fatalf("private candidates rejected: %#v", check)
}
want := []VPCTransportCandidate{
{AddressCIDR: "10.20.30.40/16", Address: "10.20.30.40", Prefix: 16,
Interface: "bond0", MTU: 9000, Bridge: false},
{AddressCIDR: "172.16.0.11/24", Address: "172.16.0.11", Prefix: 24,
Interface: "br-private", MTU: 1500, Bridge: true},
}
if !reflect.DeepEqual(candidates, want) {
t.Fatalf("private candidates = %#v, want %#v", candidates, want)
}
}
func TestVPCTransportCandidatesFailClosed(t *testing.T) {
candidates, check := normalizeVPCTransportCandidates([]vpcInterfaceSnapshot{{
Name: "eth0", Flags: net.FlagUp, MTU: 1500,
Addresses: []string{"203.0.113.5/24"},
}})
if check.Available || check.Reason != vpcNoTransportCandidates || len(candidates) != 0 {
t.Fatalf("public-only candidate result = %#v, %#v", candidates, check)
}
many := make([]vpcInterfaceSnapshot, 0, maxVPCTransportCandidates+1)
for index := 0; index <= maxVPCTransportCandidates; index++ {
many = append(many, vpcInterfaceSnapshot{
Name: "br0", Flags: net.FlagUp, MTU: 1500,
Addresses: []string{privateTestCIDR(index)}, Bridge: true,
})
}
candidates, check = normalizeVPCTransportCandidates(many)
if check.Available || check.Reason != capabilityInvalid || len(candidates) != 0 {
t.Fatalf("oversized candidate result = %#v, %#v", candidates, check)
}
}
func TestProbeIPv4Forwarding(t *testing.T) {
path := t.TempDir() + "/ip_forward"
if err := os.WriteFile(path, []byte("0\n"), 0o600); err != nil {
t.Fatal(err)
}
if check := probeIPv4Forwarding(path); check.Available ||
check.Reason != vpcIPv4ForwardingDisabled {
t.Fatalf("disabled forwarding check = %#v", check)
}
if err := os.WriteFile(path, []byte("1\n"), 0o600); err != nil {
t.Fatal(err)
}
if check := probeIPv4Forwarding(path); !check.Available || check.Reason != "" {
t.Fatalf("enabled forwarding check = %#v", check)
}
if check := probeIPv4Forwarding(path + "-missing"); check.Available ||
check.Reason != capabilityUnreadable {
t.Fatalf("missing forwarding check = %#v", check)
}
}
func TestProbeVPCL3CapabilityPreservesReadinessFailureCode(t *testing.T) {
router := &vpcCapabilityProbeRouter{err: errors.Join(
errors.New("readiness failed"),
vpcCapabilityProbeError{code: "vpc_firewalld_conflict"},
)}
_, checks := probeVPCL3Capability(router)
if router.calls != 1 {
t.Fatalf("router availability calls=%d, want one", router.calls)
}
check := checks["vpc_l3_apply"]
if check.Available || check.Reason != "vpc_firewalld_conflict" {
t.Fatalf("VPC readiness check=%#v, want exact firewalld reason", check)
}
}
func privateTestCIDR(index int) string {
return "10.0." + strconv.Itoa(index) + ".1/24"
}