Add some config tests
Still some more to add
This commit is contained in:
parent
71574dd8a9
commit
5c5388d683
34
config.go
34
config.go
@ -1,6 +1,7 @@
|
|||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"errors"
|
||||||
"gopkg.in/yaml.v2"
|
"gopkg.in/yaml.v2"
|
||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
"log"
|
"log"
|
||||||
@ -17,6 +18,12 @@ type Config struct {
|
|||||||
// IsValid checks config validity and returns true if valid
|
// IsValid checks config validity and returns true if valid
|
||||||
func (config Config) IsValid() (isValid bool) {
|
func (config Config) IsValid() (isValid bool) {
|
||||||
isValid = true
|
isValid = true
|
||||||
|
|
||||||
|
// Validate monitors
|
||||||
|
if config.Monitors == nil || len(config.Monitors) == 0 {
|
||||||
|
log.Printf("ERROR: Invalid monitor configuration: Must provide at least one monitor")
|
||||||
|
isValid = false
|
||||||
|
}
|
||||||
for _, monitor := range config.Monitors {
|
for _, monitor := range config.Monitors {
|
||||||
if !monitor.IsValid() {
|
if !monitor.IsValid() {
|
||||||
log.Printf("ERROR: Invalid monitor configuration: %s", monitor.Name)
|
log.Printf("ERROR: Invalid monitor configuration: %s", monitor.Name)
|
||||||
@ -24,6 +31,11 @@ func (config Config) IsValid() (isValid bool) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Validate alerts
|
||||||
|
if config.Alerts == nil || len(config.Alerts) == 0 {
|
||||||
|
log.Printf("ERROR: Invalid alert configuration: Must provide at least one alert")
|
||||||
|
isValid = false
|
||||||
|
}
|
||||||
for _, alert := range config.Alerts {
|
for _, alert := range config.Alerts {
|
||||||
if !alert.IsValid() {
|
if !alert.IsValid() {
|
||||||
log.Printf("ERROR: Invalid alert configuration: %s", alert.Name)
|
log.Printf("ERROR: Invalid alert configuration: %s", alert.Name)
|
||||||
@ -35,38 +47,40 @@ func (config Config) IsValid() (isValid bool) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Init performs extra initialization on top of loading the config from file
|
// Init performs extra initialization on top of loading the config from file
|
||||||
func (config *Config) Init() {
|
func (config *Config) Init() (err error) {
|
||||||
for name, alert := range config.Alerts {
|
for name, alert := range config.Alerts {
|
||||||
alert.Name = name
|
alert.Name = name
|
||||||
if err := alert.BuildTemplates(); err != nil {
|
if err = alert.BuildTemplates(); err != nil {
|
||||||
panic(err)
|
return
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
// LoadConfig will read config from the given path and parse it
|
// LoadConfig will read config from the given path and parse it
|
||||||
func LoadConfig(filePath string) (config Config) {
|
func LoadConfig(filePath string) (config Config, err error) {
|
||||||
data, err := ioutil.ReadFile(filePath)
|
data, err := ioutil.ReadFile(filePath)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
panic(err)
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
// TODO: Decide if this is better expanded here, or only when executing
|
// TODO: Decide if this is better expanded here, or only when executing
|
||||||
envExpanded := os.ExpandEnv(string(data))
|
envExpanded := os.ExpandEnv(string(data))
|
||||||
err = yaml.Unmarshal([]byte(envExpanded), &config)
|
err = yaml.Unmarshal([]byte(envExpanded), &config)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatalf("ERROR: %v", err)
|
return
|
||||||
panic(err)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
log.Printf("config:\n%v\n", config)
|
log.Printf("config:\n%v\n", config)
|
||||||
|
|
||||||
if !config.IsValid() {
|
if !config.IsValid() {
|
||||||
panic("Cannot continue with invalid configuration")
|
err = errors.New("Invalid configuration")
|
||||||
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
// Finish initializing configuration
|
// Finish initializing configuration
|
||||||
config.Init()
|
err = config.Init()
|
||||||
|
|
||||||
return config
|
return
|
||||||
}
|
}
|
||||||
|
30
config_test.go
Normal file
30
config_test.go
Normal file
@ -0,0 +1,30 @@
|
|||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"log"
|
||||||
|
"testing"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestLoadConfig(t *testing.T) {
|
||||||
|
cases := []struct {
|
||||||
|
configPath string
|
||||||
|
expectErr bool
|
||||||
|
name string
|
||||||
|
}{
|
||||||
|
{"./test/valid-config.yml", false, "Valid config file"},
|
||||||
|
{"./test/does-not-exist", true, "Invalid config path"},
|
||||||
|
{"./test/invalid-config-type.yml", true, "Invalid config type for key"},
|
||||||
|
{"./test/invalid-config-missing-alerts.yml", true, "Invalid config missing alerts"},
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, c := range cases {
|
||||||
|
log.Printf("Testing case %s", c.name)
|
||||||
|
_, err := LoadConfig(c.configPath)
|
||||||
|
hasErr := (err != nil)
|
||||||
|
if hasErr != c.expectErr {
|
||||||
|
t.Errorf("LoadConfig(%v), expected=%v actual=%v", c.name, "Err", err)
|
||||||
|
log.Printf("Case failed: %s", c.name)
|
||||||
|
}
|
||||||
|
log.Println("-----")
|
||||||
|
}
|
||||||
|
}
|
5
main.go
5
main.go
@ -6,7 +6,10 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
config := LoadConfig("config.yml")
|
config, err := LoadConfig("config.yml")
|
||||||
|
if err != nil {
|
||||||
|
log.Fatalf("Error loading config: %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
for {
|
for {
|
||||||
for _, monitor := range config.Monitors {
|
for _, monitor := range config.Monitors {
|
||||||
|
9
test/invalid-config-missing-alerts.yml
Normal file
9
test/invalid-config-missing-alerts.yml
Normal file
@ -0,0 +1,9 @@
|
|||||||
|
check_interval: 1
|
||||||
|
|
||||||
|
monitors:
|
||||||
|
- name: Command
|
||||||
|
command: ['echo', '$PATH']
|
||||||
|
alert_down: [ 'alert_down', 'log_shell', 'log_command' ]
|
||||||
|
# alert_every: -1
|
||||||
|
alert_every: 0
|
||||||
|
|
1
test/invalid-config-type.yml
Normal file
1
test/invalid-config-type.yml
Normal file
@ -0,0 +1 @@
|
|||||||
|
check_interval: woops, I'm not an int!
|
22
test/valid-config.yml
Normal file
22
test/valid-config.yml
Normal file
@ -0,0 +1,22 @@
|
|||||||
|
check_interval: 1
|
||||||
|
|
||||||
|
monitors:
|
||||||
|
- name: Command
|
||||||
|
command: ['echo', '$PATH']
|
||||||
|
alert_down: [ 'log_command', 'log_shell' ]
|
||||||
|
alert_every: 0
|
||||||
|
- name: Shell
|
||||||
|
command_shell: >
|
||||||
|
echo 'Some string with stuff';
|
||||||
|
echo 'another line';
|
||||||
|
echo $PATH;
|
||||||
|
exit 1
|
||||||
|
alert_down: [ 'log_command', 'log_shell' ]
|
||||||
|
alert_after: 5
|
||||||
|
alert_every: 0
|
||||||
|
|
||||||
|
alerts:
|
||||||
|
log_command:
|
||||||
|
command: [ 'echo', 'regular', '"command!!!"', "{{.MonitorName}}" ]
|
||||||
|
log_shell:
|
||||||
|
command_shell: echo "Failure on {{.MonitorName}} User is $USER"
|
Loading…
Reference in New Issue
Block a user