File size: 10,061 Bytes
ea775bd | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 | package api
import (
"net/http"
"net/netip"
"testing"
"time"
"github.com/Resinat/Resin/internal/config"
"github.com/Resinat/Resin/internal/node"
"github.com/Resinat/Resin/internal/probe"
"github.com/Resinat/Resin/internal/service"
"github.com/Resinat/Resin/internal/subscription"
"github.com/Resinat/Resin/internal/testutil"
)
func addNodeForNodeListTest(t *testing.T, cp *service.ControlPlaneService, sub *subscription.Subscription, raw string, egressIP string) {
addNodeForNodeListTestWithTag(t, cp, sub, raw, egressIP, "tag")
}
func addNodeForNodeListTestWithTag(
t *testing.T,
cp *service.ControlPlaneService,
sub *subscription.Subscription,
raw string,
egressIP string,
tag string,
) {
t.Helper()
hash := node.HashFromRawOptions([]byte(raw))
cp.Pool.AddNodeFromSub(hash, []byte(raw), sub.ID)
sub.ManagedNodes().StoreNode(hash, subscription.ManagedNode{Tags: []string{tag}})
if egressIP == "" {
return
}
entry, ok := cp.Pool.GetEntry(hash)
if !ok {
t.Fatalf("node %s missing after add", hash.Hex())
}
entry.SetEgressIP(netip.MustParseAddr(egressIP))
}
func markNodeHealthyForNodeListTest(t *testing.T, cp *service.ControlPlaneService, raw string) {
t.Helper()
hash := node.HashFromRawOptions([]byte(raw))
entry, ok := cp.Pool.GetEntry(hash)
if !ok {
t.Fatalf("node %s missing after add", hash.Hex())
}
ob := testutil.NewNoopOutbound()
entry.Outbound.Store(&ob)
entry.CircuitOpenSince.Store(0)
}
func TestHandleListNodes_TagKeywordFiltersByNodeName(t *testing.T) {
srv, cp, _ := newControlPlaneTestServer(t)
subA := subscription.NewSubscription("11111111-1111-1111-1111-111111111111", "sub-a", "https://example.com/a", true, false)
cp.SubMgr.Register(subA)
addNodeForNodeListTestWithTag(t, cp, subA, `{"type":"ss","server":"1.1.1.1","port":443}`, "", "hongkong-fast-01")
addNodeForNodeListTestWithTag(t, cp, subA, `{"type":"ss","server":"2.2.2.2","port":443}`, "", "japan-slow-01")
rec := doJSONRequest(
t,
srv,
http.MethodGet,
"/api/v1/nodes?subscription_id="+subA.ID+"&tag_keyword=FAST",
nil,
true,
)
if rec.Code != http.StatusOK {
t.Fatalf("list nodes with tag_keyword status: got %d, want %d, body=%s", rec.Code, http.StatusOK, rec.Body.String())
}
body := decodeJSONMap(t, rec)
if body["total"] != float64(1) {
t.Fatalf("tag_keyword total: got %v, want 1", body["total"])
}
}
func TestHandleListNodes_UniqueEgressIPsUsesFilteredResult(t *testing.T) {
srv, cp, _ := newControlPlaneTestServer(t)
subA := subscription.NewSubscription("11111111-1111-1111-1111-111111111111", "sub-a", "https://example.com/a", true, false)
subB := subscription.NewSubscription("22222222-2222-2222-2222-222222222222", "sub-b", "https://example.com/b", true, false)
cp.SubMgr.Register(subA)
cp.SubMgr.Register(subB)
const rawA1 = `{"type":"ss","server":"1.1.1.1","port":443}`
const rawA2 = `{"type":"ss","server":"2.2.2.2","port":443}`
const rawA3 = `{"type":"ss","server":"3.3.3.3","port":443}`
const rawA4 = `{"type":"ss","server":"4.4.4.4","port":443}`
const rawB1 = `{"type":"ss","server":"5.5.5.5","port":443}`
addNodeForNodeListTest(t, cp, subA, rawA1, "203.0.113.10")
addNodeForNodeListTest(t, cp, subA, rawA2, "203.0.113.10")
addNodeForNodeListTest(t, cp, subA, rawA3, "203.0.113.11")
addNodeForNodeListTest(t, cp, subA, rawA4, "")
addNodeForNodeListTest(t, cp, subB, rawB1, "203.0.113.99")
// Healthy condition: has outbound + not circuit-open.
markNodeHealthyForNodeListTest(t, cp, rawA1)
markNodeHealthyForNodeListTest(t, cp, rawA2)
rec := doJSONRequest(t, srv, http.MethodGet, "/api/v1/nodes?subscription_id="+subA.ID, nil, true)
if rec.Code != http.StatusOK {
t.Fatalf("list nodes status: got %d, want %d, body=%s", rec.Code, http.StatusOK, rec.Body.String())
}
body := decodeJSONMap(t, rec)
if body["total"] != float64(4) {
t.Fatalf("total: got %v, want 4", body["total"])
}
if body["unique_egress_ips"] != float64(2) {
t.Fatalf("unique_egress_ips: got %v, want 2", body["unique_egress_ips"])
}
if body["unique_healthy_egress_ips"] != float64(1) {
t.Fatalf("unique_healthy_egress_ips: got %v, want 1", body["unique_healthy_egress_ips"])
}
rec = doJSONRequest(
t,
srv,
http.MethodGet,
"/api/v1/nodes?subscription_id="+subA.ID+"&limit=1",
nil,
true,
)
if rec.Code != http.StatusOK {
t.Fatalf("list nodes paged status: got %d, want %d, body=%s", rec.Code, http.StatusOK, rec.Body.String())
}
body = decodeJSONMap(t, rec)
if body["total"] != float64(4) {
t.Fatalf("paged total: got %v, want 4", body["total"])
}
if body["unique_egress_ips"] != float64(2) {
t.Fatalf("paged unique_egress_ips: got %v, want 2", body["unique_egress_ips"])
}
if body["unique_healthy_egress_ips"] != float64(1) {
t.Fatalf("paged unique_healthy_egress_ips: got %v, want 1", body["unique_healthy_egress_ips"])
}
rec = doJSONRequest(
t,
srv,
http.MethodGet,
"/api/v1/nodes?subscription_id="+subA.ID+"&egress_ip=203.0.113.10",
nil,
true,
)
if rec.Code != http.StatusOK {
t.Fatalf("list nodes with egress filter status: got %d, want %d, body=%s", rec.Code, http.StatusOK, rec.Body.String())
}
body = decodeJSONMap(t, rec)
if body["total"] != float64(2) {
t.Fatalf("filtered total: got %v, want 2", body["total"])
}
if body["unique_egress_ips"] != float64(1) {
t.Fatalf("filtered unique_egress_ips: got %v, want 1", body["unique_egress_ips"])
}
if body["unique_healthy_egress_ips"] != float64(1) {
t.Fatalf("filtered unique_healthy_egress_ips: got %v, want 1", body["unique_healthy_egress_ips"])
}
}
func TestHandleListNodes_IncludesReferenceLatencyMs(t *testing.T) {
srv, cp, runtimeCfg := newControlPlaneTestServer(t)
cfg := config.NewDefaultRuntimeConfig()
cfg.LatencyAuthorities = []string{"cloudflare.com", "github.com", "google.com"}
runtimeCfg.Store(cfg)
subA := subscription.NewSubscription("11111111-1111-1111-1111-111111111111", "sub-a", "https://example.com/a", true, false)
cp.SubMgr.Register(subA)
raw := `{"type":"ss","server":"1.1.1.1","port":443}`
hash := node.HashFromRawOptions([]byte(raw))
addNodeForNodeListTest(t, cp, subA, raw, "203.0.113.10")
entry, ok := cp.Pool.GetEntry(hash)
if !ok {
t.Fatalf("node %s missing after add", hash.Hex())
}
entry.LatencyTable.LoadEntry("cloudflare.com", node.DomainLatencyStats{
Ewma: 40 * time.Millisecond,
LastUpdated: time.Now(),
})
entry.LatencyTable.LoadEntry("github.com", node.DomainLatencyStats{
Ewma: 80 * time.Millisecond,
LastUpdated: time.Now(),
})
entry.LatencyTable.LoadEntry("example.com", node.DomainLatencyStats{
Ewma: 10 * time.Millisecond,
LastUpdated: time.Now(),
})
rec := doJSONRequest(t, srv, http.MethodGet, "/api/v1/nodes?subscription_id="+subA.ID, nil, true)
if rec.Code != http.StatusOK {
t.Fatalf("list nodes status: got %d, want %d, body=%s", rec.Code, http.StatusOK, rec.Body.String())
}
body := decodeJSONMap(t, rec)
items, ok := body["items"].([]any)
if !ok || len(items) != 1 {
t.Fatalf("items mismatch: got %T len=%d", body["items"], len(items))
}
item, ok := items[0].(map[string]any)
if !ok {
t.Fatalf("item type: got %T", items[0])
}
if item["reference_latency_ms"] != float64(60) {
t.Fatalf("reference_latency_ms: got %v, want 60", item["reference_latency_ms"])
}
}
func TestHandleProbeEgress_ReturnsRegion(t *testing.T) {
srv, cp, _ := newControlPlaneTestServer(t)
sub := subscription.NewSubscription("11111111-1111-1111-1111-111111111111", "sub-a", "https://example.com/a", true, false)
cp.SubMgr.Register(sub)
raw := []byte(`{"type":"ss","server":"1.1.1.1","port":443}`)
hash := node.HashFromRawOptions(raw)
cp.Pool.AddNodeFromSub(hash, raw, sub.ID)
sub.ManagedNodes().StoreNode(hash, subscription.ManagedNode{Tags: []string{"tag"}})
entry, ok := cp.Pool.GetEntry(hash)
if !ok {
t.Fatalf("node %s missing after add", hash.Hex())
}
ob := testutil.NewNoopOutbound()
entry.Outbound.Store(&ob)
cp.ProbeMgr = probe.NewProbeManager(probe.ProbeConfig{
Pool: cp.Pool,
Fetcher: func(_ node.Hash, _ string) ([]byte, time.Duration, error) {
return []byte("ip=203.0.113.88\nloc=JP"), 25 * time.Millisecond, nil
},
})
rec := doJSONRequest(t, srv, http.MethodPost, "/api/v1/nodes/"+hash.Hex()+"/actions/probe-egress", nil, true)
if rec.Code != http.StatusOK {
t.Fatalf("probe-egress status: got %d, want %d, body=%s", rec.Code, http.StatusOK, rec.Body.String())
}
body := decodeJSONMap(t, rec)
if body["egress_ip"] != "203.0.113.88" {
t.Fatalf("egress_ip: got %v, want %q", body["egress_ip"], "203.0.113.88")
}
if body["region"] != "jp" {
t.Fatalf("region: got %v, want %q", body["region"], "jp")
}
}
func TestHandleListNodes_EnabledFilter(t *testing.T) {
srv, cp, _ := newControlPlaneTestServer(t)
subEnabled := subscription.NewSubscription("11111111-1111-1111-1111-111111111111", "sub-enabled", "https://example.com/a", true, false)
subDisabled := subscription.NewSubscription("22222222-2222-2222-2222-222222222222", "sub-disabled", "https://example.com/b", false, false)
cp.SubMgr.Register(subEnabled)
cp.SubMgr.Register(subDisabled)
addNodeForNodeListTestWithTag(t, cp, subEnabled, `{"type":"ss","server":"1.1.1.1","port":443}`, "", "enabled-tag")
addNodeForNodeListTestWithTag(t, cp, subDisabled, `{"type":"ss","server":"2.2.2.2","port":443}`, "", "disabled-tag")
rec := doJSONRequest(t, srv, http.MethodGet, "/api/v1/nodes?enabled=true", nil, true)
if rec.Code != http.StatusOK {
t.Fatalf("enabled=true status: got %d, want %d, body=%s", rec.Code, http.StatusOK, rec.Body.String())
}
body := decodeJSONMap(t, rec)
if body["total"] != float64(1) {
t.Fatalf("enabled=true total: got %v, want 1", body["total"])
}
rec = doJSONRequest(t, srv, http.MethodGet, "/api/v1/nodes?enabled=false", nil, true)
if rec.Code != http.StatusOK {
t.Fatalf("enabled=false status: got %d, want %d, body=%s", rec.Code, http.StatusOK, rec.Body.String())
}
body = decodeJSONMap(t, rec)
if body["total"] != float64(1) {
t.Fatalf("enabled=false total: got %v, want 1", body["total"])
}
}
|