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

add ConditionsMatch function #397

Closed
wants to merge 9 commits into from
Closed
Show file tree
Hide file tree
Changes from 3 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
29 changes: 29 additions & 0 deletions pkg/condition/condition.go
Original file line number Diff line number Diff line change
Expand Up @@ -146,3 +146,32 @@
con, found := FindConditionByType(conditions, conditionType)
return found && con.Reason == reason
}

// ConditionsMatch checks whether the specified conditions match and return true if they do
func ConditionsMatch(first, second []toolchainv1alpha1.Condition) bool {
if len(first) != len(second) {
return false
}
for _, c := range first {
if !ContainsCondition(second, c) {
return false
}
}
for _, c := range second {
sbryzak marked this conversation as resolved.
Show resolved Hide resolved
if !ContainsCondition(first, c) {
return false

Check warning on line 162 in pkg/condition/condition.go

View check run for this annotation

Codecov / codecov/patch

pkg/condition/condition.go#L162

Added line #L162 was not covered by tests
}
}
return true
}

// ContainsCondition returns true if the specified list of conditions contains the specified condition and the statuses of the conditions match.
// LastTransitionTime is ignored.
func ContainsCondition(conditions []toolchainv1alpha1.Condition, contains toolchainv1alpha1.Condition) bool {
for _, c := range conditions {
sbryzak marked this conversation as resolved.
Show resolved Hide resolved
if c.Type == contains.Type {
return contains.Status == c.Status && contains.Reason == c.Reason && contains.Message == c.Message
}
}
return false
}
55 changes: 55 additions & 0 deletions pkg/condition/condition_blackbox_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -543,3 +543,58 @@ func reverseStatus(status apiv1.ConditionStatus) apiv1.ConditionStatus {
return apiv1.ConditionFalse
}
}

func TestConditionsMatch(t *testing.T) {
conditions1 := []toolchainv1alpha1.Condition{
{
Type: toolchainv1alpha1.UserSignupComplete,
Status: apiv1.ConditionTrue,
Reason: toolchainv1alpha1.UserSignupApprovedAutomaticallyReason,
Message: "",
},
{
Type: toolchainv1alpha1.UserSignupUserDeactivatingNotificationCreated,
Status: apiv1.ConditionTrue,
Reason: toolchainv1alpha1.UserSignupUserDeactivatingReason,
Message: "",
},
}
conditions2 := []toolchainv1alpha1.Condition{
{
Type: toolchainv1alpha1.UserSignupComplete,
Status: apiv1.ConditionTrue,
Reason: toolchainv1alpha1.UserSignupApprovedAutomaticallyReason,
Message: "",
},
{
Type: toolchainv1alpha1.UserSignupUserDeactivatingNotificationCreated,
Status: apiv1.ConditionFalse,
Reason: "Foo",
Message: "",
},
}

// The conditions are different so no match
require.False(t, condition.ConditionsMatch(conditions1, conditions2))

// Update the condition so that they now match
conditions2[1].Status = apiv1.ConditionTrue
conditions2[1].Reason = toolchainv1alpha1.UserSignupUserDeactivatingReason
require.True(t, condition.ConditionsMatch(conditions1, conditions2))

// Add another condition, now they should not match
conditions2 = append(conditions2, toolchainv1alpha1.Condition{
Type: toolchainv1alpha1.UserSignupUserDeactivatedNotificationCreated,
Status: apiv1.ConditionTrue,
Reason: toolchainv1alpha1.UserSignupUserDeactivatedReason,
Message: "",
})

require.False(t, condition.ConditionsMatch(conditions1, conditions2))

conditions3 := []toolchainv1alpha1.Condition{}
conditions3 = append(conditions3, conditions2[0])
conditions3 = append(conditions3, conditions2[2])

require.False(t, condition.ConditionsMatch(conditions1, conditions3))
}
Loading