dao_test.go 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  1. package dao
  2. import (
  3. "context"
  4. "flag"
  5. "fmt"
  6. "log"
  7. "os"
  8. "strconv"
  9. "testing"
  10. "time"
  11. . "github.com/smartystreets/goconvey/convey"
  12. "go-common/app/service/main/dapper-query/conf"
  13. "golang.org/x/sys/unix"
  14. )
  15. var cfg *conf.Config
  16. var flagMap = map[string]string{
  17. "app_id": "main.common-arch.dapper-query",
  18. "conf_appid": "main.common-arch.dapper-query",
  19. "conf_token": "ed3241c850735df94d24d7b49f69ddd7",
  20. "tree_id": "60617",
  21. "conf_version": "docker-1",
  22. "deploy_env": "uat",
  23. "conf_env": "uat",
  24. "conf_host": "config.bilibili.co",
  25. "conf_path": os.TempDir(),
  26. "region": "sh",
  27. "zone": "sh001",
  28. }
  29. // only for ut runner
  30. func hackHosts() {
  31. hostsPath := "/etc/hosts"
  32. if unix.Access(hostsPath, unix.W_OK) != nil {
  33. return
  34. }
  35. fp, err := os.OpenFile(hostsPath, os.O_WRONLY, 0644)
  36. if err != nil {
  37. log.Printf("open hosts file error: %s", err)
  38. }
  39. defer fp.Close()
  40. fmt.Fprintf(fp, "\n")
  41. fmt.Fprintln(fp, "172.22.33.146 nvm-test-dapper-influxdb-01")
  42. }
  43. func TestMain(m *testing.M) {
  44. hackHosts()
  45. for key, val := range flagMap {
  46. flag.Set(key, val)
  47. }
  48. flag.Parse()
  49. if err := conf.Init(); err != nil {
  50. log.Printf("init config from remote error: %s", err)
  51. }
  52. if hbaseAddrs := os.Getenv("TEST_HBASE_ADDRS"); hbaseAddrs != "" {
  53. cfg = new(conf.Config)
  54. cfg.HBase = &conf.HBaseConfig{Addrs: hbaseAddrs, Namespace: "ugc"}
  55. if influxdbAddr := os.Getenv("TEST_INFLUXDB_ADDR"); influxdbAddr != "" {
  56. cfg.InfluxDB = &conf.InfluxDBConfig{Addr: influxdbAddr, Database: "dapper_uat"}
  57. }
  58. }
  59. if cfg == nil {
  60. cfg = conf.Conf
  61. if cfg.InfluxDB != nil {
  62. cfg.InfluxDB.Database = "dapper_uat"
  63. }
  64. }
  65. os.Exit(m.Run())
  66. }
  67. func TestDao(t *testing.T) {
  68. if cfg == nil {
  69. t.Skipf("no config provide skipped")
  70. }
  71. daoImpl, err := New(cfg)
  72. if err != nil {
  73. t.Fatalf("new dao error: %s", err)
  74. }
  75. ctx := context.Background()
  76. serviceName := "main.community.tag"
  77. operationName := "/x/internal/tag/archive/tags"
  78. Convey("query serviceNames", t, func() {
  79. serviceNames, err := daoImpl.ServiceNames(ctx)
  80. So(err, ShouldBeNil)
  81. So(serviceNames, ShouldNotBeEmpty)
  82. t.Logf("serviceNames: %v", serviceNames)
  83. Convey("query operationNames", func() {
  84. // FIXME: make mock data frist
  85. operationNames, err := daoImpl.OperationNames(ctx, serviceName)
  86. So(err, ShouldBeNil)
  87. So(operationNames, ShouldNotBeEmpty)
  88. t.Logf("operationNames for %s :%v", serviceName, operationNames)
  89. })
  90. })
  91. Convey("test QuerySpanListTime Asc", t, func() {
  92. // FIXME: make mock data frist
  93. spanListRefs, err := daoImpl.QuerySpanList(ctx, serviceName, operationName, &Selector{
  94. Start: time.Now().Unix() - 3600,
  95. End: time.Now().Unix(),
  96. Limit: 10,
  97. Offset: 10,
  98. }, TimeAsc)
  99. So(err, ShouldBeNil)
  100. So(spanListRefs, ShouldNotBeEmpty)
  101. t.Logf("spanListRefs: %v", spanListRefs)
  102. })
  103. Convey("test QuerySpanListTime Desc", t, func() {
  104. // FIXME: make mock data frist
  105. spanListRefs, err := daoImpl.QuerySpanList(ctx, serviceName, operationName, &Selector{
  106. Start: time.Now().Unix() - 3600*12,
  107. End: time.Now().Unix(),
  108. Limit: 10,
  109. Offset: 10,
  110. }, TimeDesc)
  111. So(err, ShouldBeNil)
  112. So(spanListRefs, ShouldNotBeEmpty)
  113. t.Logf("spanListRefs: %v", spanListRefs)
  114. Convey("test get trace", func() {
  115. spanListRef := spanListRefs[0]
  116. spans, err := daoImpl.Trace(ctx, spanListRef.TraceID)
  117. So(err, ShouldBeNil)
  118. So(spans, ShouldNotBeEmpty)
  119. t.Logf("spans %v", spans)
  120. })
  121. })
  122. Convey("test QuerySpanListDuration Desc", t, func() {
  123. // FIXME: make mock data frist
  124. spanListRefs, err := daoImpl.QuerySpanList(ctx, serviceName, operationName, &Selector{
  125. Start: time.Now().Unix() - 3600*12,
  126. End: time.Now().Unix(),
  127. Limit: 10,
  128. Offset: 10,
  129. }, DurationDesc)
  130. So(err, ShouldBeNil)
  131. So(spanListRefs, ShouldNotBeEmpty)
  132. t.Logf("spanListRefs: %v", spanListRefs)
  133. Convey("test get trace", func() {
  134. spanListRef := spanListRefs[len(spanListRefs)-1]
  135. spans, err := daoImpl.Trace(ctx, spanListRef.TraceID)
  136. So(err, ShouldBeNil)
  137. So(spans, ShouldNotBeEmpty)
  138. t.Logf("spans %v", spans)
  139. })
  140. })
  141. Convey("test QuerySpanListDuration Asc", t, func() {
  142. // FIXME: make mock data frist
  143. spanListRefs, err := daoImpl.QuerySpanList(ctx, serviceName, operationName, &Selector{
  144. Start: time.Now().Unix() - 3600*12,
  145. End: time.Now().Unix(),
  146. Limit: 10,
  147. Offset: 10,
  148. }, DurationAsc)
  149. So(err, ShouldBeNil)
  150. So(spanListRefs, ShouldNotBeEmpty)
  151. t.Logf("spanListRefs: %v", spanListRefs)
  152. Convey("test get trace", func() {
  153. spanListRef := spanListRefs[len(spanListRefs)-1]
  154. spans, err := daoImpl.Trace(ctx, spanListRef.TraceID)
  155. So(err, ShouldBeNil)
  156. So(spans, ShouldNotBeEmpty)
  157. t.Logf("spans %v", spans)
  158. })
  159. })
  160. Convey("test MeanOperationNameField", t, func() {
  161. start := time.Now().Unix() - 3600
  162. end := time.Now().Unix()
  163. values, err := daoImpl.MeanOperationNameField(ctx, map[string]string{"service_name": serviceName}, "max_duration", start, end, []string{"operation_name"})
  164. if err != nil {
  165. t.Error(err)
  166. }
  167. So(values, ShouldNotBeEmpty)
  168. })
  169. Convey("test SpanSeriesMean", t, func() {
  170. start := time.Now().Unix() - 3600
  171. end := time.Now().Unix()
  172. series, err := daoImpl.SpanSeriesMean(ctx, serviceName, operationName, []string{"max_duration", "min_duration"}, start, end, 30)
  173. if err != nil {
  174. t.Error(err)
  175. }
  176. So(series.Timestamps, ShouldNotBeEmpty)
  177. So(series.Items, ShouldNotBeEmpty)
  178. // FIXME
  179. //for _, item := range series.Items {
  180. // So(len(series.Timestamps), ShouldEqual, len(item.Rows))
  181. //}
  182. t.Logf("%#v\n", series)
  183. })
  184. Convey("test SpanSeriesCount", t, func() {
  185. start := time.Now().Unix() - 3600
  186. end := time.Now().Unix()
  187. series, err := daoImpl.SpanSeriesCount(ctx, serviceName, operationName, []string{"max_duration", "min_duration"}, start, end, 30)
  188. if err != nil {
  189. t.Error(err)
  190. }
  191. So(series.Timestamps, ShouldNotBeEmpty)
  192. So(series.Items, ShouldNotBeEmpty)
  193. // FIXME
  194. //for _, item := range series.Items {
  195. // So(len(series.Timestamps), ShouldEqual, len(item.Rows))
  196. //}
  197. t.Logf("%#v\n", series)
  198. })
  199. Convey("test PeerService", t, func() {
  200. serviceName := "main.bangumi.season-service"
  201. peerServices, err := daoImpl.PeerService(ctx, serviceName)
  202. if err != nil {
  203. t.Error(err)
  204. }
  205. So(peerServices, ShouldNotBeEmpty)
  206. })
  207. Convey("test trace", t, func() {
  208. traceID, _ := strconv.ParseUint("100056da7886666c", 16, 64)
  209. spans, err := daoImpl.Trace(ctx, traceID)
  210. if err != nil {
  211. t.Error(err)
  212. }
  213. So(spans, ShouldNotBeEmpty)
  214. })
  215. Convey("test ping close", t, func() {
  216. So(daoImpl.Ping(ctx), ShouldBeNil)
  217. So(daoImpl.Close(ctx), ShouldBeNil)
  218. })
  219. }