Skip to content

Commit

Permalink
Invert and test CheckRestart merge logic
Browse files Browse the repository at this point in the history
  • Loading branch information
schmichael committed Jan 9, 2018
1 parent c341626 commit 84a1013
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 8 deletions.
14 changes: 6 additions & 8 deletions api/tasks.go
Original file line number Diff line number Diff line change
Expand Up @@ -128,15 +128,15 @@ func (c *CheckRestart) Merge(o *CheckRestart) *CheckRestart {
return nc
}

if nc.Limit == 0 {
if o.Limit > 0 {
nc.Limit = o.Limit
}

if nc.Grace == nil {
if o.Grace != nil {
nc.Grace = o.Grace
}

if !nc.IgnoreWarnings {
if o.IgnoreWarnings {
nc.IgnoreWarnings = o.IgnoreWarnings
}

Expand Down Expand Up @@ -185,13 +185,11 @@ func (s *Service) Canonicalize(t *Task, tg *TaskGroup, job *Job) {
s.AddressMode = "auto"
}

s.CheckRestart.Canonicalize()

// Canonicallize CheckRestart on Checks and merge Service.CheckRestart
// into each check.
for i, c := range s.Checks {
s.Checks[i].CheckRestart = c.CheckRestart.Merge(s.CheckRestart)
c.CheckRestart.Canonicalize()
for i, check := range s.Checks {
s.Checks[i].CheckRestart = s.CheckRestart.Merge(check.CheckRestart)
s.Checks[i].CheckRestart.Canonicalize()
}
}

Expand Down
49 changes: 49 additions & 0 deletions api/tasks_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package api
import (
"reflect"
"testing"
"time"

"github.com/hashicorp/nomad/helper"
"github.com/stretchr/testify/assert"
Expand Down Expand Up @@ -266,3 +267,51 @@ func TestTaskGroup_Canonicalize_Update(t *testing.T) {
tg.Canonicalize(job)
assert.Nil(t, tg.Update)
}

// TestService_CheckRestart asserts Service.CheckRestart settings are properly
// inherited by Checks.
func TestService_CheckRestart(t *testing.T) {
job := &Job{Name: helper.StringToPtr("job")}
tg := &TaskGroup{Name: helper.StringToPtr("group")}
task := &Task{Name: "task"}
service := &Service{
CheckRestart: &CheckRestart{
Limit: 11,
Grace: helper.TimeToPtr(11 * time.Second),
IgnoreWarnings: true,
},
Checks: []ServiceCheck{
{
Name: "all-set",
CheckRestart: &CheckRestart{
Limit: 22,
Grace: helper.TimeToPtr(22 * time.Second),
IgnoreWarnings: true,
},
},
{
Name: "some-set",
CheckRestart: &CheckRestart{
Limit: 33,
Grace: helper.TimeToPtr(33 * time.Second),
},
},
{
Name: "unset",
},
},
}

service.Canonicalize(task, tg, job)
assert.Equal(t, service.Checks[0].CheckRestart.Limit, 22)
assert.Equal(t, *service.Checks[0].CheckRestart.Grace, 22*time.Second)
assert.True(t, service.Checks[0].CheckRestart.IgnoreWarnings)

assert.Equal(t, service.Checks[1].CheckRestart.Limit, 33)
assert.Equal(t, *service.Checks[1].CheckRestart.Grace, 33*time.Second)
assert.True(t, service.Checks[1].CheckRestart.IgnoreWarnings)

assert.Equal(t, service.Checks[2].CheckRestart.Limit, 11)
assert.Equal(t, *service.Checks[2].CheckRestart.Grace, 11*time.Second)
assert.True(t, service.Checks[2].CheckRestart.IgnoreWarnings)
}

0 comments on commit 84a1013

Please sign in to comment.