sigs.k8s.io/cluster-api@v1.7.1/util/conditions/merge_test.go (about) 1 /* 2 Copyright 2020 The Kubernetes 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 conditions 18 19 import ( 20 "testing" 21 22 . "github.com/onsi/gomega" 23 corev1 "k8s.io/api/core/v1" 24 25 clusterv1 "sigs.k8s.io/cluster-api/api/v1beta1" 26 ) 27 28 func TestNewConditionsGroup(t *testing.T) { 29 g := NewWithT(t) 30 31 conditions := []*clusterv1.Condition{nil1, true1, true1, falseInfo1, falseWarning1, falseWarning1, falseError1, unknown1} 32 33 got := getConditionGroups(conditionsWithSource(&clusterv1.Cluster{}, conditions...)) 34 35 g.Expect(got).ToNot(BeNil()) 36 g.Expect(got).To(HaveLen(5)) 37 38 // The top group should be False/Error and it should have one condition 39 g.Expect(got.TopGroup().status).To(Equal(corev1.ConditionFalse)) 40 g.Expect(got.TopGroup().severity).To(Equal(clusterv1.ConditionSeverityError)) 41 g.Expect(got.TopGroup().conditions).To(HaveLen(1)) 42 43 // The true group should be true and it should have two conditions 44 g.Expect(got.TrueGroup().status).To(Equal(corev1.ConditionTrue)) 45 g.Expect(got.TrueGroup().severity).To(Equal(clusterv1.ConditionSeverityNone)) 46 g.Expect(got.TrueGroup().conditions).To(HaveLen(2)) 47 48 // The error group should be False/Error and it should have one condition 49 g.Expect(got.ErrorGroup().status).To(Equal(corev1.ConditionFalse)) 50 g.Expect(got.ErrorGroup().severity).To(Equal(clusterv1.ConditionSeverityError)) 51 g.Expect(got.ErrorGroup().conditions).To(HaveLen(1)) 52 53 // The warning group should be False/Warning and it should have two conditions 54 g.Expect(got.WarningGroup().status).To(Equal(corev1.ConditionFalse)) 55 g.Expect(got.WarningGroup().severity).To(Equal(clusterv1.ConditionSeverityWarning)) 56 g.Expect(got.WarningGroup().conditions).To(HaveLen(2)) 57 58 // got[0] should be False/Error and it should have one condition 59 g.Expect(got[0].status).To(Equal(corev1.ConditionFalse)) 60 g.Expect(got[0].severity).To(Equal(clusterv1.ConditionSeverityError)) 61 g.Expect(got[0].conditions).To(HaveLen(1)) 62 63 // got[1] should be False/Warning and it should have two conditions 64 g.Expect(got[1].status).To(Equal(corev1.ConditionFalse)) 65 g.Expect(got[1].severity).To(Equal(clusterv1.ConditionSeverityWarning)) 66 g.Expect(got[1].conditions).To(HaveLen(2)) 67 68 // got[2] should be False/Info and it should have one condition 69 g.Expect(got[2].status).To(Equal(corev1.ConditionFalse)) 70 g.Expect(got[2].severity).To(Equal(clusterv1.ConditionSeverityInfo)) 71 g.Expect(got[2].conditions).To(HaveLen(1)) 72 73 // got[3] should be True and it should have two conditions 74 g.Expect(got[3].status).To(Equal(corev1.ConditionTrue)) 75 g.Expect(got[3].severity).To(Equal(clusterv1.ConditionSeverityNone)) 76 g.Expect(got[3].conditions).To(HaveLen(2)) 77 78 // got[4] should be Unknown and it should have one condition 79 g.Expect(got[4].status).To(Equal(corev1.ConditionUnknown)) 80 g.Expect(got[4].severity).To(Equal(clusterv1.ConditionSeverityNone)) 81 g.Expect(got[4].conditions).To(HaveLen(1)) 82 83 // nil conditions are ignored 84 } 85 86 func TestMergeRespectPriority(t *testing.T) { 87 tests := []struct { 88 name string 89 conditions []*clusterv1.Condition 90 want *clusterv1.Condition 91 }{ 92 { 93 name: "aggregate nil list return nil", 94 conditions: nil, 95 want: nil, 96 }, 97 { 98 name: "aggregate empty list return nil", 99 conditions: []*clusterv1.Condition{}, 100 want: nil, 101 }, 102 { 103 name: "When there is false/error it returns false/error", 104 conditions: []*clusterv1.Condition{falseError1, falseWarning1, falseInfo1, unknown1, true1}, 105 want: FalseCondition("foo", "reason falseError1", clusterv1.ConditionSeverityError, "message falseError1"), 106 }, 107 { 108 name: "When there is false/warning and no false/error, it returns false/warning", 109 conditions: []*clusterv1.Condition{falseWarning1, falseInfo1, unknown1, true1}, 110 want: FalseCondition("foo", "reason falseWarning1", clusterv1.ConditionSeverityWarning, "message falseWarning1"), 111 }, 112 { 113 name: "When there is false/info and no false/error or false/warning, it returns false/info", 114 conditions: []*clusterv1.Condition{falseInfo1, unknown1, true1}, 115 want: FalseCondition("foo", "reason falseInfo1", clusterv1.ConditionSeverityInfo, "message falseInfo1"), 116 }, 117 { 118 name: "When there is true and no false/*, it returns info", 119 conditions: []*clusterv1.Condition{unknown1, true1}, 120 want: TrueCondition("foo"), 121 }, 122 { 123 name: "When there is unknown and no true or false/*, it returns unknown", 124 conditions: []*clusterv1.Condition{unknown1}, 125 want: UnknownCondition("foo", "reason unknown1", "message unknown1"), 126 }, 127 { 128 name: "nil conditions are ignored", 129 conditions: []*clusterv1.Condition{nil1, nil1, nil1}, 130 want: nil, 131 }, 132 } 133 134 for _, tt := range tests { 135 t.Run(tt.name, func(t *testing.T) { 136 g := NewWithT(t) 137 138 got := merge(conditionsWithSource(&clusterv1.Cluster{}, tt.conditions...), "foo", &mergeOptions{}) 139 140 if tt.want == nil { 141 g.Expect(got).To(BeNil()) 142 return 143 } 144 g.Expect(got).To(HaveSameStateOf(tt.want)) 145 }) 146 } 147 } 148 149 func conditionsWithSource(obj Setter, conditions ...*clusterv1.Condition) []localizedCondition { 150 obj.SetConditions(conditionList(conditions...)) 151 152 ret := []localizedCondition{} 153 for i := range conditions { 154 ret = append(ret, localizedCondition{ 155 Condition: conditions[i], 156 Getter: obj, 157 }) 158 } 159 160 return ret 161 }