file_store_test.go 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. package stores
  2. import (
  3. "fmt"
  4. "io"
  5. "io/ioutil"
  6. "os"
  7. "path/filepath"
  8. "testing"
  9. . "github.com/smartystreets/goconvey/convey"
  10. "github.com/torkelo/grafana-pro/pkg/models"
  11. )
  12. func TestFileStore(t *testing.T) {
  13. GivenFileStore("When saving a dashboard", t, func(store *fileStore) {
  14. dashboard := models.NewDashboard("hello")
  15. err := store.Save(dashboard)
  16. Convey("should be saved to disk", func() {
  17. So(err, ShouldBeNil)
  18. _, err = os.Stat(store.getFilePathForDashboard("hello"))
  19. So(err, ShouldBeNil)
  20. })
  21. })
  22. GivenFileStore("When getting a saved dashboard", t, func(store *fileStore) {
  23. copyDashboardToTempData("default.json", "", store.dashDir)
  24. dash, err := store.GetById("default")
  25. Convey("should be read from disk", func() {
  26. So(err, ShouldBeNil)
  27. So(dash, ShouldNotBeNil)
  28. So(dash.Title(), ShouldEqual, "Grafana Play Home")
  29. })
  30. })
  31. GivenFileStore("when getting dashboard with capital letters", t, func(store *fileStore) {
  32. copyDashboardToTempData("annotations.json", "", store.dashDir)
  33. dash, err := store.GetById("AnnoTations")
  34. Convey("should be read from disk", func() {
  35. So(err, ShouldBeNil)
  36. So(dash, ShouldNotBeNil)
  37. So(dash.Title(), ShouldEqual, "Annotations")
  38. })
  39. })
  40. GivenFileStore("When copying dashboards into data dir", t, func(store *fileStore) {
  41. copyDashboardToTempData("annotations.json", "", store.dashDir)
  42. copyDashboardToTempData("default.json", "", store.dashDir)
  43. copyDashboardToTempData("graph-styles.json", "", store.dashDir)
  44. store.scanFiles()
  45. Convey("scan should generate index of all dashboards", func() {
  46. result, err := store.Query("*")
  47. So(err, ShouldBeNil)
  48. So(len(result), ShouldEqual, 3)
  49. })
  50. })
  51. }
  52. func copyDashboardToTempData(name string, destName string, dir string) {
  53. if destName == "" {
  54. destName = name
  55. }
  56. source, _ := filepath.Abs("../../data/dashboards/" + name)
  57. dest := filepath.Join(dir, destName)
  58. err := copyFile(dest, source)
  59. if err != nil {
  60. panic(fmt.Sprintf("failed to copy file %v", name))
  61. }
  62. }
  63. func GivenFileStore(desc string, t *testing.T, f func(store *fileStore)) {
  64. Convey(desc, t, func() {
  65. tempDir, _ := ioutil.TempDir("", "store")
  66. store := NewFileStore(tempDir)
  67. f(store)
  68. Reset(func() {
  69. os.RemoveAll(tempDir)
  70. })
  71. })
  72. }
  73. func copyFile(dst, src string) error {
  74. in, err := os.Open(src)
  75. if err != nil {
  76. return err
  77. }
  78. defer in.Close()
  79. out, err := os.Create(dst)
  80. if err != nil {
  81. return err
  82. }
  83. defer out.Close()
  84. _, err = io.Copy(out, in)
  85. cerr := out.Close()
  86. if err != nil {
  87. return err
  88. }
  89. return cerr
  90. }