Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

config: not properly loaded error #140

Merged
merged 3 commits into from
Apr 18, 2016
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
47 changes: 44 additions & 3 deletions config/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,41 @@ 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)
}
Expand All @@ -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)
}
4 changes: 2 additions & 2 deletions utils/errors/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We try to declare the errors are locally as possible - it should be in config/config.go as this error is very specific and not meant to be re-used.

Also, I am not sure that everybody will understand that error message. Maybe something like could not load configuration: no database source specified would be more appropriate?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OK don't know for the locality of errors. I will move it.
Better message indeed. Thanks for the feedback

)

// ErrBadRequest occurs when a method has been passed an inappropriate argument.
Expand Down