diff --git a/client/task_runner.go b/client/task_runner.go index 987578e84..c953a76c8 100644 --- a/client/task_runner.go +++ b/client/task_runner.go @@ -1450,6 +1450,18 @@ func interpolateServices(taskEnv *env.TaskEnv, task *structs.Task) *structs.Task check.Protocol = taskEnv.ReplaceEnv(check.Protocol) check.PortLabel = taskEnv.ReplaceEnv(check.PortLabel) check.InitialStatus = taskEnv.ReplaceEnv(check.InitialStatus) + check.Method = taskEnv.ReplaceEnv(check.Method) + if len(check.Header) > 0 { + header := make(map[string][]string, len(check.Header)) + for k, vs := range check.Header { + newVals := make([]string, len(vs)) + for i, v := range vs { + newVals[i] = taskEnv.ReplaceEnv(v) + } + header[taskEnv.ReplaceEnv(k)] = newVals + } + check.Header = header + } } service.Name = taskEnv.ReplaceEnv(service.Name) service.PortLabel = taskEnv.ReplaceEnv(service.PortLabel) diff --git a/client/task_runner_test.go b/client/task_runner_test.go index dffcc4238..da4e8f7e0 100644 --- a/client/task_runner_test.go +++ b/client/task_runner_test.go @@ -9,6 +9,7 @@ import ( "os" "path/filepath" "reflect" + "strings" "syscall" "testing" "time" @@ -17,11 +18,13 @@ import ( "github.com/golang/snappy" "github.com/hashicorp/nomad/client/allocdir" "github.com/hashicorp/nomad/client/config" + "github.com/hashicorp/nomad/client/driver/env" cstructs "github.com/hashicorp/nomad/client/structs" "github.com/hashicorp/nomad/client/vaultclient" "github.com/hashicorp/nomad/nomad/mock" "github.com/hashicorp/nomad/nomad/structs" "github.com/hashicorp/nomad/testutil" + "github.com/kr/pretty" ) func testLogger() *log.Logger { @@ -1615,6 +1618,88 @@ func TestTaskRunner_Pre06ScriptCheck(t *testing.T) { t.Run(run("0.5.6", "mock_driver", "tcp", false)) } +func TestTaskRunner_interpolateServices(t *testing.T) { + t.Parallel() + task := &structs.Task{ + Services: []*structs.Service{ + { + Name: "${name}", + PortLabel: "${portlabel}", + Tags: []string{"${tags}"}, + Checks: []*structs.ServiceCheck{ + { + Name: "${checkname}", + Type: "${checktype}", + Command: "${checkcmd}", + Args: []string{"${checkarg}"}, + Path: "${checkstr}", + Protocol: "${checkproto}", + PortLabel: "${checklabel}", + InitialStatus: "${checkstatus}", + Method: "${checkmethod}", + Header: map[string][]string{ + "${checkheaderk}": {"${checkheaderv}"}, + }, + }, + }, + }, + }, + } + + env := &env.TaskEnv{ + EnvMap: map[string]string{ + "name": "name", + "portlabel": "portlabel", + "tags": "tags", + "checkname": "checkname", + "checktype": "checktype", + "checkcmd": "checkcmd", + "checkarg": "checkarg", + "checkstr": "checkstr", + "checkpath": "checkpath", + "checkproto": "checkproto", + "checklabel": "checklabel", + "checkstatus": "checkstatus", + "checkmethod": "checkmethod", + "checkheaderk": "checkheaderk", + "checkheaderv": "checkheaderv", + }, + } + + interpTask := interpolateServices(env, task) + + exp := &structs.Task{ + Services: []*structs.Service{ + { + Name: "name", + PortLabel: "portlabel", + Tags: []string{"tags"}, + Checks: []*structs.ServiceCheck{ + { + Name: "checkname", + Type: "checktype", + Command: "checkcmd", + Args: []string{"checkarg"}, + Path: "checkstr", + Protocol: "checkproto", + PortLabel: "checklabel", + InitialStatus: "checkstatus", + Method: "checkmethod", + Header: map[string][]string{ + "checkheaderk": {"checkheaderv"}, + }, + }, + }, + }, + }, + } + + if diff := pretty.Diff(interpTask, exp); len(diff) > 0 { + t.Fatalf("diff:\n%s\n", strings.Join(diff, "\n")) + } +} + + func TestTaskRunner_ShutdownDelay(t *testing.T) { t.Parallel()