Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rework status constraint logic for successes #1515

Merged
merged 4 commits into from
Jan 2, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 3 additions & 4 deletions pipeline/frontend/yaml/compiler/convert.go
Original file line number Diff line number Diff line change
Expand Up @@ -134,11 +134,10 @@ func (c *Compiler) createProcess(name string, container *yaml.Container, section
cpuSet = c.reslimit.CPUSet
}

// all constraints must exclude success.
onSuccess := container.When.IsEmpty() ||
!container.When.ExcludesStatus("success")
// at least one constraint contain status success, or all constraints have no status set
onSuccess := container.When.IncludesStatusSuccess()
// at least one constraint must include the status failure.
onFailure := container.When.IncludesStatus("failure")
onFailure := container.When.IncludesStatusFailure()

failure := container.Failure
if container.Failure == "" {
Expand Down
19 changes: 12 additions & 7 deletions pipeline/frontend/yaml/constraint/constraint.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,24 +79,29 @@ func (when *When) Match(metadata frontend.Metadata, global bool) (bool, error) {
return false, nil
}

func (when *When) IncludesStatus(status string) bool {
func (when *When) IncludesStatusFailure() bool {
for _, c := range when.Constraints {
if c.Status.Includes(status) {
if c.Status.Includes("failure") {
return true
}
}

return false
}

func (when *When) ExcludesStatus(status string) bool {
func (when *When) IncludesStatusSuccess() bool {
// "success" acts differently than "failure" in that it's
// presumed to be included unless it's specifically not part
// of the list
if when.IsEmpty() {
return true
}
for _, c := range when.Constraints {
if !c.Status.Excludes(status) {
return false
if len(c.Status.Include) == 0 || c.Status.Includes("success") {
return true
}
}

return len(when.Constraints) > 0
return false
}

// False if (any) non local
Expand Down
18 changes: 18 additions & 0 deletions pipeline/frontend/yaml/constraint/constraint_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -381,6 +381,24 @@ func TestConstraintMap(t *testing.T) {
}
}

func TestConstraintStatusSuccess(t *testing.T) {
testdata := []struct {
conf string
want bool
}{
{conf: "", want: true},
{conf: "{status: [failure]}", want: false},
{conf: "{status: [success]}", want: true},
{conf: "{status: [failure, success]}", want: true},
{conf: "{status: {exclude: [success], include: [failure]}}", want: false},
{conf: "{status: {exclude: [failure], include: [success]}}", want: true},
}
for _, test := range testdata {
c := parseConstraints(t, test.conf)
assert.Equal(t, test.want, c.IncludesStatusSuccess(), "when: '%s'", test.conf)
}
}

func TestConstraints(t *testing.T) {
testdata := []struct {
desc string
Expand Down