From 1ae7dafc5a1d0035ec4699bec82e5157a2cfb548 Mon Sep 17 00:00:00 2001 From: Stefan Prodan Date: Sat, 3 Feb 2024 10:41:53 +0200 Subject: [PATCH] runtime: Add `HasAnyReason` to conditions getter Signed-off-by: Stefan Prodan --- runtime/conditions/getter.go | 11 +++++++++++ runtime/conditions/getter_test.go | 15 ++++++++++++++- 2 files changed, 25 insertions(+), 1 deletion(-) diff --git a/runtime/conditions/getter.go b/runtime/conditions/getter.go index f2f75f50..144eafa4 100644 --- a/runtime/conditions/getter.go +++ b/runtime/conditions/getter.go @@ -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 { diff --git a/runtime/conditions/getter_test.go b/runtime/conditions/getter_test.go index 2847d327..7c76837f 100644 --- a/runtime/conditions/getter_test.go +++ b/runtime/conditions/getter_test.go @@ -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" ) @@ -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)