canal_test.go 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  1. package service
  2. import (
  3. "context"
  4. "net/url"
  5. "testing"
  6. "go-common/app/admin/main/apm/model/canal"
  7. "github.com/BurntSushi/toml"
  8. . "github.com/smartystreets/goconvey/convey"
  9. )
  10. var (
  11. cookie = "username=fengshanshan; _AJSESSIONID=ee7c557c29e9b3f405b21c49a9aaa72a; sven-apm=9fc2bd4165a90a21df5803b4abc5cf7a81b7564e5119a8a547fd763bda757ceb"
  12. _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"]} ],"infoc":{"taskID":"000846","proto":"tcp","addr":"172.19.100.20:5401","reporterAddr":"172.19.40.195:6200"}},{ "schema":"456","table":[ {"name":"abc" ,"primarykey":["order_id","new_id"],"omitfield":["new","old"]} , {"name":"def" } ,{"name":"sfg"} ],"databus": { "group": "LiveTime-LiveLive-P","addr": "172.16.33.158:6205"}}]`
  13. )
  14. func TestService_GetAllErrors(t *testing.T) {
  15. Convey("test getAll errors", t, func() {
  16. //svr = New(conf.Conf)
  17. errS, err := svr.GetAllErrors(context.Background())
  18. So(err, ShouldBeNil)
  19. t.Log("getAllErrors=", errS)
  20. })
  21. }
  22. func TestGetCanalInstance(t *testing.T) {
  23. Convey("TestGetCanalInstance", t, func() {
  24. host, err := svr.getCanalInstance(context.Background())
  25. So(err, ShouldBeNil)
  26. t.Log("canalinstance=", host)
  27. })
  28. }
  29. func TestCheckMaster(t *testing.T) {
  30. Convey("TestCheckMaster", t, func() {
  31. req := &canal.ConfigReq{
  32. Addr: "172.16.33.205:3310",
  33. User: "gosync",
  34. Password: "xmJ4KlRuXzc9UfNerOGHP0LpaS26VqoM",
  35. }
  36. err := svr.CheckMaster(context.Background(), req)
  37. So(err, ShouldNotBeNil)
  38. t.Log("err=", err)
  39. })
  40. }
  41. func TestScanByAddrFromConfig(t *testing.T) {
  42. Convey("test ScanByAddrFromConfig", t, func() {
  43. //svr = New(conf.Conf)
  44. res, err := svr.ScanByAddrFromConfig(context.Background(), "172.16.33.205:3308", cookie)
  45. var document canal.Document
  46. data, _ := toml.Decode(res.Comment, &document)
  47. t.Log("toml=", data)
  48. So(res, ShouldNotBeNil)
  49. So(err, ShouldBeNil)
  50. })
  51. }
  52. func TestScanInfo(t *testing.T) {
  53. Convey("TestScanInfo", t, func() {
  54. v := &canal.ScanReq{
  55. Addr: "172.16.33.223:3308",
  56. }
  57. res, err := svr.GetScanInfo(context.Background(), v, "fss", cookie)
  58. t.Logf("toml=%+v", res.Document)
  59. So(res, ShouldNotBeNil)
  60. So(err, ShouldBeNil)
  61. })
  62. }
  63. func TestGetBuildID(t *testing.T) {
  64. Convey("test getBuildID", t, func() {
  65. res, err := svr.getBuildID(context.Background(), cookie)
  66. t.Log("buidID=", res)
  67. So(res, ShouldNotBeNil)
  68. So(err, ShouldBeNil)
  69. })
  70. }
  71. func TestGetConfigID(t *testing.T) {
  72. Convey("test getConfigID", t, func() {
  73. res, err := svr.getConfigID(context.Background(), "13", cookie)
  74. t.Log("ConfigID=", res)
  75. So(res, ShouldNotBeNil)
  76. So(err, ShouldBeNil)
  77. })
  78. }
  79. func TestGetConfigValue(t *testing.T) {
  80. Convey("test getConfigValue", t, func() {
  81. res, err := svr.getConfigValue(context.Background(), "112", cookie)
  82. t.Log("ConfigValue=", res)
  83. So(res, ShouldNotBeNil)
  84. So(err, ShouldBeNil)
  85. })
  86. }
  87. func TestConfigByName(t *testing.T) {
  88. Convey("test configsByName", t, func() {
  89. params := "172.16.33.205:3308"
  90. res, err := svr.getConfigsByName(context.Background(), params, cookie)
  91. t.Logf("configsByName:%+v,%+v", res, err)
  92. So(res, ShouldNotBeEmpty)
  93. })
  94. }
  95. func TestProcessCanalInfo(t *testing.T) {
  96. Convey("TestProcessCanalInfo", t, func() {
  97. v := &canal.ConfigReq{
  98. Addr: "127.0.0.1:8001",
  99. User: "admin",
  100. Password: "admin",
  101. // Project: "main.web-svr",
  102. // Leader: "fss",
  103. Databases: _jsonstring,
  104. Mark: "sfs",
  105. }
  106. _ = svr.ProcessCanalInfo(context.Background(), v, "fengshanshan")
  107. })
  108. }
  109. func TestJointConfigInfo(t *testing.T) {
  110. Convey("TestJointConfigInfo", t, func() {
  111. v := &canal.ConfigReq{
  112. Addr: "172.16.33.999:3308",
  113. User: "admin",
  114. Password: "admin",
  115. MonitorPeriod: "2h",
  116. Project: "main.web-svr",
  117. Leader: "fss",
  118. Databases: _jsonstring,
  119. Mark: "sfs",
  120. }
  121. comment, _ := svr.jointConfigInfo(context.Background(), v, cookie)
  122. t.Logf("comment:%s", comment)
  123. })
  124. }
  125. func TestCreateConfig(t *testing.T) {
  126. Convey("test createConfig", t, func() {
  127. params := url.Values{}
  128. params.Set("comment", "23232ew")
  129. params.Set("name", "demo1214s2.toml")
  130. params.Set("state", "2")
  131. params.Set("mark", "demo")
  132. params.Set("from", "0")
  133. params.Set("user", "sada")
  134. res, err := svr.createConfig(context.Background(), params, cookie)
  135. if err != nil {
  136. So(err, ShouldContain, "643")
  137. }
  138. So(res, ShouldBeNil)
  139. t.Log("createConfig=", res)
  140. })
  141. }
  142. func TestUpdateConfig(t *testing.T) {
  143. Convey("test updateConfig", t, func() {
  144. params := url.Values{}
  145. params.Set("config_id", "364")
  146. params.Set("mtime", "1528975519")
  147. params.Set("state", "2")
  148. params.Set("mark", "update")
  149. params.Set("comment", "dsdfasdsads")
  150. res, err := svr.updateConfig(context.Background(), params, cookie)
  151. if err != nil {
  152. So(err, ShouldContain, "643")
  153. }
  154. So(res, ShouldBeNil)
  155. t.Log("updateConfig=", res)
  156. })
  157. }
  158. func TestGetGroupInfo(t *testing.T) {
  159. Convey("test getGroupInfo", t, func() {
  160. res, _, err := svr.getGroupInfo("test-group")
  161. t.Log("GroupInfo=", res)
  162. So(err, ShouldBeNil)
  163. })
  164. }
  165. func TestGetAppInfo(t *testing.T) {
  166. Convey("test getAppInfo", t, func() {
  167. res, err := svr.getAppInfo("test-group")
  168. t.Log("AppInfo=", res)
  169. So(err, ShouldBeNil)
  170. })
  171. }
  172. func TestGetAction(t *testing.T) {
  173. Convey("test getAction", t, func() {
  174. res := svr.getAction("LiveTimeS-LiveLivePÍ")
  175. t.Log("Action=", res)
  176. })
  177. }
  178. func TestGetTableInfo(t *testing.T) {
  179. Convey("test getTableInfo", t, func() {
  180. table := "172.16.33.12,172.16.33.48"
  181. res, err := svr.TableInfo(table)
  182. t.Log("table=", res)
  183. So(err, ShouldBeNil)
  184. })
  185. }
  186. func TestGetDatabusInfo(t *testing.T) {
  187. Convey("test getDatabusInfo", t, func() {
  188. tab1 := &canal.Table{
  189. Name: "abc",
  190. Primarykey: []string{"new", "old"},
  191. Omitfield: []string{"new", "old"},
  192. }
  193. tables := []*canal.Table{tab1}
  194. res, err := svr.databusInfo("LiveTime-LiveLive-P", "172.16.33.22", "relation", tables)
  195. t.Logf("res:%+v", res)
  196. So(err, ShouldBeNil)
  197. })
  198. }
  199. func TestUpdateProcessTag(t *testing.T) {
  200. Convey("test UpdateProcessTag", t, func() {
  201. err := svr.UpdateProcessTag(context.Background(), 355, cookie)
  202. So(err, ShouldBeNil)
  203. })
  204. }
  205. func TestGetServerID(t *testing.T) {
  206. Convey("test TestGetServerID", t, func() {
  207. sid, err := svr.getServerID("172.16.448.789:9000")
  208. t.Logf("sid:%v\n", sid)
  209. So(sid, ShouldNotBeNil)
  210. So(err, ShouldBeNil)
  211. })
  212. }
  213. func TestServiceSendWechatMessage(t *testing.T) {
  214. Convey("SendWechatMessage", t, func() {
  215. var (
  216. c = context.Background()
  217. addr = "127.0.0.1:8000"
  218. aType = canal.TypeMap[canal.TypeReview]
  219. result = canal.TypeMap[canal.ReviewSuccess]
  220. sender = "fengshanshan"
  221. receiver = []string{"fengshanshan"}
  222. note = "test"
  223. )
  224. err := svr.SendWechatMessage(c, addr, aType, result, sender, note, receiver)
  225. So(err, ShouldBeNil)
  226. })
  227. }