95 lines
2.8 KiB
Go
95 lines
2.8 KiB
Go
package dnstt
|
|
|
|
import (
|
|
"net/netip"
|
|
"testing"
|
|
"time"
|
|
)
|
|
|
|
func TestCollectorTracksActiveAndPeakClients(t *testing.T) {
|
|
now := time.Unix(1000, 0)
|
|
c := NewCollector([]string{"tunnel.example.com"}, WithNow(func() time.Time { return now }))
|
|
|
|
c.RecordQuery("tunnel.example.com", "client-a", 120)
|
|
c.RecordQuery("tunnel.example.com", "client-b", 80)
|
|
c.RecordResponse("tunnel.example.com", 200)
|
|
|
|
snapshot := c.Snapshot()
|
|
tunnel := snapshot.Tunnels["tunnel.example.com"]
|
|
if tunnel.ActiveClients != 2 {
|
|
t.Fatalf("active clients = %d, want 2", tunnel.ActiveClients)
|
|
}
|
|
if tunnel.PeakClients != 2 {
|
|
t.Fatalf("peak clients = %d, want 2", tunnel.PeakClients)
|
|
}
|
|
if tunnel.TotalSessions != 2 {
|
|
t.Fatalf("total sessions = %d, want 2", tunnel.TotalSessions)
|
|
}
|
|
if tunnel.TotalQueries != 2 {
|
|
t.Fatalf("queries = %d, want 2", tunnel.TotalQueries)
|
|
}
|
|
if tunnel.BytesIn != 200 {
|
|
t.Fatalf("bytes in = %d, want 200", tunnel.BytesIn)
|
|
}
|
|
if tunnel.BytesOut != 200 {
|
|
t.Fatalf("bytes out = %d, want 200", tunnel.BytesOut)
|
|
}
|
|
|
|
now = now.Add(ClientTimeout + time.Second)
|
|
snapshot = c.Snapshot()
|
|
tunnel = snapshot.Tunnels["tunnel.example.com"]
|
|
if tunnel.ActiveClients != 0 {
|
|
t.Fatalf("active clients after timeout = %d, want 0", tunnel.ActiveClients)
|
|
}
|
|
if tunnel.PeakClients != 2 {
|
|
t.Fatalf("peak clients after timeout = %d, want 2", tunnel.PeakClients)
|
|
}
|
|
}
|
|
|
|
func TestCollectorMatchesSubdomainsToRegisteredTunnel(t *testing.T) {
|
|
now := time.Unix(1000, 0)
|
|
c := NewCollector([]string{"tunnel.example.com"}, WithNow(func() time.Time { return now }))
|
|
|
|
c.RecordQuery("abcd.tunnel.example.com", "client-a", 120)
|
|
|
|
tunnel := c.Snapshot().Tunnels["tunnel.example.com"]
|
|
if tunnel.TotalQueries != 1 {
|
|
t.Fatalf("queries = %d, want 1", tunnel.TotalQueries)
|
|
}
|
|
if tunnel.ActiveClients != 1 {
|
|
t.Fatalf("active clients = %d, want 1", tunnel.ActiveClients)
|
|
}
|
|
}
|
|
|
|
func TestCollectorUpdatesPeakWhenActiveClientChangesGeoKey(t *testing.T) {
|
|
now := time.Unix(1000, 0)
|
|
firstResolver := netip.MustParseAddr("192.0.2.53")
|
|
secondResolver := netip.MustParseAddr("198.51.100.53")
|
|
c := NewCollector(
|
|
[]string{"tunnel.example.com"},
|
|
WithNow(func() time.Time { return now }),
|
|
WithGeoResolver(fakeGeoResolver{
|
|
labelNames: []string{"asn"},
|
|
labels: map[netip.Addr]GeoLabels{
|
|
firstResolver: {ASN: "64500"},
|
|
secondResolver: {ASN: "64501"},
|
|
},
|
|
}),
|
|
)
|
|
|
|
c.RecordQueryFrom("tunnel.example.com", "client-a", firstResolver, 120)
|
|
c.RecordQueryFrom("tunnel.example.com", "client-a", secondResolver, 120)
|
|
|
|
foundChangedASN := false
|
|
for _, series := range c.Snapshot().Tunnels["tunnel.example.com"].Series {
|
|
if series.ASN == "64501" && series.PeakClients != 1 {
|
|
t.Fatalf("peak clients for changed ASN = %d, want 1", series.PeakClients)
|
|
}
|
|
if series.ASN == "64501" {
|
|
foundChangedASN = true
|
|
}
|
|
}
|
|
if !foundChangedASN {
|
|
t.Fatal("series for changed ASN not found")
|
|
}
|
|
}
|