Skip to content

Commit

Permalink
Merge pull request #760 from hashicorp/f-var-interprelation
Browse files Browse the repository at this point in the history
Only interpret vars wrapped in braces
  • Loading branch information
dadgar committed Feb 5, 2016
2 parents d5e6a9c + 0c46a0e commit 5b1c6f8
Show file tree
Hide file tree
Showing 23 changed files with 76 additions and 80 deletions.
18 changes: 9 additions & 9 deletions client/driver/env/env_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ func testTaskEnvironment() *TaskEnvironment {
func TestEnvironment_ParseAndReplace_Env(t *testing.T) {
env := testTaskEnvironment()

input := []string{fmt.Sprintf(`"$%v"!`, envOneKey), fmt.Sprintf("$%s$%s", envOneKey, envTwoKey)}
input := []string{fmt.Sprintf(`"${%v}"!`, envOneKey), fmt.Sprintf("${%s}${%s}", envOneKey, envTwoKey)}
act := env.ParseAndReplace(input)
exp := []string{fmt.Sprintf(`"%s"!`, envOneVal), fmt.Sprintf("%s%s", envOneVal, envTwoVal)}

Expand All @@ -71,7 +71,7 @@ func TestEnvironment_ParseAndReplace_Env(t *testing.T) {
}

func TestEnvironment_ParseAndReplace_Meta(t *testing.T) {
input := []string{fmt.Sprintf("$%v%v", nodeMetaPrefix, metaKey)}
input := []string{fmt.Sprintf("${%v%v}", nodeMetaPrefix, metaKey)}
exp := []string{metaVal}
env := testTaskEnvironment()
act := env.ParseAndReplace(input)
Expand All @@ -82,7 +82,7 @@ func TestEnvironment_ParseAndReplace_Meta(t *testing.T) {
}

func TestEnvironment_ParseAndReplace_Attr(t *testing.T) {
input := []string{fmt.Sprintf("$%v%v", nodeAttributePrefix, attrKey)}
input := []string{fmt.Sprintf("${%v%v}", nodeAttributePrefix, attrKey)}
exp := []string{attrVal}
env := testTaskEnvironment()
act := env.ParseAndReplace(input)
Expand All @@ -93,7 +93,7 @@ func TestEnvironment_ParseAndReplace_Attr(t *testing.T) {
}

func TestEnvironment_ParseAndReplace_Node(t *testing.T) {
input := []string{fmt.Sprintf("$%v", nodeNameKey), fmt.Sprintf("$%v", nodeClassKey)}
input := []string{fmt.Sprintf("${%v}", nodeNameKey), fmt.Sprintf("${%v}", nodeClassKey)}
exp := []string{nodeName, nodeClass}
env := testTaskEnvironment()
act := env.ParseAndReplace(input)
Expand All @@ -105,9 +105,9 @@ func TestEnvironment_ParseAndReplace_Node(t *testing.T) {

func TestEnvironment_ParseAndReplace_Mixed(t *testing.T) {
input := []string{
fmt.Sprintf("$%v$%v%v", nodeNameKey, nodeAttributePrefix, attrKey),
fmt.Sprintf("$%v$%v%v", nodeClassKey, nodeMetaPrefix, metaKey),
fmt.Sprintf("$%v$%v", envTwoKey, nodeClassKey),
fmt.Sprintf("${%v}${%v%v}", nodeNameKey, nodeAttributePrefix, attrKey),
fmt.Sprintf("${%v}${%v%v}", nodeClassKey, nodeMetaPrefix, metaKey),
fmt.Sprintf("${%v}${%v}", envTwoKey, nodeClassKey),
}
exp := []string{
fmt.Sprintf("%v%v", nodeName, attrVal),
Expand All @@ -123,7 +123,7 @@ func TestEnvironment_ParseAndReplace_Mixed(t *testing.T) {
}

func TestEnvironment_ReplaceEnv_Mixed(t *testing.T) {
input := fmt.Sprintf("$%v$%v%v", nodeNameKey, nodeAttributePrefix, attrKey)
input := fmt.Sprintf("${%v}${%v%v}", nodeNameKey, nodeAttributePrefix, attrKey)
exp := fmt.Sprintf("%v%v", nodeName, attrVal)
env := testTaskEnvironment()
act := env.ReplaceEnv(input)
Expand Down Expand Up @@ -193,7 +193,7 @@ func TestEnvironment_ClearEnvvars(t *testing.T) {

func TestEnvironment_Interprolate(t *testing.T) {
env := testTaskEnvironment().
SetEnvvars(map[string]string{"test": "$node.class", "test2": "$attr.arch"}).
SetEnvvars(map[string]string{"test": "${node.class}", "test2": "${attr.arch}"}).
Build()

act := env.EnvList()
Expand Down
2 changes: 1 addition & 1 deletion client/driver/exec_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -212,7 +212,7 @@ func TestExecDriver_Start_Wait_AllocDir(t *testing.T) {
"command": "/bin/bash",
"args": []string{
"-c",
fmt.Sprintf(`sleep 1; echo -n %s > $%s/%s`, string(exp), env.AllocDir, file),
fmt.Sprintf(`sleep 1; echo -n %s > ${%s}/%s`, string(exp), env.AllocDir, file),
},
},
Resources: basicResources,
Expand Down
2 changes: 1 addition & 1 deletion client/driver/raw_exec_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -231,7 +231,7 @@ func TestRawExecDriver_Start_Wait_AllocDir(t *testing.T) {
t.Parallel()
exp := []byte{'w', 'i', 'n'}
file := "output.txt"
outPath := fmt.Sprintf(`$%s/%s`, env.AllocDir, file)
outPath := fmt.Sprintf(`${%s}/%s`, env.AllocDir, file)
task := &structs.Task{
Name: "sleep",
Config: map[string]interface{}{
Expand Down
2 changes: 1 addition & 1 deletion command/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ job "example" {
# Restrict our job to only linux. We can specify multiple
# constraints as needed.
constraint {
attribute = "$attr.kernel.name"
attribute = "${attr.kernel.name}"
value = "linux"
}
Expand Down
8 changes: 2 additions & 6 deletions helper/args/args.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,15 @@ package args
import "regexp"

var (
envRe = regexp.MustCompile(`\$({[a-zA-Z0-9_\.]+}|[a-zA-Z0-9_\.]+)`)
envRe = regexp.MustCompile(`\${[a-zA-Z0-9_\.]+}`)
)

// ReplaceEnv takes an arg and replaces all occurences of environment variables.
// If the variable is found in the passed map it is replaced, otherwise the
// original string is returned.
func ReplaceEnv(arg string, environents ...map[string]string) string {
return envRe.ReplaceAllStringFunc(arg, func(arg string) string {
stripped := arg[1:]
if stripped[0] == '{' {
stripped = stripped[1 : len(stripped)-1]
}

stripped := arg[2 : len(arg)-1]
for _, env := range environents {
if value, ok := env[stripped]; ok {
return value
Expand Down
10 changes: 5 additions & 5 deletions helper/args/args_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@ var (
)

func TestArgs_ReplaceEnv_Invalid(t *testing.T) {
input := "$FOO"
exp := "$FOO"
input := "${FOO}"
exp := input
act := ReplaceEnv(input, envVars)

if !reflect.DeepEqual(act, exp) {
Expand All @@ -34,7 +34,7 @@ func TestArgs_ReplaceEnv_Invalid(t *testing.T) {
}

func TestArgs_ReplaceEnv_Valid(t *testing.T) {
input := fmt.Sprintf(`"$%v"!`, ipKey)
input := fmt.Sprintf(`"${%v}"!`, ipKey)
exp := fmt.Sprintf("\"%s\"!", ipVal)
act := ReplaceEnv(input, envVars)

Expand All @@ -44,7 +44,7 @@ func TestArgs_ReplaceEnv_Valid(t *testing.T) {
}

func TestArgs_ReplaceEnv_Period(t *testing.T) {
input := fmt.Sprintf(`"$%v"!`, periodKey)
input := fmt.Sprintf(`"${%v}"!`, periodKey)
exp := fmt.Sprintf("\"%s\"!", periodVal)
act := ReplaceEnv(input, envVars)

Expand All @@ -54,7 +54,7 @@ func TestArgs_ReplaceEnv_Period(t *testing.T) {
}

func TestArgs_ReplaceEnv_Chained(t *testing.T) {
input := fmt.Sprintf("$%s$%s", ipKey, portKey)
input := fmt.Sprintf("${%s}${%s}", ipKey, portKey)
exp := fmt.Sprintf("%s%s", ipVal, portVal)
act := ReplaceEnv(input, envVars)

Expand Down
4 changes: 2 additions & 2 deletions nomad/mock/mock.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ func Job() *structs.Job {
Datacenters: []string{"dc1"},
Constraints: []*structs.Constraint{
&structs.Constraint{
LTarget: "$attr.kernel.name",
LTarget: "${attr.kernel.name}",
RTarget: "linux",
Operand: "=",
},
Expand Down Expand Up @@ -145,7 +145,7 @@ func SystemJob() *structs.Job {
Datacenters: []string{"dc1"},
Constraints: []*structs.Constraint{
&structs.Constraint{
LTarget: "$attr.kernel.name",
LTarget: "${attr.kernel.name}",
RTarget: "linux",
Operand: "=",
},
Expand Down
6 changes: 3 additions & 3 deletions nomad/structs/node_class.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,11 +82,11 @@ func EscapedConstraints(constraints []*Constraint) []*Constraint {
// computed node class optimization.
func constraintTargetEscapes(target string) bool {
switch {
case strings.HasPrefix(target, "$node.unique."):
case strings.HasPrefix(target, "${node.unique."):
return true
case strings.HasPrefix(target, "$attr.unique."):
case strings.HasPrefix(target, "${attr.unique."):
return true
case strings.HasPrefix(target, "$meta.unique."):
case strings.HasPrefix(target, "${meta.unique."):
return true
default:
return false
Expand Down
12 changes: 6 additions & 6 deletions nomad/structs/node_class_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -172,34 +172,34 @@ func TestNode_ComputedClass_Meta(t *testing.T) {
func TestNode_EscapedConstraints(t *testing.T) {
// Non-escaped constraints
ne1 := &Constraint{
LTarget: "$attr.kernel.name",
LTarget: "${attr.kernel.name}",
RTarget: "linux",
Operand: "=",
}
ne2 := &Constraint{
LTarget: "$meta.key_foo",
LTarget: "${meta.key_foo}",
RTarget: "linux",
Operand: "<",
}
ne3 := &Constraint{
LTarget: "$node.dc",
LTarget: "${node.dc}",
RTarget: "test",
Operand: "!=",
}

// Escaped constraints
e1 := &Constraint{
LTarget: "$attr.unique.kernel.name",
LTarget: "${attr.unique.kernel.name}",
RTarget: "linux",
Operand: "=",
}
e2 := &Constraint{
LTarget: "$meta.unique.key_foo",
LTarget: "${meta.unique.key_foo}",
RTarget: "linux",
Operand: "<",
}
e3 := &Constraint{
LTarget: "$unique.node.id",
LTarget: "${unique.node.id}",
RTarget: "test",
Operand: "!=",
}
Expand Down
8 changes: 4 additions & 4 deletions scheduler/context_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -166,22 +166,22 @@ func TestEvalEligibility_TaskGroupStatus(t *testing.T) {
func TestEvalEligibility_SetJob(t *testing.T) {
e := NewEvalEligibility()
ne1 := &structs.Constraint{
LTarget: "$attr.kernel.name",
LTarget: "${attr.kernel.name}",
RTarget: "linux",
Operand: "=",
}
e1 := &structs.Constraint{
LTarget: "$attr.unique.kernel.name",
LTarget: "${attr.unique.kernel.name}",
RTarget: "linux",
Operand: "=",
}
e2 := &structs.Constraint{
LTarget: "$meta.unique.key_foo",
LTarget: "${meta.unique.key_foo}",
RTarget: "linux",
Operand: "<",
}
e3 := &structs.Constraint{
LTarget: "$meta.unique.key_foo",
LTarget: "${meta.unique.key_foo}",
RTarget: "Windows",
Operand: "<",
}
Expand Down
16 changes: 8 additions & 8 deletions scheduler/feasible.go
Original file line number Diff line number Diff line change
Expand Up @@ -296,25 +296,25 @@ func resolveConstraintTarget(target string, node *structs.Node) (interface{}, bo

// Handle the interpolations
switch {
case "$node.unique.id" == target:
case "${node.unique.id}" == target:
return node.ID, true

case "$node.datacenter" == target:
case "${node.datacenter}" == target:
return node.Datacenter, true

case "$node.unique.name" == target:
case "${node.unique.name}" == target:
return node.Name, true

case "$node.class" == target:
case "${node.class}" == target:
return node.NodeClass, true

case strings.HasPrefix(target, "$attr."):
attr := strings.TrimPrefix(target, "$attr.")
case strings.HasPrefix(target, "${attr."):
attr := strings.TrimSuffix(strings.TrimPrefix(target, "${attr."), "}")
val, ok := node.Attributes[attr]
return val, ok

case strings.HasPrefix(target, "$meta."):
meta := strings.TrimPrefix(target, "$meta.")
case strings.HasPrefix(target, "${meta."):
meta := strings.TrimSuffix(strings.TrimPrefix(target, "${meta."), "}")
val, ok := node.Meta[meta]
return val, ok

Expand Down
24 changes: 12 additions & 12 deletions scheduler/feasible_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -139,17 +139,17 @@ func TestConstraintChecker(t *testing.T) {
constraints := []*structs.Constraint{
&structs.Constraint{
Operand: "=",
LTarget: "$node.datacenter",
LTarget: "${node.datacenter}",
RTarget: "dc1",
},
&structs.Constraint{
Operand: "is",
LTarget: "$attr.kernel.name",
LTarget: "${attr.kernel.name}",
RTarget: "linux",
},
&structs.Constraint{
Operand: "is",
LTarget: "$node.class",
LTarget: "${node.class}",
RTarget: "large",
},
}
Expand Down Expand Up @@ -189,53 +189,53 @@ func TestResolveConstraintTarget(t *testing.T) {
node := mock.Node()
cases := []tcase{
{
target: "$node.unique.id",
target: "${node.unique.id}",
node: node,
val: node.ID,
result: true,
},
{
target: "$node.datacenter",
target: "${node.datacenter}",
node: node,
val: node.Datacenter,
result: true,
},
{
target: "$node.unique.name",
target: "${node.unique.name}",
node: node,
val: node.Name,
result: true,
},
{
target: "$node.class",
target: "${node.class}",
node: node,
val: node.NodeClass,
result: true,
},
{
target: "$node.foo",
target: "${node.foo}",
node: node,
result: false,
},
{
target: "$attr.kernel.name",
target: "${attr.kernel.name}",
node: node,
val: node.Attributes["kernel.name"],
result: true,
},
{
target: "$attr.rand",
target: "${attr.rand}",
node: node,
result: false,
},
{
target: "$meta.pci-dss",
target: "${meta.pci-dss}",
node: node,
val: node.Meta["pci-dss"],
result: true,
},
{
target: "$meta.rand",
target: "${meta.rand}",
node: node,
result: false,
},
Expand Down
2 changes: 1 addition & 1 deletion scheduler/generic_sched_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -238,7 +238,7 @@ func TestServiceSched_JobRegister_FeasibleAndInfeasibleTG(t *testing.T) {
job.TaskGroups[0].Count = 2
job.TaskGroups[0].Constraints = append(job.Constraints,
&structs.Constraint{
LTarget: "$node.class",
LTarget: "${node.class}",
RTarget: "class_0",
Operand: "=",
},
Expand Down
Loading

0 comments on commit 5b1c6f8

Please sign in to comment.