annotation_test.go 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  1. package sqlstore
  2. import (
  3. "testing"
  4. . "github.com/smartystreets/goconvey/convey"
  5. "github.com/grafana/grafana/pkg/models"
  6. "github.com/grafana/grafana/pkg/services/annotations"
  7. )
  8. func TestSavingTags(t *testing.T) {
  9. Convey("Testing annotation saving/loading", t, func() {
  10. InitTestDB(t)
  11. repo := SqlAnnotationRepo{}
  12. Convey("Can save tags", func() {
  13. tagPairs := []*models.Tag{
  14. {Key: "outage"},
  15. {Key: "type", Value: "outage"},
  16. {Key: "server", Value: "server-1"},
  17. {Key: "error"},
  18. }
  19. tags, err := repo.ensureTagsExist(newSession(), tagPairs)
  20. So(err, ShouldBeNil)
  21. So(len(tags), ShouldEqual, 4)
  22. })
  23. })
  24. }
  25. func TestAnnotations(t *testing.T) {
  26. Convey("Testing annotation saving/loading", t, func() {
  27. InitTestDB(t)
  28. repo := SqlAnnotationRepo{}
  29. Convey("Can save annotation", func() {
  30. annotation := &annotations.Item{
  31. OrgId: 1,
  32. UserId: 1,
  33. DashboardId: 1,
  34. Text: "hello",
  35. Type: "alert",
  36. Epoch: 10,
  37. Tags: []string{"outage", "error", "type:outage", "server:server-1"},
  38. }
  39. err := repo.Save(annotation)
  40. So(err, ShouldBeNil)
  41. So(annotation.Id, ShouldBeGreaterThan, 0)
  42. annotation2 := &annotations.Item{
  43. OrgId: 1,
  44. UserId: 1,
  45. DashboardId: 2,
  46. Text: "hello",
  47. Type: "alert",
  48. Epoch: 20,
  49. Tags: []string{"outage", "error", "type:outage", "server:server-1"},
  50. RegionId: 1,
  51. }
  52. err = repo.Save(annotation2)
  53. So(err, ShouldBeNil)
  54. So(annotation2.Id, ShouldBeGreaterThan, 0)
  55. Convey("Can query for annotation", func() {
  56. items, err := repo.Find(&annotations.ItemQuery{
  57. OrgId: 1,
  58. DashboardId: 1,
  59. From: 0,
  60. To: 15,
  61. })
  62. So(err, ShouldBeNil)
  63. So(items, ShouldHaveLength, 1)
  64. Convey("Can read tags", func() {
  65. So(items[0].Tags, ShouldResemble, []string{"outage", "error", "type:outage", "server:server-1"})
  66. })
  67. })
  68. Convey("Can query for annotation by id", func() {
  69. items, err := repo.Find(&annotations.ItemQuery{
  70. OrgId: 1,
  71. AnnotationId: annotation2.Id,
  72. })
  73. So(err, ShouldBeNil)
  74. So(items, ShouldHaveLength, 1)
  75. So(items[0].Id, ShouldEqual, annotation2.Id)
  76. })
  77. Convey("Can query for annotation by region id", func() {
  78. items, err := repo.Find(&annotations.ItemQuery{
  79. OrgId: 1,
  80. RegionId: annotation2.RegionId,
  81. })
  82. So(err, ShouldBeNil)
  83. So(items, ShouldHaveLength, 1)
  84. So(items[0].Id, ShouldEqual, annotation2.Id)
  85. })
  86. Convey("Should not find any when item is outside time range", func() {
  87. items, err := repo.Find(&annotations.ItemQuery{
  88. OrgId: 1,
  89. DashboardId: 1,
  90. From: 12,
  91. To: 15,
  92. })
  93. So(err, ShouldBeNil)
  94. So(items, ShouldHaveLength, 0)
  95. })
  96. Convey("Should not find one when tag filter does not match", func() {
  97. items, err := repo.Find(&annotations.ItemQuery{
  98. OrgId: 1,
  99. DashboardId: 1,
  100. From: 1,
  101. To: 15,
  102. Tags: []string{"asd"},
  103. })
  104. So(err, ShouldBeNil)
  105. So(items, ShouldHaveLength, 0)
  106. })
  107. Convey("Should not find one when type filter does not match", func() {
  108. items, err := repo.Find(&annotations.ItemQuery{
  109. OrgId: 1,
  110. DashboardId: 1,
  111. From: 1,
  112. To: 15,
  113. Type: "alert",
  114. })
  115. So(err, ShouldBeNil)
  116. So(items, ShouldHaveLength, 0)
  117. })
  118. Convey("Should find one when all tag filters does match", func() {
  119. items, err := repo.Find(&annotations.ItemQuery{
  120. OrgId: 1,
  121. DashboardId: 1,
  122. From: 1,
  123. To: 15,
  124. Tags: []string{"outage", "error"},
  125. })
  126. So(err, ShouldBeNil)
  127. So(items, ShouldHaveLength, 1)
  128. })
  129. Convey("Should find one when all key value tag filters does match", func() {
  130. items, err := repo.Find(&annotations.ItemQuery{
  131. OrgId: 1,
  132. DashboardId: 1,
  133. From: 1,
  134. To: 15,
  135. Tags: []string{"type:outage", "server:server-1"},
  136. })
  137. So(err, ShouldBeNil)
  138. So(items, ShouldHaveLength, 1)
  139. })
  140. Convey("Can update annotation and remove all tags", func() {
  141. query := &annotations.ItemQuery{
  142. OrgId: 1,
  143. DashboardId: 1,
  144. From: 0,
  145. To: 15,
  146. }
  147. items, err := repo.Find(query)
  148. So(err, ShouldBeNil)
  149. annotationId := items[0].Id
  150. err = repo.Update(&annotations.Item{
  151. Id: annotationId,
  152. OrgId: 1,
  153. Text: "something new",
  154. Tags: []string{},
  155. })
  156. So(err, ShouldBeNil)
  157. items, err = repo.Find(query)
  158. So(err, ShouldBeNil)
  159. Convey("Can read tags", func() {
  160. So(items[0].Id, ShouldEqual, annotationId)
  161. So(len(items[0].Tags), ShouldEqual, 0)
  162. So(items[0].Text, ShouldEqual, "something new")
  163. })
  164. })
  165. Convey("Can update annotation with new tags", func() {
  166. query := &annotations.ItemQuery{
  167. OrgId: 1,
  168. DashboardId: 1,
  169. From: 0,
  170. To: 15,
  171. }
  172. items, err := repo.Find(query)
  173. So(err, ShouldBeNil)
  174. annotationId := items[0].Id
  175. err = repo.Update(&annotations.Item{
  176. Id: annotationId,
  177. OrgId: 1,
  178. Text: "something new",
  179. Tags: []string{"newtag1", "newtag2"},
  180. })
  181. So(err, ShouldBeNil)
  182. items, err = repo.Find(query)
  183. So(err, ShouldBeNil)
  184. Convey("Can read tags", func() {
  185. So(items[0].Id, ShouldEqual, annotationId)
  186. So(items[0].Tags, ShouldResemble, []string{"newtag1", "newtag2"})
  187. So(items[0].Text, ShouldEqual, "something new")
  188. })
  189. })
  190. Convey("Can delete annotation", func() {
  191. query := &annotations.ItemQuery{
  192. OrgId: 1,
  193. DashboardId: 1,
  194. From: 0,
  195. To: 15,
  196. }
  197. items, err := repo.Find(query)
  198. So(err, ShouldBeNil)
  199. annotationId := items[0].Id
  200. err = repo.Delete(&annotations.DeleteParams{Id: annotationId})
  201. items, err = repo.Find(query)
  202. So(err, ShouldBeNil)
  203. Convey("Should be deleted", func() {
  204. So(len(items), ShouldEqual, 0)
  205. })
  206. })
  207. })
  208. })
  209. }