open-consul/agent/structs/service_definition_test.go
preetapan f6066f8305 Fixes agent error handling when check definition is invalid. Distingu… (#3560)
* Fixes agent error handling when check definition is invalid. Distinguishes between empty checks vs invalid checks

* Made CheckTypes return Checks from service definition struct rather than a new copy, and other changes from code review. This also errors when json payload contains empty structs

* Simplify and improve validate method, and make sure that CheckTypes always returns a new copy of validated check definitions

* Tweaks some small style things and error messages.

* Updates the change log.
2017-10-10 16:54:06 -07:00

57 lines
1.3 KiB
Go

package structs
import (
"fmt"
"testing"
"time"
"github.com/pascaldekloe/goe/verify"
)
func TestAgentStructs_CheckTypes(t *testing.T) {
t.Parallel()
svc := new(ServiceDefinition)
// Singular Check field works
svc.Check = CheckType{
Script: "/foo/bar",
Interval: 10 * time.Second,
}
// Returns HTTP checks
svc.Checks = append(svc.Checks, &CheckType{
HTTP: "http://foo/bar",
Interval: 10 * time.Second,
})
// Returns Script checks
svc.Checks = append(svc.Checks, &CheckType{
Script: "/foo/bar",
Interval: 10 * time.Second,
})
// Returns TTL checks
svc.Checks = append(svc.Checks, &CheckType{
TTL: 10 * time.Second,
})
// Validate checks
cases := []struct {
in *CheckType
err error
desc string
}{
{&CheckType{HTTP: "http://foo/baz"}, fmt.Errorf("Interval must be > 0 for Script, HTTP, or TCP checks"), "Missing interval"},
{&CheckType{TTL: -1}, fmt.Errorf("TTL must be > 0 for TTL checks"), "Negative TTL"},
{&CheckType{TTL: 20 * time.Second, Interval: 10 * time.Second}, fmt.Errorf("Interval and TTL cannot both be specified"), "Interval and TTL both set"},
}
for _, tc := range cases {
svc.Check = *tc.in
checks, err := svc.CheckTypes()
verify.Values(t, tc.desc, err.Error(), tc.err.Error())
if len(checks) != 0 {
t.Fatalf("bad: %#v", svc)
}
}
}