server_test.go 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. package manager
  2. import (
  3. "encoding/json"
  4. "fmt"
  5. "io/ioutil"
  6. "math/rand"
  7. "net/http"
  8. "strings"
  9. "testing"
  10. "time"
  11. . "github.com/smartystreets/goconvey/convey"
  12. )
  13. type mockDBAdapter struct {
  14. workerStore map[string]worker
  15. statusStore map[string]mirrorStatus
  16. }
  17. func (b *mockDBAdapter) ListWorkers() ([]worker, error) {
  18. workers := make([]worker, len(b.workerStore))
  19. idx := 0
  20. for _, w := range b.workerStore {
  21. workers[idx] = w
  22. idx++
  23. }
  24. return workers, nil
  25. }
  26. func (b *mockDBAdapter) GetWorker(workerID string) (worker, error) {
  27. w, ok := b.workerStore[workerID]
  28. if !ok {
  29. return worker{}, fmt.Errorf("inexist workerId")
  30. }
  31. return w, nil
  32. }
  33. func (b *mockDBAdapter) CreateWorker(w worker) (worker, error) {
  34. _, ok := b.workerStore[w.id]
  35. if ok {
  36. return worker{}, fmt.Errorf("duplicate worker name")
  37. }
  38. b.workerStore[w.id] = w
  39. return w, nil
  40. }
  41. func (b *mockDBAdapter) GetMirrorStatus(workerID, mirrorID string) (mirrorStatus, error) {
  42. // TODO: need to check worker exist first
  43. id := workerID + "/" + mirrorID
  44. status, ok := b.statusStore[id]
  45. if !ok {
  46. return mirrorStatus{}, fmt.Errorf("no mirror %s exists in worker %s", mirrorID, workerID)
  47. }
  48. return status, nil
  49. }
  50. func (b *mockDBAdapter) UpdateMirrorStatus(workerID, mirrorID string, status mirrorStatus) (mirrorStatus, error) {
  51. id := workerID + "/" + mirrorID
  52. b.statusStore[id] = status
  53. return status, nil
  54. }
  55. func (b *mockDBAdapter) ListMirrorStatus(workerID string) ([]mirrorStatus, error) {
  56. var mirrorStatusList []mirrorStatus
  57. for k, v := range b.statusStore {
  58. if wID := strings.Split(k, "/")[1]; wID == workerID {
  59. mirrorStatusList = append(mirrorStatusList, v)
  60. }
  61. }
  62. return mirrorStatusList, nil
  63. }
  64. func (b *mockDBAdapter) ListAllMirrorStatus() ([]mirrorStatus, error) {
  65. var mirrorStatusList []mirrorStatus
  66. for _, v := range b.statusStore {
  67. mirrorStatusList = append(mirrorStatusList, v)
  68. }
  69. return mirrorStatusList, nil
  70. }
  71. func TestHTTPServer(t *testing.T) {
  72. Convey("HTTP server should work", t, func() {
  73. s := makeHTTPServer(false)
  74. So(s, ShouldNotBeNil)
  75. port := rand.Intn(10000) + 20000
  76. go func() {
  77. s.Run(fmt.Sprintf("127.0.0.1:%d", port))
  78. }()
  79. time.Sleep(50 * time.Microsecond)
  80. resp, err := http.Get(fmt.Sprintf("http://127.0.0.1:%d/ping", port))
  81. So(err, ShouldBeNil)
  82. So(resp.StatusCode, ShouldEqual, http.StatusOK)
  83. So(resp.Header.Get("Content-Type"), ShouldEqual, "application/json; charset=utf-8")
  84. defer resp.Body.Close()
  85. body, err := ioutil.ReadAll(resp.Body)
  86. So(err, ShouldBeNil)
  87. var p map[string]string
  88. err = json.Unmarshal(body, &p)
  89. So(err, ShouldBeNil)
  90. So(p["msg"], ShouldEqual, "pong")
  91. })
  92. }