knative.dev/pkg@v0.0.0-20260602142205-ac97e43f6622/apis/testing/conditions.go (about) 1 /* 2 Copyright 2019 The Knative Authors 3 Licensed under the Apache License, Version 2.0 (the "License"); 4 you may not use this file except in compliance with the License. 5 You may obtain a copy of the License at 6 7 http://www.apache.org/licenses/LICENSE-2.0 8 9 Unless required by applicable law or agreed to in writing, software 10 distributed under the License is distributed on an "AS IS" BASIS, 11 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 See the License for the specific language governing permissions and 13 limitations under the License. 14 */ 15 16 package testing 17 18 import ( 19 "fmt" 20 21 corev1 "k8s.io/api/core/v1" 22 "knative.dev/pkg/apis" 23 ) 24 25 type ti interface { 26 Helper() 27 Error(args ...interface{}) 28 } 29 30 // CheckCondition checks if condition `c` on `cc` has value `cs`. 31 func CheckCondition(a apis.ConditionAccessor, c apis.ConditionType, cs corev1.ConditionStatus) error { 32 cond := a.GetCondition(c) 33 if cond == nil { 34 return fmt.Errorf("condition %v is nil", c) 35 } 36 if cond.Status != cs { 37 return fmt.Errorf("condition(%v) = %v, wanted: %v", c, cond, cs) 38 } 39 return nil 40 } 41 42 // CheckConditionOngoing checks if the condition is in state `Unknown`. 43 func CheckConditionOngoing(a apis.ConditionAccessor, c apis.ConditionType, t ti) { 44 t.Helper() 45 if err := CheckCondition(a, c, corev1.ConditionUnknown); err != nil { 46 t.Error(err) 47 } 48 } 49 50 // CheckConditionFailed checks if the condition is in state `False`. 51 func CheckConditionFailed(a apis.ConditionAccessor, c apis.ConditionType, t ti) { 52 t.Helper() 53 if err := CheckCondition(a, c, corev1.ConditionFalse); err != nil { 54 t.Error(err) 55 } 56 } 57 58 // CheckConditionSucceeded checks if the condition is in state `True`. 59 func CheckConditionSucceeded(a apis.ConditionAccessor, c apis.ConditionType, t ti) { 60 t.Helper() 61 if err := CheckCondition(a, c, corev1.ConditionTrue); err != nil { 62 t.Error(err) 63 } 64 }