diff --git a/pkg/services/provisioning/dashboards/file_reader.go b/pkg/services/provisioning/dashboards/file_reader.go index b448d609176..99c835bd319 100644 --- a/pkg/services/provisioning/dashboards/file_reader.go +++ b/pkg/services/provisioning/dashboards/file_reader.go @@ -206,7 +206,6 @@ func (fr *FileReader) saveDashboard(path string, folderID int64, fileInfo os.Fil } provisionedData, alreadyProvisioned := provisionedDashboardRefs[path] - upToDate := alreadyProvisioned && provisionedData.Updated >= resolvedFileInfo.ModTime().Unix() jsonFile, err := fr.readDashboardFromFile(path, resolvedFileInfo.ModTime(), folderID) if err != nil { @@ -214,8 +213,9 @@ func (fr *FileReader) saveDashboard(path string, folderID int64, fileInfo os.Fil return provisioningMetadata, nil } - if provisionedData != nil && jsonFile.checkSum == provisionedData.CheckSum { - upToDate = true + upToDate := alreadyProvisioned + if provisionedData != nil { + upToDate = jsonFile.checkSum == provisionedData.CheckSum } // keeps track of which UIDs and titles we have already provisioned diff --git a/pkg/services/provisioning/dashboards/file_reader_test.go b/pkg/services/provisioning/dashboards/file_reader_test.go index 0093d67b691..ee3f5f3b339 100644 --- a/pkg/services/provisioning/dashboards/file_reader_test.go +++ b/pkg/services/provisioning/dashboards/file_reader_test.go @@ -152,6 +152,126 @@ func TestDashboardFileReader(t *testing.T) { So(len(fakeService.inserted), ShouldEqual, 1) }) + Convey("Dashboard with older timestamp and the same checksum will not replace imported dashboard", func() { + cfg.Options["path"] = oneDashboard + absPath, err := filepath.Abs(oneDashboard + "/dashboard1.json") + So(err, ShouldBeNil) + stat, err := os.Stat(oneDashboard + "/dashboard1.json") + So(err, ShouldBeNil) + file, err := os.Open(filepath.Clean(absPath)) + So(err, ShouldBeNil) + t.Cleanup(func() { + _ = file.Close() + }) + + checksum, err := util.Md5Sum(file) + So(err, ShouldBeNil) + + fakeService.provisioned = map[string][]*models.DashboardProvisioning{ + "Default": { + { + Name: "Default", + ExternalId: absPath, + Updated: stat.ModTime().AddDate(0, 0, +1).Unix(), + CheckSum: checksum, + }, + }, + } + + reader, err := NewDashboardFileReader(cfg, logger, nil) + So(err, ShouldBeNil) + + err = reader.walkDisk() + So(err, ShouldBeNil) + So(len(fakeService.inserted), ShouldEqual, 0) + }) + + Convey("Dashboard with older timestamp and different checksum will replace imported dashboard", func() { + cfg.Options["path"] = oneDashboard + absPath, err := filepath.Abs(oneDashboard + "/dashboard1.json") + So(err, ShouldBeNil) + stat, err := os.Stat(oneDashboard + "/dashboard1.json") + So(err, ShouldBeNil) + + fakeService.provisioned = map[string][]*models.DashboardProvisioning{ + "Default": { + { + Name: "Default", + ExternalId: absPath, + Updated: stat.ModTime().AddDate(0, 0, +1).Unix(), + CheckSum: "fakechecksum", + }, + }, + } + + reader, err := NewDashboardFileReader(cfg, logger, nil) + So(err, ShouldBeNil) + + err = reader.walkDisk() + So(err, ShouldBeNil) + So(len(fakeService.inserted), ShouldEqual, 1) + }) + + Convey("Dashboard with newer timestamp and the same checksum will not replace imported dashboard", func() { + cfg.Options["path"] = oneDashboard + absPath, err := filepath.Abs(oneDashboard + "/dashboard1.json") + So(err, ShouldBeNil) + stat, err := os.Stat(oneDashboard + "/dashboard1.json") + So(err, ShouldBeNil) + file, err := os.Open(filepath.Clean(absPath)) + So(err, ShouldBeNil) + t.Cleanup(func() { + _ = file.Close() + }) + + checksum, err := util.Md5Sum(file) + So(err, ShouldBeNil) + + fakeService.provisioned = map[string][]*models.DashboardProvisioning{ + "Default": { + { + Name: "Default", + ExternalId: absPath, + Updated: stat.ModTime().AddDate(0, 0, -1).Unix(), + CheckSum: checksum, + }, + }, + } + + reader, err := NewDashboardFileReader(cfg, logger, nil) + So(err, ShouldBeNil) + + err = reader.walkDisk() + So(err, ShouldBeNil) + So(len(fakeService.inserted), ShouldEqual, 0) + }) + + Convey("Dashboard with newer timestamp and different checksum should replace imported dashboard", func() { + cfg.Options["path"] = oneDashboard + absPath, err := filepath.Abs(oneDashboard + "/dashboard1.json") + So(err, ShouldBeNil) + stat, err := os.Stat(oneDashboard + "/dashboard1.json") + So(err, ShouldBeNil) + + fakeService.provisioned = map[string][]*models.DashboardProvisioning{ + "Default": { + { + Name: "Default", + ExternalId: absPath, + Updated: stat.ModTime().AddDate(0, 0, -1).Unix(), + CheckSum: "fakechecksum", + }, + }, + } + + reader, err := NewDashboardFileReader(cfg, logger, nil) + So(err, ShouldBeNil) + + err = reader.walkDisk() + So(err, ShouldBeNil) + So(len(fakeService.inserted), ShouldEqual, 1) + }) + Convey("Overrides id from dashboard.json files", func() { cfg.Options["path"] = containingID