Fix timeout validation for script checks

It appears this was disabled a year ago when defaulting and validation
code was probably different: 91bbc5c3c7

Fixes an issue reported via the mailing list:
https://groups.google.com/d/msg/nomad-tool/yiPCTfOpYn4/yv7MMPUABQAJ
This commit is contained in:
Michael Schurter
2017-08-14 10:23:52 -07:00
parent 2a0f267771
commit c1091edc98
2 changed files with 19 additions and 15 deletions

View File

@@ -2696,28 +2696,15 @@ func (sc *ServiceCheck) Canonicalize(serviceName string) {
func (sc *ServiceCheck) validate() error {
switch strings.ToLower(sc.Type) {
case ServiceCheckTCP:
if sc.Timeout == 0 {
return fmt.Errorf("missing required value timeout. Timeout cannot be less than %v", minCheckInterval)
} else if sc.Timeout < minCheckTimeout {
return fmt.Errorf("timeout (%v) is lower than required minimum timeout %v", sc.Timeout, minCheckInterval)
}
case ServiceCheckHTTP:
if sc.Path == "" {
return fmt.Errorf("http type must have a valid http path")
}
if sc.Timeout == 0 {
return fmt.Errorf("missing required value timeout. Timeout cannot be less than %v", minCheckInterval)
} else if sc.Timeout < minCheckTimeout {
return fmt.Errorf("timeout (%v) is lower than required minimum timeout %v", sc.Timeout, minCheckInterval)
}
case ServiceCheckScript:
if sc.Command == "" {
return fmt.Errorf("script type must have a valid script path")
}
// TODO: enforce timeout on the Client side and reenable
// validation.
default:
return fmt.Errorf(`invalid type (%+q), must be one of "http", "tcp", or "script" type`, sc.Type)
}
@@ -2728,6 +2715,12 @@ func (sc *ServiceCheck) validate() error {
return fmt.Errorf("interval (%v) cannot be lower than %v", sc.Interval, minCheckInterval)
}
if sc.Timeout == 0 {
return fmt.Errorf("missing required value timeout. Timeout cannot be less than %v", minCheckInterval)
} else if sc.Timeout < minCheckTimeout {
return fmt.Errorf("timeout (%v) is lower than required minimum timeout %v", sc.Timeout, minCheckInterval)
}
switch sc.InitialStatus {
case "":
// case api.HealthUnknown: TODO: Add when Consul releases 0.7.1

View File

@@ -1084,6 +1084,18 @@ func TestTask_Validate_Services(t *testing.T) {
func TestTask_Validate_Service_Check(t *testing.T) {
invalidCheck := ServiceCheck{
Name: "check-name",
Command: "/bin/true",
Type: ServiceCheckScript,
Interval: 10 * time.Second,
}
err := invalidCheck.validate()
if err == nil || !strings.Contains(err.Error(), "Timeout cannot be less") {
t.Fatalf("expected a timeout validation error but received: %q", err)
}
check1 := ServiceCheck{
Name: "check-name",
Type: ServiceCheckTCP,
@@ -1091,8 +1103,7 @@ func TestTask_Validate_Service_Check(t *testing.T) {
Timeout: 2 * time.Second,
}
err := check1.validate()
if err != nil {
if err := check1.validate(); err != nil {
t.Fatalf("err: %v", err)
}