Commit c62591e7 by Carl Bergquist Committed by GitHub

Provisioning: Makes file the default dashboard provisioner type (#24856)

Co-authored-by: Arve Knudsen <arve.knudsen@gmail.com>
parent 2f01f132
...@@ -297,6 +297,7 @@ github.com/smartystreets/goconvey v0.0.0-20181108003508-044398e4856c/go.mod h1:X ...@@ -297,6 +297,7 @@ github.com/smartystreets/goconvey v0.0.0-20181108003508-044398e4856c/go.mod h1:X
github.com/smartystreets/goconvey v0.0.0-20190731233626-505e41936337 h1:WN9BUFbdyOsSH/XohnWpXOlq9NBD5sGAB2FciQMUEe8= github.com/smartystreets/goconvey v0.0.0-20190731233626-505e41936337 h1:WN9BUFbdyOsSH/XohnWpXOlq9NBD5sGAB2FciQMUEe8=
github.com/smartystreets/goconvey v0.0.0-20190731233626-505e41936337/go.mod h1:syvi0/a8iFYH4r/RixwvyeAJjdLS9QV7WQ/tjFTllLA= github.com/smartystreets/goconvey v0.0.0-20190731233626-505e41936337/go.mod h1:syvi0/a8iFYH4r/RixwvyeAJjdLS9QV7WQ/tjFTllLA=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/objx v0.1.1 h1:2vfRuCMp5sSVIDSqO8oNnWJq7mPa6KVP3iPIwFBuy8A=
github.com/stretchr/objx v0.1.1/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= github.com/stretchr/objx v0.1.1/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.2.0/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs= github.com/stretchr/testify v1.2.0/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs=
github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs= github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs=
......
...@@ -88,6 +88,10 @@ func (cr *configReader) readConfig() ([]*config, error) { ...@@ -88,6 +88,10 @@ func (cr *configReader) readConfig() ([]*config, error) {
dashboard.OrgID = 1 dashboard.OrgID = 1
} }
if dashboard.Type == "" {
dashboard.Type = "file"
}
if dashboard.UpdateIntervalSeconds == 0 { if dashboard.UpdateIntervalSeconds == 0 {
dashboard.UpdateIntervalSeconds = 10 dashboard.UpdateIntervalSeconds = 10
} }
......
...@@ -4,87 +4,99 @@ import ( ...@@ -4,87 +4,99 @@ import (
"os" "os"
"testing" "testing"
"github.com/stretchr/testify/require"
"github.com/grafana/grafana/pkg/infra/log" "github.com/grafana/grafana/pkg/infra/log"
. "github.com/smartystreets/goconvey/convey"
) )
var ( var (
simpleDashboardConfig = "./testdata/test-configs/dashboards-from-disk" simpleDashboardConfig = "./testdata/test-configs/dashboards-from-disk"
oldVersion = "./testdata/test-configs/version-0" oldVersion = "./testdata/test-configs/version-0"
brokenConfigs = "./testdata/test-configs/broken-configs" brokenConfigs = "./testdata/test-configs/broken-configs"
appliedDefaults = "./testdata/test-configs/applied-defaults"
) )
func TestDashboardsAsConfig(t *testing.T) { func TestDashboardsAsConfig(t *testing.T) {
Convey("Dashboards as configuration", t, func() { t.Run("Dashboards as configuration", func(t *testing.T) {
logger := log.New("test-logger") logger := log.New("test-logger")
Convey("Can read config file version 1 format", func() { t.Run("default values should be applied", func(t *testing.T) {
cfgProvider := configReader{path: appliedDefaults, log: logger}
cfg, err := cfgProvider.readConfig()
require.NoError(t, err)
require.Equal(t, "file", cfg[0].Type)
require.Equal(t, int64(1), cfg[0].OrgID)
require.Equal(t, int64(10), cfg[0].UpdateIntervalSeconds)
})
t.Run("Can read config file version 1 format", func(t *testing.T) {
_ = os.Setenv("TEST_VAR", "general") _ = os.Setenv("TEST_VAR", "general")
cfgProvider := configReader{path: simpleDashboardConfig, log: logger} cfgProvider := configReader{path: simpleDashboardConfig, log: logger}
cfg, err := cfgProvider.readConfig() cfg, err := cfgProvider.readConfig()
_ = os.Unsetenv("TEST_VAR") _ = os.Unsetenv("TEST_VAR")
So(err, ShouldBeNil) require.NoError(t, err)
validateDashboardAsConfig(t, cfg) validateDashboardAsConfig(t, cfg)
}) })
Convey("Can read config file in version 0 format", func() { t.Run("Can read config file in version 0 format", func(t *testing.T) {
cfgProvider := configReader{path: oldVersion, log: logger} cfgProvider := configReader{path: oldVersion, log: logger}
cfg, err := cfgProvider.readConfig() cfg, err := cfgProvider.readConfig()
So(err, ShouldBeNil) require.NoError(t, err)
validateDashboardAsConfig(t, cfg) validateDashboardAsConfig(t, cfg)
}) })
Convey("Should skip invalid path", func() { t.Run("Should skip invalid path", func(t *testing.T) {
cfgProvider := configReader{path: "/invalid-directory", log: logger} cfgProvider := configReader{path: "/invalid-directory", log: logger}
cfg, err := cfgProvider.readConfig() cfg, err := cfgProvider.readConfig()
if err != nil { if err != nil {
t.Fatalf("readConfig return an error %v", err) t.Fatalf("readConfig return an error %v", err)
} }
So(len(cfg), ShouldEqual, 0) require.Equal(t, 0, len(cfg))
}) })
Convey("Should skip broken config files", func() { t.Run("Should skip broken config files", func(t *testing.T) {
cfgProvider := configReader{path: brokenConfigs, log: logger} cfgProvider := configReader{path: brokenConfigs, log: logger}
cfg, err := cfgProvider.readConfig() cfg, err := cfgProvider.readConfig()
if err != nil { if err != nil {
t.Fatalf("readConfig return an error %v", err) t.Fatalf("readConfig return an error %v", err)
} }
So(len(cfg), ShouldEqual, 0) require.Equal(t, 0, len(cfg))
}) })
}) })
} }
func validateDashboardAsConfig(t *testing.T, cfg []*config) { func validateDashboardAsConfig(t *testing.T, cfg []*config) {
t.Helper() t.Helper()
So(len(cfg), ShouldEqual, 2) require.Equal(t, 2, len(cfg))
ds := cfg[0] ds := cfg[0]
So(ds.Name, ShouldEqual, "general dashboards") require.Equal(t, ds.Name, "general dashboards")
So(ds.Type, ShouldEqual, "file") require.Equal(t, ds.Type, "file")
So(ds.OrgID, ShouldEqual, 2) require.Equal(t, ds.OrgID, int64(2))
So(ds.Folder, ShouldEqual, "developers") require.Equal(t, ds.Folder, "developers")
So(ds.FolderUID, ShouldEqual, "xyz") require.Equal(t, ds.FolderUID, "xyz")
So(ds.Editable, ShouldBeTrue) require.True(t, ds.Editable)
So(len(ds.Options), ShouldEqual, 1) require.Equal(t, len(ds.Options), 1)
So(ds.Options["path"], ShouldEqual, "/var/lib/grafana/dashboards") require.Equal(t, ds.Options["path"], "/var/lib/grafana/dashboards")
So(ds.DisableDeletion, ShouldBeTrue) require.True(t, ds.DisableDeletion)
So(ds.UpdateIntervalSeconds, ShouldEqual, 15) require.Equal(t, ds.UpdateIntervalSeconds, int64(15))
ds2 := cfg[1] ds2 := cfg[1]
So(ds2.Name, ShouldEqual, "default") require.Equal(t, ds2.Name, "default")
So(ds2.Type, ShouldEqual, "file") require.Equal(t, ds2.Type, "file")
So(ds2.OrgID, ShouldEqual, 1) require.Equal(t, ds2.OrgID, int64(1))
So(ds2.Folder, ShouldEqual, "") require.Equal(t, ds2.Folder, "")
So(ds2.FolderUID, ShouldEqual, "") require.Equal(t, ds2.FolderUID, "")
So(ds2.Editable, ShouldBeFalse) require.False(t, ds2.Editable)
So(len(ds2.Options), ShouldEqual, 1) require.Equal(t, len(ds2.Options), 1)
So(ds2.Options["path"], ShouldEqual, "/var/lib/grafana/dashboards") require.Equal(t, ds2.Options["path"], "/var/lib/grafana/dashboards")
So(ds2.DisableDeletion, ShouldBeFalse) require.False(t, ds2.DisableDeletion)
So(ds2.UpdateIntervalSeconds, ShouldEqual, 10) require.Equal(t, ds2.UpdateIntervalSeconds, int64(10))
} }
apiVersion: 1
providers:
- name: 'applied-defaults'
options:
path: /var/lib/grafana/dashboards
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment