-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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
- Loading branch information
Showing
2 changed files
with
90 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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) | ||
} |