mirror of
https://github.com/grafana/grafana.git
synced 2025-02-25 18:55:37 -06:00
Provisioning: Use dashboard checksum field as change indicator (#29797)
* Add tests for checksum validation Check dashboard checksum for provisioned dashboards Remove extra error Remove extra error Ignore mtime when checking privisioned dashboards; Fix tests Fix linting errors * Fix tests Signed-off-by: Arve Knudsen <arve.knudsen@gmail.com> * Clean up tests Signed-off-by: Arve Knudsen <arve.knudsen@gmail.com> Co-authored-by: Arve Knudsen <arve.knudsen@gmail.com>
This commit is contained in:
parent
a7d18bbc89
commit
7b7ea5a1ce
@ -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
|
||||
|
@ -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
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user