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 getcondition func #198

Merged
merged 1 commit into from
Apr 21, 2022
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
15 changes: 15 additions & 0 deletions apis/meta/v1alpha1/condition.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,21 @@ func IsConditionChanged(current, old apis.ConditionAccessor, conditionType apis.
currentCondition.Reason != oldCondition.Reason
}

// GetCondition will return the first condition pointer filter by type in conditions
func GetCondition(conditions apis.Conditions, t apis.ConditionType) *apis.Condition {
if len(conditions) == 0 {
return nil
}

for i := range conditions {
if conditions[i].Type == t {
return &conditions[i]
}
}

return nil
}

// ConditionType is a camel-cased condition type.
type ConditionType string

Expand Down
61 changes: 61 additions & 0 deletions apis/meta/v1alpha1/condition_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,67 @@ func (s *StatusTest) GetCondition(t apis.ConditionType) *apis.Condition {
return stageTestCondSet.Manage(s).GetCondition(t)
}

func TestGetCondition(t *testing.T) {
table := map[string]struct {
source apis.Conditions
t apis.ConditionType
expectedIndex int
}{
"length is 0": {source: apis.Conditions{}, t: apis.ConditionType("WHAT"), expectedIndex: -1},
"source is nil": {source: nil, t: apis.ConditionType("WHAT"), expectedIndex: -1},
"contains in source": {
source: apis.Conditions{
{
Type: "AType",
},
{
Type: "BType",
},
},
t: "AType",
expectedIndex: 0,
},
"not contains in source": {
source: apis.Conditions{
{
Type: "AType",
},
{
Type: "BType",
},
},
t: "CType",
expectedIndex: -1,
},
"empty type": {
source: apis.Conditions{
{
Type: "AType",
},
{
Type: "BType",
},
},
t: "",
expectedIndex: -1,
},
}

for name, item := range table {
t.Run(name, func(t *testing.T) {
actual := GetCondition(item.source, item.t)
g := NewGomegaWithT(t)
if item.expectedIndex < 0 {
g.Expect(actual).Should(BeNil())
} else {
g.Expect(fmt.Sprintf("%p", actual)).Should(BeEquivalentTo(fmt.Sprintf("%p", &item.source[item.expectedIndex])))
actual.Message = "changed"
g.Expect(item.source[item.expectedIndex].Message).Should(BeEquivalentTo(actual.Message))
}
})
}
}

func TestIsConditionChanged(t *testing.T) {
now := metav1.Now()
oneSecondAgo := metav1.NewTime(now.Add(-time.Second))
Expand Down