2019-09-06 04:02:00 +00:00
|
|
|
package config
|
|
|
|
|
2019-09-09 01:07:08 +00:00
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
)
|
2019-09-06 04:02:00 +00:00
|
|
|
|
2019-10-20 02:03:55 +00:00
|
|
|
func TestParseAndValidateConfigBytes(t *testing.T) {
|
|
|
|
config, err := parseAndValidateConfigBytes([]byte(`
|
2019-09-06 04:02:00 +00:00
|
|
|
services:
|
|
|
|
- name: twinnation
|
|
|
|
url: https://twinnation.org/actuator/health
|
2019-09-09 01:07:08 +00:00
|
|
|
interval: 15s
|
2019-09-06 04:02:00 +00:00
|
|
|
conditions:
|
2019-12-04 22:27:27 +00:00
|
|
|
- "[STATUS] == 200"
|
2019-09-06 04:02:00 +00:00
|
|
|
- name: github
|
2019-09-09 01:07:08 +00:00
|
|
|
url: https://api.github.com/healthz
|
2019-09-06 04:02:00 +00:00
|
|
|
conditions:
|
2019-12-04 22:27:27 +00:00
|
|
|
- "[STATUS] != 400"
|
|
|
|
- "[STATUS] != 500"
|
2019-09-06 04:02:00 +00:00
|
|
|
`))
|
2019-10-20 02:03:55 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Error("No error should've been returned")
|
|
|
|
}
|
2019-09-06 04:02:00 +00:00
|
|
|
if len(config.Services) != 2 {
|
|
|
|
t.Error("Should have returned two services")
|
|
|
|
}
|
|
|
|
if config.Services[0].Url != "https://twinnation.org/actuator/health" {
|
|
|
|
t.Errorf("URL should have been %s", "https://twinnation.org/actuator/health")
|
|
|
|
}
|
2019-09-09 01:07:08 +00:00
|
|
|
if config.Services[1].Url != "https://api.github.com/healthz" {
|
|
|
|
t.Errorf("URL should have been %s", "https://api.github.com/healthz")
|
2019-09-06 04:02:00 +00:00
|
|
|
}
|
2019-09-09 01:07:08 +00:00
|
|
|
fmt.Println(config.Services[0].Interval)
|
|
|
|
if config.Services[0].Interval != 15*time.Second {
|
|
|
|
t.Errorf("Interval should have been %s", 15*time.Second)
|
2019-09-06 04:02:00 +00:00
|
|
|
}
|
2019-09-09 01:07:08 +00:00
|
|
|
if config.Services[1].Interval != 10*time.Second {
|
|
|
|
t.Errorf("Interval should have been %s, because it is the default value", 10*time.Second)
|
2019-09-06 04:02:00 +00:00
|
|
|
}
|
|
|
|
if len(config.Services[0].Conditions) != 1 {
|
|
|
|
t.Errorf("There should have been %d conditions", 1)
|
|
|
|
}
|
|
|
|
if len(config.Services[1].Conditions) != 2 {
|
|
|
|
t.Errorf("There should have been %d conditions", 2)
|
|
|
|
}
|
|
|
|
}
|
2019-09-10 02:21:18 +00:00
|
|
|
|
2019-10-20 02:03:55 +00:00
|
|
|
func TestParseAndValidateConfigBytesDefault(t *testing.T) {
|
|
|
|
config, err := parseAndValidateConfigBytes([]byte(`
|
2019-09-10 02:21:18 +00:00
|
|
|
services:
|
|
|
|
- name: twinnation
|
|
|
|
url: https://twinnation.org/actuator/health
|
|
|
|
conditions:
|
2019-12-04 22:27:27 +00:00
|
|
|
- "[STATUS] == 200"
|
2019-09-10 02:21:18 +00:00
|
|
|
`))
|
2019-10-20 02:03:55 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Error("No error should've been returned")
|
|
|
|
}
|
2019-11-16 20:47:56 +00:00
|
|
|
if config.Metrics {
|
|
|
|
t.Error("Metrics should've been false by default")
|
|
|
|
}
|
|
|
|
if config.Services[0].Url != "https://twinnation.org/actuator/health" {
|
|
|
|
t.Errorf("URL should have been %s", "https://twinnation.org/actuator/health")
|
|
|
|
}
|
|
|
|
if config.Services[0].Interval != 10*time.Second {
|
|
|
|
t.Errorf("Interval should have been %s, because it is the default value", 10*time.Second)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestParseAndValidateConfigBytesWithMetrics(t *testing.T) {
|
|
|
|
config, err := parseAndValidateConfigBytes([]byte(`
|
|
|
|
metrics: true
|
|
|
|
services:
|
|
|
|
- name: twinnation
|
|
|
|
url: https://twinnation.org/actuator/health
|
|
|
|
conditions:
|
2019-12-04 22:27:27 +00:00
|
|
|
- "[STATUS] == 200"
|
2019-11-16 20:47:56 +00:00
|
|
|
`))
|
|
|
|
if err != nil {
|
|
|
|
t.Error("No error should've been returned")
|
|
|
|
}
|
|
|
|
if !config.Metrics {
|
|
|
|
t.Error("Metrics should have been true")
|
|
|
|
}
|
2019-09-10 02:21:18 +00:00
|
|
|
if config.Services[0].Url != "https://twinnation.org/actuator/health" {
|
|
|
|
t.Errorf("URL should have been %s", "https://twinnation.org/actuator/health")
|
|
|
|
}
|
|
|
|
if config.Services[0].Interval != 10*time.Second {
|
|
|
|
t.Errorf("Interval should have been %s, because it is the default value", 10*time.Second)
|
|
|
|
}
|
|
|
|
}
|
2019-10-20 02:03:55 +00:00
|
|
|
|
|
|
|
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")
|
|
|
|
}
|
|
|
|
}
|