File size: 6,894 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 | package api
import (
"net/http"
"net/http/httptest"
"strings"
"sync/atomic"
"testing"
"time"
"github.com/Resinat/Resin/internal/netutil"
"github.com/Resinat/Resin/internal/node"
"github.com/Resinat/Resin/internal/topology"
)
func TestAPIContract_SubscriptionRefreshAction_E2EHTTPSource(t *testing.T) {
srv, cp, _ := newControlPlaneTestServer(t)
const rawOutbound = `{"type":"shadowsocks","tag":"edge-refresh","server":"1.1.1.1","server_port":443,"method":"aes-256-gcm","password":"secret"}`
subPayload := `{"outbounds":[` + rawOutbound + `]}`
const userAgent = "resin-api-e2e"
var subscriptionHits atomic.Int32
subscriptionSource := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
subscriptionHits.Add(1)
if got := r.URL.Path; got != "/sub" {
t.Fatalf("subscription path: got %q, want %q", got, "/sub")
}
if got := r.Header.Get("User-Agent"); got != userAgent {
t.Fatalf("subscription user-agent: got %q, want %q", got, userAgent)
}
_, _ = w.Write([]byte(subPayload))
}))
defer subscriptionSource.Close()
cp.Scheduler = topology.NewSubscriptionScheduler(topology.SchedulerConfig{
SubManager: cp.SubMgr,
Pool: cp.Pool,
Downloader: netutil.NewDirectDownloader(
func() time.Duration { return 2 * time.Second },
func() string { return userAgent },
),
})
createRec := doJSONRequest(t, srv, http.MethodPost, "/api/v1/subscriptions", map[string]any{
"name": "sub-e2e",
"url": subscriptionSource.URL + "/sub",
"incremental_alive_nodes": true,
}, 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())
}
if got := createBody["node_count"]; got != float64(0) {
t.Fatalf("create subscription node_count: got %v, want %v", got, 0)
}
if got := createBody["healthy_node_count"]; got != float64(0) {
t.Fatalf("create subscription healthy_node_count: got %v, want %v", got, 0)
}
if got := createBody["incremental_alive_nodes"]; got != true {
t.Fatalf("create subscription incremental_alive_nodes: got %v, want %v", got, true)
}
refreshRec := doJSONRequest(
t,
srv,
http.MethodPost,
"/api/v1/subscriptions/"+subID+"/actions/refresh",
nil,
true,
)
if refreshRec.Code != http.StatusOK {
t.Fatalf("refresh subscription status: got %d, want %d, body=%s", refreshRec.Code, http.StatusOK, refreshRec.Body.String())
}
refreshBody := decodeJSONMap(t, refreshRec)
if refreshBody["status"] != "ok" {
t.Fatalf("refresh response status: got %v, want ok", refreshBody["status"])
}
if subscriptionHits.Load() == 0 {
t.Fatal("subscription HTTP source was not requested")
}
getRec := doJSONRequest(t, srv, http.MethodGet, "/api/v1/subscriptions/"+subID, nil, true)
if getRec.Code != http.StatusOK {
t.Fatalf("get subscription status: got %d, want %d, body=%s", getRec.Code, http.StatusOK, getRec.Body.String())
}
subBody := decodeJSONMap(t, getRec)
if got, _ := subBody["id"].(string); got != subID {
t.Fatalf("subscription id: got %q, want %q", got, subID)
}
lastChecked, _ := subBody["last_checked"].(string)
if strings.TrimSpace(lastChecked) == "" {
t.Fatalf("last_checked should be non-empty after refresh, body=%s", getRec.Body.String())
}
lastUpdated, _ := subBody["last_updated"].(string)
if strings.TrimSpace(lastUpdated) == "" {
t.Fatalf("last_updated should be non-empty after refresh, body=%s", getRec.Body.String())
}
if v, ok := subBody["last_error"]; ok {
if s, _ := v.(string); s != "" {
t.Fatalf("last_error should be empty after successful refresh, got %q", s)
}
}
if got := subBody["node_count"]; got != float64(1) {
t.Fatalf("subscription node_count after refresh: got %v, want %v", got, 1)
}
if got := subBody["healthy_node_count"]; got != float64(0) {
t.Fatalf("subscription healthy_node_count after refresh: got %v, want %v", got, 0)
}
nodesRec := doJSONRequest(t, srv, http.MethodGet, "/api/v1/nodes?subscription_id="+subID, nil, true)
if nodesRec.Code != http.StatusOK {
t.Fatalf("list nodes by subscription status: got %d, want %d, body=%s", nodesRec.Code, http.StatusOK, nodesRec.Body.String())
}
nodesBody := decodeJSONMap(t, nodesRec)
items, ok := nodesBody["items"].([]any)
if !ok {
t.Fatalf("items type: got %T", nodesBody["items"])
}
if len(items) != 1 {
t.Fatalf("expected exactly 1 node after refresh, got %d, body=%s", len(items), nodesRec.Body.String())
}
item, ok := items[0].(map[string]any)
if !ok {
t.Fatalf("node item type: got %T", items[0])
}
wantHash := node.HashFromRawOptions([]byte(rawOutbound)).Hex()
gotHash, _ := item["node_hash"].(string)
if gotHash != wantHash {
t.Fatalf("node hash: got %q, want %q", gotHash, wantHash)
}
}
func TestAPIContract_SubscriptionRefreshAction_E2ELocalSource(t *testing.T) {
srv, _, _ := newControlPlaneTestServer(t)
const rawOutbound = `{"type":"shadowsocks","tag":"edge-local","server":"1.1.1.1","server_port":443,"method":"aes-256-gcm","password":"secret"}`
localContent := `{"outbounds":[` + rawOutbound + `]}`
createRec := doJSONRequest(t, srv, http.MethodPost, "/api/v1/subscriptions", map[string]any{
"name": "sub-local-e2e",
"source_type": "local",
"content": localContent,
}, true)
if createRec.Code != http.StatusCreated {
t.Fatalf("create local 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 local subscription missing id: body=%s", createRec.Body.String())
}
if got, _ := createBody["source_type"].(string); got != "local" {
t.Fatalf("create local source_type: got %q, want %q", got, "local")
}
refreshRec := doJSONRequest(
t,
srv,
http.MethodPost,
"/api/v1/subscriptions/"+subID+"/actions/refresh",
nil,
true,
)
if refreshRec.Code != http.StatusOK {
t.Fatalf("refresh local subscription status: got %d, want %d, body=%s", refreshRec.Code, http.StatusOK, refreshRec.Body.String())
}
getRec := doJSONRequest(t, srv, http.MethodGet, "/api/v1/subscriptions/"+subID, nil, true)
if getRec.Code != http.StatusOK {
t.Fatalf("get local subscription status: got %d, want %d, body=%s", getRec.Code, http.StatusOK, getRec.Body.String())
}
subBody := decodeJSONMap(t, getRec)
if got := subBody["node_count"]; got != float64(1) {
t.Fatalf("local subscription node_count after refresh: got %v, want %v", got, 1)
}
if got, _ := subBody["last_error"].(string); got != "" {
t.Fatalf("local subscription last_error: got %q, want empty", got)
}
}
|