@@ -17,6 +17,7 @@ limitations under the License.
17
17
package dispatch
18
18
19
19
import (
20
+ "reflect"
20
21
"testing"
21
22
22
23
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
@@ -171,3 +172,121 @@ func TestRetainHealthCheckNodePortInServiceFields(t *testing.T) {
171
172
})
172
173
}
173
174
}
175
+
176
+ func TestRetainClusterIPsInServiceFields (t * testing.T ) {
177
+ tests := []struct {
178
+ name string
179
+ desiredObj * unstructured.Unstructured
180
+ clusterObj * unstructured.Unstructured
181
+ retainSucceed bool
182
+ expectedClusterIPValue * string
183
+ expectedClusterIPsValue []string
184
+ }{
185
+ {
186
+ "cluster object has no clusterIP or clusterIPs" ,
187
+ & unstructured.Unstructured {
188
+ Object : map [string ]interface {}{},
189
+ },
190
+ & unstructured.Unstructured {
191
+ Object : map [string ]interface {}{},
192
+ },
193
+ true ,
194
+ nil ,
195
+ nil ,
196
+ },
197
+ {
198
+ "cluster object has clusterIP" ,
199
+ & unstructured.Unstructured {
200
+ Object : map [string ]interface {}{},
201
+ },
202
+ & unstructured.Unstructured {
203
+ Object : map [string ]interface {}{
204
+ "spec" : map [string ]interface {}{
205
+ "clusterIP" : - 1000 ,
206
+ },
207
+ },
208
+ },
209
+ false ,
210
+ nil ,
211
+ nil ,
212
+ },
213
+ {
214
+ "cluster object has clusterIP only" ,
215
+ & unstructured.Unstructured {
216
+ Object : map [string ]interface {}{},
217
+ },
218
+ & unstructured.Unstructured {
219
+ Object : map [string ]interface {}{
220
+ "spec" : map [string ]interface {}{
221
+ "clusterIP" : "1.2.3.4" ,
222
+ },
223
+ },
224
+ },
225
+ true ,
226
+ pointer .String ("1.2.3.4" ),
227
+ nil ,
228
+ },
229
+ {
230
+ "cluster object has clusterIPs only" ,
231
+ & unstructured.Unstructured {
232
+ Object : map [string ]interface {}{},
233
+ },
234
+ & unstructured.Unstructured {
235
+ Object : map [string ]interface {}{
236
+ "spec" : map [string ]interface {}{
237
+ "clusterIPs" : []interface {}{"1.2.3.4" , "5.6.7.8" },
238
+ },
239
+ },
240
+ },
241
+ true ,
242
+ nil ,
243
+ []string {"1.2.3.4" , "5.6.7.8" },
244
+ },
245
+ {
246
+ "cluster object has both clusterIP and clusterIPs" ,
247
+ & unstructured.Unstructured {
248
+ Object : map [string ]interface {}{},
249
+ },
250
+ & unstructured.Unstructured {
251
+ Object : map [string ]interface {}{
252
+ "spec" : map [string ]interface {}{
253
+ "clusterIP" : "1.2.3.4" ,
254
+ "clusterIPs" : []interface {}{"5.6.7.8" , "9.10.11.12" },
255
+ },
256
+ },
257
+ },
258
+ true ,
259
+ pointer .String ("1.2.3.4" ),
260
+ []string {"5.6.7.8" , "9.10.11.12" },
261
+ },
262
+ }
263
+ for _ , test := range tests {
264
+ t .Run (test .name , func (t * testing.T ) {
265
+ if err := retainServiceFields (test .desiredObj , test .clusterObj ); (err == nil ) != test .retainSucceed {
266
+ t .Fatalf ("test %s fails: unexpected returned error %v" , test .name , err )
267
+ }
268
+
269
+ currentClusterIPValue , ok , err := unstructured .NestedString (test .desiredObj .Object , "spec" , "clusterIP" )
270
+ if err != nil {
271
+ t .Fatalf ("test %s fails: %v" , test .name , err )
272
+ }
273
+ if ! ok && test .expectedClusterIPValue != nil {
274
+ t .Fatalf ("test %s fails: expect specified clusterIP but not found" , test .name )
275
+ }
276
+ if ok && (test .expectedClusterIPValue == nil || * test .expectedClusterIPValue != currentClusterIPValue ) {
277
+ t .Fatalf ("test %s fails: unexpected current clusterIP %s" , test .name , currentClusterIPValue )
278
+ }
279
+
280
+ currentClusterIPsValue , ok , err := unstructured .NestedStringSlice (test .desiredObj .Object , "spec" , "clusterIPs" )
281
+ if err != nil {
282
+ t .Fatalf ("test %s fails: %v" , test .name , err )
283
+ }
284
+ if ! ok && test .expectedClusterIPsValue != nil {
285
+ t .Fatalf ("test %s fails: expect specified clusterIPs but not found" , test .name )
286
+ }
287
+ if ok && ! reflect .DeepEqual (test .expectedClusterIPsValue , currentClusterIPsValue ) {
288
+ t .Fatalf ("test %s fails: unexpected current clusterIPs %v" , test .name , currentClusterIPsValue )
289
+ }
290
+ })
291
+ }
292
+ }
0 commit comments