file_reader_test.go 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  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. cfg := &DashboardsAsConfig{
  118. Name: "Default",
  119. Type: "file",
  120. OrgId: 1,
  121. Folder: "",
  122. Options: map[string]interface{}{
  123. "path": defaultDashboards,
  124. },
  125. }
  126. reader, err := NewDashboardFileReader(cfg, log.New("test-logger"))
  127. So(err, ShouldBeNil)
  128. noFiles := map[string]os.FileInfo{}
  129. Convey("should skip dirs that starts with .", func() {
  130. shouldSkip := reader.createWalk(noFiles)("path", &FakeFileInfo{isDirectory: true, name: ".folder"}, nil)
  131. So(shouldSkip, ShouldEqual, filepath.SkipDir)
  132. })
  133. Convey("should keep walking if file is not .json", func() {
  134. shouldSkip := reader.createWalk(noFiles)("path", &FakeFileInfo{isDirectory: true, name: "folder"}, nil)
  135. So(shouldSkip, ShouldBeNil)
  136. })
  137. })
  138. Convey("Can use bpth path and folder as dashboard path", func() {
  139. cfg := &DashboardsAsConfig{
  140. Name: "Default",
  141. Type: "file",
  142. OrgId: 1,
  143. Folder: "",
  144. Options: map[string]interface{}{},
  145. }
  146. Convey("using path parameter", func() {
  147. cfg.Options["path"] = defaultDashboards
  148. reader, err := NewDashboardFileReader(cfg, log.New("test-logger"))
  149. So(err, ShouldBeNil)
  150. So(reader.Path, ShouldEqual, defaultDashboards)
  151. })
  152. Convey("using folder as options", func() {
  153. cfg.Options["folder"] = defaultDashboards
  154. reader, err := NewDashboardFileReader(cfg, log.New("test-logger"))
  155. So(err, ShouldBeNil)
  156. So(reader.Path, ShouldEqual, defaultDashboards)
  157. })
  158. })
  159. })
  160. }
  161. type FakeFileInfo struct {
  162. isDirectory bool
  163. name string
  164. }
  165. func (ffi *FakeFileInfo) IsDir() bool {
  166. return ffi.isDirectory
  167. }
  168. func (ffi FakeFileInfo) Size() int64 {
  169. return 1
  170. }
  171. func (ffi FakeFileInfo) Mode() os.FileMode {
  172. return 0777
  173. }
  174. func (ffi FakeFileInfo) Name() string {
  175. return ffi.name
  176. }
  177. func (ffi FakeFileInfo) ModTime() time.Time {
  178. return time.Time{}
  179. }
  180. func (ffi FakeFileInfo) Sys() interface{} {
  181. return nil
  182. }
  183. type fakeDashboardRepo struct {
  184. inserted []*dashboards.SaveDashboardDTO
  185. provisioned []*models.DashboardProvisioning
  186. getDashboard []*models.Dashboard
  187. }
  188. func (repo *fakeDashboardRepo) SaveDashboard(json *dashboards.SaveDashboardDTO) (*models.Dashboard, error) {
  189. repo.inserted = append(repo.inserted, json)
  190. return json.Dashboard, nil
  191. }
  192. func (repo *fakeDashboardRepo) GetProvisionedDashboardData(name string) ([]*models.DashboardProvisioning, error) {
  193. return repo.provisioned, nil
  194. }
  195. func (repo *fakeDashboardRepo) SaveProvisionedDashboard(dto *dashboards.SaveDashboardDTO, provisioning *models.DashboardProvisioning) (*models.Dashboard, error) {
  196. repo.inserted = append(repo.inserted, dto)
  197. repo.provisioned = append(repo.provisioned, provisioning)
  198. return dto.Dashboard, nil
  199. }
  200. func mockGetDashboardQuery(cmd *models.GetDashboardQuery) error {
  201. for _, d := range fakeRepo.getDashboard {
  202. if d.Slug == cmd.Slug {
  203. cmd.Result = d
  204. return nil
  205. }
  206. }
  207. return models.ErrDashboardNotFound
  208. }