mirror of
https://github.com/grafana/grafana.git
synced 2025-02-25 18:55:37 -06:00
datasource as cfg: refactor tests to use yaml files
This commit is contained in:
parent
3f0eb52389
commit
ba4bbd1d47
@ -1,30 +1,20 @@
|
|||||||
purgeOtherDatasources: false
|
purge_other_datasources: false
|
||||||
datasources:
|
datasources:
|
||||||
- name: Graphite202
|
- name: Graphite
|
||||||
type: graphite
|
type: graphite
|
||||||
access: proxy
|
access: proxy
|
||||||
url: http://localhost:8080
|
url: http://localhost:8080
|
||||||
password:
|
password: #string
|
||||||
user:
|
user: #string
|
||||||
database:
|
database: #string
|
||||||
basicAuth:
|
basic_auth: #bool
|
||||||
basicAuthUser:
|
basic_authUser: #string
|
||||||
basicAuthPassword:
|
basic_auth_password: #string
|
||||||
withCredentials:
|
with_credentials: #bool
|
||||||
isDefault: true
|
is_default: true #bool
|
||||||
jsonData: {}
|
json_data: '{"graphiteVersion":"0.9"}' # string json
|
||||||
secureJsonFields: {}
|
secure_json_fields: '' #string json
|
||||||
- name: Prometheus
|
- name: Prometheus
|
||||||
type: prometheus
|
type: prometheus
|
||||||
access: proxy
|
access: proxy
|
||||||
url: http://localhost:9090
|
url: http://localhost:9090
|
||||||
password:
|
|
||||||
user:
|
|
||||||
database:
|
|
||||||
basicAuth:
|
|
||||||
basicAuthUser:
|
|
||||||
basicAuthPassword:
|
|
||||||
withCredentials:
|
|
||||||
isDefault: true
|
|
||||||
jsonData: {}
|
|
||||||
secureJsonFields: {}
|
|
||||||
|
@ -1,6 +1,7 @@
|
|||||||
package datasources
|
package datasources
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"errors"
|
||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
|
|
||||||
@ -11,10 +12,9 @@ import (
|
|||||||
yaml "gopkg.in/yaml.v2"
|
yaml "gopkg.in/yaml.v2"
|
||||||
)
|
)
|
||||||
|
|
||||||
type DatasourcesAsConfig struct {
|
var (
|
||||||
PurgeOtherDatasources bool
|
ErrInvalidConfigToManyDefault = errors.New("datasource.yaml config is invalid. Only one datasource can be marked as default")
|
||||||
Datasources []DataSourceFromConfig
|
)
|
||||||
}
|
|
||||||
|
|
||||||
func Init(configPath string) error {
|
func Init(configPath string) error {
|
||||||
dc := NewDatasourceConfiguration()
|
dc := NewDatasourceConfiguration()
|
||||||
@ -28,14 +28,14 @@ type DatasourceConfigurator struct {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func NewDatasourceConfiguration() DatasourceConfigurator {
|
func NewDatasourceConfiguration() DatasourceConfigurator {
|
||||||
return newDatasourceConfiguration(log.New("setting.datasource"), diskConfigReader{}, sqlDatasourceRepository{})
|
return newDatasourceConfiguration(log.New("setting.datasource"), sqlDatasourceRepository{})
|
||||||
}
|
}
|
||||||
|
|
||||||
func newDatasourceConfiguration(log log.Logger, cfgProvider configProvider, repo datasourceRepository) DatasourceConfigurator {
|
func newDatasourceConfiguration(log log.Logger, repo datasourceRepository) DatasourceConfigurator {
|
||||||
return DatasourceConfigurator{
|
return DatasourceConfigurator{
|
||||||
log: log.New("setting.datasource"),
|
log: log,
|
||||||
repository: repo,
|
repository: repo,
|
||||||
cfgProvider: cfgProvider,
|
cfgProvider: configProvider{},
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -45,15 +45,23 @@ func (dc *DatasourceConfigurator) applyChanges(configPath string) error {
|
|||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
allDatasources, err := dc.repository.loadAllDatasources()
|
defaultCount := 0
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
for i := range cfg.Datasources {
|
for i := range cfg.Datasources {
|
||||||
if cfg.Datasources[i].OrgId == 0 {
|
if cfg.Datasources[i].OrgId == 0 {
|
||||||
cfg.Datasources[i].OrgId = 1
|
cfg.Datasources[i].OrgId = 1
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if cfg.Datasources[i].IsDefault {
|
||||||
|
defaultCount++
|
||||||
|
if defaultCount > 1 {
|
||||||
|
return ErrInvalidConfigToManyDefault
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
allDatasources, err := dc.repository.loadAllDatasources()
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
if err := dc.deleteDatasourcesNotInConfiguration(cfg, allDatasources); err != nil {
|
if err := dc.deleteDatasourcesNotInConfiguration(cfg, allDatasources); err != nil {
|
||||||
@ -73,11 +81,11 @@ func (dc *DatasourceConfigurator) applyChanges(configPath string) error {
|
|||||||
dc.log.Info("inserting datasource from configuration ", "name", ds.Name)
|
dc.log.Info("inserting datasource from configuration ", "name", ds.Name)
|
||||||
insertCmd := createInsertCommand(ds)
|
insertCmd := createInsertCommand(ds)
|
||||||
err := dc.repository.insert(insertCmd)
|
err := dc.repository.insert(insertCmd)
|
||||||
if err != nil && err != models.ErrDataSourceNameExists {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
dc.log.Info("updating datasource from configuration", "name", ds.Name)
|
dc.log.Debug("updating datasource from configuration", "name", ds.Name)
|
||||||
updateCmd := createUpdateCommand(ds, dbDatasource.Id)
|
updateCmd := createUpdateCommand(ds, dbDatasource.Id)
|
||||||
if err := dc.repository.update(updateCmd); err != nil {
|
if err := dc.repository.update(updateCmd); err != nil {
|
||||||
return err
|
return err
|
||||||
@ -119,14 +127,10 @@ type datasourceRepository interface {
|
|||||||
loadAllDatasources() ([]*models.DataSource, error)
|
loadAllDatasources() ([]*models.DataSource, error)
|
||||||
}
|
}
|
||||||
|
|
||||||
type configProvider interface {
|
|
||||||
readConfig(string) (*DatasourcesAsConfig, error)
|
|
||||||
}
|
|
||||||
|
|
||||||
type sqlDatasourceRepository struct{}
|
type sqlDatasourceRepository struct{}
|
||||||
type diskConfigReader struct{}
|
type configProvider struct{}
|
||||||
|
|
||||||
func (diskConfigReader) readConfig(path string) (*DatasourcesAsConfig, error) {
|
func (configProvider) readConfig(path string) (*DatasourcesAsConfig, error) {
|
||||||
filename, _ := filepath.Abs(path)
|
filename, _ := filepath.Abs(path)
|
||||||
yamlFile, err := ioutil.ReadFile(filename)
|
yamlFile, err := ioutil.ReadFile(filename)
|
||||||
|
|
||||||
|
@ -9,79 +9,59 @@ import (
|
|||||||
. "github.com/smartystreets/goconvey/convey"
|
. "github.com/smartystreets/goconvey/convey"
|
||||||
)
|
)
|
||||||
|
|
||||||
var logger log.Logger = log.New("fake.logger")
|
var (
|
||||||
|
logger log.Logger = log.New("fake.logger")
|
||||||
|
oneDatasourcesConfig string = ""
|
||||||
|
twoDatasourcesConfig string = "./test-configs/two-datasources.yaml"
|
||||||
|
twoDatasourcesConfigPurgeOthers string = "./test-configs/two-datasources-purge-others.yaml"
|
||||||
|
doubleDatasourcesConfig string = "./test-configs/double-default-datasources.yaml"
|
||||||
|
)
|
||||||
|
|
||||||
func TestDatasourceAsConfig(t *testing.T) {
|
func TestDatasourceAsConfig(t *testing.T) {
|
||||||
Convey("Testing datasource as configuration", t, func() {
|
Convey("Testing datasource as configuration", t, func() {
|
||||||
fakeCfg := &fakeConfig{}
|
|
||||||
fakeRepo := &fakeRepository{}
|
fakeRepo := &fakeRepository{}
|
||||||
|
|
||||||
Convey("One configured datasource", func() {
|
Convey("One configured datasource", func() {
|
||||||
fakeCfg.cfg = &DatasourcesAsConfig{
|
|
||||||
PurgeOtherDatasources: false,
|
|
||||||
Datasources: []DataSourceFromConfig{
|
|
||||||
{Name: "graphite", OrgId: 1},
|
|
||||||
},
|
|
||||||
}
|
|
||||||
|
|
||||||
Convey("no datasource in database", func() {
|
Convey("no datasource in database", func() {
|
||||||
dc := newDatasourceConfiguration(logger, fakeCfg, fakeRepo)
|
dc := newDatasourceConfiguration(logger, fakeRepo)
|
||||||
err := dc.applyChanges("mock/config.yaml")
|
err := dc.applyChanges(twoDatasourcesConfig)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatalf("applyChanges return an error %v", err)
|
t.Fatalf("applyChanges return an error %v", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
So(len(fakeRepo.deleted), ShouldEqual, 0)
|
So(len(fakeRepo.deleted), ShouldEqual, 0)
|
||||||
So(len(fakeRepo.inserted), ShouldEqual, 1)
|
So(len(fakeRepo.inserted), ShouldEqual, 2)
|
||||||
So(len(fakeRepo.updated), ShouldEqual, 0)
|
So(len(fakeRepo.updated), ShouldEqual, 0)
|
||||||
})
|
})
|
||||||
|
|
||||||
Convey("One datasource in database with same name", func() {
|
Convey("One datasource in database with same name", func() {
|
||||||
fakeRepo.loadAll = []*models.DataSource{
|
fakeRepo.loadAll = []*models.DataSource{
|
||||||
{Name: "graphite", OrgId: 1, Id: 1},
|
{Name: "Graphite", OrgId: 1, Id: 1},
|
||||||
}
|
}
|
||||||
|
|
||||||
Convey("should update one datasource", func() {
|
Convey("should update one datasource", func() {
|
||||||
dc := newDatasourceConfiguration(logger, fakeCfg, fakeRepo)
|
dc := newDatasourceConfiguration(logger, fakeRepo)
|
||||||
err := dc.applyChanges("mock/config.yaml")
|
err := dc.applyChanges(twoDatasourcesConfig)
|
||||||
if err != nil {
|
|
||||||
t.Fatalf("applyChanges return an error %v", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
So(len(fakeRepo.deleted), ShouldEqual, 0)
|
|
||||||
So(len(fakeRepo.inserted), ShouldEqual, 0)
|
|
||||||
So(len(fakeRepo.updated), ShouldEqual, 1)
|
|
||||||
})
|
|
||||||
})
|
|
||||||
|
|
||||||
Convey("One datasource in database with new name", func() {
|
|
||||||
fakeRepo.loadAll = []*models.DataSource{
|
|
||||||
{Name: "old-graphite", OrgId: 1, Id: 1},
|
|
||||||
}
|
|
||||||
|
|
||||||
Convey("should update one datasource", func() {
|
|
||||||
dc := newDatasourceConfiguration(logger, fakeCfg, fakeRepo)
|
|
||||||
err := dc.applyChanges("mock/config.yaml")
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatalf("applyChanges return an error %v", err)
|
t.Fatalf("applyChanges return an error %v", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
So(len(fakeRepo.deleted), ShouldEqual, 0)
|
So(len(fakeRepo.deleted), ShouldEqual, 0)
|
||||||
So(len(fakeRepo.inserted), ShouldEqual, 1)
|
So(len(fakeRepo.inserted), ShouldEqual, 1)
|
||||||
So(len(fakeRepo.updated), ShouldEqual, 0)
|
So(len(fakeRepo.updated), ShouldEqual, 1)
|
||||||
|
})
|
||||||
|
})
|
||||||
|
|
||||||
|
Convey("Two datasources with is_default", func() {
|
||||||
|
dc := newDatasourceConfiguration(logger, fakeRepo)
|
||||||
|
err := dc.applyChanges(doubleDatasourcesConfig)
|
||||||
|
Convey("should raise error", func() {
|
||||||
|
So(err, ShouldEqual, ErrInvalidConfigToManyDefault)
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
|
||||||
Convey("Two configured datasource and purge others ", func() {
|
Convey("Two configured datasource and purge others ", func() {
|
||||||
fakeCfg.cfg = &DatasourcesAsConfig{
|
|
||||||
PurgeOtherDatasources: true,
|
|
||||||
Datasources: []DataSourceFromConfig{
|
|
||||||
{Name: "graphite", OrgId: 1},
|
|
||||||
{Name: "prometheus", OrgId: 1},
|
|
||||||
},
|
|
||||||
}
|
|
||||||
|
|
||||||
Convey("two other datasources in database", func() {
|
Convey("two other datasources in database", func() {
|
||||||
fakeRepo.loadAll = []*models.DataSource{
|
fakeRepo.loadAll = []*models.DataSource{
|
||||||
{Name: "old-graphite", OrgId: 1, Id: 1},
|
{Name: "old-graphite", OrgId: 1, Id: 1},
|
||||||
@ -89,8 +69,8 @@ func TestDatasourceAsConfig(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
Convey("should have two new datasources", func() {
|
Convey("should have two new datasources", func() {
|
||||||
dc := newDatasourceConfiguration(logger, fakeCfg, fakeRepo)
|
dc := newDatasourceConfiguration(logger, fakeRepo)
|
||||||
err := dc.applyChanges("mock/config.yaml")
|
err := dc.applyChanges(twoDatasourcesConfigPurgeOthers)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatalf("applyChanges return an error %v", err)
|
t.Fatalf("applyChanges return an error %v", err)
|
||||||
}
|
}
|
||||||
@ -103,23 +83,15 @@ func TestDatasourceAsConfig(t *testing.T) {
|
|||||||
})
|
})
|
||||||
|
|
||||||
Convey("Two configured datasource and purge others = false", func() {
|
Convey("Two configured datasource and purge others = false", func() {
|
||||||
fakeCfg.cfg = &DatasourcesAsConfig{
|
|
||||||
PurgeOtherDatasources: false,
|
|
||||||
Datasources: []DataSourceFromConfig{
|
|
||||||
{Name: "graphite", OrgId: 1},
|
|
||||||
{Name: "prometheus", OrgId: 1},
|
|
||||||
},
|
|
||||||
}
|
|
||||||
|
|
||||||
Convey("two other datasources in database", func() {
|
Convey("two other datasources in database", func() {
|
||||||
fakeRepo.loadAll = []*models.DataSource{
|
fakeRepo.loadAll = []*models.DataSource{
|
||||||
{Name: "graphite", OrgId: 1, Id: 1},
|
{Name: "Graphite", OrgId: 1, Id: 1},
|
||||||
{Name: "old-graphite2", OrgId: 1, Id: 2},
|
{Name: "old-graphite2", OrgId: 1, Id: 2},
|
||||||
}
|
}
|
||||||
|
|
||||||
Convey("should have two new datasources", func() {
|
Convey("should have two new datasources", func() {
|
||||||
dc := newDatasourceConfiguration(logger, fakeCfg, fakeRepo)
|
dc := newDatasourceConfiguration(logger, fakeRepo)
|
||||||
err := dc.applyChanges("mock/config.yaml")
|
err := dc.applyChanges(twoDatasourcesConfig)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatalf("applyChanges return an error %v", err)
|
t.Fatalf("applyChanges return an error %v", err)
|
||||||
}
|
}
|
||||||
@ -141,14 +113,6 @@ type fakeRepository struct {
|
|||||||
loadAll []*models.DataSource
|
loadAll []*models.DataSource
|
||||||
}
|
}
|
||||||
|
|
||||||
type fakeConfig struct {
|
|
||||||
cfg *DatasourcesAsConfig
|
|
||||||
}
|
|
||||||
|
|
||||||
func (fc *fakeConfig) readConfig(path string) (*DatasourcesAsConfig, error) {
|
|
||||||
return fc.cfg, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func (fc *fakeRepository) delete(cmd *models.DeleteDataSourceByIdCommand) error {
|
func (fc *fakeRepository) delete(cmd *models.DeleteDataSourceByIdCommand) error {
|
||||||
fc.deleted = append(fc.deleted, cmd)
|
fc.deleted = append(fc.deleted, cmd)
|
||||||
return nil
|
return nil
|
||||||
|
@ -0,0 +1,12 @@
|
|||||||
|
purge_other_datasources: false
|
||||||
|
datasources:
|
||||||
|
- name: Graphite
|
||||||
|
type: graphite
|
||||||
|
access: proxy
|
||||||
|
url: http://localhost:8080
|
||||||
|
is_default: true
|
||||||
|
- name: Prometheus
|
||||||
|
type: prometheus
|
||||||
|
access: proxy
|
||||||
|
url: http://localhost:9090
|
||||||
|
is_default: true
|
@ -0,0 +1,10 @@
|
|||||||
|
purge_other_datasources: true
|
||||||
|
datasources:
|
||||||
|
- name: Graphite
|
||||||
|
type: graphite
|
||||||
|
access: proxy
|
||||||
|
url: http://localhost:8080
|
||||||
|
- name: Prometheus
|
||||||
|
type: prometheus
|
||||||
|
access: proxy
|
||||||
|
url: http://localhost:9090
|
10
pkg/setting/datasources/test-configs/two-datasources.yaml
Normal file
10
pkg/setting/datasources/test-configs/two-datasources.yaml
Normal file
@ -0,0 +1,10 @@
|
|||||||
|
purge_other_datasources: false
|
||||||
|
datasources:
|
||||||
|
- name: Graphite
|
||||||
|
type: graphite
|
||||||
|
access: proxy
|
||||||
|
url: http://localhost:8080
|
||||||
|
- name: Prometheus
|
||||||
|
type: prometheus
|
||||||
|
access: proxy
|
||||||
|
url: http://localhost:9090
|
@ -0,0 +1,2 @@
|
|||||||
|
purge_other_datasources: false
|
||||||
|
datasources:
|
@ -3,25 +3,29 @@ package datasources
|
|||||||
import "github.com/grafana/grafana/pkg/models"
|
import "github.com/grafana/grafana/pkg/models"
|
||||||
import "github.com/grafana/grafana/pkg/components/simplejson"
|
import "github.com/grafana/grafana/pkg/components/simplejson"
|
||||||
|
|
||||||
type DataSourceFromConfig struct {
|
type DatasourcesAsConfig struct {
|
||||||
Id int64
|
PurgeOtherDatasources bool `json:"purge_other_datasources" yaml:"purge_other_datasources"`
|
||||||
OrgId int64
|
Datasources []DataSourceFromConfig `json:"datasources" yaml:"datasources"`
|
||||||
Version int
|
}
|
||||||
|
|
||||||
Name string
|
type DataSourceFromConfig struct {
|
||||||
Type string
|
OrgId int64 `json:"org_id" yaml:"org_id"`
|
||||||
Access string
|
Version int `json:"version" yaml:"version"`
|
||||||
Url string
|
|
||||||
Password string
|
Name string `json:"name" yaml:"name"`
|
||||||
User string
|
Type string `json:"type" yaml:"type"`
|
||||||
Database string
|
Access string `json:"access" yaml:"access"`
|
||||||
BasicAuth bool
|
Url string `json:"url" yaml:"url"`
|
||||||
BasicAuthUser string
|
Password string `json:"password" yaml:"password"`
|
||||||
BasicAuthPassword string
|
User string `json:"user" yaml:"user"`
|
||||||
WithCredentials bool
|
Database string `json:"database" yaml:"database"`
|
||||||
IsDefault bool
|
BasicAuth bool `json:"basic_auth" yaml:"basic_auth"`
|
||||||
JsonData string
|
BasicAuthUser string `json:"basic_auth_user" yaml:"basic_auth_user"`
|
||||||
SecureJsonData map[string]string
|
BasicAuthPassword string `json:"basic_auth_password" yaml:"basic_auth_password"`
|
||||||
|
WithCredentials bool `json:"with_credentials" yaml:"with_credentials"`
|
||||||
|
IsDefault bool `json:"is_default" yaml:"is_default"`
|
||||||
|
JsonData string `json:"json_data" yaml:"json_data"`
|
||||||
|
SecureJsonData map[string]string `json:"secure_json_data" yaml:"secure_json_data"`
|
||||||
}
|
}
|
||||||
|
|
||||||
func createInsertCommand(ds DataSourceFromConfig) *models.AddDataSourceCommand {
|
func createInsertCommand(ds DataSourceFromConfig) *models.AddDataSourceCommand {
|
||||||
|
Loading…
Reference in New Issue
Block a user