Add test for bad configuration
This commit is contained in:
@ -1,6 +1,7 @@
|
||||
package config
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"github.com/TwinProduction/gatus/core"
|
||||
"gopkg.in/yaml.v2"
|
||||
"io/ioutil"
|
||||
@ -11,7 +12,10 @@ type Config struct {
|
||||
Services []*core.Service `yaml:"services"`
|
||||
}
|
||||
|
||||
var config *Config
|
||||
var (
|
||||
ErrNoServiceInConfig = errors.New("configuration file should contain at least 1 service")
|
||||
config *Config
|
||||
)
|
||||
|
||||
func Get() *Config {
|
||||
if config == nil {
|
||||
@ -24,23 +28,26 @@ func Get() *Config {
|
||||
return config
|
||||
}
|
||||
|
||||
func readConfigurationFile(fileName string) (*Config, error) {
|
||||
if bytes, err := ioutil.ReadFile(fileName); err == nil {
|
||||
func readConfigurationFile(fileName string) (config *Config, err error) {
|
||||
var bytes []byte
|
||||
if bytes, err = ioutil.ReadFile(fileName); err == nil {
|
||||
// file exists, so we'll parse it and return it
|
||||
return parseConfigBytes(bytes)
|
||||
} else {
|
||||
return nil, err
|
||||
return parseAndValidateConfigBytes(bytes)
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
func parseConfigBytes(yamlBytes []byte) (config *Config, err error) {
|
||||
func parseAndValidateConfigBytes(yamlBytes []byte) (config *Config, err error) {
|
||||
err = yaml.Unmarshal(yamlBytes, &config)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
for _, service := range config.Services {
|
||||
if service.Interval == 0 {
|
||||
service.Interval = 10 * time.Second
|
||||
// Check if the configuration file at least has services.
|
||||
if config == nil || len(config.Services) == 0 {
|
||||
err = ErrNoServiceInConfig
|
||||
} else {
|
||||
// Set the default values if they aren't set
|
||||
for _, service := range config.Services {
|
||||
if service.Interval == 0 {
|
||||
service.Interval = 10 * time.Second
|
||||
}
|
||||
}
|
||||
}
|
||||
return
|
||||
|
Reference in New Issue
Block a user