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:
Cristian C 2021-04-13 18:02:25 +03:00 committed by GitHub
parent a7d18bbc89
commit 7b7ea5a1ce
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 123 additions and 3 deletions

View File

@ -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

View File

@ -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