server_test.go 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556
  1. package manager
  2. import (
  3. "encoding/json"
  4. "fmt"
  5. "io/ioutil"
  6. "math/rand"
  7. "net/http"
  8. "strings"
  9. "sync"
  10. "sync/atomic"
  11. "testing"
  12. "time"
  13. "github.com/gin-gonic/gin"
  14. . "github.com/smartystreets/goconvey/convey"
  15. . "github.com/tuna/tunasync/internal"
  16. )
  17. const (
  18. _magicBadWorkerID = "magic_bad_worker_id"
  19. )
  20. func TestHTTPServer(t *testing.T) {
  21. var listenPort = 5000
  22. Convey("HTTP server should work", t, func(ctx C) {
  23. listenPort++
  24. port := listenPort
  25. addr := "127.0.0.1"
  26. baseURL := fmt.Sprintf("http://%s:%d", addr, port)
  27. InitLogger(true, true, false)
  28. s := GetTUNASyncManager(&Config{Debug: true})
  29. s.cfg.Server.Addr = addr
  30. s.cfg.Server.Port = port
  31. So(s, ShouldNotBeNil)
  32. s.setDBAdapter(&mockDBAdapter{
  33. workerStore: map[string]WorkerStatus{
  34. _magicBadWorkerID: WorkerStatus{
  35. ID: _magicBadWorkerID,
  36. }},
  37. statusStore: make(map[string]MirrorStatus),
  38. })
  39. go s.Run()
  40. time.Sleep(50 * time.Millisecond)
  41. resp, err := http.Get(baseURL + "/ping")
  42. So(err, ShouldBeNil)
  43. So(resp.StatusCode, ShouldEqual, http.StatusOK)
  44. So(resp.Header.Get("Content-Type"), ShouldEqual, "application/json; charset=utf-8")
  45. defer resp.Body.Close()
  46. body, err := ioutil.ReadAll(resp.Body)
  47. So(err, ShouldBeNil)
  48. var p map[string]string
  49. err = json.Unmarshal(body, &p)
  50. So(err, ShouldBeNil)
  51. So(p[_infoKey], ShouldEqual, "pong")
  52. Convey("when database fail", func(ctx C) {
  53. resp, err := http.Get(fmt.Sprintf("%s/workers/%s/jobs", baseURL, _magicBadWorkerID))
  54. So(err, ShouldBeNil)
  55. So(resp.StatusCode, ShouldEqual, http.StatusInternalServerError)
  56. defer resp.Body.Close()
  57. var msg map[string]string
  58. err = json.NewDecoder(resp.Body).Decode(&msg)
  59. So(err, ShouldBeNil)
  60. So(msg[_errorKey], ShouldEqual, fmt.Sprintf("failed to list jobs of worker %s: %s", _magicBadWorkerID, "database fail"))
  61. })
  62. Convey("when register multiple workers", func(ctx C) {
  63. N := 10
  64. var cnt uint32
  65. for i := 0; i < N; i++ {
  66. go func(id int) {
  67. w := WorkerStatus{
  68. ID: fmt.Sprintf("worker%d", id),
  69. }
  70. resp, err := PostJSON(baseURL+"/workers", w, nil)
  71. ctx.So(err, ShouldBeNil)
  72. ctx.So(resp.StatusCode, ShouldEqual, http.StatusOK)
  73. atomic.AddUint32(&cnt, 1)
  74. }(i)
  75. }
  76. time.Sleep(2 * time.Second)
  77. So(cnt, ShouldEqual, N)
  78. Convey("list all workers", func(ctx C) {
  79. resp, err := http.Get(baseURL + "/workers")
  80. So(err, ShouldBeNil)
  81. defer resp.Body.Close()
  82. var actualResponseObj []WorkerStatus
  83. err = json.NewDecoder(resp.Body).Decode(&actualResponseObj)
  84. So(err, ShouldBeNil)
  85. So(len(actualResponseObj), ShouldEqual, N+1)
  86. })
  87. })
  88. Convey("when register a worker", func(ctx C) {
  89. w := WorkerStatus{
  90. ID: "test_worker1",
  91. }
  92. resp, err := PostJSON(baseURL+"/workers", w, nil)
  93. So(err, ShouldBeNil)
  94. So(resp.StatusCode, ShouldEqual, http.StatusOK)
  95. Convey("list all workers", func(ctx C) {
  96. resp, err := http.Get(baseURL + "/workers")
  97. So(err, ShouldBeNil)
  98. defer resp.Body.Close()
  99. var actualResponseObj []WorkerStatus
  100. err = json.NewDecoder(resp.Body).Decode(&actualResponseObj)
  101. So(err, ShouldBeNil)
  102. So(len(actualResponseObj), ShouldEqual, 2)
  103. })
  104. Convey("delete an existent worker", func(ctx C) {
  105. req, err := http.NewRequest("DELETE", fmt.Sprintf("%s/workers/%s", baseURL, w.ID), nil)
  106. So(err, ShouldBeNil)
  107. clt := &http.Client{}
  108. resp, err := clt.Do(req)
  109. So(err, ShouldBeNil)
  110. defer resp.Body.Close()
  111. res := map[string]string{}
  112. err = json.NewDecoder(resp.Body).Decode(&res)
  113. So(err, ShouldBeNil)
  114. So(res[_infoKey], ShouldEqual, "deleted")
  115. })
  116. Convey("delete non-existent worker", func(ctx C) {
  117. invalidWorker := "test_worker233"
  118. req, err := http.NewRequest("DELETE", fmt.Sprintf("%s/workers/%s", baseURL, invalidWorker), nil)
  119. So(err, ShouldBeNil)
  120. clt := &http.Client{}
  121. resp, err := clt.Do(req)
  122. So(err, ShouldBeNil)
  123. defer resp.Body.Close()
  124. res := map[string]string{}
  125. err = json.NewDecoder(resp.Body).Decode(&res)
  126. So(err, ShouldBeNil)
  127. So(res[_errorKey], ShouldEqual, "invalid workerID "+invalidWorker)
  128. })
  129. Convey("flush disabled jobs", func(ctx C) {
  130. req, err := http.NewRequest("DELETE", baseURL+"/jobs/disabled", nil)
  131. So(err, ShouldBeNil)
  132. clt := &http.Client{}
  133. resp, err := clt.Do(req)
  134. So(err, ShouldBeNil)
  135. defer resp.Body.Close()
  136. res := map[string]string{}
  137. err = json.NewDecoder(resp.Body).Decode(&res)
  138. So(err, ShouldBeNil)
  139. So(res[_infoKey], ShouldEqual, "flushed")
  140. })
  141. Convey("update mirror status of a existed worker", func(ctx C) {
  142. status := MirrorStatus{
  143. Name: "arch-sync1",
  144. Worker: "test_worker1",
  145. IsMaster: true,
  146. Status: Success,
  147. Upstream: "mirrors.tuna.tsinghua.edu.cn",
  148. Size: "unknown",
  149. }
  150. resp, err := PostJSON(fmt.Sprintf("%s/workers/%s/jobs/%s", baseURL, status.Worker, status.Name), status, nil)
  151. So(err, ShouldBeNil)
  152. defer resp.Body.Close()
  153. So(resp.StatusCode, ShouldEqual, http.StatusOK)
  154. Convey("list mirror status of an existed worker", func(ctx C) {
  155. var ms []MirrorStatus
  156. resp, err := GetJSON(baseURL+"/workers/test_worker1/jobs", &ms, nil)
  157. So(err, ShouldBeNil)
  158. So(resp.StatusCode, ShouldEqual, http.StatusOK)
  159. // err = json.NewDecoder(resp.Body).Decode(&mirrorStatusList)
  160. m := ms[0]
  161. So(m.Name, ShouldEqual, status.Name)
  162. So(m.Worker, ShouldEqual, status.Worker)
  163. So(m.Status, ShouldEqual, status.Status)
  164. So(m.Upstream, ShouldEqual, status.Upstream)
  165. So(m.Size, ShouldEqual, status.Size)
  166. So(m.IsMaster, ShouldEqual, status.IsMaster)
  167. So(time.Since(m.LastUpdate), ShouldBeLessThan, 1*time.Second)
  168. So(m.LastStarted.IsZero(), ShouldBeTrue) // hasn't been initialized yet
  169. So(time.Since(m.LastEnded), ShouldBeLessThan, 1*time.Second)
  170. })
  171. // start syncing
  172. status.Status = PreSyncing
  173. time.Sleep(1 * time.Second)
  174. resp, err = PostJSON(fmt.Sprintf("%s/workers/%s/jobs/%s", baseURL, status.Worker, status.Name), status, nil)
  175. So(err, ShouldBeNil)
  176. defer resp.Body.Close()
  177. So(resp.StatusCode, ShouldEqual, http.StatusOK)
  178. Convey("update mirror status to PreSync - starting sync", func(ctx C) {
  179. var ms []MirrorStatus
  180. resp, err := GetJSON(baseURL+"/workers/test_worker1/jobs", &ms, nil)
  181. So(err, ShouldBeNil)
  182. So(resp.StatusCode, ShouldEqual, http.StatusOK)
  183. // err = json.NewDecoder(resp.Body).Decode(&mirrorStatusList)
  184. m := ms[0]
  185. So(m.Name, ShouldEqual, status.Name)
  186. So(m.Worker, ShouldEqual, status.Worker)
  187. So(m.Status, ShouldEqual, status.Status)
  188. So(m.Upstream, ShouldEqual, status.Upstream)
  189. So(m.Size, ShouldEqual, status.Size)
  190. So(m.IsMaster, ShouldEqual, status.IsMaster)
  191. So(time.Since(m.LastUpdate), ShouldBeLessThan, 3*time.Second)
  192. So(time.Since(m.LastUpdate), ShouldBeGreaterThan, 1*time.Second)
  193. So(time.Since(m.LastStarted), ShouldBeLessThan, 2*time.Second)
  194. So(time.Since(m.LastEnded), ShouldBeLessThan, 3*time.Second)
  195. So(time.Since(m.LastEnded), ShouldBeGreaterThan, 1*time.Second)
  196. })
  197. Convey("list all job status of all workers", func(ctx C) {
  198. var ms []WebMirrorStatus
  199. resp, err := GetJSON(baseURL+"/jobs", &ms, nil)
  200. So(err, ShouldBeNil)
  201. So(resp.StatusCode, ShouldEqual, http.StatusOK)
  202. m := ms[0]
  203. So(m.Name, ShouldEqual, status.Name)
  204. So(m.Status, ShouldEqual, status.Status)
  205. So(m.Upstream, ShouldEqual, status.Upstream)
  206. So(m.Size, ShouldEqual, status.Size)
  207. So(m.IsMaster, ShouldEqual, status.IsMaster)
  208. So(time.Since(m.LastUpdate.Time), ShouldBeLessThan, 3*time.Second)
  209. So(time.Since(m.LastStarted.Time), ShouldBeLessThan, 2*time.Second)
  210. So(time.Since(m.LastEnded.Time), ShouldBeLessThan, 3*time.Second)
  211. })
  212. Convey("Update size of a valid mirror", func(ctx C) {
  213. msg := struct {
  214. Name string `json:"name"`
  215. Size string `json:"size"`
  216. }{status.Name, "5GB"}
  217. url := fmt.Sprintf("%s/workers/%s/jobs/%s/size", baseURL, status.Worker, status.Name)
  218. resp, err := PostJSON(url, msg, nil)
  219. So(err, ShouldBeNil)
  220. So(resp.StatusCode, ShouldEqual, http.StatusOK)
  221. Convey("Get new size of a mirror", func(ctx C) {
  222. var ms []MirrorStatus
  223. resp, err := GetJSON(baseURL+"/workers/test_worker1/jobs", &ms, nil)
  224. So(err, ShouldBeNil)
  225. So(resp.StatusCode, ShouldEqual, http.StatusOK)
  226. // err = json.NewDecoder(resp.Body).Decode(&mirrorStatusList)
  227. m := ms[0]
  228. So(m.Name, ShouldEqual, status.Name)
  229. So(m.Worker, ShouldEqual, status.Worker)
  230. So(m.Status, ShouldEqual, status.Status)
  231. So(m.Upstream, ShouldEqual, status.Upstream)
  232. So(m.Size, ShouldEqual, "5GB")
  233. So(m.IsMaster, ShouldEqual, status.IsMaster)
  234. So(time.Since(m.LastUpdate), ShouldBeLessThan, 3*time.Second)
  235. So(time.Since(m.LastStarted), ShouldBeLessThan, 2*time.Second)
  236. So(time.Since(m.LastEnded), ShouldBeLessThan, 3*time.Second)
  237. })
  238. })
  239. Convey("Update schedule of valid mirrors", func(ctx C) {
  240. msg := MirrorSchedules{
  241. Schedules: []MirrorSchedule{
  242. MirrorSchedule{MirrorName: "arch-sync1", NextSchedule: time.Now().Add(time.Minute * 10)},
  243. MirrorSchedule{MirrorName: "arch-sync2", NextSchedule: time.Now().Add(time.Minute * 7)},
  244. },
  245. }
  246. url := fmt.Sprintf("%s/workers/%s/schedules", baseURL, status.Worker)
  247. resp, err := PostJSON(url, msg, nil)
  248. So(err, ShouldBeNil)
  249. So(resp.StatusCode, ShouldEqual, http.StatusOK)
  250. })
  251. Convey("Update size of an invalid mirror", func(ctx C) {
  252. msg := struct {
  253. Name string `json:"name"`
  254. Size string `json:"size"`
  255. }{"Invalid mirror", "5GB"}
  256. url := fmt.Sprintf("%s/workers/%s/jobs/%s/size", baseURL, status.Worker, status.Name)
  257. resp, err := PostJSON(url, msg, nil)
  258. So(err, ShouldBeNil)
  259. So(resp.StatusCode, ShouldEqual, http.StatusInternalServerError)
  260. })
  261. // what if status changed to failed
  262. status.Status = Failed
  263. time.Sleep(3 * time.Second)
  264. resp, err = PostJSON(fmt.Sprintf("%s/workers/%s/jobs/%s", baseURL, status.Worker, status.Name), status, nil)
  265. So(err, ShouldBeNil)
  266. defer resp.Body.Close()
  267. So(resp.StatusCode, ShouldEqual, http.StatusOK)
  268. Convey("What if syncing job failed", func(ctx C) {
  269. var ms []MirrorStatus
  270. resp, err := GetJSON(baseURL+"/workers/test_worker1/jobs", &ms, nil)
  271. So(err, ShouldBeNil)
  272. So(resp.StatusCode, ShouldEqual, http.StatusOK)
  273. // err = json.NewDecoder(resp.Body).Decode(&mirrorStatusList)
  274. m := ms[0]
  275. So(m.Name, ShouldEqual, status.Name)
  276. So(m.Worker, ShouldEqual, status.Worker)
  277. So(m.Status, ShouldEqual, status.Status)
  278. So(m.Upstream, ShouldEqual, status.Upstream)
  279. So(m.Size, ShouldEqual, status.Size)
  280. So(m.IsMaster, ShouldEqual, status.IsMaster)
  281. So(time.Since(m.LastUpdate), ShouldBeGreaterThan, 3*time.Second)
  282. So(time.Since(m.LastStarted), ShouldBeGreaterThan, 3*time.Second)
  283. So(time.Since(m.LastEnded), ShouldBeLessThan, 1*time.Second)
  284. })
  285. })
  286. Convey("update mirror status of an inexisted worker", func(ctx C) {
  287. invalidWorker := "test_worker2"
  288. status := MirrorStatus{
  289. Name: "arch-sync2",
  290. Worker: invalidWorker,
  291. IsMaster: true,
  292. Status: Success,
  293. LastUpdate: time.Now(),
  294. LastStarted: time.Now(),
  295. LastEnded: time.Now(),
  296. Upstream: "mirrors.tuna.tsinghua.edu.cn",
  297. Size: "4GB",
  298. }
  299. resp, err := PostJSON(fmt.Sprintf("%s/workers/%s/jobs/%s",
  300. baseURL, status.Worker, status.Name), status, nil)
  301. So(err, ShouldBeNil)
  302. So(resp.StatusCode, ShouldEqual, http.StatusBadRequest)
  303. defer resp.Body.Close()
  304. var msg map[string]string
  305. err = json.NewDecoder(resp.Body).Decode(&msg)
  306. So(err, ShouldBeNil)
  307. So(msg[_errorKey], ShouldEqual, "invalid workerID "+invalidWorker)
  308. })
  309. Convey("update schedule of an non-existent worker", func(ctx C) {
  310. invalidWorker := "test_worker2"
  311. sch := MirrorSchedules{
  312. Schedules: []MirrorSchedule{
  313. MirrorSchedule{MirrorName: "arch-sync1", NextSchedule: time.Now().Add(time.Minute * 10)},
  314. MirrorSchedule{MirrorName: "arch-sync2", NextSchedule: time.Now().Add(time.Minute * 7)},
  315. },
  316. }
  317. resp, err := PostJSON(fmt.Sprintf("%s/workers/%s/schedules",
  318. baseURL, invalidWorker), sch, nil)
  319. So(err, ShouldBeNil)
  320. So(resp.StatusCode, ShouldEqual, http.StatusBadRequest)
  321. defer resp.Body.Close()
  322. var msg map[string]string
  323. err = json.NewDecoder(resp.Body).Decode(&msg)
  324. So(err, ShouldBeNil)
  325. So(msg[_errorKey], ShouldEqual, "invalid workerID "+invalidWorker)
  326. })
  327. Convey("handle client command", func(ctx C) {
  328. cmdChan := make(chan WorkerCmd, 1)
  329. workerServer := makeMockWorkerServer(cmdChan)
  330. workerPort := rand.Intn(10000) + 30000
  331. bindAddress := fmt.Sprintf("127.0.0.1:%d", workerPort)
  332. workerBaseURL := fmt.Sprintf("http://%s", bindAddress)
  333. w := WorkerStatus{
  334. ID: "test_worker_cmd",
  335. URL: workerBaseURL + "/cmd",
  336. }
  337. resp, err := PostJSON(baseURL+"/workers", w, nil)
  338. So(err, ShouldBeNil)
  339. So(resp.StatusCode, ShouldEqual, http.StatusOK)
  340. go func() {
  341. // run the mock worker server
  342. workerServer.Run(bindAddress)
  343. }()
  344. time.Sleep(50 * time.Millisecond)
  345. // verify the worker mock server is running
  346. workerResp, err := http.Get(workerBaseURL + "/ping")
  347. So(err, ShouldBeNil)
  348. defer workerResp.Body.Close()
  349. So(workerResp.StatusCode, ShouldEqual, http.StatusOK)
  350. Convey("when client send wrong cmd", func(ctx C) {
  351. clientCmd := ClientCmd{
  352. Cmd: CmdStart,
  353. MirrorID: "ubuntu-sync",
  354. WorkerID: "not_exist_worker",
  355. }
  356. resp, err := PostJSON(baseURL+"/cmd", clientCmd, nil)
  357. So(err, ShouldBeNil)
  358. defer resp.Body.Close()
  359. So(resp.StatusCode, ShouldEqual, http.StatusBadRequest)
  360. })
  361. Convey("when client send correct cmd", func(ctx C) {
  362. clientCmd := ClientCmd{
  363. Cmd: CmdStart,
  364. MirrorID: "ubuntu-sync",
  365. WorkerID: w.ID,
  366. }
  367. resp, err := PostJSON(baseURL+"/cmd", clientCmd, nil)
  368. So(err, ShouldBeNil)
  369. defer resp.Body.Close()
  370. So(resp.StatusCode, ShouldEqual, http.StatusOK)
  371. time.Sleep(50 * time.Microsecond)
  372. select {
  373. case cmd := <-cmdChan:
  374. ctx.So(cmd.Cmd, ShouldEqual, clientCmd.Cmd)
  375. ctx.So(cmd.MirrorID, ShouldEqual, clientCmd.MirrorID)
  376. default:
  377. ctx.So(0, ShouldEqual, 1)
  378. }
  379. })
  380. })
  381. })
  382. })
  383. }
  384. type mockDBAdapter struct {
  385. workerStore map[string]WorkerStatus
  386. statusStore map[string]MirrorStatus
  387. workerLock sync.RWMutex
  388. statusLock sync.RWMutex
  389. }
  390. func (b *mockDBAdapter) Init() error {
  391. return nil
  392. }
  393. func (b *mockDBAdapter) ListWorkers() ([]WorkerStatus, error) {
  394. b.workerLock.RLock()
  395. workers := make([]WorkerStatus, len(b.workerStore))
  396. idx := 0
  397. for _, w := range b.workerStore {
  398. workers[idx] = w
  399. idx++
  400. }
  401. b.workerLock.RUnlock()
  402. return workers, nil
  403. }
  404. func (b *mockDBAdapter) GetWorker(workerID string) (WorkerStatus, error) {
  405. b.workerLock.RLock()
  406. defer b.workerLock.RUnlock()
  407. w, ok := b.workerStore[workerID]
  408. if !ok {
  409. return WorkerStatus{}, fmt.Errorf("invalid workerId")
  410. }
  411. return w, nil
  412. }
  413. func (b *mockDBAdapter) DeleteWorker(workerID string) error {
  414. b.workerLock.Lock()
  415. delete(b.workerStore, workerID)
  416. b.workerLock.Unlock()
  417. return nil
  418. }
  419. func (b *mockDBAdapter) CreateWorker(w WorkerStatus) (WorkerStatus, error) {
  420. // _, ok := b.workerStore[w.ID]
  421. // if ok {
  422. // return workerStatus{}, fmt.Errorf("duplicate worker name")
  423. // }
  424. b.workerLock.Lock()
  425. b.workerStore[w.ID] = w
  426. b.workerLock.Unlock()
  427. return w, nil
  428. }
  429. func (b *mockDBAdapter) RefreshWorker(workerID string) (w WorkerStatus, err error) {
  430. w, err = b.GetWorker(workerID)
  431. if err == nil {
  432. w.LastOnline = time.Now()
  433. w, err = b.CreateWorker(w)
  434. }
  435. return w, err
  436. }
  437. func (b *mockDBAdapter) GetMirrorStatus(workerID, mirrorID string) (MirrorStatus, error) {
  438. id := mirrorID + "/" + workerID
  439. b.statusLock.RLock()
  440. status, ok := b.statusStore[id]
  441. b.statusLock.RUnlock()
  442. if !ok {
  443. return MirrorStatus{}, fmt.Errorf("no mirror %s exists in worker %s", mirrorID, workerID)
  444. }
  445. return status, nil
  446. }
  447. func (b *mockDBAdapter) UpdateMirrorStatus(workerID, mirrorID string, status MirrorStatus) (MirrorStatus, error) {
  448. // if _, ok := b.workerStore[workerID]; !ok {
  449. // // unregistered worker
  450. // return MirrorStatus{}, fmt.Errorf("invalid workerID %s", workerID)
  451. // }
  452. id := mirrorID + "/" + workerID
  453. b.statusLock.Lock()
  454. b.statusStore[id] = status
  455. b.statusLock.Unlock()
  456. return status, nil
  457. }
  458. func (b *mockDBAdapter) ListMirrorStatus(workerID string) ([]MirrorStatus, error) {
  459. var mirrorStatusList []MirrorStatus
  460. // simulating a database fail
  461. if workerID == _magicBadWorkerID {
  462. return []MirrorStatus{}, fmt.Errorf("database fail")
  463. }
  464. b.statusLock.RLock()
  465. for k, v := range b.statusStore {
  466. if wID := strings.Split(k, "/")[1]; wID == workerID {
  467. mirrorStatusList = append(mirrorStatusList, v)
  468. }
  469. }
  470. b.statusLock.RUnlock()
  471. return mirrorStatusList, nil
  472. }
  473. func (b *mockDBAdapter) ListAllMirrorStatus() ([]MirrorStatus, error) {
  474. var mirrorStatusList []MirrorStatus
  475. b.statusLock.RLock()
  476. for _, v := range b.statusStore {
  477. mirrorStatusList = append(mirrorStatusList, v)
  478. }
  479. b.statusLock.RUnlock()
  480. return mirrorStatusList, nil
  481. }
  482. func (b *mockDBAdapter) Close() error {
  483. return nil
  484. }
  485. func (b *mockDBAdapter) FlushDisabledJobs() error {
  486. return nil
  487. }
  488. func makeMockWorkerServer(cmdChan chan WorkerCmd) *gin.Engine {
  489. r := gin.Default()
  490. r.GET("/ping", func(c *gin.Context) {
  491. c.JSON(http.StatusOK, gin.H{_infoKey: "pong"})
  492. })
  493. r.POST("/cmd", func(c *gin.Context) {
  494. var cmd WorkerCmd
  495. c.BindJSON(&cmd)
  496. cmdChan <- cmd
  497. })
  498. return r
  499. }