check if datasource is set

This commit is contained in:
jgsqware 2016-04-18 07:44:02 +02:00
parent 34e99466a6
commit 73b5c37da7
3 changed files with 50 additions and 7 deletions

View File

@ -112,10 +112,12 @@ func Load(path string) (config *Config, err error) {
return
}
config = &cfgFile.Clair
if config.API == nil || config.Database == nil || config.Notifier == nil || config.Updater == nil {
err = cerrors.ErrConfigNotLoaded
if config.Database.Source == "" {
err = cerrors.ErrDatasourceNotLoaded
return
}
// Generate a pagination key if none is provided.
if config.API.PaginationKey == "" {
var key fernet.Key

View File

@ -15,6 +15,34 @@ dummyKey:
wrong:true
`
const goodConfig = `
clair:
database:
source: postgresql://postgres:root@postgres:5432?sslmode=disable
cacheSize: 16384
api:
port: 6060
healthport: 6061
timeout: 900s
paginationKey:
servername:
cafile:
keyfile:
certfile:
updater:
interval: 2h
notifier:
attempts: 3
renotifyInterval: 2h
http:
endpoint:
servername:
cafile:
keyfile:
certfile:
proxy:
`
func TestLoadWrongConfiguration(t *testing.T) {
tmpfile, err := ioutil.TempFile("", "clair-config")
if err != nil {
@ -22,7 +50,6 @@ func TestLoadWrongConfiguration(t *testing.T) {
}
defer os.Remove(tmpfile.Name()) // clean up
if _, err := tmpfile.Write([]byte(wrongConfig)); err != nil {
log.Fatal(err)
}
@ -32,10 +59,24 @@ func TestLoadWrongConfiguration(t *testing.T) {
_, err = Load(tmpfile.Name())
assert.EqualError(t, err, cerrors.ErrConfigNotLoaded.Error())
assert.EqualError(t, err, cerrors.ErrDatasourceNotLoaded.Error())
}
func TestLoad(t *testing.T) {
_, err := Load("../config.example.yaml")
tmpfile, err := ioutil.TempFile("", "clair-config")
if err != nil {
log.Fatal(err)
}
defer os.Remove(tmpfile.Name()) // clean up
if _, err := tmpfile.Write([]byte(goodConfig)); err != nil {
log.Fatal(err)
}
if err := tmpfile.Close(); err != nil {
log.Fatal(err)
}
_, err = Load(tmpfile.Name())
assert.NoError(t, err)
}

View File

@ -30,8 +30,8 @@ var (
// ErrCouldNotParse is returned when a fetcher fails to parse the update data.
ErrCouldNotParse = errors.New("updater/fetchers: could not parse")
//ErrConfigNotLoaded is returned when the configuration file is not loaded properly
ErrConfigNotLoaded = errors.New("could not load configuration properly")
// ErrDatasourceNotLoaded is returned when the datasource variable in the configuration file is not loaded properly
ErrDatasourceNotLoaded = errors.New("could not load datasource configuration properly")
)
// ErrBadRequest occurs when a method has been passed an inappropriate argument.