From e6bf43e8258686bc84f5936416e22b7ba2ccf122 Mon Sep 17 00:00:00 2001 From: Will Owens Date: Wed, 5 Jun 2024 09:02:39 -0400 Subject: [PATCH] jobspec2: add test for parsing contraint alternates (#23175) --- jobspec2/parse_test.go | 100 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 100 insertions(+) diff --git a/jobspec2/parse_test.go b/jobspec2/parse_test.go index 93226e6b4..bfbb01cbf 100644 --- a/jobspec2/parse_test.go +++ b/jobspec2/parse_test.go @@ -953,6 +953,106 @@ func TestParse_Meta_Alternatives(t *testing.T) { } +func TestParse_Constraint_Alternatives(t *testing.T) { + ci.Parallel(t) + + hclOpVal := ` +job "example" { + constraint { + operator = "distinct_hosts" + value = "true" + } + constraint { + operator = "distinct_property" + attribute = "${meta.rack}" + value = "1" + } + group "group" { + constraint { + operator = "distinct_hosts" + value = "false" + } + constraint { + operator = "distinct_property" + attribute = "${meta.rack}" + value = "2" + } + task "task" { + constraint { + operator = "distinct_hosts" + value = "true" + } + constraint { + operator = "distinct_property" + attribute = "${meta.rack}" + value = "3" + } + driver = "config" + config {} + } + } +} +` + hclCompact := ` +job "example" { + constraint { + distinct_hosts = true + } + constraint { + distinct_property = "${meta.rack}" + value = "1" + } + group "group" { + constraint { + distinct_hosts = false + } + constraint { + distinct_property = "${meta.rack}" + value = "2" + } + task "task" { + constraint { + distinct_hosts = true + } + constraint { + distinct_property = "${meta.rack}" + value = "3" + } + driver = "config" + config {} + } + } +} +` + asOpValue, err := ParseWithConfig(&ParseConfig{ + Path: "input.hcl", + Body: []byte(hclOpVal), + }) + must.NoError(t, err) + + asCompact, err := ParseWithConfig(&ParseConfig{ + Path: "input.hcl", + Body: []byte(hclCompact), + }) + must.NoError(t, err) + + constraint := func(l, r, op string) *api.Constraint { + return &api.Constraint{ + LTarget: l, + RTarget: r, + Operand: op, + } + } + + must.Eq(t, asOpValue, asCompact) + must.Eq(t, constraint("", "true", "distinct_hosts"), asOpValue.Constraints[0]) + must.Eq(t, constraint("", "false", "distinct_hosts"), asOpValue.TaskGroups[0].Constraints[0]) + must.Eq(t, constraint("", "true", "distinct_hosts"), asOpValue.TaskGroups[0].Tasks[0].Constraints[0]) + must.Eq(t, constraint("${meta.rack}", "1", "distinct_property"), asOpValue.Constraints[1]) + must.Eq(t, constraint("${meta.rack}", "2", "distinct_property"), asOpValue.TaskGroups[0].Constraints[1]) + must.Eq(t, constraint("${meta.rack}", "3", "distinct_property"), asOpValue.TaskGroups[0].Tasks[0].Constraints[1]) +} + // TestParse_UndefinedVariables asserts that values with undefined variables are left // intact in the job representation func TestParse_UndefinedVariables(t *testing.T) {