From 73b5c37da7e45b5d8b3ca85db31c9b62e236e8cf Mon Sep 17 00:00:00 2001 From: jgsqware Date: Mon, 18 Apr 2016 07:44:02 +0200 Subject: [PATCH] check if datasource is set --- config/config.go | 6 ++++-- config/config_test.go | 47 +++++++++++++++++++++++++++++++++++++++--- utils/errors/errors.go | 4 ++-- 3 files changed, 50 insertions(+), 7 deletions(-) diff --git a/config/config.go b/config/config.go index d2921f00..f0260ef5 100644 --- a/config/config.go +++ b/config/config.go @@ -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 diff --git a/config/config_test.go b/config/config_test.go index 958d7e99..abea170c 100644 --- a/config/config_test.go +++ b/config/config_test.go @@ -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) } diff --git a/utils/errors/errors.go b/utils/errors/errors.go index ed4234da..00b7b089 100644 --- a/utils/errors/errors.go +++ b/utils/errors/errors.go @@ -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.