1
0

node_sync_test.go 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343
  1. package nodee2e
  2. import (
  3. "encoding/json"
  4. "fmt"
  5. "net/http"
  6. "slices"
  7. "strconv"
  8. "testing"
  9. "time"
  10. )
  11. // TestNodeSync walks one master/node pair per enrollment scope through every
  12. // operation that must converge onto the node. Each subtest names its invariant.
  13. func TestNodeSync(t *testing.T) {
  14. bin := panelBinary(t)
  15. for _, scope := range []string{"admin", "node-sync"} {
  16. t.Run("enrolled with "+scope+" token", func(t *testing.T) {
  17. runNodeSyncScenarios(t, bin, scope)
  18. })
  19. }
  20. }
  21. type pair struct {
  22. t *testing.T
  23. master *panel
  24. node *panel
  25. nodeID int
  26. }
  27. func (pr *pair) nodeInbound(port int) (inboundView, bool) { return pr.node.inboundOnPort(port) }
  28. func (pr *pair) masterInbound(port int) (inboundView, bool) {
  29. for _, ib := range pr.master.inbounds() {
  30. if ib.Port == port && ib.NodeID != nil && *ib.NodeID == pr.nodeID {
  31. return ib, true
  32. }
  33. }
  34. return inboundView{}, false
  35. }
  36. func (pr *pair) waitNode(what string, port int, ok func(inboundView) bool) {
  37. pr.t.Helper()
  38. eventually(pr.t, settleTimeout, what, func() (bool, string) {
  39. ib, found := pr.nodeInbound(port)
  40. if !found {
  41. return ok(inboundView{}) && false, fmt.Sprintf("node has no inbound on %d", port)
  42. }
  43. return ok(ib), fmt.Sprintf("node inbound %d: enable=%v remark=%q emails=%v", port, ib.Enable, ib.Remark, ib.emails())
  44. })
  45. }
  46. func (pr *pair) waitNodeAbsent(what string, port int) {
  47. pr.t.Helper()
  48. eventually(pr.t, settleTimeout, what, func() (bool, string) {
  49. ib, found := pr.nodeInbound(port)
  50. return !found, fmt.Sprintf("node still has inbound %d with %v", port, ib.emails())
  51. })
  52. }
  53. func (pr *pair) bulkAttach(emails []string, masterInboundID int) {
  54. pr.t.Helper()
  55. var res struct {
  56. Attached []string `json:"attached"`
  57. Errors []string `json:"errors"`
  58. }
  59. obj := pr.master.call(http.MethodPost, "/panel/api/clients/bulkAttach", map[string]any{"emails": emails, "inboundIds": []int{masterInboundID}})
  60. if err := json.Unmarshal(obj, &res); err != nil {
  61. pr.t.Fatalf("decode bulkAttach: %v", err)
  62. }
  63. if len(res.Errors) != 0 || len(res.Attached) != len(emails) {
  64. pr.t.Fatalf("bulkAttach attached=%d/%d errors=%v", len(res.Attached), len(emails), res.Errors)
  65. }
  66. }
  67. func emailRange(prefix string, from, to int) []string {
  68. out := make([]string, 0, to-from+1)
  69. for i := from; i <= to; i++ {
  70. out = append(out, prefix+strconv.Itoa(i))
  71. }
  72. return out
  73. }
  74. func hasAll(have []string, want ...string) bool {
  75. for _, w := range want {
  76. if !slices.Contains(have, w) {
  77. return false
  78. }
  79. }
  80. return true
  81. }
  82. func runNodeSyncScenarios(t *testing.T, bin, scope string) {
  83. master := newPanel(t, bin, "master")
  84. node := newPanel(t, bin, "node")
  85. linkToken := node.mintToken("master-link", scope)
  86. master.start()
  87. node.start()
  88. pr := &pair{t: t, master: master, node: node}
  89. var (
  90. adoptedPort = freePort(t)
  91. madePort = freePort(t)
  92. lostPort = freePort(t)
  93. droppedPort = freePort(t)
  94. offlinePort = freePort(t)
  95. unmanagedPort = freePort(t)
  96. localPort = freePort(t)
  97. )
  98. node.call(http.MethodPost, "/panel/api/inbounds/add", vlessInbound("pre-existing", adoptedPort, nil))
  99. local := master.call(http.MethodPost, "/panel/api/inbounds/add", vlessInbound("local-pool", localPort, nil))
  100. var localIb inboundView
  101. _ = json.Unmarshal(local, &localIb)
  102. for _, email := range emailRange("p", 1, 45) {
  103. master.call(http.MethodPost, "/panel/api/clients/add", map[string]any{
  104. "client": map[string]any{"email": email, "enable": true}, "inboundIds": []int{localIb.Id},
  105. })
  106. }
  107. var nodeView struct {
  108. Id int `json:"id"`
  109. }
  110. obj := master.call(http.MethodPost, "/panel/api/nodes/add", map[string]any{
  111. "name": "n1", "scheme": "http", "address": "127.0.0.1", "port": node.port, "basePath": "/",
  112. "apiToken": linkToken, "enable": true, "allowPrivateAddress": true,
  113. })
  114. if err := json.Unmarshal(obj, &nodeView); err != nil || nodeView.Id == 0 {
  115. t.Fatalf("decode node add: %v (%s)", err, obj)
  116. }
  117. pr.nodeID = nodeView.Id
  118. var adoptedID, madeID int
  119. t.Run("an inbound already on the node is adopted by the master", func(t *testing.T) {
  120. pr.t = t
  121. eventually(t, settleTimeout, "master adopts the node inbound", func() (bool, string) {
  122. ib, ok := pr.masterInbound(adoptedPort)
  123. adoptedID = ib.Id
  124. return ok, "not adopted yet"
  125. })
  126. })
  127. if adoptedID == 0 {
  128. t.Fatal("no adopted inbound; later scenarios depend on it")
  129. }
  130. t.Run("an inbound created on the master for the node lands there with its clients", func(t *testing.T) {
  131. pr.t = t
  132. obj := master.call(http.MethodPost, "/panel/api/inbounds/add",
  133. vlessInbound("made-on-master", madePort, &pr.nodeID, vlessClient("m1"), vlessClient("m2")))
  134. var ib inboundView
  135. _ = json.Unmarshal(obj, &ib)
  136. madeID = ib.Id
  137. pr.waitNode("node holds the master-made inbound", madePort, func(ib inboundView) bool {
  138. return hasAll(ib.emails(), "m1", "m2")
  139. })
  140. })
  141. t.Run("editing the inbound on the master updates the node and keeps its clients", func(t *testing.T) {
  142. pr.t = t
  143. body := vlessInbound("renamed-on-master", madePort, &pr.nodeID)
  144. body["settings"] = `{"decryption":"none"}`
  145. master.call(http.MethodPost, "/panel/api/inbounds/update/"+strconv.Itoa(madeID), body)
  146. pr.waitNode("node shows the new remark with both clients", madePort, func(ib inboundView) bool {
  147. return ib.Remark == "renamed-on-master" && hasAll(ib.emails(), "m1", "m2")
  148. })
  149. })
  150. t.Run("attaching a few existing clients reaches the node", func(t *testing.T) {
  151. pr.t = t
  152. pr.bulkAttach([]string{"p1", "p2", "p3"}, madeID)
  153. pr.waitNode("node holds p1..p3", madePort, func(ib inboundView) bool {
  154. return hasAll(ib.emails(), "m1", "m2", "p1", "p2", "p3")
  155. })
  156. })
  157. t.Run("attaching more clients than the per-client push limit reaches the node", func(t *testing.T) {
  158. pr.t = t
  159. emails := emailRange("p", 4, 43)
  160. pr.bulkAttach(emails, adoptedID)
  161. pr.waitNode("node holds all 40", adoptedPort, func(ib inboundView) bool {
  162. return hasAll(ib.emails(), emails...)
  163. })
  164. })
  165. t.Run("disabling a client on the master disables it on the node", func(t *testing.T) {
  166. pr.t = t
  167. mib, _ := pr.masterInbound(madePort)
  168. entry := mib.client("p1")
  169. if entry == nil {
  170. t.Fatalf("master inbound has no p1: %v", mib.emails())
  171. }
  172. entry["enable"] = false
  173. master.call(http.MethodPost, "/panel/api/clients/update/p1", entry)
  174. pr.waitNode("node p1 disabled", madePort, func(ib inboundView) bool {
  175. c := ib.client("p1")
  176. return c != nil && c["enable"] == false
  177. })
  178. })
  179. t.Run("detaching a client from the node inbound removes it there", func(t *testing.T) {
  180. pr.t = t
  181. master.call(http.MethodPost, "/panel/api/clients/p2/detach", map[string]any{"inboundIds": []int{madeID}})
  182. pr.waitNode("node drops p2", madePort, func(ib inboundView) bool {
  183. return ib.client("p2") == nil && ib.client("p3") != nil
  184. })
  185. })
  186. t.Run("deleting a client on the master removes it from the node", func(t *testing.T) {
  187. pr.t = t
  188. master.call(http.MethodPost, "/panel/api/clients/del/p4", nil)
  189. pr.waitNode("node drops p4", adoptedPort, func(ib inboundView) bool {
  190. return ib.client("p4") == nil && ib.client("p5") != nil
  191. })
  192. })
  193. t.Run("switching the inbound off on the master switches it off on the node", func(t *testing.T) {
  194. pr.t = t
  195. master.call(http.MethodPost, "/panel/api/inbounds/setEnable/"+strconv.Itoa(madeID), map[string]any{"enable": false})
  196. pr.waitNode("node inbound disabled", madePort, func(ib inboundView) bool { return !ib.Enable })
  197. })
  198. t.Run("node traffic reaches the master and a master reset clears the node", func(t *testing.T) {
  199. pr.t = t
  200. node.call(http.MethodPost, "/panel/api/clients/updateTraffic/p5", map[string]any{"upload": 1000, "download": 2000})
  201. usage := func(p *panel) int64 {
  202. var tr struct{ Up, Down int64 }
  203. _ = json.Unmarshal(p.call(http.MethodGet, "/panel/api/clients/traffic/p5", nil), &tr)
  204. return tr.Up + tr.Down
  205. }
  206. eventually(t, settleTimeout, "master sees p5's node traffic", func() (bool, string) {
  207. u := usage(master)
  208. return u == 3000, fmt.Sprintf("master p5 usage %d", u)
  209. })
  210. master.call(http.MethodPost, "/panel/api/clients/resetTraffic/p5", nil)
  211. eventually(t, settleTimeout, "node p5 usage reset", func() (bool, string) {
  212. u := usage(node)
  213. return u == 0, fmt.Sprintf("node p5 usage %d", u)
  214. })
  215. time.Sleep(12 * time.Second)
  216. if u := usage(master); u != 0 {
  217. t.Fatalf("master p5 usage %d after reset settled, want 0", u)
  218. }
  219. })
  220. t.Run("an inbound deleted on the node is removed from the master too", func(t *testing.T) {
  221. pr.t = t
  222. master.call(http.MethodPost, "/panel/api/inbounds/add", vlessInbound("deleted-on-node", lostPort, &pr.nodeID, vlessClient("l1")))
  223. pr.waitNode("node holds the inbound", lostPort, func(ib inboundView) bool { return ib.client("l1") != nil })
  224. nib, _ := pr.nodeInbound(lostPort)
  225. node.call(http.MethodPost, "/panel/api/inbounds/del/"+strconv.Itoa(nib.Id), nil)
  226. eventually(t, settleTimeout, "master mirrors the node-side delete (#6219)", func() (bool, string) {
  227. _, still := pr.masterInbound(lostPort)
  228. return !still, "master still has the inbound"
  229. })
  230. })
  231. t.Run("deleting the inbound on the master removes it from the node", func(t *testing.T) {
  232. pr.t = t
  233. obj := master.call(http.MethodPost, "/panel/api/inbounds/add", vlessInbound("deleted-on-master", droppedPort, &pr.nodeID, vlessClient("d1")))
  234. var ib inboundView
  235. _ = json.Unmarshal(obj, &ib)
  236. pr.waitNode("node holds the inbound", droppedPort, func(ib inboundView) bool { return ib.client("d1") != nil })
  237. master.call(http.MethodPost, "/panel/api/inbounds/del/"+strconv.Itoa(ib.Id), nil)
  238. pr.waitNodeAbsent("node drops the inbound", droppedPort)
  239. })
  240. t.Run("a change made while the node is down reaches it once it is back", func(t *testing.T) {
  241. pr.t = t
  242. node.stop()
  243. pr.bulkAttach([]string{"p44"}, adoptedID)
  244. node.start()
  245. pr.waitNode("node holds p44 after restart", adoptedPort, func(ib inboundView) bool {
  246. return ib.client("p44") != nil
  247. })
  248. })
  249. t.Run("an inbound the node lost while down is re-created with the master's pending change", func(t *testing.T) {
  250. pr.t = t
  251. nib, ok := pr.nodeInbound(adoptedPort)
  252. if !ok {
  253. t.Fatal("node has no adopted inbound to lose")
  254. }
  255. node.stop()
  256. node.deleteInboundRow(nib.Id)
  257. pr.bulkAttach([]string{"p45"}, adoptedID)
  258. node.start()
  259. pr.waitNode("node re-creates the inbound with p44 and p45", adoptedPort, func(ib inboundView) bool {
  260. return ib.client("p44") != nil && ib.client("p45") != nil
  261. })
  262. })
  263. t.Run("an inbound created and edited while the node is down lands once it is back", func(t *testing.T) {
  264. pr.t = t
  265. node.stop()
  266. obj := master.call(http.MethodPost, "/panel/api/inbounds/add", vlessInbound("made-while-down", offlinePort, &pr.nodeID, vlessClient("o1")))
  267. var ib inboundView
  268. _ = json.Unmarshal(obj, &ib)
  269. body := vlessInbound("edited-while-down", offlinePort, &pr.nodeID)
  270. body["settings"] = `{"decryption":"none"}`
  271. master.call(http.MethodPost, "/panel/api/inbounds/update/"+strconv.Itoa(ib.Id), body)
  272. node.start()
  273. pr.waitNode("node holds the edited inbound with o1", offlinePort, func(ib inboundView) bool {
  274. return ib.Remark == "edited-while-down" && ib.client("o1") != nil
  275. })
  276. })
  277. t.Run("a change made while the node is disabled on the master lands once it is re-enabled", func(t *testing.T) {
  278. pr.t = t
  279. nodePath := "/panel/api/nodes/setEnable/" + strconv.Itoa(pr.nodeID)
  280. master.call(http.MethodPost, nodePath, map[string]any{"enable": false})
  281. pr.bulkAttach([]string{"p6"}, madeID)
  282. time.Sleep(6 * time.Second)
  283. if ib, _ := pr.nodeInbound(madePort); ib.client("p6") != nil {
  284. t.Fatal("a disabled node received a push")
  285. }
  286. master.call(http.MethodPost, nodePath, map[string]any{"enable": true})
  287. pr.waitNode("node holds p6 after re-enable", madePort, func(ib inboundView) bool { return ib.client("p6") != nil })
  288. })
  289. t.Run("selected sync mode leaves the node's unselected inbounds alone", func(t *testing.T) {
  290. pr.t = t
  291. var selected []string
  292. for _, ib := range master.inbounds() {
  293. if ib.NodeID != nil && *ib.NodeID == pr.nodeID {
  294. selected = append(selected, ib.Tag)
  295. }
  296. }
  297. master.call(http.MethodPost, "/panel/api/nodes/update/"+strconv.Itoa(pr.nodeID), map[string]any{
  298. "name": "n1", "scheme": "http", "address": "127.0.0.1", "port": node.port, "basePath": "/",
  299. "enable": true, "allowPrivateAddress": true, "inboundSyncMode": "selected", "inboundTags": selected,
  300. })
  301. node.call(http.MethodPost, "/panel/api/inbounds/add", vlessInbound("node-only", unmanagedPort, nil, vlessClient("u1")))
  302. pr.bulkAttach([]string{"p7"}, madeID)
  303. pr.waitNode("selected inbound still converges", madePort, func(ib inboundView) bool { return ib.client("p7") != nil })
  304. time.Sleep(12 * time.Second)
  305. if _, adopted := pr.masterInbound(unmanagedPort); adopted {
  306. t.Fatal("master adopted an unselected node inbound")
  307. }
  308. if ib, ok := pr.nodeInbound(unmanagedPort); !ok || ib.client("u1") == nil {
  309. t.Fatal("reconcile swept or rewrote an unselected node inbound")
  310. }
  311. })
  312. }