diff --git a/modules/common/condition/funcs.go b/modules/common/condition/funcs.go index 68146601..ecfb31a7 100644 --- a/modules/common/condition/funcs.go +++ b/modules/common/condition/funcs.go @@ -34,6 +34,7 @@ import ( // Optional conditions list can be passed as parameter which allows to initialize // additional conditions at the beginning. func (conditions *Conditions) Init(cl *Conditions) { + conditions.Reset() conditions.Set(UnknownCondition(ReadyCondition, RequestedReason, ReadyInitMessage)) // add all optional conditions if no not nil @@ -102,6 +103,11 @@ func (conditions *Conditions) Remove(t Type) { *conditions = newConditions } +// Reset - removes all conditions +func (conditions *Conditions) Reset() { + *conditions = Conditions{} +} + // Get returns the condition with the given type, if the condition does not exists, // it returns nil. func (conditions *Conditions) Get(t Type) *Condition { diff --git a/modules/common/condition/funcs_test.go b/modules/common/condition/funcs_test.go index 56aabe2c..88d60556 100644 --- a/modules/common/condition/funcs_test.go +++ b/modules/common/condition/funcs_test.go @@ -83,7 +83,10 @@ func TestInit(t *testing.T) { t.Run(tt.name, func(t *testing.T) { g := NewWithT(t) - conditions := Conditions{} + someCondition := TrueCondition("foo", "to be removed on Init()") + conditions := Conditions{ + *someCondition, + } conditions.Init(&tt.conditions) g.Expect(conditions).To(haveSameConditionsOf(tt.want)) @@ -228,6 +231,38 @@ func TestRemove(t *testing.T) { } } +func TestReset(t *testing.T) { + tests := []struct { + name string + conditions Conditions + expected Conditions + }{ + { + name: "empty", + conditions: Conditions{}, + expected: Conditions{}, + }, + { + name: "present", + conditions: CreateList( + unknownReady, + unknownA, + unknownB, + ), + expected: Conditions{}, + }, + } + + g := NewWithT(t) + + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + tt.conditions.Reset() + g.Expect(tt.expected).To(haveSameConditionsOf(tt.conditions)) + }) + } +} + func TestHasSameState(t *testing.T) { g := NewWithT(t)