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 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
40 changes: 40 additions & 0 deletions pkg/condition/condition.go
Original file line number Diff line number Diff line change
Expand Up @@ -146,3 +146,43 @@ func HasConditionReason(conditions []toolchainv1alpha1.Condition, conditionType
con, found := FindConditionByType(conditions, conditionType)
return found && con.Reason == reason
}

// ConditionsMatch checks whether the specified conditions match and return true if they do, ignoring the value of the
// message property.
func ConditionsMatch(first, second []toolchainv1alpha1.Condition) bool {
if len(first) != len(second) {
return false
}
for _, c := range first {
if !ContainsCondition(second, c) {
return false
}
}
return true
}

func containsConditions(conditions []toolchainv1alpha1.Condition, contains toolchainv1alpha1.Condition, ignoreMessage bool) bool {
for _, c := range conditions {
sbryzak marked this conversation as resolved.
Show resolved Hide resolved
if c.Type == contains.Type {
if ignoreMessage {
return contains.Status == c.Status && contains.Reason == c.Reason
}
return contains.Status == c.Status && contains.Reason == c.Reason && contains.Message == c.Message
}
}
return false
}

// ContainsCondition returns true if the specified list of conditions contains the specified condition and the statuses
// of the conditions match. This function does not compare the values of the message property.
// LastTransitionTime is ignored.
func ContainsCondition(conditions []toolchainv1alpha1.Condition, contains toolchainv1alpha1.Condition) bool {
return containsConditions(conditions, contains, true)
}

// ContainsConditionWithMessage returns true if the specified list of conditions contains the specified condition and the
// statuses and the message property of the conditions match.
// LastTransitionTime is ignored.
func ContainsConditionWithMessage(conditions []toolchainv1alpha1.Condition, contains toolchainv1alpha1.Condition) bool {
sbryzak marked this conversation as resolved.
Show resolved Hide resolved
return containsConditions(conditions, contains, false)
}
87 changes: 87 additions & 0 deletions pkg/condition/condition_blackbox_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -543,3 +543,90 @@ func reverseStatus(status apiv1.ConditionStatus) apiv1.ConditionStatus {
return apiv1.ConditionFalse
}
}

func TestContainsConditionWithMessage(t *testing.T) {
conditions1 := []toolchainv1alpha1.Condition{
{
Type: toolchainv1alpha1.UserSignupComplete,
Status: apiv1.ConditionTrue,
Reason: toolchainv1alpha1.UserSignupApprovedAutomaticallyReason,
Message: "foo",
},
{
Type: toolchainv1alpha1.UserSignupUserDeactivatingNotificationCreated,
Status: apiv1.ConditionTrue,
Reason: toolchainv1alpha1.UserSignupUserDeactivatingReason,
Message: "bar",
},
}
require.True(t, condition.ContainsConditionWithMessage(conditions1,
toolchainv1alpha1.Condition{
Type: toolchainv1alpha1.UserSignupUserDeactivatingNotificationCreated,
Status: apiv1.ConditionTrue,
Reason: toolchainv1alpha1.UserSignupUserDeactivatingReason,
Message: "bar",
}))

require.False(t, condition.ContainsConditionWithMessage(conditions1,
toolchainv1alpha1.Condition{
Type: toolchainv1alpha1.UserSignupUserDeactivatingNotificationCreated,
Status: apiv1.ConditionTrue,
Reason: toolchainv1alpha1.UserSignupUserDeactivatingReason,
Message: "foo",
}))
}

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))
}
26 changes: 9 additions & 17 deletions pkg/test/condition.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package test

import (
"fmt"
"github.com/codeready-toolchain/toolchain-common/pkg/condition"
"time"

toolchainv1alpha1 "github.com/codeready-toolchain/api/api/v1alpha1"
Expand Down Expand Up @@ -53,19 +54,15 @@ func AssertTimestampsAreRecent(t T, conditions []toolchainv1alpha1.Condition) {
}
}

// ConditionsMatch returns true if the specified list A of conditions is equal to specified
// list B of conditions ignoring the order of the elements
func ConditionsMatch(actual []toolchainv1alpha1.Condition, expected ...toolchainv1alpha1.Condition) bool {
if len(expected) != len(actual) {
// ConditionsMatch returns true if the specified list of conditions (first) is equal to the other specified
// list of conditions (second) ignoring the order of the elements, and comparing the message properties also.
// Note that the alternative ConditionsMatch function in the condition package does *NOT* compare the message properties
func ConditionsMatch(first []toolchainv1alpha1.Condition, second ...toolchainv1alpha1.Condition) bool {
if len(first) != len(second) {
return false
}
for _, c := range expected {
if !ContainsCondition(actual, c) {
return false
}
}
for _, c := range actual {
if !ContainsCondition(expected, c) {
for _, c := range first {
if !condition.ContainsConditionWithMessage(second, c) {
return false
}
}
Expand All @@ -75,10 +72,5 @@ func ConditionsMatch(actual []toolchainv1alpha1.Condition, expected ...toolchain
// ContainsCondition returns true if the specified list of conditions contains the specified condition.
// LastTransitionTime is ignored.
func ContainsCondition(conditions []toolchainv1alpha1.Condition, contains toolchainv1alpha1.Condition) bool {
for _, c := range conditions {
if c.Type == contains.Type {
return contains.Status == c.Status && contains.Reason == c.Reason && contains.Message == c.Message
}
}
return false
return condition.ContainsConditionWithMessage(conditions, contains)
}
Loading