github.com/cilium/cilium@v1.16.2/operator/pkg/gateway-api/status_test.go (about) 1 // SPDX-License-Identifier: Apache-2.0 2 // Copyright Authors of Cilium 3 4 package gateway_api 5 6 import ( 7 "testing" 8 "time" 9 10 "github.com/stretchr/testify/assert" 11 metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" 12 gatewayv1 "sigs.k8s.io/gateway-api/apis/v1" 13 ) 14 15 func Test_merge(t *testing.T) { 16 now := time.Now() 17 later := time.Now() 18 19 testCases := []struct { 20 name string 21 current []metav1.Condition 22 updates []metav1.Condition 23 expected []metav1.Condition 24 }{ 25 { 26 name: "status updated", 27 current: []metav1.Condition{ 28 newCondition("Ready", metav1.ConditionFalse, "Reason", "Message", now, 1), 29 }, 30 updates: []metav1.Condition{ 31 newCondition("Ready", metav1.ConditionTrue, "Reason", "Message", later, 1), 32 }, 33 expected: []metav1.Condition{ 34 newCondition("Ready", metav1.ConditionTrue, "Reason", "Message", later, 1), 35 }, 36 }, 37 { 38 name: "reason updated", 39 current: []metav1.Condition{ 40 newCondition("Ready", metav1.ConditionFalse, "Reason", "Message", now, 1), 41 }, 42 updates: []metav1.Condition{ 43 newCondition("Ready", metav1.ConditionFalse, "New Reason", "Message", now, 1), 44 }, 45 expected: []metav1.Condition{ 46 newCondition("Ready", metav1.ConditionFalse, "New Reason", "Message", now, 1), 47 }, 48 }, 49 { 50 name: "message updated", 51 current: []metav1.Condition{ 52 newCondition("Ready", metav1.ConditionFalse, "Reason", "Message", now, 1), 53 }, 54 updates: []metav1.Condition{ 55 newCondition("Ready", metav1.ConditionFalse, "Reason", "New Message", now, 1), 56 }, 57 expected: []metav1.Condition{ 58 newCondition("Ready", metav1.ConditionFalse, "Reason", "New Message", now, 1), 59 }, 60 }, 61 { 62 name: "new condition", 63 current: []metav1.Condition{ 64 newCondition("Ready", metav1.ConditionFalse, "Reason", "Message", now, 1), 65 }, 66 updates: []metav1.Condition{ 67 newCondition("Accepted", metav1.ConditionTrue, "Reason", "Another Message", now, 1), 68 }, 69 expected: []metav1.Condition{ 70 newCondition("Ready", metav1.ConditionFalse, "Reason", "Message", now, 1), 71 newCondition("Accepted", metav1.ConditionTrue, "Reason", "Another Message", now, 1), 72 }, 73 }, 74 } 75 76 for _, tc := range testCases { 77 t.Run(tc.name, func(t *testing.T) { 78 got := merge(tc.current, tc.updates...) 79 if conditionChanged(tc.expected[0], got[0]) { 80 assert.Equal(t, tc.expected, got, tc.name) 81 } 82 }) 83 } 84 } 85 86 func Test_conditionChanged(t *testing.T) { 87 testCases := []struct { 88 name string 89 expected bool 90 a, b metav1.Condition 91 }{ 92 { 93 name: "nil and non-nil current are equal", 94 expected: false, 95 a: metav1.Condition{}, 96 }, 97 { 98 name: "empty slices should be equal", 99 expected: false, 100 a: metav1.Condition{}, 101 b: metav1.Condition{}, 102 }, 103 { 104 name: "condition LastTransitionTime should be ignored", 105 expected: false, 106 a: metav1.Condition{ 107 Type: string(gatewayv1.GatewayClassConditionStatusAccepted), 108 Status: metav1.ConditionTrue, 109 LastTransitionTime: metav1.Unix(0, 0), 110 }, 111 b: metav1.Condition{ 112 Type: string(gatewayv1.GatewayClassConditionStatusAccepted), 113 Status: metav1.ConditionTrue, 114 LastTransitionTime: metav1.Unix(1, 0), 115 }, 116 }, 117 { 118 name: "check condition reason differs", 119 expected: true, 120 a: metav1.Condition{ 121 Type: string(gatewayv1.GatewayConditionReady), 122 Status: metav1.ConditionFalse, 123 Reason: "foo", 124 }, 125 b: metav1.Condition{ 126 Type: string(gatewayv1.GatewayConditionReady), 127 Status: metav1.ConditionFalse, 128 Reason: "bar", 129 }, 130 }, 131 { 132 name: "condition status differs", 133 expected: true, 134 a: metav1.Condition{ 135 Type: string(gatewayv1.GatewayClassConditionStatusAccepted), 136 Status: metav1.ConditionTrue, 137 }, 138 b: metav1.Condition{ 139 Type: string(gatewayv1.GatewayClassConditionStatusAccepted), 140 Status: metav1.ConditionFalse, 141 }, 142 }, 143 { 144 name: "observed generation differs", 145 expected: true, 146 a: metav1.Condition{ 147 Type: string(gatewayv1.GatewayClassConditionStatusAccepted), 148 ObservedGeneration: 1, 149 }, 150 b: metav1.Condition{ 151 Type: string(gatewayv1.GatewayClassConditionStatusAccepted), 152 ObservedGeneration: 2, 153 }, 154 }, 155 } 156 157 for _, tc := range testCases { 158 t.Run(tc.name, func(t *testing.T) { 159 res := conditionChanged(tc.a, tc.b) 160 assert.Equal(t, tc.expected, res) 161 }) 162 } 163 }