Skip to content

Commit

Permalink
Merge pull request #590 from stuggi/reset_conditions
Browse files Browse the repository at this point in the history
[conditions] Reset conditions on Init()
  • Loading branch information
stuggi authored Dec 16, 2024
2 parents 41a4a62 + fb21c47 commit d172b3a
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 1 deletion.
6 changes: 6 additions & 0 deletions modules/common/condition/funcs.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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 {
Expand Down
37 changes: 36 additions & 1 deletion modules/common/condition/funcs_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down Expand Up @@ -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)

Expand Down

0 comments on commit d172b3a

Please sign in to comment.