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/cmd/virt-agent/network_observation_test.go

133 lines
4.5 KiB
Go

package main
import (
"context"
"errors"
"sync/atomic"
"testing"
"time"
"git.luoli.live/root/virt/agent/hypervisor"
"git.luoli.live/root/virt/agent/networkobserve"
"git.luoli.live/root/virt/agent/publicnetwork"
)
const observationTestGeneration = "0123456789abcdef0123456789abcdef"
type observationInventoryBackend struct {
vms []hypervisor.VMMetadata
metrics map[string]hypervisor.Metrics
metricErrs map[string]error
}
func (backend observationInventoryBackend) ListVMs(context.Context) ([]hypervisor.VMMetadata, error) {
return append([]hypervisor.VMMetadata(nil), backend.vms...), nil
}
func (backend observationInventoryBackend) GetMetrics(_ context.Context, uuid string) (hypervisor.Metrics, error) {
if err := backend.metricErrs[uuid]; err != nil {
return hypervisor.Metrics{}, err
}
return backend.metrics[uuid], nil
}
type observationSource struct{ closed atomic.Int32 }
func (*observationSource) Probe(context.Context) (networkobserve.ProbeEvidence, error) {
return networkobserve.ProbeEvidence{Conntrack: true, IPv4: true, IPv6: true}, nil
}
func (*observationSource) Snapshot(context.Context) (networkobserve.KernelSnapshot, error) {
return networkobserve.KernelSnapshot{}, nil
}
func (source *observationSource) Close() error {
source.closed.Add(1)
return nil
}
func observationPublicNetwork(ipv4, ipv6 string) publicnetwork.Intent {
return publicnetwork.Intent{
Contract: publicnetwork.ContractVersion, NetworkSelector: "bridge:br0",
IPv4: publicnetwork.AddressIntent{
PoolKey: "11111111111111111111111111111111", PoolCIDR: "8.8.8.0/24",
AddressCIDR: ipv4 + "/24", Gateway: "8.8.8.1",
},
IPv6: publicnetwork.AddressIntent{
PoolKey: "22222222222222222222222222222222", PoolCIDR: "2606:4700:4700::/64",
AddressCIDR: ipv6 + "/64", Gateway: "2606:4700:4700::1",
},
DNSServers: []string{"1.1.1.1", "2606:4700:4700::1111"},
}
}
func TestHypervisorObservationInventoryKeepsExactIdentityWhenMetricsUnavailable(t *testing.T) {
const first = "123e4567-e89b-42d3-a456-426614174000"
const second = "223e4567-e89b-42d3-a456-426614174000"
rxPackets := uint64(12)
backend := observationInventoryBackend{
vms: []hypervisor.VMMetadata{
{UUID: first, PublicNetwork: observationPublicNetwork("8.8.8.10", "2606:4700:4700::10")},
{UUID: second, PublicNetwork: observationPublicNetwork("8.8.8.11", "2606:4700:4700::11")},
},
metrics: map[string]hypervisor.Metrics{
first: {UUID: first, RXBytes: 100, TXBytes: 200, RXPackets: &rxPackets},
},
metricErrs: map[string]error{second: errors.New("domain is stopped")},
}
inventory := hypervisorNetworkObservationInventory{backend: backend}
targets, err := inventory.NetworkObservationInventory(context.Background())
if err != nil {
t.Fatal(err)
}
if len(targets) != 2 || targets[0].IPv4.String() != "8.8.8.10" ||
targets[1].IPv6.String() != "2606:4700:4700::11" {
t.Fatalf("targets=%#v", targets)
}
if targets[0].Interface.RXBytes == nil || *targets[0].Interface.RXBytes != 100 ||
targets[0].Interface.RXPackets == nil || *targets[0].Interface.RXPackets != 12 {
t.Fatalf("first counters=%#v", targets[0].Interface)
}
if targets[1].Interface.RXBytes != nil || targets[1].Interface.TXBytes != nil {
t.Fatalf("failed metrics must remain explicit nil counters: %#v", targets[1].Interface)
}
}
func TestHypervisorObservationInventoryNeverGuessesInvalidPublicIdentity(t *testing.T) {
backend := observationInventoryBackend{
vms: []hypervisor.VMMetadata{{
UUID: "123e4567-e89b-42d3-a456-426614174000",
PublicNetwork: observationPublicNetwork("10.0.0.10", "2606:4700:4700::10"),
}},
metrics: map[string]hypervisor.Metrics{}, metricErrs: map[string]error{},
}
_, err := (hypervisorNetworkObservationInventory{backend: backend}).NetworkObservationInventory(context.Background())
if err == nil {
t.Fatal("invalid public identity was accepted")
}
}
func TestNetworkObservationRuntimeStopsBeforeHypervisorClosure(t *testing.T) {
source := &observationSource{}
runtime, err := newNetworkObservationRuntimeWithSource(
observationInventoryBackend{metrics: map[string]hypervisor.Metrics{}, metricErrs: map[string]error{}},
source, observationTestGeneration,
)
if err != nil {
t.Fatal(err)
}
parent, cancelParent := context.WithCancel(context.Background())
if err := runtime.Start(parent); err != nil {
t.Fatal(err)
}
cancelParent()
ctx, cancel := context.WithTimeout(context.Background(), time.Second)
defer cancel()
if err := runtime.Shutdown(ctx); err != nil {
t.Fatal(err)
}
if source.closed.Load() != 1 {
t.Fatalf("source close count=%d", source.closed.Load())
}
}