manager_reload_test.go 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  1. package mtproto
  2. import (
  3. "encoding/json"
  4. "fmt"
  5. "net/http"
  6. "net/http/httptest"
  7. "os"
  8. "path/filepath"
  9. "strings"
  10. "testing"
  11. "time"
  12. )
  13. // TestMain lets the test binary re-exec itself as a stand-in for the mtg
  14. // child process: with MTG_FAKE_CHILD=1 it records its pid and blocks, so the
  15. // manager can start and stop it without a real mtg-multi binary.
  16. func TestMain(m *testing.M) {
  17. if os.Getenv("MTG_FAKE_CHILD") == "1" {
  18. if f, err := os.OpenFile(os.Getenv("MTG_FAKE_PIDFILE"), os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0o644); err == nil {
  19. fmt.Fprintf(f, "%d\n", os.Getpid())
  20. f.Close()
  21. }
  22. select {}
  23. }
  24. os.Exit(m.Run())
  25. }
  26. func installFakeMtg(t *testing.T) string {
  27. t.Helper()
  28. binDir := t.TempDir()
  29. self, err := os.Executable()
  30. if err != nil {
  31. t.Fatalf("locate test binary: %v", err)
  32. }
  33. payload, err := os.ReadFile(self)
  34. if err != nil {
  35. t.Fatalf("read test binary: %v", err)
  36. }
  37. if err := os.WriteFile(filepath.Join(binDir, GetBinaryName()), payload, 0o755); err != nil {
  38. t.Fatalf("install fake mtg: %v", err)
  39. }
  40. pidFile := filepath.Join(binDir, "mtg-pids.txt")
  41. t.Setenv("XUI_BIN_FOLDER", binDir)
  42. t.Setenv("MTG_FAKE_CHILD", "1")
  43. t.Setenv("MTG_FAKE_PIDFILE", pidFile)
  44. return pidFile
  45. }
  46. func spawnCount(t *testing.T, pidFile string) int {
  47. t.Helper()
  48. data, err := os.ReadFile(pidFile)
  49. if os.IsNotExist(err) {
  50. return 0
  51. }
  52. if err != nil {
  53. t.Fatalf("read pid file: %v", err)
  54. }
  55. return len(strings.Fields(string(data)))
  56. }
  57. func waitSpawnCount(t *testing.T, pidFile string, want int) {
  58. t.Helper()
  59. deadline := time.Now().Add(5 * time.Second)
  60. for {
  61. got := spawnCount(t, pidFile)
  62. if got == want {
  63. return
  64. }
  65. if got > want {
  66. t.Fatalf("expected %d spawn(s), got %d", want, got)
  67. }
  68. if time.Now().After(deadline) {
  69. t.Fatalf("expected %d spawn(s), still %d after timeout", want, got)
  70. }
  71. time.Sleep(20 * time.Millisecond)
  72. }
  73. }
  74. func mtgInst(id int, secrets ...SecretEntry) Instance {
  75. return Instance{Id: id, Tag: fmt.Sprintf("inbound-%d", id), Listen: "127.0.0.1", Port: 24000 + id, Secrets: secrets}
  76. }
  77. func TestEnsureActionFor(t *testing.T) {
  78. cases := []struct {
  79. name string
  80. running bool
  81. curStruct, curSecrets, newStruct, newSecrets string
  82. want ensureAction
  83. }{
  84. {"dead process restarts", false, "s", "a", "s", "a", ensureRestart},
  85. {"structural change restarts", true, "s1", "a", "s2", "a", ensureRestart},
  86. {"secrets change reloads", true, "s", "a", "s", "b", ensureReload},
  87. {"identical is a noop", true, "s", "a", "s", "a", ensureNoop},
  88. {"dead beats a secrets-only change", false, "s", "a", "s", "b", ensureRestart},
  89. }
  90. for _, tc := range cases {
  91. t.Run(tc.name, func(t *testing.T) {
  92. if got := ensureActionFor(tc.running, tc.curStruct, tc.curSecrets, tc.newStruct, tc.newSecrets); got != tc.want {
  93. t.Fatalf("ensureActionFor = %d, want %d", got, tc.want)
  94. }
  95. })
  96. }
  97. }
  98. func TestApplySecrets(t *testing.T) {
  99. cases := []struct {
  100. name string
  101. status int
  102. want bool
  103. }{
  104. {"ok", http.StatusOK, true},
  105. {"not found on old binary", http.StatusNotFound, false},
  106. {"bad request", http.StatusBadRequest, false},
  107. {"unavailable", http.StatusServiceUnavailable, false},
  108. }
  109. for _, tc := range cases {
  110. t.Run(tc.name, func(t *testing.T) {
  111. var gotMethod, gotPath string
  112. var gotBody secretsPutBody
  113. srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
  114. gotMethod, gotPath = r.Method, r.URL.Path
  115. _ = json.NewDecoder(r.Body).Decode(&gotBody)
  116. w.WriteHeader(tc.status)
  117. }))
  118. defer srv.Close()
  119. inst := mtgInst(1, SecretEntry{Name: "alice", Secret: "ee01"})
  120. inst.AdTag = "0123456789abcdef0123456789abcdef"
  121. if got := applySecrets(serverPort(t, srv), inst); got != tc.want {
  122. t.Fatalf("applySecrets = %v, want %v", got, tc.want)
  123. }
  124. if gotMethod != http.MethodPut || gotPath != "/secrets" {
  125. t.Fatalf("expected PUT /secrets, got %s %s", gotMethod, gotPath)
  126. }
  127. if gotBody.Secrets["alice"].Secret != "ee01" || gotBody.AdTag != "0123456789abcdef0123456789abcdef" {
  128. t.Fatalf("payload must carry the secret and ad-tag: %+v", gotBody)
  129. }
  130. })
  131. }
  132. t.Run("refused connection", func(t *testing.T) {
  133. srv := httptest.NewServer(http.NotFoundHandler())
  134. port := serverPort(t, srv)
  135. srv.Close()
  136. if applySecrets(port, mtgInst(1, SecretEntry{Name: "a", Secret: "ee"})) {
  137. t.Fatal("a refused connection must yield false")
  138. }
  139. })
  140. }
  141. func TestEnsureHotReloadKeepsProcess(t *testing.T) {
  142. pidFile := installFakeMtg(t)
  143. mgr := &Manager{procs: map[int]*managed{}, swept: true}
  144. inst := mtgInst(1, SecretEntry{Name: "alice", Secret: "ee01"})
  145. if err := mgr.Ensure(inst); err != nil {
  146. t.Fatalf("initial ensure: %v", err)
  147. }
  148. waitSpawnCount(t, pidFile, 1)
  149. orig := mgr.procs[1].proc
  150. reloaded := make(chan struct{}, 1)
  151. srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
  152. if r.Method == http.MethodPut && r.URL.Path == "/secrets" {
  153. reloaded <- struct{}{}
  154. w.WriteHeader(http.StatusOK)
  155. return
  156. }
  157. http.NotFound(w, r)
  158. }))
  159. defer srv.Close()
  160. mgr.procs[1].apiPort = serverPort(t, srv)
  161. rekeyed := mtgInst(1, SecretEntry{Name: "alice", Secret: "ee01"}, SecretEntry{Name: "bob", Secret: "ee02"})
  162. if err := mgr.Ensure(rekeyed); err != nil {
  163. t.Fatalf("reload ensure: %v", err)
  164. }
  165. select {
  166. case <-reloaded:
  167. case <-time.After(3 * time.Second):
  168. t.Fatal("expected a PUT /secrets request")
  169. }
  170. if got := spawnCount(t, pidFile); got != 1 {
  171. t.Fatalf("hot reload must not spawn a new process, got %d", got)
  172. }
  173. if mgr.procs[1].proc != orig {
  174. t.Fatal("hot reload must keep the same process")
  175. }
  176. if mgr.procs[1].secretsFP != rekeyed.secretsFingerprint() {
  177. t.Fatal("stored secrets fingerprint must advance after a reload")
  178. }
  179. cfg, err := os.ReadFile(configPathForID(1))
  180. if err != nil {
  181. t.Fatalf("read config: %v", err)
  182. }
  183. if !strings.Contains(string(cfg), `"bob" = "ee02"`) {
  184. t.Fatalf("reloaded config must carry the new secret:\n%s", cfg)
  185. }
  186. if !strings.Contains(string(cfg), fmt.Sprintf("api-bind-to = \"127.0.0.1:%d\"", serverPort(t, srv))) {
  187. t.Fatalf("reload must reuse the same api port:\n%s", cfg)
  188. }
  189. mgr.StopAll()
  190. }
  191. func TestEnsureReloadFallbackRestarts(t *testing.T) {
  192. pidFile := installFakeMtg(t)
  193. mgr := &Manager{procs: map[int]*managed{}, swept: true}
  194. if err := mgr.Ensure(mtgInst(2, SecretEntry{Name: "alice", Secret: "ee01"})); err != nil {
  195. t.Fatalf("initial ensure: %v", err)
  196. }
  197. waitSpawnCount(t, pidFile, 1)
  198. srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
  199. w.WriteHeader(http.StatusNotFound)
  200. }))
  201. defer srv.Close()
  202. mgr.procs[2].apiPort = serverPort(t, srv)
  203. if err := mgr.Ensure(mtgInst(2, SecretEntry{Name: "carol", Secret: "ee03"})); err != nil {
  204. t.Fatalf("fallback ensure: %v", err)
  205. }
  206. waitSpawnCount(t, pidFile, 2)
  207. mgr.StopAll()
  208. }
  209. func TestEnsureNoopKeepsProcess(t *testing.T) {
  210. pidFile := installFakeMtg(t)
  211. mgr := &Manager{procs: map[int]*managed{}, swept: true}
  212. inst := mtgInst(3, SecretEntry{Name: "alice", Secret: "ee01"}, SecretEntry{Name: "bob", Secret: "ee02"})
  213. if err := mgr.Ensure(inst); err != nil {
  214. t.Fatalf("initial ensure: %v", err)
  215. }
  216. waitSpawnCount(t, pidFile, 1)
  217. if err := mgr.Ensure(inst); err != nil {
  218. t.Fatalf("repeat ensure: %v", err)
  219. }
  220. reordered := mtgInst(3, SecretEntry{Name: "bob", Secret: "ee02"}, SecretEntry{Name: "alice", Secret: "ee01"})
  221. if err := mgr.Ensure(reordered); err != nil {
  222. t.Fatalf("reordered ensure: %v", err)
  223. }
  224. time.Sleep(300 * time.Millisecond)
  225. if got := spawnCount(t, pidFile); got != 1 {
  226. t.Fatalf("an unchanged instance must keep the one process, got %d spawns", got)
  227. }
  228. mgr.StopAll()
  229. }