knative.dev/pkg@v0.0.0-20260602142205-ac97e43f6622/apis/condition_types_test.go (about) 1 /* 2 Copyright 2019 The Knative Authors 3 4 Licensed under the Apache License, Version 2.0 (the "License"); 5 you may not use this file except in compliance with the License. 6 You may obtain a copy of the License at 7 8 http://www.apache.org/licenses/LICENSE-2.0 9 10 Unless required by applicable law or agreed to in writing, software 11 distributed under the License is distributed on an "AS IS" BASIS, 12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 See the License for the specific language governing permissions and 14 limitations under the License. 15 */ 16 17 package apis 18 19 import ( 20 "encoding/json" 21 "testing" 22 "time" 23 24 "github.com/google/go-cmp/cmp" 25 corev1 "k8s.io/api/core/v1" 26 metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" 27 ) 28 29 func TestIsTrue(t *testing.T) { 30 cases := []struct { 31 name string 32 condition *Condition 33 truth bool 34 }{{ 35 name: "empty should be false", 36 condition: &Condition{}, 37 truth: false, 38 }, { 39 name: "True should be true", 40 condition: &Condition{ 41 Status: corev1.ConditionTrue, 42 }, 43 truth: true, 44 }, { 45 name: "False should be false", 46 condition: &Condition{ 47 Status: corev1.ConditionFalse, 48 }, 49 truth: false, 50 }, { 51 name: "Unknown should be false", 52 condition: &Condition{ 53 Status: corev1.ConditionUnknown, 54 }, 55 truth: false, 56 }, { 57 name: "Nil should be false", 58 condition: nil, 59 truth: false, 60 }} 61 62 for _, tc := range cases { 63 t.Run(tc.name, func(t *testing.T) { 64 if e, a := tc.truth, tc.condition.IsTrue(); e != a { 65 t.Errorf("%q expected: %v got: %v", tc.name, e, a) 66 } 67 }) 68 } 69 } 70 71 func TestIsFalse(t *testing.T) { 72 cases := []struct { 73 name string 74 condition *Condition 75 truth bool 76 }{{ 77 name: "empty should be false", 78 condition: &Condition{}, 79 truth: false, 80 }, { 81 name: "True should be false", 82 condition: &Condition{ 83 Status: corev1.ConditionTrue, 84 }, 85 truth: false, 86 }, { 87 name: "False should be true", 88 condition: &Condition{ 89 Status: corev1.ConditionFalse, 90 }, 91 truth: true, 92 }, { 93 name: "Unknown should be false", 94 condition: &Condition{ 95 Status: corev1.ConditionUnknown, 96 }, 97 truth: false, 98 }, { 99 name: "Nil should be false", 100 condition: nil, 101 truth: false, 102 }} 103 104 for _, tc := range cases { 105 t.Run(tc.name, func(t *testing.T) { 106 if e, a := tc.truth, tc.condition.IsFalse(); e != a { 107 t.Errorf("%q expected: %v got: %v", tc.name, e, a) 108 } 109 }) 110 } 111 } 112 113 func TestGetReason(t *testing.T) { 114 cases := []struct { 115 name string 116 condition *Condition 117 want string 118 }{{ 119 name: "nil should be ''", 120 condition: nil, 121 want: "", 122 }, { 123 name: "empty should be ''", 124 condition: &Condition{}, 125 want: "", 126 }, { 127 name: "Set should be set", 128 condition: &Condition{ 129 Reason: "SomeReason", 130 }, 131 want: "SomeReason", 132 }} 133 134 for _, tc := range cases { 135 t.Run(tc.name, func(t *testing.T) { 136 if e, a := tc.want, tc.condition.GetReason(); e != a { 137 t.Errorf("%q expected: %v got: %v", tc.name, e, a) 138 } 139 }) 140 } 141 } 142 143 func TestGetMessage(t *testing.T) { 144 cases := []struct { 145 name string 146 condition *Condition 147 want string 148 }{{ 149 name: "nil should be ''", 150 condition: nil, 151 want: "", 152 }, { 153 name: "empty should be ''", 154 condition: &Condition{}, 155 want: "", 156 }, { 157 name: "Set should be set", 158 condition: &Condition{ 159 Message: "SomeMessage", 160 }, 161 want: "SomeMessage", 162 }} 163 164 for _, tc := range cases { 165 t.Run(tc.name, func(t *testing.T) { 166 if e, a := tc.want, tc.condition.GetMessage(); e != a { 167 t.Errorf("%q expected: %v got: %v", tc.name, e, a) 168 } 169 }) 170 } 171 } 172 173 func TestIsUnknown(t *testing.T) { 174 cases := []struct { 175 name string 176 condition *Condition 177 truth bool 178 }{{ 179 name: "empty should be false", 180 condition: &Condition{}, 181 truth: false, 182 }, { 183 name: "True should be false", 184 condition: &Condition{ 185 Status: corev1.ConditionTrue, 186 }, 187 truth: false, 188 }, { 189 name: "False should be false", 190 condition: &Condition{ 191 Status: corev1.ConditionFalse, 192 }, 193 truth: false, 194 }, { 195 name: "Unknown should be true", 196 condition: &Condition{ 197 Status: corev1.ConditionUnknown, 198 }, 199 truth: true, 200 }, { 201 name: "Nil should be true", 202 condition: nil, 203 truth: true, 204 }} 205 206 for _, tc := range cases { 207 t.Run(tc.name, func(t *testing.T) { 208 if e, a := tc.truth, tc.condition.IsUnknown(); e != a { 209 t.Errorf("%q expected: %v got: %v", tc.name, e, a) 210 } 211 }) 212 } 213 } 214 215 func TestJSON(t *testing.T) { 216 cases := []struct { 217 name string 218 raw string 219 condition *Condition 220 }{{ 221 name: "empty", 222 raw: `{}`, 223 condition: &Condition{}, 224 }, { 225 name: "Type", 226 raw: `{"type":"Foo"}`, 227 condition: &Condition{ 228 Type: "Foo", 229 }, 230 }, { 231 name: "Status", 232 raw: `{"status":"True"}`, 233 condition: &Condition{ 234 Status: corev1.ConditionTrue, 235 }, 236 }, { 237 name: "LastTransitionTime", 238 raw: `{"lastTransitionTime":"1984-02-28T18:52:00Z"}`, 239 condition: &Condition{ 240 LastTransitionTime: VolatileTime{Inner: metav1.NewTime(time.Date(1984, 2, 28, 18, 52, 0, 0, time.UTC))}, 241 }, 242 }, { 243 name: "Reason", 244 raw: `{"reason":"DatTest"}`, 245 condition: &Condition{ 246 Reason: "DatTest", 247 }, 248 }, { 249 name: "Message", 250 raw: `{"message":"this is just a test"}`, 251 condition: &Condition{ 252 Message: "this is just a test", 253 }, 254 }, { 255 name: "all", 256 raw: `{ 257 "type":"Foo", 258 "status":"True", 259 "lastTransitionTime":"1984-02-28T18:52:00Z", 260 "reason":"DatTest", 261 "message":"this is just a test" 262 }`, 263 condition: &Condition{ 264 Type: "Foo", 265 Status: corev1.ConditionTrue, 266 LastTransitionTime: VolatileTime{Inner: metav1.NewTime(time.Date(1984, 2, 28, 18, 52, 0, 0, time.UTC))}, 267 Reason: "DatTest", 268 Message: "this is just a test", 269 }, 270 }} 271 272 for _, tc := range cases { 273 t.Run(tc.name, func(t *testing.T) { 274 cond := &Condition{} 275 if err := json.Unmarshal([]byte(tc.raw), cond); err != nil { 276 t.Errorf("%q unexpected error from json.Unmarshal: %v", tc.name, err) 277 } 278 if diff := cmp.Diff(tc.condition, cond); diff != "" { 279 t.Errorf("%q unexpected diff (-want +got): %s", tc.name, diff) 280 } 281 }) 282 } 283 }