mirror of https://github.com/knative/pkg.git
use interfaces when testing conditions so we don't need versioned methods (#1120)
This commit is contained in:
parent
8dc3f910f5
commit
5bc49f27e6
|
@ -35,6 +35,13 @@ type ConditionsAccessor interface {
|
||||||
SetConditions(Conditions)
|
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
|
// ConditionSet is an abstract collection of the possible ConditionType values
|
||||||
// that a particular resource might expose. It also holds the "happy condition"
|
// 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
|
// 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
|
// ConditionManager allows a resource to operate on its Conditions using higher
|
||||||
// order operations.
|
// order operations.
|
||||||
type ConditionManager interface {
|
type ConditionManager interface {
|
||||||
|
ConditionAccessor
|
||||||
|
|
||||||
// IsHappy looks at the happy condition and returns true if that condition is
|
// IsHappy looks at the happy condition and returns true if that condition is
|
||||||
// set to true.
|
// set to true.
|
||||||
IsHappy() bool
|
IsHappy() bool
|
||||||
|
@ -55,10 +64,6 @@ type ConditionManager interface {
|
||||||
// GetTopLevelCondition finds and returns the top level Condition (happy Condition).
|
// GetTopLevelCondition finds and returns the top level Condition (happy Condition).
|
||||||
GetTopLevelCondition() *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.
|
// SetCondition sets or updates the Condition on Conditions for Condition.Type.
|
||||||
// If there is an update, Conditions are stored back sorted.
|
// If there is an update, Conditions are stored back sorted.
|
||||||
SetCondition(new Condition)
|
SetCondition(new Condition)
|
||||||
|
|
|
@ -20,14 +20,12 @@ import (
|
||||||
|
|
||||||
corev1 "k8s.io/api/core/v1"
|
corev1 "k8s.io/api/core/v1"
|
||||||
"knative.dev/pkg/apis"
|
"knative.dev/pkg/apis"
|
||||||
duckv1b1 "knative.dev/pkg/apis/duck/v1beta1"
|
|
||||||
"knative.dev/pkg/test"
|
"knative.dev/pkg/test"
|
||||||
)
|
)
|
||||||
|
|
||||||
// CheckCondition checks if condition `c` on `cc` has value `cs`.
|
// CheckCondition checks if condition `c` on `cc` has value `cs`.
|
||||||
// DEPRECATED: Use versioned test helper
|
func CheckCondition(a apis.ConditionAccessor, c apis.ConditionType, cs corev1.ConditionStatus) error {
|
||||||
func CheckCondition(s *duckv1b1.Status, c apis.ConditionType, cs corev1.ConditionStatus) error {
|
cond := a.GetCondition(c)
|
||||||
cond := s.GetCondition(c)
|
|
||||||
if cond == nil {
|
if cond == nil {
|
||||||
return fmt.Errorf("condition %v is nil", c)
|
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`.
|
// CheckConditionOngoing checks if the condition is in state `Unknown`.
|
||||||
// DEPRECATED: Use versioned test helper
|
func CheckConditionOngoing(a apis.ConditionAccessor, c apis.ConditionType, t test.T) {
|
||||||
func CheckConditionOngoing(s *duckv1b1.Status, c apis.ConditionType, t test.T) {
|
|
||||||
t.Helper()
|
t.Helper()
|
||||||
if err := CheckCondition(s, c, corev1.ConditionUnknown); err != nil {
|
if err := CheckCondition(a, c, corev1.ConditionUnknown); err != nil {
|
||||||
t.Error(err)
|
t.Error(err)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// CheckConditionFailed checks if the condition is in state `False`.
|
// CheckConditionFailed checks if the condition is in state `False`.
|
||||||
// DEPRECATED: Use versioned test helper
|
func CheckConditionFailed(a apis.ConditionAccessor, c apis.ConditionType, t test.T) {
|
||||||
func CheckConditionFailed(s *duckv1b1.Status, c apis.ConditionType, t test.T) {
|
|
||||||
t.Helper()
|
t.Helper()
|
||||||
if err := CheckCondition(s, c, corev1.ConditionFalse); err != nil {
|
if err := CheckCondition(a, c, corev1.ConditionFalse); err != nil {
|
||||||
t.Error(err)
|
t.Error(err)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// CheckConditionSucceeded checks if the condition is in state `True`.
|
// CheckConditionSucceeded checks if the condition is in state `True`.
|
||||||
// DEPRECATED: Use versioned test helper
|
func CheckConditionSucceeded(a apis.ConditionAccessor, c apis.ConditionType, t test.T) {
|
||||||
func CheckConditionSucceeded(s *duckv1b1.Status, c apis.ConditionType, t test.T) {
|
|
||||||
t.Helper()
|
t.Helper()
|
||||||
if err := CheckCondition(s, c, corev1.ConditionTrue); err != nil {
|
if err := CheckCondition(a, c, corev1.ConditionTrue); err != nil {
|
||||||
t.Error(err)
|
t.Error(err)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -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)
|
|
||||||
}
|
|
||||||
}
|
|
|
@ -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)
|
|
||||||
}
|
|
||||||
}
|
|
Loading…
Reference in New Issue