file_reader_test.go 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  1. package dashboards
  2. import (
  3. "os"
  4. "path/filepath"
  5. "testing"
  6. "time"
  7. "github.com/grafana/grafana/pkg/bus"
  8. "github.com/grafana/grafana/pkg/models"
  9. "github.com/grafana/grafana/pkg/services/dashboards"
  10. "github.com/grafana/grafana/pkg/log"
  11. . "github.com/smartystreets/goconvey/convey"
  12. )
  13. var (
  14. defaultDashboards string = "./test-dashboards/folder-one"
  15. brokenDashboards string = "./test-dashboards/broken-dashboards"
  16. oneDashboard string = "./test-dashboards/one-dashboard"
  17. fakeRepo *fakeDashboardRepo
  18. )
  19. func TestDashboardFileReader(t *testing.T) {
  20. Convey("Dashboard file reader", t, func() {
  21. bus.ClearBusHandlers()
  22. fakeRepo = &fakeDashboardRepo{}
  23. bus.AddHandler("test", mockGetDashboardQuery)
  24. dashboards.SetRepository(fakeRepo)
  25. logger := log.New("test.logger")
  26. Convey("Reading dashboards from disk", func() {
  27. cfg := &DashboardsAsConfig{
  28. Name: "Default",
  29. Type: "file",
  30. OrgId: 1,
  31. Folder: "",
  32. Options: map[string]interface{}{},
  33. }
  34. Convey("Can read default dashboard", func() {
  35. cfg.Options["path"] = defaultDashboards
  36. cfg.Folder = "Team A"
  37. reader, err := NewDashboardFileReader(cfg, logger)
  38. So(err, ShouldBeNil)
  39. err = reader.startWalkingDisk()
  40. So(err, ShouldBeNil)
  41. folders := 0
  42. dashboards := 0
  43. for _, i := range fakeRepo.inserted {
  44. if i.Dashboard.IsFolder {
  45. folders++
  46. } else {
  47. dashboards++
  48. }
  49. }
  50. So(folders, ShouldEqual, 1)
  51. So(dashboards, ShouldEqual, 2)
  52. })
  53. Convey("Can read default dashboard and replace old version in database", func() {
  54. cfg.Options["path"] = oneDashboard
  55. stat, _ := os.Stat(oneDashboard + "/dashboard1.json")
  56. fakeRepo.getDashboard = append(fakeRepo.getDashboard, &models.Dashboard{
  57. Updated: stat.ModTime().AddDate(0, 0, -1),
  58. Slug: "grafana",
  59. })
  60. reader, err := NewDashboardFileReader(cfg, logger)
  61. So(err, ShouldBeNil)
  62. err = reader.startWalkingDisk()
  63. So(err, ShouldBeNil)
  64. So(len(fakeRepo.inserted), ShouldEqual, 1)
  65. })
  66. Convey("Invalid configuration should return error", func() {
  67. cfg := &DashboardsAsConfig{
  68. Name: "Default",
  69. Type: "file",
  70. OrgId: 1,
  71. Folder: "",
  72. }
  73. _, err := NewDashboardFileReader(cfg, logger)
  74. So(err, ShouldNotBeNil)
  75. })
  76. Convey("Broken dashboards should not cause error", func() {
  77. cfg.Options["path"] = brokenDashboards
  78. _, err := NewDashboardFileReader(cfg, logger)
  79. So(err, ShouldBeNil)
  80. })
  81. })
  82. Convey("Should not create new folder if folder name is missing", func() {
  83. cfg := &DashboardsAsConfig{
  84. Name: "Default",
  85. Type: "file",
  86. OrgId: 1,
  87. Folder: "",
  88. Options: map[string]interface{}{
  89. "folder": defaultDashboards,
  90. },
  91. }
  92. _, err := getOrCreateFolderId(cfg, fakeRepo)
  93. So(err, ShouldEqual, ErrFolderNameMissing)
  94. })
  95. Convey("can get or Create dashboard folder", func() {
  96. cfg := &DashboardsAsConfig{
  97. Name: "Default",
  98. Type: "file",
  99. OrgId: 1,
  100. Folder: "TEAM A",
  101. Options: map[string]interface{}{
  102. "folder": defaultDashboards,
  103. },
  104. }
  105. folderId, err := getOrCreateFolderId(cfg, fakeRepo)
  106. So(err, ShouldBeNil)
  107. inserted := false
  108. for _, d := range fakeRepo.inserted {
  109. if d.Dashboard.IsFolder && d.Dashboard.Id == folderId {
  110. inserted = true
  111. }
  112. }
  113. So(len(fakeRepo.inserted), ShouldEqual, 1)
  114. So(inserted, ShouldBeTrue)
  115. })
  116. Convey("Walking the folder with dashboards", func() {
  117. noFiles := map[string]os.FileInfo{}
  118. Convey("should skip dirs that starts with .", func() {
  119. shouldSkip := createWalkFn(noFiles)("path", &FakeFileInfo{isDirectory: true, name: ".folder"}, nil)
  120. So(shouldSkip, ShouldEqual, filepath.SkipDir)
  121. })
  122. Convey("should keep walking if file is not .json", func() {
  123. shouldSkip := createWalkFn(noFiles)("path", &FakeFileInfo{isDirectory: true, name: "folder"}, nil)
  124. So(shouldSkip, ShouldBeNil)
  125. })
  126. })
  127. Convey("Can use bpth path and folder as dashboard path", func() {
  128. cfg := &DashboardsAsConfig{
  129. Name: "Default",
  130. Type: "file",
  131. OrgId: 1,
  132. Folder: "",
  133. Options: map[string]interface{}{},
  134. }
  135. Convey("using path parameter", func() {
  136. cfg.Options["path"] = defaultDashboards
  137. reader, err := NewDashboardFileReader(cfg, log.New("test-logger"))
  138. So(err, ShouldBeNil)
  139. So(reader.Path, ShouldEqual, defaultDashboards)
  140. })
  141. Convey("using folder as options", func() {
  142. cfg.Options["folder"] = defaultDashboards
  143. reader, err := NewDashboardFileReader(cfg, log.New("test-logger"))
  144. So(err, ShouldBeNil)
  145. So(reader.Path, ShouldEqual, defaultDashboards)
  146. })
  147. })
  148. })
  149. }
  150. type FakeFileInfo struct {
  151. isDirectory bool
  152. name string
  153. }
  154. func (ffi *FakeFileInfo) IsDir() bool {
  155. return ffi.isDirectory
  156. }
  157. func (ffi FakeFileInfo) Size() int64 {
  158. return 1
  159. }
  160. func (ffi FakeFileInfo) Mode() os.FileMode {
  161. return 0777
  162. }
  163. func (ffi FakeFileInfo) Name() string {
  164. return ffi.name
  165. }
  166. func (ffi FakeFileInfo) ModTime() time.Time {
  167. return time.Time{}
  168. }
  169. func (ffi FakeFileInfo) Sys() interface{} {
  170. return nil
  171. }
  172. type fakeDashboardRepo struct {
  173. inserted []*dashboards.SaveDashboardDTO
  174. provisioned []*models.DashboardProvisioning
  175. getDashboard []*models.Dashboard
  176. }
  177. func (repo *fakeDashboardRepo) SaveDashboard(json *dashboards.SaveDashboardDTO) (*models.Dashboard, error) {
  178. repo.inserted = append(repo.inserted, json)
  179. return json.Dashboard, nil
  180. }
  181. func (repo *fakeDashboardRepo) GetProvisionedDashboardData(name string) ([]*models.DashboardProvisioning, error) {
  182. return repo.provisioned, nil
  183. }
  184. func (repo *fakeDashboardRepo) SaveProvisionedDashboard(dto *dashboards.SaveDashboardDTO, provisioning *models.DashboardProvisioning) (*models.Dashboard, error) {
  185. repo.inserted = append(repo.inserted, dto)
  186. repo.provisioned = append(repo.provisioned, provisioning)
  187. return dto.Dashboard, nil
  188. }
  189. func mockGetDashboardQuery(cmd *models.GetDashboardQuery) error {
  190. for _, d := range fakeRepo.getDashboard {
  191. if d.Slug == cmd.Slug {
  192. cmd.Result = d
  193. return nil
  194. }
  195. }
  196. return models.ErrDashboardNotFound
  197. }