Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
N
nexpie-grafana-theme
Overview
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Registry
Registry
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Kornkitt Poolsup
nexpie-grafana-theme
Commits
ba4bbd1d
Commit
ba4bbd1d
authored
Oct 21, 2017
by
bergquist
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
datasource as cfg: refactor tests to use yaml files
parent
3f0eb523
Hide whitespace changes
Inline
Side-by-side
Showing
8 changed files
with
116 additions
and
120 deletions
+116
-120
conf/datasources.yaml
+12
-22
pkg/setting/datasources/datasources.go
+25
-21
pkg/setting/datasources/datasources_test.go
+24
-60
pkg/setting/datasources/test-configs/double-default-datasources.yaml
+12
-0
pkg/setting/datasources/test-configs/two-datasources-purge-others.yaml
+10
-0
pkg/setting/datasources/test-configs/two-datasources.yaml
+10
-0
pkg/setting/datasources/test-configs/zero-datasources.yaml
+2
-0
pkg/setting/datasources/types.go
+21
-17
No files found.
conf/datasources.yaml
View file @
ba4bbd1d
purge
OtherD
atasources
:
false
purge
_other_d
atasources
:
false
datasources
:
datasources
:
-
name
:
Graphite
202
-
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
basic
Auth
:
basic
_auth
:
#bool
basic
AuthUser
:
basic
_authUser
:
#string
basic
AuthPassword
:
basic
_auth_password
:
#string
with
Credentials
:
with
_credentials
:
#bool
is
Default
:
true
is
_default
:
true
#bool
json
Data
:
{}
json
_data
:
'
{"graphiteVersion":"0.9"}'
# string json
secure
JsonFields
:
{}
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
:
{}
pkg/setting/datasources/datasources.go
View file @
ba4bbd1d
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
:
c
fgProvider
,
cfgProvider
:
c
onfigProvider
{}
,
}
}
}
}
...
@@ -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
diskConfigRea
der
struct
{}
type
configProvi
der
struct
{}
func
(
diskConfigRea
der
)
readConfig
(
path
string
)
(
*
DatasourcesAsConfig
,
error
)
{
func
(
configProvi
der
)
readConfig
(
path
string
)
(
*
DatasourcesAsConfig
,
error
)
{
filename
,
_
:=
filepath
.
Abs
(
path
)
filename
,
_
:=
filepath
.
Abs
(
path
)
yamlFile
,
err
:=
ioutil
.
ReadFile
(
filename
)
yamlFile
,
err
:=
ioutil
.
ReadFile
(
filename
)
...
...
pkg/setting/datasources/datasources_test.go
View file @
ba4bbd1d
...
@@ -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
,
fake
Cfg
,
fake
Repo
)
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
:
"
g
raphite"
,
OrgId
:
1
,
Id
:
1
},
{
Name
:
"
G
raphite"
,
OrgId
:
1
,
Id
:
1
},
}
}
Convey
(
"should update one datasource"
,
func
()
{
Convey
(
"should update one datasource"
,
func
()
{
dc
:=
newDatasourceConfiguration
(
logger
,
fake
Cfg
,
fake
Repo
)
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
,
fake
Cfg
,
fake
Repo
)
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
:
"
g
raphite"
,
OrgId
:
1
,
Id
:
1
},
{
Name
:
"
G
raphite"
,
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
,
fake
Cfg
,
fake
Repo
)
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
...
...
pkg/setting/datasources/test-configs/double-default-datasources.yaml
0 → 100644
View file @
ba4bbd1d
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
pkg/setting/datasources/test-configs/two-datasources-purge-others.yaml
0 → 100644
View file @
ba4bbd1d
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
pkg/setting/datasources/test-configs/two-datasources.yaml
0 → 100644
View file @
ba4bbd1d
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
pkg/setting/datasources/test-configs/zero-datasources.yaml
0 → 100644
View file @
ba4bbd1d
purge_other_datasources
:
false
datasources
:
pkg/setting/datasources/types.go
View file @
ba4bbd1d
...
@@ -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
{
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment