knative.dev/pkg@v0.0.0-20260602142205-ac97e43f6622/apis/condition_set_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 "testing" 21 22 corev1 "k8s.io/api/core/v1" 23 ) 24 25 func TestNewLivingConditionSet(t *testing.T) { 26 cases := []struct { 27 name string 28 types []ConditionType 29 count int // count includes the happy condition type. 30 }{{ 31 name: "empty", 32 types: []ConditionType(nil), 33 count: 1, 34 }, { 35 name: "one", 36 types: []ConditionType{"Foo"}, 37 count: 2, 38 }, { 39 name: "duplicate in happy", 40 types: []ConditionType{ConditionReady}, 41 count: 1, 42 }, { 43 name: "duplicate in dependents", 44 types: []ConditionType{"Foo", "Bar", "Foo"}, 45 count: 3, 46 }} 47 48 for _, tc := range cases { 49 t.Run(tc.name, func(t *testing.T) { 50 set := NewLivingConditionSet(tc.types...) 51 if e, a := tc.count, 1+len(set.dependents); e != a { 52 t.Errorf("%q expected: %v got: %v", tc.name, e, a) 53 } 54 }) 55 } 56 } 57 58 func TestNewBatchConditionSet(t *testing.T) { 59 cases := []struct { 60 name string 61 types []ConditionType 62 count int // count includes the happy condition type. 63 }{{ 64 name: "empty", 65 types: []ConditionType(nil), 66 count: 1, 67 }, { 68 name: "one", 69 types: []ConditionType{"Foo"}, 70 count: 2, 71 }, { 72 name: "duplicate in happy", 73 types: []ConditionType{ConditionSucceeded}, 74 count: 1, 75 }, { 76 name: "duplicate in dependents", 77 types: []ConditionType{"Foo", "Bar", "Foo"}, 78 count: 3, 79 }} 80 81 for _, tc := range cases { 82 t.Run(tc.name, func(t *testing.T) { 83 set := NewBatchConditionSet(tc.types...) 84 if e, a := tc.count, 1+len(set.dependents); e != a { 85 t.Errorf("%q expected: %v got: %v", tc.name, e, a) 86 } 87 }) 88 } 89 } 90 91 func TestNonTerminalCondition(t *testing.T) { 92 set := NewLivingConditionSet("Foo") 93 status := &TestStatus{} 94 95 manager := set.Manage(status) 96 manager.InitializeConditions() 97 98 if got, want := len(status.c), 2; got != want { 99 t.Errorf("InitializeConditions() = %v, wanted %v", got, want) 100 } 101 102 // Setting the other "terminal" condition makes Ready true. 103 manager.MarkTrue("Foo") 104 if got, want := manager.GetCondition("Ready").Status, corev1.ConditionTrue; got != want { 105 t.Errorf("MarkTrue(Foo) = %v, wanted %v", got, want) 106 } 107 108 // Setting a "non-terminal" condition, doesn't change Ready. 109 manager.MarkUnknown("Bar", "", "") 110 if got, want := manager.GetCondition("Ready").Status, corev1.ConditionTrue; got != want { 111 t.Errorf("MarkUnknown(Bar) = %v, wanted %v", got, want) 112 } 113 114 // Setting the other "terminal" condition by Unknown makes Ready false 115 manager.MarkUnknown("Foo", "", "") 116 if got, want := manager.GetCondition("Ready").Status, corev1.ConditionUnknown; got != want { 117 t.Errorf("MarkUnknown(Foo) = %v, wanted %v", got, want) 118 } 119 120 // Setting the other "terminal" condition by True makes Ready true 121 manager.MarkTrueWithReason("Foo", "", "") 122 if got, want := manager.GetCondition("Ready").Status, corev1.ConditionTrue; got != want { 123 t.Errorf("MarkUnknown(Foo) = %v, wanted %v", got, want) 124 } 125 126 // Setting a "non-terminal" condition, doesn't change Ready. 127 manager.MarkFalse("Bar", "", "") 128 if got, want := manager.GetCondition("Ready").Status, corev1.ConditionTrue; got != want { 129 t.Errorf("MarkFalse(Bar) = %v, wanted %v", got, want) 130 } 131 }