Build new env from new alloc before interpolating

This commit is contained in:
Michael Schurter 2017-05-04 15:06:15 -07:00
parent cafefa049b
commit f286b6b798
3 changed files with 30 additions and 6 deletions

View file

@ -56,7 +56,7 @@ func (m *mockConsulServiceClient) UpdateTask(allocID string, old, new *structs.T
m.mu.Lock()
defer m.mu.Unlock()
m.logger.Printf("[TEST] mock_consul: UpdateTask(%q, %v, %v, %T)", allocID, old, new, exec)
m.ops = append(m.ops, newMockConsulOp("update", allocID, old, exec))
m.ops = append(m.ops, newMockConsulOp("update", allocID, new, exec))
return nil
}

View file

@ -1418,7 +1418,7 @@ func (r *TaskRunner) handleUpdate(update *structs.Allocation) error {
mErr.Errors = append(mErr.Errors, fmt.Errorf("updating task resources failed: %v", err))
}
if err := r.updateServices(drv, r.handle, r.task, updatedTask); err != nil {
if err := r.updateServices(drv, r.handle, r.task, updatedTask, update); err != nil {
mErr.Errors = append(mErr.Errors, fmt.Errorf("error updating services and checks in Consul: %v", err))
}
}
@ -1436,13 +1436,17 @@ func (r *TaskRunner) handleUpdate(update *structs.Allocation) error {
}
// updateServices and checks with Consul.
func (r *TaskRunner) updateServices(d driver.Driver, h driver.ScriptExecutor, old, new *structs.Task) error {
func (r *TaskRunner) updateServices(d driver.Driver, h driver.ScriptExecutor, old, new *structs.Task, newAlloc *structs.Allocation) error {
var exec driver.ScriptExecutor
if d.Abilities().Exec {
// Allow set the script executor if the driver supports it
exec = h
}
interpolateServices(r.getTaskEnv(), new)
newTaskEnv, err := driver.GetTaskEnv(r.taskDir, r.config.Node, new, newAlloc, r.config, r.vaultFuture.Get())
if err != nil {
return err
}
interpolateServices(newTaskEnv, new)
return r.consul.UpdateTask(r.alloc.ID, old, new, exec)
}

View file

@ -279,6 +279,7 @@ func TestTaskRunner_Update(t *testing.T) {
// Change command to ensure we run for a bit
ctx.tr.task.Config["command"] = "/bin/sleep"
ctx.tr.task.Config["args"] = []string{"100"}
ctx.tr.task.Services[0].Checks[0].Args[0] = "${NOMAD_META_foo}"
go ctx.tr.Run()
defer ctx.Cleanup()
@ -290,8 +291,12 @@ func TestTaskRunner_Update(t *testing.T) {
newMode := "foo"
newTG.RestartPolicy.Mode = newMode
newTask := updateAlloc.Job.TaskGroups[0].Tasks[0]
newTask.Driver = "foobar"
newTask := newTG.Tasks[0]
newTask.Driver = "mock_driver"
// Update meta to make sure service checks are interpolated correctly
// #2180
newTask.Meta["foo"] = "UPDATE"
// Update the kill timeout
testutil.WaitForResult(func() (bool, error) {
@ -322,6 +327,21 @@ func TestTaskRunner_Update(t *testing.T) {
if ctx.tr.handle.ID() == oldHandle {
return false, fmt.Errorf("handle not ctx.updated")
}
// Make sure Consul services were interpolated correctly during
// the update #2180
consul := ctx.tr.consul.(*mockConsulServiceClient)
consul.mu.Lock()
defer consul.mu.Unlock()
if len(consul.ops) < 2 {
return false, fmt.Errorf("expected at least 2 consul ops found: %d", len(consul.ops))
}
lastOp := consul.ops[len(consul.ops)-1]
if lastOp.op != "update" {
return false, fmt.Errorf("expected last consul op to be update not %q", lastOp.op)
}
if found := lastOp.task.Services[0].Checks[0].Args[0]; found != "UPDATE" {
return false, fmt.Errorf("expected consul check to be UPDATE but found: %q", found)
}
return true, nil
}, func(err error) {
t.Fatalf("err: %v", err)