rg / internal /platform /platform_test.go
xjackkx's picture
Upload 307 files
ea775bd verified
Raw History Blame Contribute Delete
9.75 kB
package platform
import (
"net/netip"
"regexp"
"testing"
"time"
"github.com/Resinat/Resin/internal/node"
"github.com/Resinat/Resin/internal/testutil"
)
// makeFullyRoutableEntry creates a NodeEntry that passes all 5 filter conditions.
func makeFullyRoutableEntry(hash node.Hash, subIDs ...string) *node.NodeEntry {
e := node.NewNodeEntry(hash, nil, time.Now(), 16)
for _, id := range subIDs {
e.AddSubscriptionID(id)
}
// Set all conditions to pass.
e.LatencyTable.LoadEntry("example.com", node.DomainLatencyStats{
Ewma: 100 * time.Millisecond,
LastUpdated: time.Now(),
})
ob := testutil.NewNoopOutbound()
e.Outbound.Store(&ob)
e.SetEgressIP(netip.MustParseAddr("1.2.3.4"))
return e
}
func alwaysLookup(subID string, hash node.Hash) (string, bool, []string, bool) {
return "TestSub", true, []string{"us-node", "fast"}, true
}
func usGeoLookup(addr netip.Addr) string { return "us" }
func TestPlatform_EvaluateNode_AllPass(t *testing.T) {
p := NewPlatform("p1", "Test", nil, nil) // no filters
h := makeHash(`{"type":"ss"}`)
entry := makeFullyRoutableEntry(h, "sub1")
p.FullRebuild(func(fn func(node.Hash, *node.NodeEntry) bool) {
fn(h, entry)
}, alwaysLookup, usGeoLookup)
if p.View().Size() != 1 {
t.Fatalf("expected 1 routable node, got %d", p.View().Size())
}
}
func TestPlatform_EvaluateNode_CircuitOpen(t *testing.T) {
p := NewPlatform("p1", "Test", nil, nil)
h := makeHash(`{"type":"ss"}`)
entry := makeFullyRoutableEntry(h, "sub1")
entry.CircuitOpenSince.Store(time.Now().UnixNano()) // circuit open
p.FullRebuild(func(fn func(node.Hash, *node.NodeEntry) bool) {
fn(h, entry)
}, alwaysLookup, usGeoLookup)
if p.View().Size() != 0 {
t.Fatal("circuit-broken node should not be routable")
}
}
func TestPlatform_EvaluateNode_NoLatency(t *testing.T) {
p := NewPlatform("p1", "Test", nil, nil)
h := makeHash(`{"type":"ss"}`)
// Create entry without latency table (maxLatencyTableEntries=0).
entry := node.NewNodeEntry(h, nil, time.Now(), 0)
entry.AddSubscriptionID("sub1")
ob := testutil.NewNoopOutbound()
entry.Outbound.Store(&ob)
entry.SetEgressIP(netip.MustParseAddr("1.2.3.4"))
p.FullRebuild(func(fn func(node.Hash, *node.NodeEntry) bool) {
fn(h, entry)
}, alwaysLookup, usGeoLookup)
if p.View().Size() != 0 {
t.Fatal("node without latency should not be routable")
}
}
func TestPlatform_EvaluateNode_NoOutbound(t *testing.T) {
p := NewPlatform("p1", "Test", nil, nil)
h := makeHash(`{"type":"ss"}`)
entry := makeFullyRoutableEntry(h, "sub1")
entry.Outbound.Store(nil) // no outbound
p.FullRebuild(func(fn func(node.Hash, *node.NodeEntry) bool) {
fn(h, entry)
}, alwaysLookup, usGeoLookup)
if p.View().Size() != 0 {
t.Fatal("node without outbound should not be routable")
}
}
func TestPlatform_EvaluateNode_NoEgressIP(t *testing.T) {
p := NewPlatform("p1", "Test", nil, nil) // no region filters
h := makeHash(`{"type":"ss"}`)
entry := makeFullyRoutableEntry(h, "sub1")
entry.SetEgressIP(netip.Addr{}) // egress unknown
p.FullRebuild(func(fn func(node.Hash, *node.NodeEntry) bool) {
fn(h, entry)
}, alwaysLookup, usGeoLookup)
if p.View().Size() != 0 {
t.Fatal("node without egress IP should not be routable")
}
}
func TestPlatform_EvaluateNode_RegexFilter(t *testing.T) {
regexes := []*regexp.Regexp{regexp.MustCompile("us")}
p := NewPlatform("p1", "Test", regexes, nil)
h := makeHash(`{"type":"ss"}`)
entry := makeFullyRoutableEntry(h, "sub1")
// Lookup returns "TestSub/us-node" which matches "us".
p.FullRebuild(func(fn func(node.Hash, *node.NodeEntry) bool) {
fn(h, entry)
}, alwaysLookup, usGeoLookup)
if p.View().Size() != 1 {
t.Fatal("node matching regex should be routable")
}
// Now with a "jp" filter β€” should NOT match.
p2 := NewPlatform("p2", "Test", []*regexp.Regexp{regexp.MustCompile("^jp")}, nil)
p2.FullRebuild(func(fn func(node.Hash, *node.NodeEntry) bool) {
fn(h, entry)
}, alwaysLookup, usGeoLookup)
if p2.View().Size() != 0 {
t.Fatal("node not matching regex should not be routable")
}
}
func TestPlatform_EvaluateNode_RegionFilter(t *testing.T) {
p := NewPlatform("p1", "Test", nil, []string{"us"})
h := makeHash(`{"type":"ss"}`)
entry := makeFullyRoutableEntry(h, "sub1")
p.FullRebuild(func(fn func(node.Hash, *node.NodeEntry) bool) {
fn(h, entry)
}, alwaysLookup, usGeoLookup)
if p.View().Size() != 1 {
t.Fatal("node in allowed region should be routable")
}
// Region filter "jp" β€” node has US egress, should fail.
p2 := NewPlatform("p2", "Test", nil, []string{"jp"})
p2.FullRebuild(func(fn func(node.Hash, *node.NodeEntry) bool) {
fn(h, entry)
}, alwaysLookup, usGeoLookup)
if p2.View().Size() != 0 {
t.Fatal("node not in allowed region should not be routable")
}
}
func TestPlatform_EvaluateNode_RegionFilter_NoEgressIP(t *testing.T) {
p := NewPlatform("p1", "Test", nil, []string{"us"})
h := makeHash(`{"type":"ss"}`)
entry := makeFullyRoutableEntry(h, "sub1")
// Don't set egress IP β€” clear it.
entry.SetEgressIP(netip.Addr{})
p.FullRebuild(func(fn func(node.Hash, *node.NodeEntry) bool) {
fn(h, entry)
}, alwaysLookup, usGeoLookup)
if p.View().Size() != 0 {
t.Fatal("node without egress IP should not be routable")
}
}
func TestPlatform_EvaluateNode_RegionFilter_PrefersStoredRegion(t *testing.T) {
p := NewPlatform("p1", "Test", nil, []string{"jp"})
h := makeHash(`{"type":"ss"}`)
entry := makeFullyRoutableEntry(h, "sub1")
entry.SetEgressRegion("jp")
geoCalled := false
geoLookup := func(netip.Addr) string {
geoCalled = true
return "us"
}
p.FullRebuild(func(fn func(node.Hash, *node.NodeEntry) bool) {
fn(h, entry)
}, alwaysLookup, geoLookup)
if p.View().Size() != 1 {
t.Fatal("stored region should be used before GeoIP fallback")
}
if geoCalled {
t.Fatal("GeoIP lookup should be skipped when stored region exists")
}
}
func TestPlatform_EvaluateNode_RegionFilter_ExcludeOnlyUnknownRegion(t *testing.T) {
p := NewPlatform("p1", "Test", nil, []string{"!hk"})
h := makeHash(`{"type":"ss"}`)
entry := makeFullyRoutableEntry(h, "sub1")
geoLookup := func(netip.Addr) string { return "" }
p.FullRebuild(func(fn func(node.Hash, *node.NodeEntry) bool) {
fn(h, entry)
}, alwaysLookup, geoLookup)
if p.View().Size() != 0 {
t.Fatal("node with unknown region should not be routable when region filters are configured")
}
}
func TestMatchRegionFilter(t *testing.T) {
tests := []struct {
name string
filters []string
region string
want bool
}{
{
name: "include only match",
filters: []string{"hk", "us"},
region: "hk",
want: true,
},
{
name: "include only miss",
filters: []string{"hk", "us"},
region: "jp",
want: false,
},
{
name: "exclude only",
filters: []string{"!hk"},
region: "us",
want: true,
},
{
name: "exclude only blocked",
filters: []string{"!hk"},
region: "hk",
want: false,
},
{
name: "exclude only unknown region",
filters: []string{"!hk"},
region: "",
want: false,
},
{
name: "mixed include and exclude allows expected",
filters: []string{"hk", "!us"},
region: "hk",
want: true,
},
{
name: "mixed include and exclude blocks excluded",
filters: []string{"hk", "!us"},
region: "us",
want: false,
},
{
name: "mixed include and same exclude blocks",
filters: []string{"hk", "!hk"},
region: "hk",
want: false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := MatchRegionFilter(tt.region, tt.filters); got != tt.want {
t.Fatalf("MatchRegionFilter(%q, %v) = %v, want %v", tt.region, tt.filters, got, tt.want)
}
})
}
}
func TestPlatform_NotifyDirty_AddRemove(t *testing.T) {
p := NewPlatform("p1", "Test", nil, nil)
h := makeHash(`{"type":"ss"}`)
entry := makeFullyRoutableEntry(h, "sub1")
entryStore := map[node.Hash]*node.NodeEntry{h: entry}
getEntry := func(hash node.Hash) (*node.NodeEntry, bool) {
e, ok := entryStore[hash]
return e, ok
}
// Initially empty β€” add via NotifyDirty.
p.NotifyDirty(h, getEntry, alwaysLookup, usGeoLookup)
if p.View().Size() != 1 {
t.Fatal("NotifyDirty should add passing node")
}
// Circuit-break β†’ NotifyDirty removes.
entry.CircuitOpenSince.Store(time.Now().UnixNano())
p.NotifyDirty(h, getEntry, alwaysLookup, usGeoLookup)
if p.View().Size() != 0 {
t.Fatal("NotifyDirty should remove circuit-broken node")
}
// Recover β†’ NotifyDirty re-adds.
entry.CircuitOpenSince.Store(0)
p.NotifyDirty(h, getEntry, alwaysLookup, usGeoLookup)
if p.View().Size() != 1 {
t.Fatal("NotifyDirty should re-add recovered node")
}
// Delete from pool β†’ NotifyDirty removes.
delete(entryStore, h)
p.NotifyDirty(h, getEntry, alwaysLookup, usGeoLookup)
if p.View().Size() != 0 {
t.Fatal("NotifyDirty should remove deleted node")
}
}
func TestPlatform_FullRebuild_ClearsOld(t *testing.T) {
p := NewPlatform("p1", "Test", nil, nil)
h1 := makeHash(`{"type":"ss","n":1}`)
h2 := makeHash(`{"type":"ss","n":2}`)
e1 := makeFullyRoutableEntry(h1, "sub1")
e2 := makeFullyRoutableEntry(h2, "sub1")
// First rebuild with 2 nodes.
p.FullRebuild(func(fn func(node.Hash, *node.NodeEntry) bool) {
fn(h1, e1)
fn(h2, e2)
}, alwaysLookup, usGeoLookup)
if p.View().Size() != 2 {
t.Fatalf("expected 2, got %d", p.View().Size())
}
// Second rebuild with only 1 node β€” old entries cleared.
p.FullRebuild(func(fn func(node.Hash, *node.NodeEntry) bool) {
fn(h1, e1)
}, alwaysLookup, usGeoLookup)
if p.View().Size() != 1 {
t.Fatalf("expected 1 after rebuild, got %d", p.View().Size())
}
if p.View().Contains(h2) {
t.Fatal("h2 should have been removed by rebuild")
}
}