Download internal/api/contract_test.go from xjackkx/rg: direct link, hf CLI and curl.
- Browser
- Download file 72.2 kB
-
https://huggingface.co/spaces/xjackkx/rg/resolve/main/internal/api/contract_test.go
- Command line
-
hf download hf://spaces/xjackkx/rg/internal/api/contract_test.go
-
curl -L -o contract_test.go https://huggingface.co/spaces/xjackkx/rg/resolve/main/internal/api/contract_test.go
72.2 kB
package api
import (
"bytes"
"encoding/base64"
"encoding/json"
"errors"
"net/http"
"net/http/httptest"
"net/netip"
"net/url"
"path/filepath"
"strings"
"sync/atomic"
"testing"
"time"
"github.com/Resinat/Resin/internal/config"
"github.com/Resinat/Resin/internal/geoip"
"github.com/Resinat/Resin/internal/metrics"
"github.com/Resinat/Resin/internal/model"
"github.com/Resinat/Resin/internal/node"
"github.com/Resinat/Resin/internal/platform"
"github.com/Resinat/Resin/internal/proxy"
"github.com/Resinat/Resin/internal/requestlog"
"github.com/Resinat/Resin/internal/routing"
"github.com/Resinat/Resin/internal/service"
"github.com/Resinat/Resin/internal/state"
"github.com/Resinat/Resin/internal/subscription"
"github.com/Resinat/Resin/internal/testutil"
"github.com/Resinat/Resin/internal/topology"
)
const testAdminToken = "test-admin-token"
func newControlPlaneTestServer(t *testing.T) (*Server, *service.ControlPlaneService, *atomic.Pointer[config.RuntimeConfig]) {
return newControlPlaneTestServerWithBodyLimit(t, 1<<20)
}
func newControlPlaneTestServerWithBodyLimit(
t *testing.T,
apiMaxBodyBytes int64,
) (*Server, *service.ControlPlaneService, *atomic.Pointer[config.RuntimeConfig]) {
t.Helper()
root := t.TempDir()
engine, closer, err := state.PersistenceBootstrap(
filepath.Join(root, "state"),
filepath.Join(root, "cache"),
)
if err != nil {
t.Fatalf("PersistenceBootstrap: %v", err)
}
t.Cleanup(func() { _ = closer.Close() })
runtimeCfg := &atomic.Pointer[config.RuntimeConfig]{}
runtimeCfg.Store(config.NewDefaultRuntimeConfig())
subMgr := topology.NewSubscriptionManager()
pool := topology.NewGlobalNodePool(topology.PoolConfig{
SubLookup: subMgr.Lookup,
GeoLookup: func(netip.Addr) string { return "us" },
MaxLatencyTableEntries: 32,
MaxConsecutiveFailures: func() int { return 3 },
LatencyDecayWindow: func() time.Duration { return 10 * time.Minute },
})
router := routing.NewRouter(routing.RouterConfig{
Pool: pool,
Authorities: func() []string { return []string{"cloudflare.com"} },
P2CWindow: func() time.Duration { return 10 * time.Minute },
})
scheduler := topology.NewSubscriptionScheduler(topology.SchedulerConfig{
SubManager: subMgr,
Pool: pool,
Fetcher: func(string) ([]byte, error) {
return nil, errors.New("test fetcher failure")
},
})
geoSvc := geoip.NewService(geoip.ServiceConfig{
CacheDir: filepath.Join(root, "geoip"),
OpenDB: geoip.NoOpOpen,
})
cp := &service.ControlPlaneService{
Engine: engine,
Pool: pool,
SubMgr: subMgr,
Scheduler: scheduler,
Router: router,
GeoIP: geoSvc,
MatcherRuntime: proxy.NewAccountMatcherRuntime(nil),
RuntimeCfg: runtimeCfg,
EnvCfg: &config.EnvConfig{
DefaultPlatformStickyTTL: 30 * time.Minute,
DefaultPlatformRegexFilters: []string{},
DefaultPlatformRegionFilters: []string{},
DefaultPlatformReverseProxyMissAction: "TREAT_AS_EMPTY",
DefaultPlatformReverseProxyEmptyAccountBehavior: "ACCOUNT_HEADER_RULE",
DefaultPlatformReverseProxyFixedAccountHeader: "Authorization",
DefaultPlatformAllocationPolicy: "BALANCED",
},
}
systemInfo := service.SystemInfo{
Version: "1.0.0-test",
GitCommit: "abc123",
BuildTime: "2026-01-01T00:00:00Z",
StartedAt: time.Date(2026, 1, 1, 12, 0, 0, 0, time.UTC),
}
srv := NewServer(0, testAdminToken, systemInfo, runtimeCfg, cp.EnvCfg, cp, apiMaxBodyBytes, nil, nil)
return srv, cp, runtimeCfg
}
func doJSONRequest(t *testing.T, srv *Server, method, path string, body any, authed bool) *httptest.ResponseRecorder {
t.Helper()
var reqBody []byte
var err error
if body != nil {
switch v := body.(type) {
case []byte:
reqBody = v
case string:
reqBody = []byte(v)
default:
reqBody, err = json.Marshal(v)
if err != nil {
t.Fatalf("marshal request body: %v", err)
}
}
}
req := httptest.NewRequest(method, path, bytes.NewReader(reqBody))
if body != nil {
req.Header.Set("Content-Type", "application/json; charset=utf-8")
}
if authed {
req.Header.Set("Authorization", "Bearer "+testAdminToken)
}
rec := httptest.NewRecorder()
srv.Handler().ServeHTTP(rec, req)
return rec
}
func decodeJSONMap(t *testing.T, rec *httptest.ResponseRecorder) map[string]any {
t.Helper()
var m map[string]any
if err := json.Unmarshal(rec.Body.Bytes(), &m); err != nil {
t.Fatalf("unmarshal body: %v body=%q", err, rec.Body.String())
}
return m
}
func assertErrorCode(t *testing.T, rec *httptest.ResponseRecorder, code string) {
t.Helper()
var er ErrorResponse
if err := json.Unmarshal(rec.Body.Bytes(), &er); err != nil {
t.Fatalf("unmarshal error response: %v body=%q", err, rec.Body.String())
}
if er.Error.Code != code {
t.Fatalf("error code: got %q, want %q (body=%s)", er.Error.Code, code, rec.Body.String())
}
}
func mustCreatePlatform(t *testing.T, srv *Server, name string) string {
t.Helper()
rec := doJSONRequest(t, srv, http.MethodPost, "/api/v1/platforms", map[string]any{
"name": name,
}, true)
if rec.Code != http.StatusCreated {
t.Fatalf("create platform status: got %d, want %d, body=%s", rec.Code, http.StatusCreated, rec.Body.String())
}
body := decodeJSONMap(t, rec)
id, _ := body["id"].(string)
if id == "" {
t.Fatalf("create platform missing id: body=%s", rec.Body.String())
}
return id
}
type contractRuntimeStats struct {
platformID string
}
func (contractRuntimeStats) TotalNodes() int { return 20 }
func (contractRuntimeStats) HealthyNodes() int { return 15 }
func (contractRuntimeStats) EgressIPCount() int { return 6 }
func (contractRuntimeStats) UniqueHealthyEgressIPCount() int {
return 4
}
func (s contractRuntimeStats) LeaseCountsByPlatform() map[string]int {
return map[string]int{s.platformID: 0}
}
func (s contractRuntimeStats) RoutableNodeCount(platformID string) (int, bool) {
if platformID != s.platformID {
return 0, false
}
return 8, true
}
func (s contractRuntimeStats) PlatformEgressIPCount(platformID string) (int, bool) {
if platformID != s.platformID {
return 0, false
}
return 3, true
}
func (p contractRuntimeStats) CollectNodeEWMAs(platformID string) []float64 {
if platformID == "" {
return []float64{50, 150, 280}
}
if platformID == p.platformID {
return []float64{80, 120}
}
return nil
}
func newObservabilityTestServer(t *testing.T) (*Server, *requestlog.Repo, *metrics.Manager, string) {
t.Helper()
root := t.TempDir()
runtimeCfg := &atomic.Pointer[config.RuntimeConfig]{}
runtimeCfg.Store(config.NewDefaultRuntimeConfig())
systemInfo := service.SystemInfo{
Version: "1.0.0-test",
GitCommit: "abc123",
BuildTime: "2026-01-01T00:00:00Z",
StartedAt: time.Date(2026, 1, 1, 12, 0, 0, 0, time.UTC),
}
requestlogRepo := requestlog.NewRepo(root, 64*1024*1024, 2)
if err := requestlogRepo.Open(); err != nil {
t.Fatalf("requestlogRepo.Open: %v", err)
}
t.Cleanup(func() { _ = requestlogRepo.Close() })
metricsRepo, err := metrics.NewMetricsRepo(filepath.Join(root, "metrics.db"))
if err != nil {
t.Fatalf("metrics.NewMetricsRepo: %v", err)
}
t.Cleanup(func() { _ = metricsRepo.Close() })
platformID := "platform-1"
metricsManager := metrics.NewManager(metrics.ManagerConfig{
Repo: metricsRepo,
LatencyBinMs: 100,
LatencyOverflowMs: 3000,
BucketSeconds: 300,
ThroughputRealtimeCapacity: 16,
ThroughputIntervalSec: 1,
ConnectionsRealtimeCapacity: 16,
ConnectionsIntervalSec: 5,
LeasesRealtimeCapacity: 16,
LeasesIntervalSec: 5,
RuntimeStats: contractRuntimeStats{platformID: platformID},
})
srv := NewServer(0, testAdminToken, systemInfo, runtimeCfg, nil, nil, 1<<20, requestlogRepo, metricsManager)
return srv, requestlogRepo, metricsManager, platformID
}
func seedObservabilityData(
t *testing.T,
requestlogRepo *requestlog.Repo,
metricsManager *metrics.Manager,
platformID string,
) string {
t.Helper()
logID := "log-contract-1"
inserted, err := requestlogRepo.InsertBatch([]proxy.RequestLogEntry{
{
ID: logID,
StartedAtNs: time.Now().Add(-2 * time.Minute).UnixNano(),
ProxyType: proxy.ProxyTypeReverse,
ClientIP: "127.0.0.1",
PlatformID: platformID,
PlatformName: "Platform One",
Account: "acct-1",
TargetHost: "example.com",
TargetURL: "https://example.com/api",
NodeHash: "node-1",
NodeTag: "sub/tag-1",
EgressIP: "8.8.8.8",
DurationNs: int64(45 * time.Millisecond),
NetOK: true,
HTTPMethod: "GET",
HTTPStatus: 200,
IngressBytes: 210,
EgressBytes: 120,
ReqHeadersLen: 10,
ReqBodyLen: 11,
RespHeadersLen: 12,
RespBodyLen: 13,
ReqHeadersTruncated: true,
ReqBodyTruncated: true,
RespHeadersTruncated: false,
RespBodyTruncated: true,
ReqHeaders: []byte("req-h-1"),
ReqBody: []byte("req-b-1"),
RespHeaders: []byte("resp-h-1"),
RespBody: []byte("resp-b-1"),
},
{
ID: "log-contract-2",
StartedAtNs: time.Now().Add(-time.Minute).UnixNano(),
ProxyType: proxy.ProxyTypeForward,
ClientIP: "127.0.0.2",
PlatformID: platformID,
Account: "acct-2",
TargetHost: "example.org",
TargetURL: "https://example.org/resource",
DurationNs: int64(12 * time.Millisecond),
NetOK: false,
HTTPMethod: "POST",
HTTPStatus: 502,
},
})
if err != nil {
t.Fatalf("requestlogRepo.InsertBatch: %v", err)
}
if inserted != 2 {
t.Fatalf("inserted: got %d, want %d", inserted, 2)
}
now := time.Now()
metricsManager.ThroughputRing().Push(metrics.RealtimeSample{
Timestamp: now.Add(-30 * time.Second),
IngressBPS: 123,
EgressBPS: 456,
})
metricsManager.ConnectionsRing().Push(metrics.RealtimeSample{
Timestamp: now.Add(-30 * time.Second),
InboundConns: 7,
OutboundConns: 5,
})
metricsManager.LeasesRing().Push(metrics.RealtimeSample{
Timestamp: now.Add(-30 * time.Second),
LeasesByPlatform: map[string]int{platformID: 3},
})
metricsManager.OnTrafficDelta(1000, 1500)
metricsManager.OnRequestFinished(proxy.RequestFinishedEvent{
PlatformID: platformID,
ProxyType: proxy.ProxyTypeForward,
NetOK: true,
DurationNs: int64(120 * time.Millisecond),
})
metricsManager.OnRequestFinished(proxy.RequestFinishedEvent{
PlatformID: platformID,
ProxyType: proxy.ProxyTypeForward,
NetOK: false,
DurationNs: int64(240 * time.Millisecond),
})
metricsManager.OnProbeEvent(metrics.ProbeEvent{Kind: metrics.ProbeKindEgress})
metricsManager.OnLeaseEvent(metrics.LeaseMetricEvent{
PlatformID: platformID,
Op: metrics.LeaseOpRemove,
LifetimeNs: int64(30 * time.Second),
})
metricsManager.Stop() // ForceFlush bucket data without starting background loops.
trafficRows, err := metricsManager.Repo().QueryTraffic(0, time.Now().Add(time.Hour).Unix())
if err != nil {
t.Fatalf("metrics QueryTraffic: %v", err)
}
if len(trafficRows) == 0 {
t.Fatal("expected flushed traffic rows")
}
bucketStart := trafficRows[0].BucketStartUnix
if err := metricsManager.Repo().WriteNodePoolSnapshot(bucketStart, 20, 15, 6); err != nil {
t.Fatalf("WriteNodePoolSnapshot: %v", err)
}
if err := metricsManager.Repo().WriteLatencyBucket(bucketStart, "", []int64{1, 2, 3}); err != nil {
t.Fatalf("WriteLatencyBucket global: %v", err)
}
if err := metricsManager.Repo().WriteLatencyBucket(bucketStart, platformID, []int64{2, 1, 0}); err != nil {
t.Fatalf("WriteLatencyBucket platform: %v", err)
}
return logID
}
func TestAPIContract_HealthzAndAuth(t *testing.T) {
srv, _, _ := newControlPlaneTestServer(t)
rec := doJSONRequest(t, srv, http.MethodGet, "/healthz", nil, false)
if rec.Code != http.StatusOK {
t.Fatalf("healthz status: got %d, want %d", rec.Code, http.StatusOK)
}
rec = doJSONRequest(t, srv, http.MethodGet, "/api/v1/platforms", nil, false)
if rec.Code != http.StatusUnauthorized {
t.Fatalf("unauthorized status: got %d, want %d", rec.Code, http.StatusUnauthorized)
}
assertErrorCode(t, rec, "UNAUTHORIZED")
}
func TestAPIContract_RequestBodyTooLarge(t *testing.T) {
srv, _, _ := newControlPlaneTestServerWithBodyLimit(t, 64)
largeName := strings.Repeat("a", 256)
rec := doJSONRequest(t, srv, http.MethodPost, "/api/v1/platforms", map[string]any{
"name": largeName,
}, true)
if rec.Code != http.StatusRequestEntityTooLarge {
t.Fatalf("status: got %d, want %d, body=%s", rec.Code, http.StatusRequestEntityTooLarge, rec.Body.String())
}
assertErrorCode(t, rec, "PAYLOAD_TOO_LARGE")
}
func TestAPIContract_GetLease_AccountPathEncoding(t *testing.T) {
srv, cp, _ := newControlPlaneTestServer(t)
platformID := mustCreatePlatform(t, srv, "lease-account-encoding")
account := "team%2Fa"
hash := node.HashFromRawOptions([]byte(`{"type":"ss","server":"1.1.1.1","port":443}`))
now := time.Now().UnixNano()
cp.Router.RestoreLeases([]model.Lease{
{
PlatformID: platformID,
Account: account,
NodeHash: hash.Hex(),
EgressIP: "1.2.3.4",
ExpiryNs: now + int64(time.Hour),
LastAccessedNs: now,
},
})
// Encode "%" as %25 in the path so one decode pass yields the literal account "team%2Fa".
rec := doJSONRequest(
t,
srv,
http.MethodGet,
"/api/v1/platforms/"+platformID+"/leases/team%252Fa",
nil,
true,
)
if rec.Code != http.StatusOK {
t.Fatalf("status: got %d, want %d, body=%s", rec.Code, http.StatusOK, rec.Body.String())
}
body := decodeJSONMap(t, rec)
if body["account"] != account {
t.Fatalf("account: got %v, want %q", body["account"], account)
}
}
func TestAPIContract_GetLease_IncludesNodeTag(t *testing.T) {
srv, cp, _ := newControlPlaneTestServer(t)
platformID := mustCreatePlatform(t, srv, "lease-node-tag")
older := subscription.NewSubscription("sub-old", "Z-Provider", "https://example.com/sub-old", true, false)
older.CreatedAtNs = 100
olderManaged := subscription.NewManagedNodes()
newer := subscription.NewSubscription("sub-new", "A-Provider", "https://example.com/sub-new", true, false)
newer.CreatedAtNs = 200
newerManaged := subscription.NewManagedNodes()
hash := node.HashFromRawOptions([]byte(`{"type":"ss","server":"198.51.100.50","port":443}`))
olderManaged.StoreNode(hash, subscription.ManagedNode{Tags: []string{"zz", "aa"}})
newerManaged.StoreNode(hash, subscription.ManagedNode{Tags: []string{"00"}})
older.SwapManagedNodes(olderManaged)
newer.SwapManagedNodes(newerManaged)
cp.SubMgr.Register(older)
cp.SubMgr.Register(newer)
raw := json.RawMessage(`{"type":"ss","server":"198.51.100.50","port":443}`)
cp.Pool.AddNodeFromSub(hash, raw, older.ID)
cp.Pool.AddNodeFromSub(hash, raw, newer.ID)
now := time.Now().UnixNano()
cp.Router.RestoreLeases([]model.Lease{
{
PlatformID: platformID,
Account: "alice",
NodeHash: hash.Hex(),
EgressIP: "203.0.113.11",
ExpiryNs: now + int64(time.Hour),
LastAccessedNs: now,
},
})
rec := doJSONRequest(
t,
srv,
http.MethodGet,
"/api/v1/platforms/"+platformID+"/leases/alice",
nil,
true,
)
if rec.Code != http.StatusOK {
t.Fatalf("status: got %d, want %d, body=%s", rec.Code, http.StatusOK, rec.Body.String())
}
body := decodeJSONMap(t, rec)
if body["node_tag"] != "Z-Provider/aa" {
t.Fatalf("node_tag: got %v, want %q", body["node_tag"], "Z-Provider/aa")
}
}
func TestAPIContract_ListLeases_AccountFuzzySearch(t *testing.T) {
srv, cp, _ := newControlPlaneTestServer(t)
platformID := mustCreatePlatform(t, srv, "lease-account-fuzzy")
hash := node.HashFromRawOptions([]byte(`{"type":"ss","server":"203.0.113.10","port":443}`))
now := time.Now().UnixNano()
cp.Router.RestoreLeases([]model.Lease{
{
PlatformID: platformID,
Account: "alpha-user-01",
NodeHash: hash.Hex(),
EgressIP: "203.0.113.10",
ExpiryNs: now + int64(time.Hour),
LastAccessedNs: now,
},
{
PlatformID: platformID,
Account: "BETA-USER-02",
NodeHash: hash.Hex(),
EgressIP: "203.0.113.11",
ExpiryNs: now + int64(time.Hour),
LastAccessedNs: now,
},
})
exactRec := doJSONRequest(
t,
srv,
http.MethodGet,
"/api/v1/platforms/"+platformID+"/leases?account=user",
nil,
true,
)
if exactRec.Code != http.StatusOK {
t.Fatalf("exact list leases status: got %d, want %d, body=%s", exactRec.Code, http.StatusOK, exactRec.Body.String())
}
exactBody := decodeJSONMap(t, exactRec)
exactItems, ok := exactBody["items"].([]any)
if !ok {
t.Fatalf("exact leases items type: got %T", exactBody["items"])
}
if len(exactItems) != 0 {
t.Fatalf("exact leases items len: got %d, want 0, body=%s", len(exactItems), exactRec.Body.String())
}
fuzzyRec := doJSONRequest(
t,
srv,
http.MethodGet,
"/api/v1/platforms/"+platformID+"/leases?account=user&fuzzy=true",
nil,
true,
)
if fuzzyRec.Code != http.StatusOK {
t.Fatalf("fuzzy list leases status: got %d, want %d, body=%s", fuzzyRec.Code, http.StatusOK, fuzzyRec.Body.String())
}
fuzzyBody := decodeJSONMap(t, fuzzyRec)
fuzzyItems, ok := fuzzyBody["items"].([]any)
if !ok {
t.Fatalf("fuzzy leases items type: got %T", fuzzyBody["items"])
}
if len(fuzzyItems) != 2 {
t.Fatalf("fuzzy leases items len: got %d, want 2, body=%s", len(fuzzyItems), fuzzyRec.Body.String())
}
foundAlpha := false
foundBeta := false
for _, item := range fuzzyItems {
row, ok := item.(map[string]any)
if !ok {
t.Fatalf("fuzzy lease item type: got %T", item)
}
switch row["account"] {
case "alpha-user-01":
foundAlpha = true
case "BETA-USER-02":
foundBeta = true
}
}
if !foundAlpha || !foundBeta {
t.Fatalf("fuzzy leases accounts missing expected items: foundAlpha=%v foundBeta=%v body=%s", foundAlpha, foundBeta, fuzzyRec.Body.String())
}
}
func TestAPIContract_ListLeases_FuzzyValidation(t *testing.T) {
srv, _, _ := newControlPlaneTestServer(t)
platformID := mustCreatePlatform(t, srv, "lease-fuzzy-validation")
rec := doJSONRequest(
t,
srv,
http.MethodGet,
"/api/v1/platforms/"+platformID+"/leases?fuzzy=1",
nil,
true,
)
if rec.Code != http.StatusBadRequest {
t.Fatalf("list leases fuzzy validation status: got %d, want %d, body=%s", rec.Code, http.StatusBadRequest, rec.Body.String())
}
assertErrorCode(t, rec, "INVALID_ARGUMENT")
}
func TestAPIContract_PaginationAndSorting(t *testing.T) {
srv, _, _ := newControlPlaneTestServer(t)
_ = mustCreatePlatform(t, srv, "zeta")
_ = mustCreatePlatform(t, srv, "alpha")
_ = mustCreatePlatform(t, srv, "beta")
rec := doJSONRequest(
t,
srv,
http.MethodGet,
"/api/v1/platforms?sort_by=name&sort_order=asc&limit=2&offset=1",
nil,
true,
)
if rec.Code != http.StatusOK {
t.Fatalf("list platforms 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 {
t.Fatalf("items type: got %T", body["items"])
}
if len(items) != 2 {
t.Fatalf("items len: got %d, want %d", len(items), 2)
}
item0 := items[0].(map[string]any)
item1 := items[1].(map[string]any)
if item0["name"] != "beta" || item1["name"] != "zeta" {
t.Fatalf("unexpected order: got [%v, %v]", item0["name"], item1["name"])
}
}
func TestAPIContract_PlatformListIncludesRoutableNodeCount(t *testing.T) {
srv, cp, _ := newControlPlaneTestServer(t)
platformID := mustCreatePlatform(t, srv, "routable-count-target")
sub := subscription.NewSubscription("sub-test", "sub-test", "https://example.com/sub", 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-test")
sub.ManagedNodes().StoreNode(hash, subscription.ManagedNode{Tags: []string{"seed"}})
entry, ok := cp.Pool.GetEntry(hash)
if !ok {
t.Fatalf("node %s missing after AddNodeFromSub", hash.Hex())
}
entry.SetEgressIP(netip.MustParseAddr("203.0.113.10"))
if entry.LatencyTable == nil {
t.Fatalf("node %s latency table not initialized", hash.Hex())
}
entry.LatencyTable.Update("example.com", 25*time.Millisecond, 10*time.Minute)
ob := testutil.NewNoopOutbound()
entry.Outbound.Store(&ob)
cp.Pool.RecordResult(hash, true)
cp.Pool.NotifyNodeDirty(hash)
rec := doJSONRequest(t, srv, http.MethodGet, "/api/v1/platforms?keyword=routable-count-target", nil, true)
if rec.Code != http.StatusOK {
t.Fatalf("list platforms 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 {
t.Fatalf("items type: got %T", body["items"])
}
if len(items) != 1 {
t.Fatalf("items len: got %d, want %d", len(items), 1)
}
item, ok := items[0].(map[string]any)
if !ok {
t.Fatalf("item type: got %T", items[0])
}
if item["id"] != platformID {
t.Fatalf("item id: got %v, want %s", item["id"], platformID)
}
if item["routable_node_count"] != float64(1) {
t.Fatalf("routable_node_count: got %v, want %v", item["routable_node_count"], 1)
}
rec = doJSONRequest(t, srv, http.MethodGet, "/api/v1/platforms/"+platformID, nil, true)
if rec.Code != http.StatusOK {
t.Fatalf("get platform status: got %d, want %d, body=%s", rec.Code, http.StatusOK, rec.Body.String())
}
body = decodeJSONMap(t, rec)
if body["routable_node_count"] != float64(1) {
t.Fatalf("get platform routable_node_count: got %v, want %v", body["routable_node_count"], 1)
}
}
func TestAPIContract_PlatformList_BuiltInFirstWithSortBy(t *testing.T) {
srv, cp, _ := newControlPlaneTestServer(t)
if err := cp.Engine.UpsertPlatform(model.Platform{
ID: platform.DefaultPlatformID,
Name: platform.DefaultPlatformName,
StickyTTLNs: int64(30 * time.Minute),
RegexFilters: []string{},
RegionFilters: []string{},
ReverseProxyMissAction: string(platform.ReverseProxyMissActionTreatAsEmpty),
AllocationPolicy: string(platform.AllocationPolicyBalanced),
UpdatedAtNs: time.Now().UnixNano(),
}); err != nil {
t.Fatalf("upsert default platform: %v", err)
}
_ = mustCreatePlatform(t, srv, "aaa")
_ = mustCreatePlatform(t, srv, "bbb")
rec := doJSONRequest(
t,
srv,
http.MethodGet,
"/api/v1/platforms?sort_by=name&sort_order=desc&limit=3&offset=0",
nil,
true,
)
if rec.Code != http.StatusOK {
t.Fatalf("list platforms 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 {
t.Fatalf("items type: got %T", body["items"])
}
if len(items) != 3 {
t.Fatalf("items len: got %d, want %d", len(items), 3)
}
item0 := items[0].(map[string]any)
item1 := items[1].(map[string]any)
item2 := items[2].(map[string]any)
if item0["id"] != platform.DefaultPlatformID || item1["name"] != "bbb" || item2["name"] != "aaa" {
t.Fatalf(
"unexpected order: [%v,%v,%v]",
item0["name"], item1["name"], item2["name"],
)
}
}
func TestAPIContract_KeywordFilteringOnListEndpoints(t *testing.T) {
srv, _, _ := newControlPlaneTestServer(t)
rec := doJSONRequest(t, srv, http.MethodPost, "/api/v1/platforms", map[string]any{
"name": "Alpha-HK",
"region_filters": []string{"hk"},
}, true)
if rec.Code != http.StatusCreated {
t.Fatalf("create platform alpha status: got %d, want %d, body=%s", rec.Code, http.StatusCreated, rec.Body.String())
}
rec = doJSONRequest(t, srv, http.MethodPost, "/api/v1/platforms", map[string]any{
"name": "Beta-US",
"region_filters": []string{"us"},
}, true)
if rec.Code != http.StatusCreated {
t.Fatalf("create platform beta status: got %d, want %d, body=%s", rec.Code, http.StatusCreated, rec.Body.String())
}
rec = doJSONRequest(t, srv, http.MethodGet, "/api/v1/platforms?keyword=ALP&sort_by=name&sort_order=asc", nil, true)
if rec.Code != http.StatusOK {
t.Fatalf("list platforms keyword status: got %d, want %d, body=%s", rec.Code, http.StatusOK, rec.Body.String())
}
body := decodeJSONMap(t, rec)
platformItems, ok := body["items"].([]any)
if !ok {
t.Fatalf("platform items type: got %T", body["items"])
}
if len(platformItems) != 1 {
t.Fatalf("platform items len: got %d, want %d, body=%s", len(platformItems), 1, rec.Body.String())
}
platform0 := platformItems[0].(map[string]any)
if platform0["name"] != "Alpha-HK" {
t.Fatalf("platform name: got %v, want %q", platform0["name"], "Alpha-HK")
}
rec = doJSONRequest(t, srv, http.MethodGet, "/api/v1/platforms?keyword=balanced", nil, true)
if rec.Code != http.StatusOK {
t.Fatalf("list platforms metadata keyword status: got %d, want %d, body=%s", rec.Code, http.StatusOK, rec.Body.String())
}
body = decodeJSONMap(t, rec)
platformItems, ok = body["items"].([]any)
if !ok {
t.Fatalf("platform metadata items type: got %T", body["items"])
}
if len(platformItems) != 0 {
t.Fatalf("platform metadata keyword should not match, got len=%d body=%s", len(platformItems), rec.Body.String())
}
rec = doJSONRequest(t, srv, http.MethodPost, "/api/v1/subscriptions", map[string]any{
"name": "Apple Feed",
"url": "https://example.com/apple",
}, true)
if rec.Code != http.StatusCreated {
t.Fatalf("create subscription apple status: got %d, want %d, body=%s", rec.Code, http.StatusCreated, rec.Body.String())
}
rec = doJSONRequest(t, srv, http.MethodPost, "/api/v1/subscriptions", map[string]any{
"name": "Banana Feed",
"url": "https://example.com/banana",
}, true)
if rec.Code != http.StatusCreated {
t.Fatalf("create subscription banana status: got %d, want %d, body=%s", rec.Code, http.StatusCreated, rec.Body.String())
}
rec = doJSONRequest(t, srv, http.MethodGet, "/api/v1/subscriptions?keyword=BANANA&sort_by=name&sort_order=asc", nil, true)
if rec.Code != http.StatusOK {
t.Fatalf("list subscriptions keyword status: got %d, want %d, body=%s", rec.Code, http.StatusOK, rec.Body.String())
}
body = decodeJSONMap(t, rec)
subItems, ok := body["items"].([]any)
if !ok {
t.Fatalf("subscription items type: got %T", body["items"])
}
if len(subItems) != 1 {
t.Fatalf("subscription items len: got %d, want %d, body=%s", len(subItems), 1, rec.Body.String())
}
sub0 := subItems[0].(map[string]any)
if sub0["name"] != "Banana Feed" {
t.Fatalf("subscription name: got %v, want %q", sub0["name"], "Banana Feed")
}
rec = doJSONRequest(t, srv, http.MethodGet, "/api/v1/subscriptions?keyword=5m", nil, true)
if rec.Code != http.StatusOK {
t.Fatalf("list subscriptions metadata keyword status: got %d, want %d, body=%s", rec.Code, http.StatusOK, rec.Body.String())
}
body = decodeJSONMap(t, rec)
subItems, ok = body["items"].([]any)
if !ok {
t.Fatalf("subscription metadata items type: got %T", body["items"])
}
if len(subItems) != 0 {
t.Fatalf("subscription metadata keyword should not match, got len=%d body=%s", len(subItems), rec.Body.String())
}
rec = doJSONRequest(t, srv, http.MethodPut, "/api/v1/account-header-rules/api.example.com%2Fv1", map[string]any{
"headers": []string{"Authorization"},
}, true)
if rec.Code != http.StatusCreated {
t.Fatalf("create rule one status: got %d, want %d, body=%s", rec.Code, http.StatusCreated, rec.Body.String())
}
rec = doJSONRequest(t, srv, http.MethodPut, "/api/v1/account-header-rules/files.example.com%2Fv2", map[string]any{
"headers": []string{"X-Trace-ID"},
}, true)
if rec.Code != http.StatusCreated {
t.Fatalf("create rule two status: got %d, want %d, body=%s", rec.Code, http.StatusCreated, rec.Body.String())
}
rec = doJSONRequest(t, srv, http.MethodGet, "/api/v1/account-header-rules?keyword=trace", nil, true)
if rec.Code != http.StatusOK {
t.Fatalf("list rules keyword status: got %d, want %d, body=%s", rec.Code, http.StatusOK, rec.Body.String())
}
body = decodeJSONMap(t, rec)
ruleItems, ok := body["items"].([]any)
if !ok {
t.Fatalf("rule items type: got %T", body["items"])
}
if len(ruleItems) != 1 {
t.Fatalf("rule items len: got %d, want %d, body=%s", len(ruleItems), 1, rec.Body.String())
}
rule0 := ruleItems[0].(map[string]any)
if rule0["url_prefix"] != "files.example.com/v2" {
t.Fatalf("rule url_prefix: got %v, want %q", rule0["url_prefix"], "files.example.com/v2")
}
rec = doJSONRequest(t, srv, http.MethodGet, "/api/v1/account-header-rules?keyword=2026", nil, true)
if rec.Code != http.StatusOK {
t.Fatalf("list rules metadata keyword status: got %d, want %d, body=%s", rec.Code, http.StatusOK, rec.Body.String())
}
body = decodeJSONMap(t, rec)
ruleItems, ok = body["items"].([]any)
if !ok {
t.Fatalf("rule metadata items type: got %T", body["items"])
}
if len(ruleItems) != 0 {
t.Fatalf("rule metadata keyword should not match, got len=%d body=%s", len(ruleItems), rec.Body.String())
}
}
func TestAPIContract_PlatformStickyTTLMustBePositive(t *testing.T) {
srv, _, _ := newControlPlaneTestServer(t)
createCases := []struct {
name string
stickyTTL string
}{
{name: "zero", stickyTTL: "0s"},
{name: "negative", stickyTTL: "-1s"},
}
for _, tc := range createCases {
t.Run("create_"+tc.name, func(t *testing.T) {
rec := doJSONRequest(t, srv, http.MethodPost, "/api/v1/platforms", map[string]any{
"name": "sticky-create-" + tc.name,
"sticky_ttl": tc.stickyTTL,
}, true)
if rec.Code != http.StatusBadRequest {
t.Fatalf("create status: got %d, want %d, body=%s", rec.Code, http.StatusBadRequest, rec.Body.String())
}
assertErrorCode(t, rec, "INVALID_ARGUMENT")
})
}
platformID := mustCreatePlatform(t, srv, "sticky-patch-target")
for _, tc := range createCases {
t.Run("patch_"+tc.name, func(t *testing.T) {
rec := doJSONRequest(t, srv, http.MethodPatch, "/api/v1/platforms/"+platformID, map[string]any{
"sticky_ttl": tc.stickyTTL,
}, true)
if rec.Code != http.StatusBadRequest {
t.Fatalf("patch status: got %d, want %d, body=%s", rec.Code, http.StatusBadRequest, rec.Body.String())
}
assertErrorCode(t, rec, "INVALID_ARGUMENT")
})
}
}
func TestAPIContract_PlatformEmptyAccountBehavior(t *testing.T) {
srv, _, _ := newControlPlaneTestServer(t)
rec := doJSONRequest(t, srv, http.MethodPost, "/api/v1/platforms", map[string]any{
"name": "behavior-fixed-header",
"reverse_proxy_empty_account_behavior": "FIXED_HEADER",
"reverse_proxy_fixed_account_header": " authorization\nx-account-id\nX-Account-Id ",
}, true)
if rec.Code != http.StatusCreated {
t.Fatalf("create status: got %d, want %d, body=%s", rec.Code, http.StatusCreated, rec.Body.String())
}
body := decodeJSONMap(t, rec)
if body["reverse_proxy_empty_account_behavior"] != "FIXED_HEADER" {
t.Fatalf(
"create reverse_proxy_empty_account_behavior: got %v, want FIXED_HEADER",
body["reverse_proxy_empty_account_behavior"],
)
}
if body["reverse_proxy_fixed_account_header"] != "Authorization\nX-Account-Id" {
t.Fatalf(
"create reverse_proxy_fixed_account_header: got %v, want Authorization\\nX-Account-Id",
body["reverse_proxy_fixed_account_header"],
)
}
platformID, _ := body["id"].(string)
if platformID == "" {
t.Fatalf("create platform missing id: body=%s", rec.Body.String())
}
rec = doJSONRequest(t, srv, http.MethodPatch, "/api/v1/platforms/"+platformID, map[string]any{
"reverse_proxy_empty_account_behavior": "RANDOM",
"reverse_proxy_fixed_account_header": "",
}, true)
if rec.Code != http.StatusOK {
t.Fatalf("patch status: got %d, want %d, body=%s", rec.Code, http.StatusOK, rec.Body.String())
}
body = decodeJSONMap(t, rec)
if body["reverse_proxy_empty_account_behavior"] != "RANDOM" {
t.Fatalf(
"patch reverse_proxy_empty_account_behavior: got %v, want RANDOM",
body["reverse_proxy_empty_account_behavior"],
)
}
if body["reverse_proxy_fixed_account_header"] != "" {
t.Fatalf(
"patch reverse_proxy_fixed_account_header: got %v, want empty",
body["reverse_proxy_fixed_account_header"],
)
}
rec = doJSONRequest(t, srv, http.MethodPatch, "/api/v1/platforms/"+platformID, map[string]any{
"reverse_proxy_empty_account_behavior": "FIXED_HEADER",
"reverse_proxy_fixed_account_header": " ",
}, true)
if rec.Code != http.StatusBadRequest {
t.Fatalf("patch invalid status: got %d, want %d, body=%s", rec.Code, http.StatusBadRequest, rec.Body.String())
}
assertErrorCode(t, rec, "INVALID_ARGUMENT")
}
func TestAPIContract_PlatformPassiveCircuitBreaker(t *testing.T) {
srv, _, _ := newControlPlaneTestServer(t)
rec := doJSONRequest(t, srv, http.MethodPost, "/api/v1/platforms", map[string]any{
"name": "passive-breaker",
"passive_circuit_breaker_disabled": true,
}, true)
if rec.Code != http.StatusCreated {
t.Fatalf("create status: got %d, want %d, body=%s", rec.Code, http.StatusCreated, rec.Body.String())
}
body := decodeJSONMap(t, rec)
if body["passive_circuit_breaker_disabled"] != true {
t.Fatalf("create passive_circuit_breaker_disabled: got %v, want true", body["passive_circuit_breaker_disabled"])
}
platformID, _ := body["id"].(string)
if platformID == "" {
t.Fatalf("create platform missing id: body=%s", rec.Body.String())
}
rec = doJSONRequest(t, srv, http.MethodPatch, "/api/v1/platforms/"+platformID, map[string]any{
"passive_circuit_breaker_disabled": false,
}, true)
if rec.Code != http.StatusOK {
t.Fatalf("patch status: got %d, want %d, body=%s", rec.Code, http.StatusOK, rec.Body.String())
}
body = decodeJSONMap(t, rec)
if body["passive_circuit_breaker_disabled"] != false {
t.Fatalf("patch passive_circuit_breaker_disabled: got %v, want false", body["passive_circuit_breaker_disabled"])
}
rec = doJSONRequest(t, srv, http.MethodPatch, "/api/v1/platforms/"+platformID, map[string]any{
"passive_circuit_breaker_disabled": "false",
}, true)
if rec.Code != http.StatusBadRequest {
t.Fatalf("patch invalid status: got %d, want %d, body=%s", rec.Code, http.StatusBadRequest, rec.Body.String())
}
assertErrorCode(t, rec, "INVALID_ARGUMENT")
}
func TestAPIContract_SystemConfigPatchSemantics(t *testing.T) {
srv, _, runtimeCfg := newControlPlaneTestServer(t)
rec := doJSONRequest(t, srv, http.MethodPatch, "/api/v1/system/config", map[string]any{
"request_log_enabled": true,
"reverse_proxy_log_req_headers_max_bytes": 2048,
"p2c_latency_window": "7m",
"cache_flush_interval": "30s",
}, true)
if rec.Code != http.StatusOK {
t.Fatalf("patch config status: got %d, want %d, body=%s", rec.Code, http.StatusOK, rec.Body.String())
}
body := decodeJSONMap(t, rec)
if body["request_log_enabled"] != true {
t.Fatalf("request_log_enabled: got %v, want true", body["request_log_enabled"])
}
if body["reverse_proxy_log_req_headers_max_bytes"] != float64(2048) {
t.Fatalf("reverse_proxy_log_req_headers_max_bytes: got %v", body["reverse_proxy_log_req_headers_max_bytes"])
}
if body["p2c_latency_window"] != "7m0s" {
t.Fatalf("p2c_latency_window: got %v, want 7m0s", body["p2c_latency_window"])
}
if body["cache_flush_interval"] != "30s" {
t.Fatalf("cache_flush_interval: got %v, want 30s", body["cache_flush_interval"])
}
snap := runtimeCfg.Load()
if !snap.RequestLogEnabled {
t.Fatal("runtime pointer did not reflect patched request_log_enabled")
}
if snap.ReverseProxyLogReqHeadersMaxBytes != 2048 {
t.Fatalf("runtime pointer reverse_proxy_log_req_headers_max_bytes=%d, want 2048", snap.ReverseProxyLogReqHeadersMaxBytes)
}
cases := []struct {
name string
body any
}{
{name: "empty patch", body: map[string]any{}},
{name: "unknown field", body: map[string]any{"unknown_field": 1}},
{name: "removed field", body: map[string]any{"ephemeral_node_evict_delay": "1h"}},
{name: "null value", body: map[string]any{"request_log_enabled": nil}},
{name: "empty latency_test_url", body: map[string]any{"latency_test_url": ""}},
}
for _, tc := range cases {
t.Run(tc.name, func(t *testing.T) {
r := doJSONRequest(t, srv, http.MethodPatch, "/api/v1/system/config", tc.body, true)
if r.Code != http.StatusBadRequest {
t.Fatalf("status: got %d, want %d, body=%s", r.Code, http.StatusBadRequest, r.Body.String())
}
assertErrorCode(t, r, "INVALID_ARGUMENT")
})
}
}
func TestAPIContract_SystemDefaultConfigSnapshot(t *testing.T) {
srv, _, _ := newControlPlaneTestServer(t)
patchRec := doJSONRequest(t, srv, http.MethodPatch, "/api/v1/system/config", map[string]any{
"request_log_enabled": true,
"max_consecutive_failures": 9,
}, true)
if patchRec.Code != http.StatusOK {
t.Fatalf("patch config status: got %d, want %d, body=%s", patchRec.Code, http.StatusOK, patchRec.Body.String())
}
defaultRec := doJSONRequest(t, srv, http.MethodGet, "/api/v1/system/config/default", nil, true)
if defaultRec.Code != http.StatusOK {
t.Fatalf("default config status: got %d, want %d, body=%s", defaultRec.Code, http.StatusOK, defaultRec.Body.String())
}
defaultBody := decodeJSONMap(t, defaultRec)
if defaultBody["request_log_enabled"] != true {
t.Fatalf("default request_log_enabled: got %v, want true", defaultBody["request_log_enabled"])
}
if defaultBody["max_consecutive_failures"] != float64(3) {
t.Fatalf("default max_consecutive_failures: got %v, want 3", defaultBody["max_consecutive_failures"])
}
currentRec := doJSONRequest(t, srv, http.MethodGet, "/api/v1/system/config", nil, true)
if currentRec.Code != http.StatusOK {
t.Fatalf("current config status: got %d, want %d, body=%s", currentRec.Code, http.StatusOK, currentRec.Body.String())
}
currentBody := decodeJSONMap(t, currentRec)
if currentBody["request_log_enabled"] != true {
t.Fatalf("current request_log_enabled: got %v, want true", currentBody["request_log_enabled"])
}
if currentBody["max_consecutive_failures"] != float64(9) {
t.Fatalf("current max_consecutive_failures: got %v, want 9", currentBody["max_consecutive_failures"])
}
}
func TestAPIContract_SystemEnvConfigSnapshot(t *testing.T) {
srv, cp, _ := newControlPlaneTestServer(t)
rec := doJSONRequest(t, srv, http.MethodGet, "/api/v1/system/config/env", nil, true)
if rec.Code != http.StatusOK {
t.Fatalf("env config status: got %d, want %d, body=%s", rec.Code, http.StatusOK, rec.Body.String())
}
body := decodeJSONMap(t, rec)
if body["default_platform_sticky_ttl"] != cp.EnvCfg.DefaultPlatformStickyTTL.String() {
t.Fatalf(
"default_platform_sticky_ttl: got %v, want %s",
body["default_platform_sticky_ttl"],
cp.EnvCfg.DefaultPlatformStickyTTL.String(),
)
}
if body["default_platform_reverse_proxy_miss_action"] != "TREAT_AS_EMPTY" {
t.Fatalf(
"default_platform_reverse_proxy_miss_action: got %v, want TREAT_AS_EMPTY",
body["default_platform_reverse_proxy_miss_action"],
)
}
if body["default_platform_reverse_proxy_empty_account_behavior"] != "ACCOUNT_HEADER_RULE" {
t.Fatalf(
"default_platform_reverse_proxy_empty_account_behavior: got %v, want ACCOUNT_HEADER_RULE",
body["default_platform_reverse_proxy_empty_account_behavior"],
)
}
if body["default_platform_reverse_proxy_fixed_account_header"] != "Authorization" {
t.Fatalf(
"default_platform_reverse_proxy_fixed_account_header: got %v, want Authorization",
body["default_platform_reverse_proxy_fixed_account_header"],
)
}
if body["default_platform_allocation_policy"] != "BALANCED" {
t.Fatalf(
"default_platform_allocation_policy: got %v, want BALANCED",
body["default_platform_allocation_policy"],
)
}
if body["admin_token_set"] != false {
t.Fatalf("admin_token_set: got %v, want false", body["admin_token_set"])
}
if body["proxy_token_set"] != false {
t.Fatalf("proxy_token_set: got %v, want false", body["proxy_token_set"])
}
if body["admin_token_weak"] != false {
t.Fatalf("admin_token_weak: got %v, want false", body["admin_token_weak"])
}
if body["proxy_token_weak"] != false {
t.Fatalf("proxy_token_weak: got %v, want false", body["proxy_token_weak"])
}
if _, ok := body["admin_token"]; ok {
t.Fatalf("admin_token should not be exposed: body=%s", rec.Body.String())
}
if _, ok := body["proxy_token"]; ok {
t.Fatalf("proxy_token should not be exposed: body=%s", rec.Body.String())
}
}
func TestAPIContract_ModuleAndActionEndpoints(t *testing.T) {
srv, _, _ := newControlPlaneTestServer(t)
// platform + leases
platformID := mustCreatePlatform(t, srv, "lease-plat")
rec := doJSONRequest(t, srv, http.MethodGet, "/api/v1/platforms/"+platformID+"/leases", nil, true)
if rec.Code != http.StatusOK {
t.Fatalf("list leases status: got %d, want %d, body=%s", rec.Code, http.StatusOK, rec.Body.String())
}
body := decodeJSONMap(t, rec)
if _, ok := body["items"]; !ok {
t.Fatalf("list leases missing items: body=%s", rec.Body.String())
}
rec = doJSONRequest(t, srv, http.MethodDelete, "/api/v1/platforms/not-a-uuid/leases", nil, true)
if rec.Code != http.StatusBadRequest {
t.Fatalf("invalid platform_id status: got %d, want %d", rec.Code, http.StatusBadRequest)
}
assertErrorCode(t, rec, "INVALID_ARGUMENT")
rec = doJSONRequest(t, srv, http.MethodGet, "/api/v1/platforms/not-a-uuid", nil, true)
if rec.Code != http.StatusBadRequest {
t.Fatalf("platform invalid id status: got %d, want %d, body=%s", rec.Code, http.StatusBadRequest, rec.Body.String())
}
assertErrorCode(t, rec, "INVALID_ARGUMENT")
rec = doJSONRequest(t, srv, http.MethodPost, "/api/v1/platforms/preview-filter", map[string]any{
"platform_id": "not-a-uuid",
}, true)
if rec.Code != http.StatusBadRequest {
t.Fatalf("preview-filter invalid platform_id status: got %d, want %d, body=%s", rec.Code, http.StatusBadRequest, rec.Body.String())
}
assertErrorCode(t, rec, "INVALID_ARGUMENT")
rec = doJSONRequest(t, srv, http.MethodPost, "/api/v1/platforms/"+platformID+"/actions/rebuild-routable-view", nil, true)
if rec.Code != http.StatusOK {
t.Fatalf("rebuild action status: got %d, want %d, body=%s", rec.Code, http.StatusOK, rec.Body.String())
}
// subscriptions
rec = doJSONRequest(t, srv, http.MethodPost, "/api/v1/subscriptions", map[string]any{
"name": "sub-a",
"url": "https://example.com/sub",
}, true)
if rec.Code != http.StatusCreated {
t.Fatalf("create subscription status: got %d, want %d, body=%s", rec.Code, http.StatusCreated, rec.Body.String())
}
rec = doJSONRequest(t, srv, http.MethodPost, "/api/v1/subscriptions/11111111-1111-1111-1111-111111111111/actions/refresh", nil, true)
if rec.Code != http.StatusNotFound {
t.Fatalf("refresh missing subscription status: got %d, want %d, body=%s", rec.Code, http.StatusNotFound, rec.Body.String())
}
assertErrorCode(t, rec, "NOT_FOUND")
rec = doJSONRequest(t, srv, http.MethodPost, "/api/v1/subscriptions/11111111-1111-1111-1111-111111111111/actions/cleanup-circuit-open-nodes", nil, true)
if rec.Code != http.StatusNotFound {
t.Fatalf("cleanup action missing subscription status: got %d, want %d, body=%s", rec.Code, http.StatusNotFound, rec.Body.String())
}
assertErrorCode(t, rec, "NOT_FOUND")
rec = doJSONRequest(t, srv, http.MethodGet, "/api/v1/subscriptions/not-a-uuid", nil, true)
if rec.Code != http.StatusBadRequest {
t.Fatalf("subscription invalid id status: got %d, want %d, body=%s", rec.Code, http.StatusBadRequest, rec.Body.String())
}
assertErrorCode(t, rec, "INVALID_ARGUMENT")
rec = doJSONRequest(t, srv, http.MethodPost, "/api/v1/subscriptions/not-a-uuid/actions/cleanup-circuit-open-nodes", nil, true)
if rec.Code != http.StatusBadRequest {
t.Fatalf("cleanup action invalid id status: got %d, want %d, body=%s", rec.Code, http.StatusBadRequest, rec.Body.String())
}
assertErrorCode(t, rec, "INVALID_ARGUMENT")
// account header rules
rec = doJSONRequest(t, srv, http.MethodPut, "/api/v1/account-header-rules/api.example.com%2Fv1", map[string]any{
"headers": []string{"Authorization"},
}, true)
if rec.Code != http.StatusCreated {
t.Fatalf("upsert rule status: got %d, want %d, body=%s", rec.Code, http.StatusCreated, rec.Body.String())
}
rec = doJSONRequest(t, srv, http.MethodPut, "/api/v1/account-header-rules/api.example.com%2Fv1", map[string]any{
"headers": []string{"x-api-key"},
}, true)
if rec.Code != http.StatusOK {
t.Fatalf("update existing rule status: got %d, want %d, body=%s", rec.Code, http.StatusOK, rec.Body.String())
}
rec = doJSONRequest(t, srv, http.MethodPost, "/api/v1/account-header-rules:resolve", map[string]any{
"url": "https://api.example.com/v1/orders/1",
}, true)
if rec.Code != http.StatusOK {
t.Fatalf("resolve rule status: got %d, want %d, body=%s", rec.Code, http.StatusOK, rec.Body.String())
}
resolved := decodeJSONMap(t, rec)
if resolved["matched_url_prefix"] != "api.example.com/v1" {
t.Fatalf("matched_url_prefix: got %v, want api.example.com/v1", resolved["matched_url_prefix"])
}
// nodes (invalid hash should be 400 before probe manager is needed)
rec = doJSONRequest(t, srv, http.MethodPost, "/api/v1/nodes/not-hex/actions/probe-egress", nil, true)
if rec.Code != http.StatusBadRequest {
t.Fatalf("probe-egress invalid hash status: got %d, want %d, body=%s", rec.Code, http.StatusBadRequest, rec.Body.String())
}
assertErrorCode(t, rec, "INVALID_ARGUMENT")
rec = doJSONRequest(t, srv, http.MethodPost, "/api/v1/nodes/not-hex/actions/probe-latency", nil, true)
if rec.Code != http.StatusBadRequest {
t.Fatalf("probe-latency invalid hash status: got %d, want %d, body=%s", rec.Code, http.StatusBadRequest, rec.Body.String())
}
assertErrorCode(t, rec, "INVALID_ARGUMENT")
rec = doJSONRequest(t, srv, http.MethodGet, "/api/v1/nodes?platform_id=not-a-uuid", nil, true)
if rec.Code != http.StatusBadRequest {
t.Fatalf("nodes invalid platform_id status: got %d, want %d, body=%s", rec.Code, http.StatusBadRequest, rec.Body.String())
}
assertErrorCode(t, rec, "INVALID_ARGUMENT")
rec = doJSONRequest(t, srv, http.MethodGet, "/api/v1/nodes?subscription_id=not-a-uuid", nil, true)
if rec.Code != http.StatusBadRequest {
t.Fatalf("nodes invalid subscription_id status: got %d, want %d, body=%s", rec.Code, http.StatusBadRequest, rec.Body.String())
}
assertErrorCode(t, rec, "INVALID_ARGUMENT")
// geoip
rec = doJSONRequest(t, srv, http.MethodGet, "/api/v1/geoip/status", nil, true)
if rec.Code != http.StatusOK {
t.Fatalf("geoip status code: got %d, want %d", rec.Code, http.StatusOK)
}
rec = doJSONRequest(t, srv, http.MethodGet, "/api/v1/geoip/lookup", nil, true)
if rec.Code != http.StatusBadRequest {
t.Fatalf("geoip lookup missing ip status: got %d, want %d, body=%s", rec.Code, http.StatusBadRequest, rec.Body.String())
}
assertErrorCode(t, rec, "INVALID_ARGUMENT")
rec = doJSONRequest(t, srv, http.MethodPost, "/api/v1/geoip/lookup", map[string]any{
"ips": []string{"1.2.3.4", "not-an-ip"},
}, true)
if rec.Code != http.StatusBadRequest {
t.Fatalf("geoip batch invalid ip status: got %d, want %d, body=%s", rec.Code, http.StatusBadRequest, rec.Body.String())
}
assertErrorCode(t, rec, "INVALID_ARGUMENT")
var geoBatchErr ErrorResponse
if err := json.Unmarshal(rec.Body.Bytes(), &geoBatchErr); err != nil {
t.Fatalf("unmarshal geoip batch invalid response: %v body=%q", err, rec.Body.String())
}
if !strings.Contains(geoBatchErr.Error.Message, "ips[1]") {
t.Fatalf("geoip batch invalid message: got %q, want contains %q", geoBatchErr.Error.Message, "ips[1]")
}
rec = doJSONRequest(t, srv, http.MethodPost, "/api/v1/geoip/lookup", map[string]any{
"ips": []string{"1.2.3.4", "8.8.8.8"},
}, true)
if rec.Code != http.StatusOK {
t.Fatalf("geoip batch success status: got %d, want %d, body=%s", rec.Code, http.StatusOK, rec.Body.String())
}
body = decodeJSONMap(t, rec)
geoResultsRaw, ok := body["results"]
if !ok {
t.Fatalf("geoip batch missing results: body=%s", rec.Body.String())
}
geoResults, ok := geoResultsRaw.([]any)
if !ok {
t.Fatalf("geoip batch results type: got %T, want []any", geoResultsRaw)
}
if len(geoResults) != 2 {
t.Fatalf("geoip batch results len: got %d, want %d", len(geoResults), 2)
}
// downloader is intentionally nil in test harness, so update-now returns INTERNAL.
rec = doJSONRequest(t, srv, http.MethodPost, "/api/v1/geoip/actions/update-now", nil, true)
if rec.Code != http.StatusInternalServerError {
t.Fatalf("geoip update-now status: got %d, want %d, body=%s", rec.Code, http.StatusInternalServerError, rec.Body.String())
}
assertErrorCode(t, rec, "INTERNAL")
}
func TestAPIContract_SubscriptionUpdateIntervalMinimum(t *testing.T) {
srv, _, _ := newControlPlaneTestServer(t)
rec := doJSONRequest(t, srv, http.MethodPost, "/api/v1/subscriptions", map[string]any{
"name": "too-fast",
"url": "https://example.com/sub-fast",
"update_interval": "10s",
}, true)
if rec.Code != http.StatusBadRequest {
t.Fatalf("create subscription invalid interval status: got %d, want %d, body=%s", rec.Code, http.StatusBadRequest, rec.Body.String())
}
assertErrorCode(t, rec, "INVALID_ARGUMENT")
rec = doJSONRequest(t, srv, http.MethodPost, "/api/v1/subscriptions", map[string]any{
"name": "normal-sub",
"url": "https://example.com/sub-normal",
}, true)
if rec.Code != http.StatusCreated {
t.Fatalf("create subscription status: got %d, want %d, body=%s", rec.Code, http.StatusCreated, rec.Body.String())
}
body := decodeJSONMap(t, rec)
subID, _ := body["id"].(string)
if subID == "" {
t.Fatalf("create subscription missing id: body=%s", rec.Body.String())
}
rec = doJSONRequest(t, srv, http.MethodPatch, "/api/v1/subscriptions/"+subID, map[string]any{
"update_interval": "5s",
}, true)
if rec.Code != http.StatusBadRequest {
t.Fatalf("patch subscription invalid interval status: got %d, want %d, body=%s", rec.Code, http.StatusBadRequest, rec.Body.String())
}
assertErrorCode(t, rec, "INVALID_ARGUMENT")
}
func TestAPIContract_SubscriptionEphemeralEvictDelay_DefaultAndCustom(t *testing.T) {
srv, _, _ := newControlPlaneTestServer(t)
defaultRec := doJSONRequest(t, srv, http.MethodPost, "/api/v1/subscriptions", map[string]any{
"name": "default-evict-delay-sub",
"url": "https://example.com/sub-default-evict-delay",
}, true)
if defaultRec.Code != http.StatusCreated {
t.Fatalf("create default delay subscription status: got %d, want %d, body=%s", defaultRec.Code, http.StatusCreated, defaultRec.Body.String())
}
defaultBody := decodeJSONMap(t, defaultRec)
if defaultBody["ephemeral_node_evict_delay"] != "72h0m0s" {
t.Fatalf(
"default ephemeral_node_evict_delay: got %v, want %q",
defaultBody["ephemeral_node_evict_delay"],
"72h0m0s",
)
}
customRec := doJSONRequest(t, srv, http.MethodPost, "/api/v1/subscriptions", map[string]any{
"name": "custom-evict-delay-sub",
"url": "https://example.com/sub-custom-evict-delay",
"ephemeral_node_evict_delay": "30m",
}, true)
if customRec.Code != http.StatusCreated {
t.Fatalf("create custom delay subscription status: got %d, want %d, body=%s", customRec.Code, http.StatusCreated, customRec.Body.String())
}
customBody := decodeJSONMap(t, customRec)
if customBody["ephemeral_node_evict_delay"] != "30m0s" {
t.Fatalf(
"custom ephemeral_node_evict_delay: got %v, want %q",
customBody["ephemeral_node_evict_delay"],
"30m0s",
)
}
}
func TestAPIContract_SubscriptionEphemeralEvictDelayPatchValidation(t *testing.T) {
srv, _, _ := newControlPlaneTestServer(t)
createRec := doJSONRequest(t, srv, http.MethodPost, "/api/v1/subscriptions", map[string]any{
"name": "patch-evict-delay-sub",
"url": "https://example.com/sub-patch-evict-delay",
}, true)
if createRec.Code != http.StatusCreated {
t.Fatalf("create subscription status: got %d, want %d, body=%s", createRec.Code, http.StatusCreated, createRec.Body.String())
}
createBody := decodeJSONMap(t, createRec)
subID, _ := createBody["id"].(string)
if subID == "" {
t.Fatalf("create subscription missing id: body=%s", createRec.Body.String())
}
invalidDurationRec := doJSONRequest(t, srv, http.MethodPatch, "/api/v1/subscriptions/"+subID, map[string]any{
"ephemeral_node_evict_delay": "not-a-duration",
}, true)
if invalidDurationRec.Code != http.StatusBadRequest {
t.Fatalf("patch invalid duration status: got %d, want %d, body=%s", invalidDurationRec.Code, http.StatusBadRequest, invalidDurationRec.Body.String())
}
assertErrorCode(t, invalidDurationRec, "INVALID_ARGUMENT")
negativeRec := doJSONRequest(t, srv, http.MethodPatch, "/api/v1/subscriptions/"+subID, map[string]any{
"ephemeral_node_evict_delay": "-1s",
}, true)
if negativeRec.Code != http.StatusBadRequest {
t.Fatalf("patch negative duration status: got %d, want %d, body=%s", negativeRec.Code, http.StatusBadRequest, negativeRec.Body.String())
}
assertErrorCode(t, negativeRec, "INVALID_ARGUMENT")
validRec := doJSONRequest(t, srv, http.MethodPatch, "/api/v1/subscriptions/"+subID, map[string]any{
"ephemeral_node_evict_delay": "0s",
}, true)
if validRec.Code != http.StatusOK {
t.Fatalf("patch valid duration status: got %d, want %d, body=%s", validRec.Code, http.StatusOK, validRec.Body.String())
}
validBody := decodeJSONMap(t, validRec)
if validBody["ephemeral_node_evict_delay"] != "0s" {
t.Fatalf(
"patched ephemeral_node_evict_delay: got %v, want %q",
validBody["ephemeral_node_evict_delay"],
"0s",
)
}
}
func TestAPIContract_PreviewFilterUsesPaginationEnvelope(t *testing.T) {
srv, _, _ := newControlPlaneTestServer(t)
rec := doJSONRequest(
t,
srv,
http.MethodPost,
"/api/v1/platforms/preview-filter?limit=5&offset=1",
map[string]any{
"platform_spec": map[string]any{
"regex_filters": []string{},
"region_filters": []string{},
},
},
true,
)
if rec.Code != http.StatusOK {
t.Fatalf("preview-filter status: got %d, want %d, body=%s", rec.Code, http.StatusOK, rec.Body.String())
}
body := decodeJSONMap(t, rec)
itemsRaw, ok := body["items"]
if !ok {
t.Fatalf("preview-filter missing items: body=%s", rec.Body.String())
}
items, ok := itemsRaw.([]any)
if !ok {
t.Fatalf("preview-filter items type: got %T, want []any", itemsRaw)
}
if len(items) != 0 {
t.Fatalf("preview-filter expected empty items, got %d", len(items))
}
if _, ok := body["nodes"]; ok {
t.Fatalf("preview-filter should not return legacy nodes field: body=%s", rec.Body.String())
}
}
func TestAPIContract_DeleteRuleRejectsInvalidPrefix(t *testing.T) {
srv, _, _ := newControlPlaneTestServer(t)
rec := doJSONRequest(t, srv, http.MethodDelete, "/api/v1/account-header-rules/api.example.com%3Fq%3D1", nil, true)
if rec.Code != http.StatusBadRequest {
t.Fatalf("delete rule invalid prefix status: got %d, want %d, body=%s", rec.Code, http.StatusBadRequest, rec.Body.String())
}
assertErrorCode(t, rec, "INVALID_ARGUMENT")
}
func TestAPIContract_DeleteFallbackRuleRejected(t *testing.T) {
srv, _, _ := newControlPlaneTestServer(t)
rec := doJSONRequest(t, srv, http.MethodPut, "/api/v1/account-header-rules/%2A", map[string]any{
"headers": []string{"Authorization", "x-api-key"},
}, true)
if rec.Code != http.StatusCreated {
t.Fatalf("create fallback rule status: got %d, want %d, body=%s", rec.Code, http.StatusCreated, rec.Body.String())
}
rec = doJSONRequest(t, srv, http.MethodDelete, "/api/v1/account-header-rules/%2A", nil, true)
if rec.Code != http.StatusBadRequest {
t.Fatalf("delete fallback rule status: got %d, want %d, body=%s", rec.Code, http.StatusBadRequest, rec.Body.String())
}
assertErrorCode(t, rec, "INVALID_ARGUMENT")
rec = doJSONRequest(t, srv, http.MethodGet, "/api/v1/account-header-rules", nil, true)
if rec.Code != http.StatusOK {
t.Fatalf("list rules status after delete fallback attempt: got %d, want %d, body=%s", rec.Code, http.StatusOK, rec.Body.String())
}
body := decodeJSONMap(t, rec)
items, ok := body["items"].([]any)
if !ok {
t.Fatalf("rule items type: got %T", body["items"])
}
if len(items) != 1 {
t.Fatalf("rule items len: got %d, want %d, body=%s", len(items), 1, rec.Body.String())
}
rule, ok := items[0].(map[string]any)
if !ok {
t.Fatalf("rule item type: got %T", items[0])
}
if rule["url_prefix"] != "*" {
t.Fatalf("fallback rule should remain, got url_prefix=%v", rule["url_prefix"])
}
}
func TestAPIContract_UpsertRuleRequiresPathPrefix(t *testing.T) {
srv, _, _ := newControlPlaneTestServer(t)
rec := doJSONRequest(t, srv, http.MethodPut, "/api/v1/account-header-rules/", map[string]any{
"url_prefix": "api.example.com/v1",
"headers": []string{"Authorization"},
}, true)
if rec.Code != http.StatusBadRequest {
t.Fatalf("upsert rule missing path prefix status: got %d, want %d, body=%s", rec.Code, http.StatusBadRequest, rec.Body.String())
}
assertErrorCode(t, rec, "INVALID_ARGUMENT")
}
func TestAPIContract_RequestLogEndpoints(t *testing.T) {
srv, requestlogRepo, metricsManager, platformID := newObservabilityTestServer(t)
logID := seedObservabilityData(t, requestlogRepo, metricsManager, platformID)
rec := doJSONRequest(t, srv, http.MethodGet, "/api/v1/request-logs?platform_id="+platformID+"&limit=1", nil, true)
if rec.Code != http.StatusOK {
t.Fatalf("list request logs status: got %d, want %d, body=%s", rec.Code, http.StatusOK, rec.Body.String())
}
body := decodeJSONMap(t, rec)
if body["limit"] != float64(1) {
t.Fatalf("list limit: got %v, want 1", body["limit"])
}
if body["has_more"] != true {
t.Fatalf("list has_more: got %v, want true", body["has_more"])
}
nextCursor, ok := body["next_cursor"].(string)
if !ok || nextCursor == "" {
t.Fatalf("list next_cursor: got %T value=%v, want non-empty string", body["next_cursor"], body["next_cursor"])
}
itemsRaw, ok := body["items"]
if !ok {
t.Fatalf("missing items field: body=%s", rec.Body.String())
}
items, ok := itemsRaw.([]any)
if !ok || len(items) != 1 {
t.Fatalf("items: got %T len=%d, want 1", itemsRaw, len(items))
}
firstItem, ok := items[0].(map[string]any)
if !ok {
t.Fatalf("first item type: got %T", items[0])
}
for _, key := range []string{"resin_error", "upstream_stage", "upstream_err_kind", "upstream_errno", "upstream_err_msg"} {
if _, exists := firstItem[key]; !exists {
t.Fatalf("first item missing field %q", key)
}
}
rec = doJSONRequest(
t,
srv,
http.MethodGet,
"/api/v1/request-logs?platform_id="+platformID+"&limit=1&cursor="+url.QueryEscape(nextCursor),
nil,
true,
)
if rec.Code != http.StatusOK {
t.Fatalf("list request logs cursor status: got %d, want %d, body=%s", rec.Code, http.StatusOK, rec.Body.String())
}
body = decodeJSONMap(t, rec)
if body["has_more"] != false {
t.Fatalf("cursor page has_more: got %v, want false", body["has_more"])
}
itemsRaw, ok = body["items"]
if !ok {
t.Fatalf("cursor page missing items field: body=%s", rec.Body.String())
}
items, ok = itemsRaw.([]any)
if !ok || len(items) != 1 {
t.Fatalf("cursor page items: got %T len=%d, want 1", itemsRaw, len(items))
}
rec = doJSONRequest(
t,
srv,
http.MethodGet,
"/api/v1/request-logs?platform_name="+url.QueryEscape("Platform One")+"&limit=20",
nil,
true,
)
if rec.Code != http.StatusOK {
t.Fatalf("list request logs by platform_name status: got %d, want %d, body=%s", rec.Code, http.StatusOK, rec.Body.String())
}
body = decodeJSONMap(t, rec)
itemsRaw, ok = body["items"]
if !ok {
t.Fatalf("platform_name filter missing items field: body=%s", rec.Body.String())
}
items, ok = itemsRaw.([]any)
if !ok || len(items) != 1 {
t.Fatalf("platform_name filter items: got %T len=%d, want 1", itemsRaw, len(items))
}
rowMap, ok := items[0].(map[string]any)
if !ok {
t.Fatalf("platform_name filter first item type: got %T", items[0])
}
if rowMap["platform_name"] != "Platform One" {
t.Fatalf("platform_name filter first item platform_name: got %v, want %q", rowMap["platform_name"], "Platform One")
}
rec = doJSONRequest(
t,
srv,
http.MethodGet,
"/api/v1/request-logs?platform_id="+url.QueryEscape("ATFORM-1")+
"&platform_name="+url.QueryEscape("ATFORM o")+
"&account="+url.QueryEscape("CT-1")+
"&target_host="+url.QueryEscape("AMPLE.CO")+
"&fuzzy=true&limit=20",
nil,
true,
)
if rec.Code != http.StatusOK {
t.Fatalf("list request logs with fuzzy search status: got %d, want %d, body=%s", rec.Code, http.StatusOK, rec.Body.String())
}
body = decodeJSONMap(t, rec)
itemsRaw, ok = body["items"]
if !ok {
t.Fatalf("fuzzy filter missing items field: body=%s", rec.Body.String())
}
items, ok = itemsRaw.([]any)
if !ok || len(items) != 1 {
t.Fatalf("fuzzy filter items: got %T len=%d, want 1", itemsRaw, len(items))
}
rowMap, ok = items[0].(map[string]any)
if !ok {
t.Fatalf("fuzzy filter first item type: got %T", items[0])
}
if rowMap["id"] != logID {
t.Fatalf("fuzzy filter first item id: got %v, want %q", rowMap["id"], logID)
}
rec = doJSONRequest(
t,
srv,
http.MethodGet,
"/api/v1/request-logs?platform_name="+url.QueryEscape("atform O")+"&limit=20",
nil,
true,
)
if rec.Code != http.StatusOK {
t.Fatalf("list request logs strict partial status: got %d, want %d, body=%s", rec.Code, http.StatusOK, rec.Body.String())
}
body = decodeJSONMap(t, rec)
itemsRaw, ok = body["items"]
if !ok {
t.Fatalf("strict partial filter missing items field: body=%s", rec.Body.String())
}
items, ok = itemsRaw.([]any)
if !ok || len(items) != 0 {
t.Fatalf("strict partial filter items: got %T len=%d, want 0", itemsRaw, len(items))
}
rec = doJSONRequest(t, srv, http.MethodGet, "/api/v1/request-logs/"+logID, nil, true)
if rec.Code != http.StatusOK {
t.Fatalf("get request log status: got %d, want %d, body=%s", rec.Code, http.StatusOK, rec.Body.String())
}
row := decodeJSONMap(t, rec)
if row["id"] != logID {
t.Fatalf("id: got %v, want %q", row["id"], logID)
}
for _, key := range []string{"resin_error", "upstream_stage", "upstream_err_kind", "upstream_errno", "upstream_err_msg"} {
if _, exists := row[key]; !exists {
t.Fatalf("row missing field %q", key)
}
}
if row["payload_present"] != true {
t.Fatalf("payload_present: got %v, want true", row["payload_present"])
}
if row["ingress_bytes"] != float64(210) || row["egress_bytes"] != float64(120) {
t.Fatalf("traffic bytes mismatch: ingress=%v egress=%v", row["ingress_bytes"], row["egress_bytes"])
}
rec = doJSONRequest(t, srv, http.MethodGet, "/api/v1/request-logs/"+logID+"/payloads", nil, true)
if rec.Code != http.StatusOK {
t.Fatalf("get request log payload status: got %d, want %d, body=%s", rec.Code, http.StatusOK, rec.Body.String())
}
payload := decodeJSONMap(t, rec)
if payload["req_headers_b64"] != base64.StdEncoding.EncodeToString([]byte("req-h-1")) {
t.Fatalf("req_headers_b64 mismatch: got %v", payload["req_headers_b64"])
}
if payload["resp_body_b64"] != base64.StdEncoding.EncodeToString([]byte("resp-b-1")) {
t.Fatalf("resp_body_b64 mismatch: got %v", payload["resp_body_b64"])
}
trunc, ok := payload["truncated"].(map[string]any)
if !ok {
t.Fatalf("truncated type: got %T", payload["truncated"])
}
if trunc["req_headers"] != true || trunc["req_body"] != true || trunc["resp_body"] != true {
t.Fatalf("truncated flags mismatch: %+v", trunc)
}
rec = doJSONRequest(t, srv, http.MethodGet, "/api/v1/request-logs?from=not-a-time", nil, true)
if rec.Code != http.StatusBadRequest {
t.Fatalf("invalid from status: got %d, want %d, body=%s", rec.Code, http.StatusBadRequest, rec.Body.String())
}
assertErrorCode(t, rec, "INVALID_ARGUMENT")
inserted, err := requestlogRepo.InsertBatch([]proxy.RequestLogEntry{{
ID: "log-contract-3",
StartedAtNs: time.Now().Add(-30 * time.Second).UnixNano(),
ProxyType: proxy.ProxyTypeSocks5Forward,
ClientIP: "127.0.0.3",
PlatformID: platformID,
Account: "acct-3",
TargetHost: "example.net:443",
DurationNs: int64(18 * time.Millisecond),
NetOK: true,
HTTPStatus: 0,
}})
if err != nil {
t.Fatalf("insert socks5 request log: %v", err)
}
if inserted != 1 {
t.Fatalf("inserted socks5 logs: got %d, want %d", inserted, 1)
}
rec = doJSONRequest(t, srv, http.MethodGet, "/api/v1/request-logs?proxy_type=3", nil, true)
if rec.Code != http.StatusOK {
t.Fatalf("proxy_type=3 status: got %d, want %d, body=%s", rec.Code, http.StatusOK, rec.Body.String())
}
filteredBody := decodeJSONMap(t, rec)
filteredItems, ok := filteredBody["items"].([]any)
if !ok {
t.Fatalf("proxy_type=3 items type: got %T", filteredBody["items"])
}
if len(filteredItems) != 1 {
t.Fatalf("proxy_type=3 items len: got %d, want 1", len(filteredItems))
}
filteredRow, ok := filteredItems[0].(map[string]any)
if !ok {
t.Fatalf("proxy_type=3 row type: got %T", filteredItems[0])
}
if filteredRow["id"] != "log-contract-3" {
t.Fatalf("proxy_type=3 row id: got %v, want %q", filteredRow["id"], "log-contract-3")
}
invalidCases := []string{
"/api/v1/request-logs?limit=bad",
"/api/v1/request-logs?limit=100001",
"/api/v1/request-logs?offset=1",
"/api/v1/request-logs?cursor=not-base64",
"/api/v1/request-logs?proxy_type=4",
"/api/v1/request-logs?net_ok=2",
"/api/v1/request-logs?net_ok=1",
"/api/v1/request-logs?net_ok=maybe",
"/api/v1/request-logs?http_status=bad",
"/api/v1/request-logs?http_status=99",
"/api/v1/request-logs?fuzzy=1",
}
for _, path := range invalidCases {
rec = doJSONRequest(t, srv, http.MethodGet, path, nil, true)
if rec.Code != http.StatusBadRequest {
t.Fatalf("%s status: got %d, want %d, body=%s", path, rec.Code, http.StatusBadRequest, rec.Body.String())
}
assertErrorCode(t, rec, "INVALID_ARGUMENT")
}
}
func TestAPIContract_MetricsEndpoints(t *testing.T) {
srv, requestlogRepo, metricsManager, platformID := newObservabilityTestServer(t)
_ = seedObservabilityData(t, requestlogRepo, metricsManager, platformID)
from := url.QueryEscape(time.Unix(0, 0).UTC().Format(time.RFC3339Nano))
to := url.QueryEscape(time.Now().Add(time.Hour).UTC().Format(time.RFC3339Nano))
checkItemsEndpoint := func(path string) {
t.Helper()
rec := doJSONRequest(t, srv, http.MethodGet, path, nil, true)
if rec.Code != http.StatusOK {
t.Fatalf("%s status: got %d, want %d, body=%s", path, rec.Code, http.StatusOK, rec.Body.String())
}
body := decodeJSONMap(t, rec)
items, ok := body["items"].([]any)
if !ok || len(items) == 0 {
t.Fatalf("%s items: got %T len=%d", path, body["items"], len(items))
}
}
checkRealtimeEndpoint := func(path string, wantStep float64) {
t.Helper()
rec := doJSONRequest(t, srv, http.MethodGet, path, nil, true)
if rec.Code != http.StatusOK {
t.Fatalf("%s status: got %d, want %d, body=%s", path, rec.Code, http.StatusOK, rec.Body.String())
}
body := decodeJSONMap(t, rec)
if body["step_seconds"] != wantStep {
t.Fatalf("%s step_seconds: got %v, want %v", path, body["step_seconds"], wantStep)
}
items, ok := body["items"].([]any)
if !ok || len(items) == 0 {
t.Fatalf("%s items: got %T len=%d", path, body["items"], len(items))
}
}
checkRealtimeEndpoint("/api/v1/metrics/realtime/throughput?from="+from+"&to="+to, 1)
checkRealtimeEndpoint("/api/v1/metrics/realtime/connections?from="+from+"&to="+to, 5)
checkRealtimeEndpoint("/api/v1/metrics/realtime/leases?platform_id="+platformID+"&from="+from+"&to="+to, 5)
rec := doJSONRequest(t, srv, http.MethodGet, "/api/v1/metrics/realtime/leases?from="+from+"&to="+to, nil, true)
if rec.Code != http.StatusOK {
t.Fatalf("global realtime leases status: got %d, want %d, body=%s", rec.Code, http.StatusOK, rec.Body.String())
}
body := decodeJSONMap(t, rec)
if body["platform_id"] != "" {
t.Fatalf("global realtime leases platform_id: got %v, want empty", body["platform_id"])
}
if items, ok := body["items"].([]any); !ok || len(items) != 1 {
t.Fatalf("global realtime leases items: got %T len=%d, want len=1", body["items"], len(items))
} else if item, ok := items[0].(map[string]any); !ok || item["active_leases"] != float64(3) {
t.Fatalf("global realtime leases active_leases: got %+v, want 3", items[0])
}
checkItemsEndpoint("/api/v1/metrics/history/traffic?from=" + from + "&to=" + to)
checkItemsEndpoint("/api/v1/metrics/history/requests?platform_id=" + platformID + "&from=" + from + "&to=" + to)
checkItemsEndpoint("/api/v1/metrics/history/access-latency?platform_id=" + platformID + "&from=" + from + "&to=" + to)
checkItemsEndpoint("/api/v1/metrics/history/probes?from=" + from + "&to=" + to)
checkItemsEndpoint("/api/v1/metrics/history/node-pool?from=" + from + "&to=" + to)
checkItemsEndpoint("/api/v1/metrics/history/lease-lifetime?platform_id=" + platformID + "&from=" + from + "&to=" + to)
// Without platform_id, mixed global+platform rows must not leak through.
rec = doJSONRequest(t, srv, http.MethodGet, "/api/v1/metrics/history/traffic?from="+from+"&to="+to, nil, true)
if rec.Code != http.StatusOK {
t.Fatalf("global traffic status: got %d, want %d, body=%s", rec.Code, http.StatusOK, rec.Body.String())
}
body = decodeJSONMap(t, rec)
if items, ok := body["items"].([]any); !ok || len(items) != 1 {
t.Fatalf("global traffic items: got %T len=%d, want len=1", body["items"], len(items))
}
rec = doJSONRequest(t, srv, http.MethodGet, "/api/v1/metrics/history/requests?from="+from+"&to="+to, nil, true)
if rec.Code != http.StatusOK {
t.Fatalf("global requests status: got %d, want %d, body=%s", rec.Code, http.StatusOK, rec.Body.String())
}
body = decodeJSONMap(t, rec)
if items, ok := body["items"].([]any); !ok || len(items) != 1 {
t.Fatalf("global requests items: got %T len=%d, want len=1", body["items"], len(items))
}
rec = doJSONRequest(t, srv, http.MethodGet, "/api/v1/metrics/history/access-latency?from="+from+"&to="+to, nil, true)
if rec.Code != http.StatusOK {
t.Fatalf("global access-latency status: got %d, want %d, body=%s", rec.Code, http.StatusOK, rec.Body.String())
}
body = decodeJSONMap(t, rec)
if items, ok := body["items"].([]any); !ok || len(items) != 1 {
t.Fatalf("global access-latency items: got %T len=%d, want len=1", body["items"], len(items))
}
rec = doJSONRequest(t, srv, http.MethodGet, "/api/v1/metrics/snapshots/node-pool", nil, true)
if rec.Code != http.StatusOK {
t.Fatalf("snapshot node-pool status: got %d, want %d, body=%s", rec.Code, http.StatusOK, rec.Body.String())
}
body = decodeJSONMap(t, rec)
if body["total_nodes"] != float64(20) || body["healthy_nodes"] != float64(15) {
t.Fatalf("snapshot node-pool values mismatch: %+v", body)
}
if body["healthy_egress_ip_count"] != float64(4) {
t.Fatalf("snapshot node-pool healthy_egress_ip_count mismatch: %+v", body)
}
rec = doJSONRequest(t, srv, http.MethodGet, "/api/v1/metrics/snapshots/platform-node-pool?platform_id="+platformID, nil, true)
if rec.Code != http.StatusOK {
t.Fatalf("snapshot platform-node-pool status: got %d, want %d, body=%s", rec.Code, http.StatusOK, rec.Body.String())
}
body = decodeJSONMap(t, rec)
if body["platform_id"] != platformID || body["routable_node_count"] != float64(8) {
t.Fatalf("snapshot platform-node-pool values mismatch: %+v", body)
}
rec = doJSONRequest(t, srv, http.MethodGet, "/api/v1/metrics/snapshots/node-latency-distribution?platform_id="+platformID, nil, true)
if rec.Code != http.StatusOK {
t.Fatalf("snapshot node-latency-distribution status: got %d, want %d, body=%s", rec.Code, http.StatusOK, rec.Body.String())
}
body = decodeJSONMap(t, rec)
if body["scope"] != "platform" || body["sample_count"] == float64(0) {
t.Fatalf("snapshot node-latency-distribution values mismatch: %+v", body)
}
rec = doJSONRequest(t, srv, http.MethodGet, "/api/v1/metrics/history/traffic?from=bad-time", nil, true)
if rec.Code != http.StatusBadRequest {
t.Fatalf("invalid metrics from status: got %d, want %d, body=%s", rec.Code, http.StatusBadRequest, rec.Body.String())
}
assertErrorCode(t, rec, "INVALID_ARGUMENT")
}
package api
import (
"bytes"
"encoding/base64"
"encoding/json"
"errors"
"net/http"
"net/http/httptest"
"net/netip"
"net/url"
"path/filepath"
"strings"
"sync/atomic"
"testing"
"time"
"github.com/Resinat/Resin/internal/config"
"github.com/Resinat/Resin/internal/geoip"
"github.com/Resinat/Resin/internal/metrics"
"github.com/Resinat/Resin/internal/model"
"github.com/Resinat/Resin/internal/node"
"github.com/Resinat/Resin/internal/platform"
"github.com/Resinat/Resin/internal/proxy"
"github.com/Resinat/Resin/internal/requestlog"
"github.com/Resinat/Resin/internal/routing"
"github.com/Resinat/Resin/internal/service"
"github.com/Resinat/Resin/internal/state"
"github.com/Resinat/Resin/internal/subscription"
"github.com/Resinat/Resin/internal/testutil"
"github.com/Resinat/Resin/internal/topology"
)
const testAdminToken = "test-admin-token"
func newControlPlaneTestServer(t *testing.T) (*Server, *service.ControlPlaneService, *atomic.Pointer[config.RuntimeConfig]) {
return newControlPlaneTestServerWithBodyLimit(t, 1<<20)
}
func newControlPlaneTestServerWithBodyLimit(
t *testing.T,
apiMaxBodyBytes int64,
) (*Server, *service.ControlPlaneService, *atomic.Pointer[config.RuntimeConfig]) {
t.Helper()
root := t.TempDir()
engine, closer, err := state.PersistenceBootstrap(
filepath.Join(root, "state"),
filepath.Join(root, "cache"),
)
if err != nil {
t.Fatalf("PersistenceBootstrap: %v", err)
}
t.Cleanup(func() { _ = closer.Close() })
runtimeCfg := &atomic.Pointer[config.RuntimeConfig]{}
runtimeCfg.Store(config.NewDefaultRuntimeConfig())
subMgr := topology.NewSubscriptionManager()
pool := topology.NewGlobalNodePool(topology.PoolConfig{
SubLookup: subMgr.Lookup,
GeoLookup: func(netip.Addr) string { return "us" },
MaxLatencyTableEntries: 32,
MaxConsecutiveFailures: func() int { return 3 },
LatencyDecayWindow: func() time.Duration { return 10 * time.Minute },
})
router := routing.NewRouter(routing.RouterConfig{
Pool: pool,
Authorities: func() []string { return []string{"cloudflare.com"} },
P2CWindow: func() time.Duration { return 10 * time.Minute },
})
scheduler := topology.NewSubscriptionScheduler(topology.SchedulerConfig{
SubManager: subMgr,
Pool: pool,
Fetcher: func(string) ([]byte, error) {
return nil, errors.New("test fetcher failure")
},
})
geoSvc := geoip.NewService(geoip.ServiceConfig{
CacheDir: filepath.Join(root, "geoip"),
OpenDB: geoip.NoOpOpen,
})
cp := &service.ControlPlaneService{
Engine: engine,
Pool: pool,
SubMgr: subMgr,
Scheduler: scheduler,
Router: router,
GeoIP: geoSvc,
MatcherRuntime: proxy.NewAccountMatcherRuntime(nil),
RuntimeCfg: runtimeCfg,
EnvCfg: &config.EnvConfig{
DefaultPlatformStickyTTL: 30 * time.Minute,
DefaultPlatformRegexFilters: []string{},
DefaultPlatformRegionFilters: []string{},
DefaultPlatformReverseProxyMissAction: "TREAT_AS_EMPTY",
DefaultPlatformReverseProxyEmptyAccountBehavior: "ACCOUNT_HEADER_RULE",
DefaultPlatformReverseProxyFixedAccountHeader: "Authorization",
DefaultPlatformAllocationPolicy: "BALANCED",