manager_reload_test.go 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267
  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, gotAuth string
  112. var gotBody secretsPutBody
  113. srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
  114. gotMethod, gotPath, gotAuth = r.Method, r.URL.Path, r.Header.Get("Authorization")
  115. _ = json.NewDecoder(r.Body).Decode(&gotBody)
  116. w.WriteHeader(tc.status)
  117. }))
  118. defer srv.Close()
  119. inst := mtgInst(1,
  120. SecretEntry{Name: "alice", Secret: "ee01"},
  121. SecretEntry{Name: "bob", Secret: "ee02", AdTag: "fedcba9876543210fedcba9876543210"})
  122. inst.AdTag = "0123456789abcdef0123456789abcdef"
  123. if got := applySecrets(serverPort(t, srv), "sesame", inst); got != tc.want {
  124. t.Fatalf("applySecrets = %v, want %v", got, tc.want)
  125. }
  126. if gotMethod != http.MethodPut || gotPath != "/secrets" {
  127. t.Fatalf("expected PUT /secrets, got %s %s", gotMethod, gotPath)
  128. }
  129. if gotAuth != "Bearer sesame" {
  130. t.Fatalf("expected the bearer token on the request, got %q", gotAuth)
  131. }
  132. if gotBody.Secrets["alice"].Secret != "ee01" || gotBody.AdTag != "0123456789abcdef0123456789abcdef" {
  133. t.Fatalf("payload must carry the secret and ad-tag: %+v", gotBody)
  134. }
  135. if gotBody.Secrets["alice"].AdTag != "" || gotBody.Secrets["bob"].AdTag != "fedcba9876543210fedcba9876543210" {
  136. t.Fatalf("payload must carry per-client ad-tag overrides only where set: %+v", gotBody)
  137. }
  138. })
  139. }
  140. t.Run("refused connection", func(t *testing.T) {
  141. srv := httptest.NewServer(http.NotFoundHandler())
  142. port := serverPort(t, srv)
  143. srv.Close()
  144. if applySecrets(port, "", mtgInst(1, SecretEntry{Name: "a", Secret: "ee"})) {
  145. t.Fatal("a refused connection must yield false")
  146. }
  147. })
  148. }
  149. func TestEnsureHotReloadKeepsProcess(t *testing.T) {
  150. pidFile := installFakeMtg(t)
  151. mgr := &Manager{procs: map[int]*managed{}, swept: true}
  152. inst := mtgInst(1, SecretEntry{Name: "alice", Secret: "ee01"})
  153. if err := mgr.Ensure(inst); err != nil {
  154. t.Fatalf("initial ensure: %v", err)
  155. }
  156. waitSpawnCount(t, pidFile, 1)
  157. orig := mgr.procs[1].proc
  158. origToken := mgr.procs[1].apiToken
  159. if origToken == "" {
  160. t.Fatal("a started process must get an api token")
  161. }
  162. reloaded := make(chan struct{}, 1)
  163. srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
  164. if r.Method == http.MethodPut && r.URL.Path == "/secrets" {
  165. reloaded <- struct{}{}
  166. w.WriteHeader(http.StatusOK)
  167. return
  168. }
  169. http.NotFound(w, r)
  170. }))
  171. defer srv.Close()
  172. mgr.procs[1].apiPort = serverPort(t, srv)
  173. rekeyed := mtgInst(1, SecretEntry{Name: "alice", Secret: "ee01"}, SecretEntry{Name: "bob", Secret: "ee02"})
  174. if err := mgr.Ensure(rekeyed); err != nil {
  175. t.Fatalf("reload ensure: %v", err)
  176. }
  177. select {
  178. case <-reloaded:
  179. case <-time.After(3 * time.Second):
  180. t.Fatal("expected a PUT /secrets request")
  181. }
  182. if got := spawnCount(t, pidFile); got != 1 {
  183. t.Fatalf("hot reload must not spawn a new process, got %d", got)
  184. }
  185. if mgr.procs[1].proc != orig {
  186. t.Fatal("hot reload must keep the same process")
  187. }
  188. if mgr.procs[1].secretsFP != rekeyed.secretsFingerprint() {
  189. t.Fatal("stored secrets fingerprint must advance after a reload")
  190. }
  191. cfg, err := os.ReadFile(configPathForID(1))
  192. if err != nil {
  193. t.Fatalf("read config: %v", err)
  194. }
  195. if !strings.Contains(string(cfg), `"bob" = "ee02"`) {
  196. t.Fatalf("reloaded config must carry the new secret:\n%s", cfg)
  197. }
  198. if !strings.Contains(string(cfg), fmt.Sprintf("api-bind-to = \"127.0.0.1:%d\"", serverPort(t, srv))) {
  199. t.Fatalf("reload must reuse the same api port:\n%s", cfg)
  200. }
  201. if !strings.Contains(string(cfg), fmt.Sprintf("api-token = %q", origToken)) {
  202. t.Fatalf("reload must reuse the token the running process was started with:\n%s", cfg)
  203. }
  204. mgr.StopAll()
  205. }
  206. func TestEnsureReloadFallbackRestarts(t *testing.T) {
  207. pidFile := installFakeMtg(t)
  208. mgr := &Manager{procs: map[int]*managed{}, swept: true}
  209. if err := mgr.Ensure(mtgInst(2, SecretEntry{Name: "alice", Secret: "ee01"})); err != nil {
  210. t.Fatalf("initial ensure: %v", err)
  211. }
  212. waitSpawnCount(t, pidFile, 1)
  213. srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
  214. w.WriteHeader(http.StatusNotFound)
  215. }))
  216. defer srv.Close()
  217. mgr.procs[2].apiPort = serverPort(t, srv)
  218. if err := mgr.Ensure(mtgInst(2, SecretEntry{Name: "carol", Secret: "ee03"})); err != nil {
  219. t.Fatalf("fallback ensure: %v", err)
  220. }
  221. waitSpawnCount(t, pidFile, 2)
  222. mgr.StopAll()
  223. }
  224. func TestEnsureNoopKeepsProcess(t *testing.T) {
  225. pidFile := installFakeMtg(t)
  226. mgr := &Manager{procs: map[int]*managed{}, swept: true}
  227. inst := mtgInst(3, SecretEntry{Name: "alice", Secret: "ee01"}, SecretEntry{Name: "bob", Secret: "ee02"})
  228. if err := mgr.Ensure(inst); err != nil {
  229. t.Fatalf("initial ensure: %v", err)
  230. }
  231. waitSpawnCount(t, pidFile, 1)
  232. if err := mgr.Ensure(inst); err != nil {
  233. t.Fatalf("repeat ensure: %v", err)
  234. }
  235. reordered := mtgInst(3, SecretEntry{Name: "bob", Secret: "ee02"}, SecretEntry{Name: "alice", Secret: "ee01"})
  236. if err := mgr.Ensure(reordered); err != nil {
  237. t.Fatalf("reordered ensure: %v", err)
  238. }
  239. time.Sleep(300 * time.Millisecond)
  240. if got := spawnCount(t, pidFile); got != 1 {
  241. t.Fatalf("an unchanged instance must keep the one process, got %d spawns", got)
  242. }
  243. mgr.StopAll()
  244. }