Skip to content

Commit

Permalink
Merge pull request #730 from fluxcd/conditions-HasAnyReasons
Browse files Browse the repository at this point in the history
runtime: Add `HasAnyReason` to conditions getter
  • Loading branch information
stefanprodan authored Feb 5, 2024
2 parents db52a67 + 1ae7daf commit 2e7e8fe
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 1 deletion.
11 changes: 11 additions & 0 deletions runtime/conditions/getter.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,17 @@ func HasAny(from Getter, t []string) bool {
return false
}

// HasAnyReason returns true if a condition with the given
// type exists and any of the given reasons exist.
func HasAnyReason(from Getter, t string, r ...string) bool {
for _, reason := range r {
if GetReason(from, t) == reason {
return true
}
}
return false
}

// IsTrue is true if the condition with the given type is True, otherwise it is false if the condition is not True or if
// the condition does not exist (is nil).
func IsTrue(from Getter, t string) bool {
Expand Down
15 changes: 14 additions & 1 deletion runtime/conditions/getter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,11 @@ import (
"testing"

fuzz "github.com/AdaLogics/go-fuzz-headers"
"github.com/fluxcd/pkg/apis/meta"
. "github.com/onsi/gomega"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"

"github.com/fluxcd/pkg/apis/meta"

"github.com/fluxcd/pkg/runtime/conditions/testdata"
)

Expand Down Expand Up @@ -66,6 +67,18 @@ func TestGetAndHas(t *testing.T) {
g.Expect(HasAny(obj, []string{"conditionX", "conditionY"})).To(BeFalse())
}

func TestHasAnyReason(t *testing.T) {
g := NewWithT(t)

obj := getterWithConditions(true1, false1, unknown1)

g.Expect(HasAnyReason(obj, "true1")).To(BeFalse())
g.Expect(HasAnyReason(obj, "true1", "reason true1")).To(BeTrue())
g.Expect(HasAnyReason(obj, "false1", "reason true1", "reason false1")).To(BeTrue())
g.Expect(HasAnyReason(obj, "unknown1", "reason unknown2", "reason unknown3")).To(BeFalse())
g.Expect(HasAnyReason(obj, "unknown2", "reason unknown1")).To(BeFalse())
}

func TestIsMethods(t *testing.T) {
g := NewWithT(t)

Expand Down

0 comments on commit 2e7e8fe

Please sign in to comment.