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

[conditions] Reset conditions on Init() #590

Merged
merged 1 commit into from
Dec 16, 2024
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
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
Loading