Commit ba4bbd1d by bergquist

datasource as cfg: refactor tests to use yaml files

parent 3f0eb523
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: {}
package datasources package datasources
import ( import (
"errors"
"io/ioutil" "io/ioutil"
"path/filepath" "path/filepath"
...@@ -11,10 +12,9 @@ import ( ...@@ -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 { ...@@ -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 { ...@@ -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 { ...@@ -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 { ...@@ -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 ( ...@@ -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 { 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, 0) So(len(fakeRepo.inserted), ShouldEqual, 1)
So(len(fakeRepo.updated), ShouldEqual, 1) So(len(fakeRepo.updated), ShouldEqual, 1)
}) })
}) })
Convey("One datasource in database with new name", func() { Convey("Two datasources with is_default", func() {
fakeRepo.loadAll = []*models.DataSource{ dc := newDatasourceConfiguration(logger, fakeRepo)
{Name: "old-graphite", OrgId: 1, Id: 1}, err := dc.applyChanges(doubleDatasourcesConfig)
} Convey("should raise error", func() {
So(err, ShouldEqual, ErrInvalidConfigToManyDefault)
Convey("should update one datasource", func() {
dc := newDatasourceConfiguration(logger, fakeCfg, fakeRepo)
err := dc.applyChanges("mock/config.yaml")
if err != nil {
t.Fatalf("applyChanges return an error %v", err)
}
So(len(fakeRepo.deleted), ShouldEqual, 0)
So(len(fakeRepo.inserted), ShouldEqual, 1)
So(len(fakeRepo.updated), ShouldEqual, 0)
}) })
}) })
}) })
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) { ...@@ -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) { ...@@ -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 { ...@@ -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
......
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
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
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
purge_other_datasources: false
datasources:
...@@ -3,25 +3,29 @@ package 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 DatasourcesAsConfig struct {
PurgeOtherDatasources bool `json:"purge_other_datasources" yaml:"purge_other_datasources"`
Datasources []DataSourceFromConfig `json:"datasources" yaml:"datasources"`
}
type DataSourceFromConfig struct { type DataSourceFromConfig struct {
Id int64 OrgId int64 `json:"org_id" yaml:"org_id"`
OrgId int64 Version int `json:"version" yaml:"version"`
Version int
Name string Name string `json:"name" yaml:"name"`
Type string Type string `json:"type" yaml:"type"`
Access string Access string `json:"access" yaml:"access"`
Url string Url string `json:"url" yaml:"url"`
Password string Password string `json:"password" yaml:"password"`
User string User string `json:"user" yaml:"user"`
Database string Database string `json:"database" yaml:"database"`
BasicAuth bool BasicAuth bool `json:"basic_auth" yaml:"basic_auth"`
BasicAuthUser string BasicAuthUser string `json:"basic_auth_user" yaml:"basic_auth_user"`
BasicAuthPassword string BasicAuthPassword string `json:"basic_auth_password" yaml:"basic_auth_password"`
WithCredentials bool WithCredentials bool `json:"with_credentials" yaml:"with_credentials"`
IsDefault bool IsDefault bool `json:"is_default" yaml:"is_default"`
JsonData string JsonData string `json:"json_data" yaml:"json_data"`
SecureJsonData map[string]string SecureJsonData map[string]string `json:"secure_json_data" yaml:"secure_json_data"`
} }
func createInsertCommand(ds DataSourceFromConfig) *models.AddDataSourceCommand { func createInsertCommand(ds DataSourceFromConfig) *models.AddDataSourceCommand {
......
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