annotation_test.go 6.8 KB

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