Add test for bad configuration

This commit is contained in:
TwinProduction 2019-10-19 22:03:55 -04:00
parent df83060c30
commit 8e71338a39
2 changed files with 46 additions and 17 deletions

View File

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

View File

@ -6,8 +6,8 @@ import (
"time" "time"
) )
func TestParseConfigBytes(t *testing.T) { func TestParseAndValidateConfigBytes(t *testing.T) {
config := parseConfigBytes([]byte(` config, err := parseAndValidateConfigBytes([]byte(`
services: services:
- name: twinnation - name: twinnation
url: https://twinnation.org/actuator/health url: https://twinnation.org/actuator/health
@ -20,6 +20,9 @@ services:
- "$STATUS != 400" - "$STATUS != 400"
- "$STATUS != 500" - "$STATUS != 500"
`)) `))
if err != nil {
t.Error("No error should've been returned")
}
if len(config.Services) != 2 { if len(config.Services) != 2 {
t.Error("Should have returned two services") t.Error("Should have returned two services")
} }
@ -44,14 +47,17 @@ services:
} }
} }
func TestParseConfigBytesDefault(t *testing.T) { func TestParseAndValidateConfigBytesDefault(t *testing.T) {
config := parseConfigBytes([]byte(` config, err := parseAndValidateConfigBytes([]byte(`
services: services:
- name: twinnation - name: twinnation
url: https://twinnation.org/actuator/health url: https://twinnation.org/actuator/health
conditions: conditions:
- "$STATUS == 200" - "$STATUS == 200"
`)) `))
if err != nil {
t.Error("No error should've been returned")
}
if config.Services[0].Url != "https://twinnation.org/actuator/health" { if config.Services[0].Url != "https://twinnation.org/actuator/health" {
t.Errorf("URL should have been %s", "https://twinnation.org/actuator/health") t.Errorf("URL should have been %s", "https://twinnation.org/actuator/health")
} }
@ -59,3 +65,19 @@ services:
t.Errorf("Interval should have been %s, because it is the default value", 10*time.Second) t.Errorf("Interval should have been %s, because it is the default value", 10*time.Second)
} }
} }
func TestParseAndValidateBadConfigBytes(t *testing.T) {
_, err := parseAndValidateConfigBytes([]byte(`
badconfig:
- asdsa: w0w
usadasdrl: asdxzczxc
asdas:
- soup
`))
if err == nil {
t.Error("An error should've been returned")
}
if err != ErrNoServiceInConfig {
t.Error("The error returned should have been of type ErrNoServiceInConfig")
}
}