use interfaces when testing conditions so we don't need versioned methods (#1120)

This commit is contained in:
Dave Protasowski 2020-02-24 12:23:08 -05:00 committed by GitHub
parent 8dc3f910f5
commit 5bc49f27e6
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 17 additions and 139 deletions

View File

@ -35,6 +35,13 @@ type ConditionsAccessor interface {
SetConditions(Conditions)
}
// ConditionAccessor is used to access a condition through it's type
type ConditionAccessor interface {
// GetCondition finds and returns the Condition that matches the ConditionType
// It should return nil if the condition type is not present
GetCondition(t ConditionType) *Condition
}
// ConditionSet is an abstract collection of the possible ConditionType values
// that a particular resource might expose. It also holds the "happy condition"
// for that resource, which we define to be one of Ready or Succeeded depending
@ -48,6 +55,8 @@ type ConditionSet struct {
// ConditionManager allows a resource to operate on its Conditions using higher
// order operations.
type ConditionManager interface {
ConditionAccessor
// IsHappy looks at the happy condition and returns true if that condition is
// set to true.
IsHappy() bool
@ -55,10 +64,6 @@ type ConditionManager interface {
// GetTopLevelCondition finds and returns the top level Condition (happy Condition).
GetTopLevelCondition() *Condition
// GetCondition finds and returns the Condition that matches the ConditionType
// previously set on Conditions.
GetCondition(t ConditionType) *Condition
// SetCondition sets or updates the Condition on Conditions for Condition.Type.
// If there is an update, Conditions are stored back sorted.
SetCondition(new Condition)

View File

@ -20,14 +20,12 @@ import (
corev1 "k8s.io/api/core/v1"
"knative.dev/pkg/apis"
duckv1b1 "knative.dev/pkg/apis/duck/v1beta1"
"knative.dev/pkg/test"
)
// CheckCondition checks if condition `c` on `cc` has value `cs`.
// DEPRECATED: Use versioned test helper
func CheckCondition(s *duckv1b1.Status, c apis.ConditionType, cs corev1.ConditionStatus) error {
cond := s.GetCondition(c)
func CheckCondition(a apis.ConditionAccessor, c apis.ConditionType, cs corev1.ConditionStatus) error {
cond := a.GetCondition(c)
if cond == nil {
return fmt.Errorf("condition %v is nil", c)
}
@ -38,28 +36,25 @@ func CheckCondition(s *duckv1b1.Status, c apis.ConditionType, cs corev1.Conditio
}
// CheckConditionOngoing checks if the condition is in state `Unknown`.
// DEPRECATED: Use versioned test helper
func CheckConditionOngoing(s *duckv1b1.Status, c apis.ConditionType, t test.T) {
func CheckConditionOngoing(a apis.ConditionAccessor, c apis.ConditionType, t test.T) {
t.Helper()
if err := CheckCondition(s, c, corev1.ConditionUnknown); err != nil {
if err := CheckCondition(a, c, corev1.ConditionUnknown); err != nil {
t.Error(err)
}
}
// CheckConditionFailed checks if the condition is in state `False`.
// DEPRECATED: Use versioned test helper
func CheckConditionFailed(s *duckv1b1.Status, c apis.ConditionType, t test.T) {
func CheckConditionFailed(a apis.ConditionAccessor, c apis.ConditionType, t test.T) {
t.Helper()
if err := CheckCondition(s, c, corev1.ConditionFalse); err != nil {
if err := CheckCondition(a, c, corev1.ConditionFalse); err != nil {
t.Error(err)
}
}
// CheckConditionSucceeded checks if the condition is in state `True`.
// DEPRECATED: Use versioned test helper
func CheckConditionSucceeded(s *duckv1b1.Status, c apis.ConditionType, t test.T) {
func CheckConditionSucceeded(a apis.ConditionAccessor, c apis.ConditionType, t test.T) {
t.Helper()
if err := CheckCondition(s, c, corev1.ConditionTrue); err != nil {
if err := CheckCondition(a, c, corev1.ConditionTrue); err != nil {
t.Error(err)
}
}

View File

@ -1,61 +0,0 @@
/*
Copyright 2019 The Knative Authors
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package v1
import (
"fmt"
corev1 "k8s.io/api/core/v1"
"knative.dev/pkg/apis"
duckv1 "knative.dev/pkg/apis/duck/v1"
"knative.dev/pkg/test"
)
// CheckCondition checks if condition `c` on `cc` has value `cs`.
func CheckCondition(s *duckv1.Status, c apis.ConditionType, cs corev1.ConditionStatus) error {
cond := s.GetCondition(c)
if cond == nil {
return fmt.Errorf("condition %v is nil", c)
}
if cond.Status != cs {
return fmt.Errorf("condition(%v) = %v, wanted: %v", c, cond, cs)
}
return nil
}
// CheckConditionOngoing checks if the condition is in state `Unknown`.
func CheckConditionOngoing(s *duckv1.Status, c apis.ConditionType, t test.T) {
t.Helper()
if err := CheckCondition(s, c, corev1.ConditionUnknown); err != nil {
t.Error(err)
}
}
// CheckConditionFailed checks if the condition is in state `False`.
func CheckConditionFailed(s *duckv1.Status, c apis.ConditionType, t test.T) {
t.Helper()
if err := CheckCondition(s, c, corev1.ConditionFalse); err != nil {
t.Error(err)
}
}
// CheckConditionSucceeded checks if the condition is in state `True`.
func CheckConditionSucceeded(s *duckv1.Status, c apis.ConditionType, t test.T) {
t.Helper()
if err := CheckCondition(s, c, corev1.ConditionTrue); err != nil {
t.Error(err)
}
}

View File

@ -1,61 +0,0 @@
/*
Copyright 2019 The Knative Authors
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package testing
import (
"fmt"
corev1 "k8s.io/api/core/v1"
"knative.dev/pkg/apis"
duckv1b1 "knative.dev/pkg/apis/duck/v1beta1"
"knative.dev/pkg/test"
)
// CheckCondition checks if condition `c` on `cc` has value `cs`.
func CheckCondition(s *duckv1b1.Status, c apis.ConditionType, cs corev1.ConditionStatus) error {
cond := s.GetCondition(c)
if cond == nil {
return fmt.Errorf("condition %v is nil", c)
}
if cond.Status != cs {
return fmt.Errorf("condition(%v) = %v, wanted: %v", c, cond, cs)
}
return nil
}
// CheckConditionOngoing checks if the condition is in state `Unknown`.
func CheckConditionOngoing(s *duckv1b1.Status, c apis.ConditionType, t test.T) {
t.Helper()
if err := CheckCondition(s, c, corev1.ConditionUnknown); err != nil {
t.Error(err)
}
}
// CheckConditionFailed checks if the condition is in state `False`.
func CheckConditionFailed(s *duckv1b1.Status, c apis.ConditionType, t test.T) {
t.Helper()
if err := CheckCondition(s, c, corev1.ConditionFalse); err != nil {
t.Error(err)
}
}
// CheckConditionSucceeded checks if the condition is in state `True`.
func CheckConditionSucceeded(s *duckv1b1.Status, c apis.ConditionType, t test.T) {
t.Helper()
if err := CheckCondition(s, c, corev1.ConditionTrue); err != nil {
t.Error(err)
}
}