annotation_test.go 7.6 KB

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