Skip to content
This repository was archived by the owner on Nov 28, 2022. It is now read-only.

Commit a7ccd48

Browse files
Diogo Nicoletidiogonicoleti
authored andcommitted
Refactory config code to add validation
1 parent 6965864 commit a7ccd48

9 files changed

+96
-9
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
vendor
2+
coverage.out
23
test_config.yml
34
postgresql_exporter
45
dist

config/config.go

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
package config
22

33
import (
4+
"errors"
5+
"fmt"
46
"io/ioutil"
57

6-
"github.com/apex/log"
78
"gopkg.in/yaml.v2"
89
)
910

@@ -16,25 +17,34 @@ type Config struct {
1617
Databases []Database `yaml:"databases,omitempty"`
1718
}
1819

19-
func Parse(path string) Config {
20+
func Parse(path string) (Config, error) {
2021
var cfg Config
2122
bts, err := ioutil.ReadFile(path)
2223
if err != nil {
23-
log.WithError(err).Fatalf("failed to read config file: %s", path)
24+
return cfg, fmt.Errorf("failed to read config file '%s': %s", path, err)
2425
}
2526
if err := yaml.Unmarshal(bts, &cfg); err != nil {
26-
log.WithError(err).Fatalf("failed to unmarshall config file: %s", path)
27+
return cfg, fmt.Errorf("failed to unmarshall config file '%s': %s", path, err)
2728
}
28-
return validate(cfg)
29+
if err := validate(cfg); err != nil {
30+
return cfg, err
31+
}
32+
return cfg, nil
2933
}
3034

31-
func validate(config Config) Config {
35+
func validate(config Config) error {
3236
names := make(map[string]bool)
3337
for _, conf := range config.Databases {
38+
if conf.Name == "" {
39+
return errors.New("failed to validate configuration. Database name cannot be empty")
40+
}
41+
if conf.URL == "" {
42+
return fmt.Errorf("failed to validate configuration. URL for database '%s' cannot be empty", conf.Name)
43+
}
3444
if names[conf.Name] {
35-
log.Fatalf("failed to validate configuration. A database named '%s' has already been declared'", conf.Name)
45+
return fmt.Errorf("failed to validate configuration. A database named '%s' has already been declared", conf.Name)
3646
}
3747
names[conf.Name] = true
3848
}
39-
return config
49+
return nil
4050
}

config/config_test.go

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
package config
2+
3+
import (
4+
"testing"
5+
)
6+
7+
func TestParseConfig(t *testing.T) {
8+
_, err := Parse("testdata/valid-config.yml")
9+
if err != nil {
10+
t.Errorf("Error loading config: %v", err)
11+
}
12+
}
13+
14+
func TestParseBadConfigs(t *testing.T) {
15+
tests := []struct {
16+
ConfigFile string
17+
ExpectedError string
18+
}{
19+
{
20+
ConfigFile: "testdata/unknown-config.yml",
21+
ExpectedError: "failed to read config file 'testdata/unknown-config.yml': open testdata/unknown-config.yml: no such file or directory",
22+
},
23+
{
24+
ConfigFile: "testdata/invalid-duplicated-config.yml",
25+
ExpectedError: "failed to validate configuration. A database named 'dba' has already been declared",
26+
},
27+
{
28+
ConfigFile: "testdata/invalid-empty-name-config.yml",
29+
ExpectedError: "failed to validate configuration. Database name cannot be empty",
30+
},
31+
{
32+
ConfigFile: "testdata/invalid-empty-url-config.yml",
33+
ExpectedError: "failed to validate configuration. URL for database 'dba' cannot be empty",
34+
},
35+
{
36+
ConfigFile: "testdata/invalid-yaml-config.yml",
37+
ExpectedError: "failed to unmarshall config file 'testdata/invalid-yaml-config.yml': yaml: unmarshal errors:\n line 1: cannot unmarshal !!seq into config.Config",
38+
},
39+
}
40+
41+
for _, test := range tests {
42+
_, err := Parse(test.ConfigFile)
43+
if err == nil {
44+
t.Errorf("In case %v:\nExpected: %v\nGot: nil", test.ConfigFile, test.ExpectedError)
45+
continue
46+
}
47+
if err.Error() != test.ExpectedError {
48+
t.Errorf("In case %v:\nExpected: %v\nGot: %v", test.ConfigFile, test.ExpectedError, err.Error())
49+
}
50+
}
51+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
databases:
2+
- name: dba
3+
url: postgres://localhost:5432/dba?sslmode=disable
4+
- name: dba
5+
url: postgres://user:[email protected]:5432/dbb
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
databases:
2+
- name: dba
3+
url: postgres://localhost:5432/dba?sslmode=disable
4+
- url: postgres://user:[email protected]:5432/dbb
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
databases:
2+
- name: dba
3+
- name: dbb
4+
url: postgres://user:[email protected]:5432/dbb
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
- name: dba
2+
url: postgres://localhost:5432/dba?sslmode=disable

config/testdata/valid-config.yml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
databases:
2+
- name: dba
3+
url: postgres://localhost:5432/dba?sslmode=disable
4+
- name: dbb
5+
url: postgres://user:[email protected]:5432/dbb

main.go

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,12 @@ func main() {
4949
`))
5050
})
5151
http.Handle("/metrics", promhttp.Handler())
52-
var config = config.Parse(*configFile)
52+
53+
var config, err = config.Parse(*configFile)
54+
if err != nil {
55+
log.WithError(err).Fatalf("error to parse config file")
56+
}
57+
5358
for _, con := range config.Databases {
5459
var log = log.WithField("db", con.Name)
5560
log.Info("started monitoring")

0 commit comments

Comments
 (0)