apply_test.go 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. package http
  2. import (
  3. "context"
  4. "flag"
  5. "fmt"
  6. "net/url"
  7. "path/filepath"
  8. "testing"
  9. "go-common/app/admin/main/apm/conf"
  10. "go-common/app/admin/main/apm/service"
  11. "go-common/library/log"
  12. xhttp "go-common/library/net/http/blademaster"
  13. . "github.com/smartystreets/goconvey/convey"
  14. )
  15. var (
  16. _domain = "http://127.0.0.1:8000"
  17. _contentType = "application/x-www-form-urlencoded"
  18. _cookie = "username=fengshanshan; _AJSESSIONID=cf400491a236da90f27fb1b9bb9c4e2d; sven-apm=994ae2b6d290e584488443f9cc4733fbee7a88a4cd376135b1295f4cf81231de"
  19. _realIP = "172.16.33.134"
  20. _configuri = "%s/x/admin/apm/canal/apply/config"
  21. _jsonstring = `[{ "schema":"123","table":[ {"name":"abc","primarykey":["order_id","new_id"],"omitfield":["new","old"]} , {"name":"def","primarykey":["order_id","new_id"],"omitfield":["new","old"] } ,{"name":"sfg","primarykey":["order_id","new_id"],"omitfield":["new","old"]} ],"databus": { "group": "LiveTime-LiveLive-P","addr": "172.16.33.158:6205"}},{ "schema":"456","table":[ {"name":"abc" ,"primarykey":["order_id","new_id"],"omitfield":["new","old"]} , {"name":"def" } ,{"name":"sfg"} ], "databus": {"group": "AccAnswer-MainManager-S","addr": "172.16.33.158:6205"}}]`
  22. )
  23. func init() {
  24. dir, _ := filepath.Abs("../cmd/apm-admin-test.toml")
  25. flag.Parse()
  26. flag.Set("conf", dir)
  27. conf.Init()
  28. log.Init(conf.Conf.Log)
  29. apmSvc = service.New(conf.Conf)
  30. Init(conf.Conf, apmSvc)
  31. }
  32. type Response struct {
  33. Code int `json:"code"`
  34. Message string `json:"message"`
  35. Data string `json:"data"`
  36. }
  37. func requests(method, uri, realIP string, params url.Values, res interface{}) (err error) {
  38. client := xhttp.NewClient(conf.Conf.HTTPClient)
  39. req, err := client.NewRequest(method, uri, realIP, params)
  40. if err != nil {
  41. return
  42. }
  43. req.Header.Set("X-BACKEND-BILI-REAL-IP", _realIP)
  44. req.Header.Set("Content-Type", _contentType)
  45. req.Header.Set("Cookie", _cookie)
  46. if err = client.Do(context.TODO(), req, &res); err != nil {
  47. return
  48. }
  49. return
  50. }
  51. func TestApplyDetailToConfig(t *testing.T) {
  52. Convey("TestApply register nonconfig and no canal_apply", t, func() {
  53. params := url.Values{}
  54. params.Set("addr", "10.20.30.34:8902")
  55. params.Set("databases", _jsonstring)
  56. params.Set("mark", "demo")
  57. params.Set("user", "admin")
  58. params.Set("password", "admin")
  59. params.Set("project", "main.web-svr")
  60. params.Set("leader", "fss")
  61. res := Response{}
  62. _ = requests("POST", fmt.Sprintf(_configuri, _domain), "", params, &res)
  63. So(res.Code, ShouldEqual, 70015)
  64. })
  65. Convey("TestApplyAddrIllegal", t, func() {
  66. params := url.Values{}
  67. params.Set("addr", "172.16.33.2553308")
  68. params.Set("databases", _jsonstring)
  69. params.Set("mark", "demo")
  70. params.Set("user", "admin")
  71. params.Set("passwd", "admin")
  72. params.Set("project", "main.web-svr")
  73. params.Set("leader", "fss")
  74. res := Response{}
  75. _ = requests("POST", fmt.Sprintf(_configuri, _domain), "", params, &res)
  76. So(res.Code, ShouldEqual, 70002)
  77. //So(res.Message, ShouldContainSubstring, "addr参数不合法")
  78. })
  79. Convey("TestApplyExist", t, func() {
  80. params := url.Values{}
  81. params.Set("addr", "10.20.30.34:8902")
  82. params.Set("databases", _jsonstring)
  83. params.Set("mark", "demo")
  84. params.Set("user", "admin")
  85. params.Set("passwd", "admin")
  86. params.Set("project", "main.web-svr")
  87. params.Set("leader", "fss")
  88. res := Response{}
  89. _ = requests("POST", fmt.Sprintf(_configuri, _domain), "", params, &res)
  90. So(res.Code, ShouldEqual, 0)
  91. //So(res.Message, ShouldContainSubstring, "己提交申请")
  92. })
  93. Convey("TestApplyAddRequestParamError", t, func() {
  94. params := url.Values{}
  95. params.Set("leader", "fss")
  96. params.Set("remark", "test")
  97. params.Set("project", "main.web-svr")
  98. res := Response{}
  99. _ = requests("POST", fmt.Sprintf(_configuri, _domain), "", params, &res)
  100. So(res.Code, ShouldEqual, -400)
  101. })
  102. }
  103. func TestApplyExist(t *testing.T) {
  104. Convey("TestApply register nonconfig and no canal_apply", t, func() {
  105. params := url.Values{}
  106. params.Set("addr", "10.20.30.34:8902")
  107. params.Set("databases", _jsonstring)
  108. params.Set("mark", "demo")
  109. params.Set("user", "a")
  110. params.Set("password", "a")
  111. params.Set("project", "main.web-svr")
  112. params.Set("leader", "fss")
  113. res := Response{}
  114. _ = requests("POST", fmt.Sprintf(_configuri, _domain), "", params, &res)
  115. t.Logf("%+v", res)
  116. So(res.Code, ShouldEqual, 70015)
  117. })
  118. }