Skip to content

Commit

Permalink
api: fix migrate stanza initialization
Browse files Browse the repository at this point in the history
Fixes Migrate to be initialized like RescheduleStrategy.

Fixes #5477
  • Loading branch information
schmichael committed Apr 11, 2019
1 parent cfb1aa2 commit 5ea4382
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 7 deletions.
12 changes: 5 additions & 7 deletions api/tasks.go
Original file line number Diff line number Diff line change
Expand Up @@ -563,13 +563,11 @@ func (g *TaskGroup) Canonicalize(job *Job) {
}

// Merge with default reschedule policy
if *job.Type == "service" {
defaultMigrateStrategy := &MigrateStrategy{}
defaultMigrateStrategy.Canonicalize()
if g.Migrate != nil {
defaultMigrateStrategy.Merge(g.Migrate)
}
g.Migrate = defaultMigrateStrategy
if g.Migrate == nil && *job.Type == "service" {
g.Migrate = &MigrateStrategy{}
}
if g.Migrate != nil {
g.Migrate.Canonicalize()
}

var defaultRestartPolicy *RestartPolicy
Expand Down
44 changes: 44 additions & 0 deletions command/agent/job_endpoint_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import (
"github.com/hashicorp/nomad/nomad/structs"
"github.com/kr/pretty"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)

func TestHTTP_JobsList(t *testing.T) {
Expand Down Expand Up @@ -2034,3 +2035,46 @@ func TestJobs_ApiJobToStructsJob(t *testing.T) {
t.Fatalf("bad:\n%s", strings.Join(diff, "\n"))
}
}

// TestHTTP_JobValidate_SystemMigrate asserts that a system job with a migrate
// stanza fails to validate but does not panic (see #5477).
func TestHTTP_JobValidate_SystemMigrate(t *testing.T) {
t.Parallel()
httpTest(t, nil, func(s *TestAgent) {
// Create the job
job := &api.Job{
Region: helper.StringToPtr("global"),
Datacenters: []string{"dc1"},
ID: helper.StringToPtr("systemmigrate"),
Name: helper.StringToPtr("systemmigrate"),
TaskGroups: []*api.TaskGroup{
{Name: helper.StringToPtr("web")},
},

// System job...
Type: helper.StringToPtr("system"),

// ...with an empty migrate stanza
Migrate: &api.MigrateStrategy{},
}

args := api.JobValidateRequest{
Job: job,
WriteRequest: api.WriteRequest{Region: "global"},
}
buf := encodeReq(args)

// Make the HTTP request
req, err := http.NewRequest("PUT", "/v1/validate/job", buf)
require.NoError(t, err)
respW := httptest.NewRecorder()

// Make the request
obj, err := s.Server.ValidateJobRequest(respW, req)
require.NoError(t, err)

// Check the response
resp := obj.(structs.JobValidateResponse)
require.Contains(t, resp.Error, `Job type "system" does not allow migrate block`)
})
}

0 comments on commit 5ea4382

Please sign in to comment.