From af2c68863482ae9f93a2db1533be260468a6ea2d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?JG=C2=B2?= Date: Mon, 18 Apr 2016 19:11:44 +0200 Subject: [PATCH] config: not properly loaded error (#140) * config: not properly loaded error When a wrong config file was used, it result in a panic. Adding some check condition to validate the Unmarshaled configuration before using it fixes #134 * check if datasource is set * move error locally instead of utils/errors --- config/config.go | 9 +++++ config/config_test.go | 81 +++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 90 insertions(+) create mode 100644 config/config_test.go diff --git a/config/config.go b/config/config.go index 9a1bac71..7e45c47a 100644 --- a/config/config.go +++ b/config/config.go @@ -15,6 +15,7 @@ package config import ( + "errors" "io/ioutil" "os" "time" @@ -23,6 +24,9 @@ import ( "gopkg.in/yaml.v2" ) +// ErrDatasourceNotLoaded is returned when the datasource variable in the configuration file is not loaded properly +var ErrDatasourceNotLoaded = errors.New("could not load configuration: no database source specified") + // File represents a YAML configuration file that namespaces all Clair // configuration under the top-level "clair" key. type File struct { @@ -112,6 +116,11 @@ func Load(path string) (config *Config, err error) { } config = &cfgFile.Clair + if config.Database.Source == "" { + err = 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 new file mode 100644 index 00000000..dec03fd2 --- /dev/null +++ b/config/config_test.go @@ -0,0 +1,81 @@ +package config + +import ( + "io/ioutil" + "log" + "os" + "testing" + + "github.com/stretchr/testify/assert" +) + +const wrongConfig = ` +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 { + log.Fatal(err) + } + + defer os.Remove(tmpfile.Name()) // clean up + if _, err := tmpfile.Write([]byte(wrongConfig)); err != nil { + log.Fatal(err) + } + if err := tmpfile.Close(); err != nil { + log.Fatal(err) + } + + _, err = Load(tmpfile.Name()) + + assert.EqualError(t, err, ErrDatasourceNotLoaded.Error()) +} + +func TestLoad(t *testing.T) { + 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) +}