Skip to content

Commit

Permalink
Merge pull request #1912 from hashicorp/b-validation
Browse files Browse the repository at this point in the history
Change the 0 timeout, interval validation to be more user friendly
  • Loading branch information
dadgar authored Nov 1, 2016
2 parents 1ce7664 + 57c0de7 commit 3884ddc
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 8 deletions.
20 changes: 13 additions & 7 deletions nomad/structs/structs.go
Original file line number Diff line number Diff line change
Expand Up @@ -1785,15 +1785,19 @@ func (sc *ServiceCheck) Canonicalize(serviceName string) {
func (sc *ServiceCheck) validate() error {
switch strings.ToLower(sc.Type) {
case ServiceCheckTCP:
if sc.Timeout < minCheckTimeout {
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 < minCheckTimeout {
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:
Expand All @@ -1807,8 +1811,10 @@ func (sc *ServiceCheck) validate() error {
return fmt.Errorf(`invalid type (%+q), must be one of "http", "tcp", or "script" type`, sc.Type)
}

if sc.Interval < minCheckInterval {
return fmt.Errorf("interval (%v) can not be lower than %v", sc.Interval, minCheckInterval)
if sc.Interval == 0 {
return fmt.Errorf("missing required value interval. Interval cannot be less than %v", minCheckInterval)
} else if sc.Interval < minCheckInterval {
return fmt.Errorf("interval (%v) cannot be lower than %v", sc.Interval, minCheckInterval)
}

switch sc.InitialStatus {
Expand Down Expand Up @@ -2150,7 +2156,7 @@ func (t *Task) Validate(ephemeralDisk *EphemeralDisk) error {
if strings.ContainsAny(t.Name, `/\`) {
// We enforce this so that when creating the directory on disk it will
// not have any slashes.
mErr.Errors = append(mErr.Errors, errors.New("Task name can not include slashes"))
mErr.Errors = append(mErr.Errors, errors.New("Task name cannot include slashes"))
}
if t.Driver == "" {
mErr.Errors = append(mErr.Errors, errors.New("Missing task driver"))
Expand Down Expand Up @@ -2759,7 +2765,7 @@ func (ta *TaskArtifact) Validate() error {
if check, ok := ta.GetterOptions["checksum"]; ok {
check = strings.TrimSpace(check)
if check == "" {
mErr.Errors = append(mErr.Errors, fmt.Errorf("checksum value can not be empty"))
mErr.Errors = append(mErr.Errors, fmt.Errorf("checksum value cannot be empty"))
return mErr.ErrorOrNil()
}

Expand Down Expand Up @@ -2955,7 +2961,7 @@ func (v *Vault) Validate() error {
}

if len(v.Policies) == 0 {
return fmt.Errorf("Policy list can not be empty")
return fmt.Errorf("Policy list cannot be empty")
}

switch v.ChangeMode {
Expand Down
11 changes: 10 additions & 1 deletion nomad/structs/structs_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -500,6 +500,11 @@ func TestTask_Validate_Services(t *testing.T) {
Type: ServiceCheckTCP,
Timeout: 2 * time.Second,
},
{
Name: "check-name",
Type: ServiceCheckTCP,
Interval: 1 * time.Second,
},
},
}

Expand Down Expand Up @@ -536,7 +541,11 @@ func TestTask_Validate_Services(t *testing.T) {
t.Fatalf("err: %v", err)
}

if !strings.Contains(err.Error(), "interval (0s) can not be lower") {
if !strings.Contains(err.Error(), "missing required value interval") {
t.Fatalf("err: %v", err)
}

if !strings.Contains(err.Error(), "cannot be less than") {
t.Fatalf("err: %v", err)
}
}
Expand Down

0 comments on commit 3884ddc

Please sign in to comment.