|
| 1 | +package backend |
| 2 | + |
| 3 | +import ( |
| 4 | + "sync" |
| 5 | + "testing" |
| 6 | + |
| 7 | + v1 "k8s.io/api/core/v1" |
| 8 | + discoveryv1 "k8s.io/api/discovery/v1" |
| 9 | +) |
| 10 | + |
| 11 | +var ( |
| 12 | + basePod1 = Pod{Name: "pod1"} |
| 13 | + basePod2 = Pod{Name: "pod2"} |
| 14 | + basePod3 = Pod{Name: "pod3"} |
| 15 | +) |
| 16 | + |
| 17 | +func TestUpdateDatastore_EndpointSliceReconciler(t *testing.T) { |
| 18 | + tests := []struct { |
| 19 | + name string |
| 20 | + datastore K8sDatastore |
| 21 | + incomingSlice *discoveryv1.EndpointSlice |
| 22 | + want K8sDatastore |
| 23 | + }{ |
| 24 | + { |
| 25 | + name: "Add new pod", |
| 26 | + datastore: K8sDatastore{ |
| 27 | + Pods: populateMap(basePod1, basePod2), |
| 28 | + Port: "8000", |
| 29 | + }, |
| 30 | + incomingSlice: &discoveryv1.EndpointSlice{ |
| 31 | + Endpoints: []discoveryv1.Endpoint{ |
| 32 | + { |
| 33 | + TargetRef: &v1.ObjectReference{ |
| 34 | + Name: "pod1", |
| 35 | + }, |
| 36 | + Zone: new(string), |
| 37 | + Conditions: discoveryv1.EndpointConditions{ |
| 38 | + Ready: truePointer(), |
| 39 | + }, |
| 40 | + Addresses: []string{"0.0.0.0"}, |
| 41 | + }, |
| 42 | + { |
| 43 | + TargetRef: &v1.ObjectReference{ |
| 44 | + Name: "pod2", |
| 45 | + }, |
| 46 | + Zone: new(string), |
| 47 | + Conditions: discoveryv1.EndpointConditions{ |
| 48 | + Ready: truePointer(), |
| 49 | + }, |
| 50 | + Addresses: []string{"0.0.0.0"}, |
| 51 | + }, |
| 52 | + { |
| 53 | + TargetRef: &v1.ObjectReference{ |
| 54 | + Name: "pod3", |
| 55 | + }, |
| 56 | + Zone: new(string), |
| 57 | + Conditions: discoveryv1.EndpointConditions{ |
| 58 | + Ready: truePointer(), |
| 59 | + }, |
| 60 | + Addresses: []string{"0.0.0.0"}, |
| 61 | + }, |
| 62 | + }, |
| 63 | + }, |
| 64 | + want: K8sDatastore{ |
| 65 | + Pods: populateMap(basePod1, basePod2, basePod3), |
| 66 | + Port: "8000", |
| 67 | + }, |
| 68 | + }, |
| 69 | + { |
| 70 | + name: "New pod, but its not ready yet. Do not add.", |
| 71 | + datastore: K8sDatastore{ |
| 72 | + Pods: populateMap(basePod1, basePod2), |
| 73 | + Port: "8000", |
| 74 | + }, |
| 75 | + incomingSlice: &discoveryv1.EndpointSlice{ |
| 76 | + Endpoints: []discoveryv1.Endpoint{ |
| 77 | + { |
| 78 | + TargetRef: &v1.ObjectReference{ |
| 79 | + Name: "pod1", |
| 80 | + }, |
| 81 | + Zone: new(string), |
| 82 | + Conditions: discoveryv1.EndpointConditions{ |
| 83 | + Ready: truePointer(), |
| 84 | + }, |
| 85 | + Addresses: []string{"0.0.0.0"}, |
| 86 | + }, |
| 87 | + { |
| 88 | + TargetRef: &v1.ObjectReference{ |
| 89 | + Name: "pod2", |
| 90 | + }, |
| 91 | + Zone: new(string), |
| 92 | + Conditions: discoveryv1.EndpointConditions{ |
| 93 | + Ready: truePointer(), |
| 94 | + }, |
| 95 | + Addresses: []string{"0.0.0.0"}, |
| 96 | + }, |
| 97 | + { |
| 98 | + TargetRef: &v1.ObjectReference{ |
| 99 | + Name: "pod3", |
| 100 | + }, |
| 101 | + Zone: new(string), |
| 102 | + Conditions: discoveryv1.EndpointConditions{ |
| 103 | + Ready: new(bool), |
| 104 | + }, |
| 105 | + Addresses: []string{"0.0.0.0"}, |
| 106 | + }, |
| 107 | + }, |
| 108 | + }, |
| 109 | + want: K8sDatastore{ |
| 110 | + Pods: populateMap(basePod1, basePod2), |
| 111 | + Port: "8000", |
| 112 | + }, |
| 113 | + }, |
| 114 | + { |
| 115 | + name: "Existing pod not ready, new pod added, and is ready", |
| 116 | + datastore: K8sDatastore{ |
| 117 | + Pods: populateMap(basePod1, basePod2), |
| 118 | + Port: "8000", |
| 119 | + }, |
| 120 | + incomingSlice: &discoveryv1.EndpointSlice{ |
| 121 | + Endpoints: []discoveryv1.Endpoint{ |
| 122 | + { |
| 123 | + TargetRef: &v1.ObjectReference{ |
| 124 | + Name: "pod1", |
| 125 | + }, |
| 126 | + Zone: new(string), |
| 127 | + Conditions: discoveryv1.EndpointConditions{ |
| 128 | + Ready: new(bool), |
| 129 | + }, |
| 130 | + Addresses: []string{"0.0.0.0"}, |
| 131 | + }, |
| 132 | + { |
| 133 | + TargetRef: &v1.ObjectReference{ |
| 134 | + Name: "pod2", |
| 135 | + }, |
| 136 | + Zone: new(string), |
| 137 | + Conditions: discoveryv1.EndpointConditions{ |
| 138 | + Ready: truePointer(), |
| 139 | + }, |
| 140 | + Addresses: []string{"0.0.0.0"}, |
| 141 | + }, |
| 142 | + { |
| 143 | + TargetRef: &v1.ObjectReference{ |
| 144 | + Name: "pod3", |
| 145 | + }, |
| 146 | + Zone: new(string), |
| 147 | + Conditions: discoveryv1.EndpointConditions{ |
| 148 | + Ready: truePointer(), |
| 149 | + }, |
| 150 | + Addresses: []string{"0.0.0.0"}, |
| 151 | + }, |
| 152 | + }, |
| 153 | + }, |
| 154 | + want: K8sDatastore{ |
| 155 | + Pods: populateMap(basePod3, basePod2), |
| 156 | + Port: "8000", |
| 157 | + }, |
| 158 | + }, |
| 159 | + } |
| 160 | + for _, test := range tests { |
| 161 | + t.Run(test.name, func(t *testing.T) { |
| 162 | + endpointSliceReconciler := &EndpointSliceReconciler{Datastore: &test.datastore, Zone: ""} |
| 163 | + endpointSliceReconciler.updateDatastore(test.incomingSlice) |
| 164 | + |
| 165 | + if mapsEqual(endpointSliceReconciler.Datastore.Pods, test.want.Pods) { |
| 166 | + t.Errorf("Unexpected output pod mismatch. \n Got %v \n Want: %v \n", endpointSliceReconciler.Datastore.Pods, test.want.Pods) |
| 167 | + } |
| 168 | + }) |
| 169 | + } |
| 170 | +} |
| 171 | + |
| 172 | +func mapsEqual(map1, map2 *sync.Map) bool { |
| 173 | + equal := true |
| 174 | + |
| 175 | + map1.Range(func(k, v any) bool { |
| 176 | + if _, ok := map2.Load(k); !ok { |
| 177 | + equal = false |
| 178 | + return false |
| 179 | + } |
| 180 | + return true |
| 181 | + }) |
| 182 | + map2.Range(func(k, v any) bool { |
| 183 | + if _, ok := map1.Load(k); !ok { |
| 184 | + equal = false |
| 185 | + return false |
| 186 | + } |
| 187 | + return true |
| 188 | + }) |
| 189 | + |
| 190 | + return equal |
| 191 | +} |
| 192 | + |
| 193 | +func truePointer() *bool { |
| 194 | + primitivePointersAreSilly := true |
| 195 | + return &primitivePointersAreSilly |
| 196 | +} |
0 commit comments