@@ -22,11 +22,23 @@ import (
22
22
. "github.com/onsi/ginkgo/v2"
23
23
. "github.com/onsi/gomega"
24
24
v1 "github.com/submariner-io/submariner/pkg/apis/submariner.io/v1"
25
+ k8snet "k8s.io/utils/net"
26
+ )
27
+
28
+ const (
29
+ ipV4Addr = "1.2.3.4"
30
+ ipV6Addr = "2001:db8:3333:4444:5555:6666:7777:8888"
25
31
)
26
32
27
33
var _ = Describe ("EndpointSpec" , func () {
28
34
Context ("GenerateName" , testGenerateName )
29
35
Context ("Equals" , testEquals )
36
+ Context ("GetHealthCheckIP" , testGetHealthCheckIP )
37
+ Context ("SetHealthCheckIP" , testSetHealthCheckIP )
38
+ Context ("GetPublicIP" , testGetPublicIP )
39
+ Context ("SetPublicIP" , testSetPublicIP )
40
+ Context ("GetPrivateIP" , testGetPrivateIP )
41
+ Context ("SetPrivateIP" , testSetPrivateIP )
30
42
})
31
43
32
44
func testGenerateName () {
@@ -138,3 +150,220 @@ func testEquals() {
138
150
})
139
151
})
140
152
}
153
+
154
+ func testGetIP (ipsSetter func (* v1.EndpointSpec , []string , string ), ipsGetter func (* v1.EndpointSpec , k8snet.IPFamily ) string ) {
155
+ var (
156
+ spec * v1.EndpointSpec
157
+ legacyIPv4IP string
158
+ ips []string
159
+ )
160
+
161
+ BeforeEach (func () {
162
+ legacyIPv4IP = ""
163
+ ips = []string {}
164
+ })
165
+
166
+ JustBeforeEach (func () {
167
+ spec = & v1.EndpointSpec {}
168
+ ipsSetter (spec , ips , legacyIPv4IP )
169
+ })
170
+
171
+ Context ("IPv4" , func () {
172
+ When ("an IPv4 address is present" , func () {
173
+ BeforeEach (func () {
174
+ ips = []string {ipV6Addr , ipV4Addr }
175
+ })
176
+
177
+ It ("should return the address" , func () {
178
+ Expect (ipsGetter (spec , k8snet .IPv4 )).To (Equal (ipV4Addr ))
179
+ })
180
+ })
181
+
182
+ When ("an IPv4 address is not present and the legacy IPv4 address is set" , func () {
183
+ BeforeEach (func () {
184
+ ips = []string {ipV6Addr }
185
+ legacyIPv4IP = ipV4Addr
186
+ })
187
+
188
+ It ("should return the legacy address" , func () {
189
+ Expect (ipsGetter (spec , k8snet .IPv4 )).To (Equal (ipV4Addr ))
190
+ })
191
+ })
192
+
193
+ When ("an IPv4 address is not present and the legacy IPv4 address is not set" , func () {
194
+ It ("should return empty string" , func () {
195
+ Expect (ipsGetter (spec , k8snet .IPv4 )).To (BeEmpty ())
196
+ })
197
+ })
198
+ })
199
+
200
+ Context ("IPv6" , func () {
201
+ When ("an IPv6 address is present" , func () {
202
+ BeforeEach (func () {
203
+ ips = []string {ipV4Addr , ipV6Addr }
204
+ })
205
+
206
+ It ("should return the address" , func () {
207
+ Expect (ipsGetter (spec , k8snet .IPv6 )).To (Equal (ipV6Addr ))
208
+ })
209
+ })
210
+
211
+ When ("an IPv6 address is not present" , func () {
212
+ BeforeEach (func () {
213
+ ips = []string {ipV4Addr }
214
+ })
215
+
216
+ It ("should return empty string" , func () {
217
+ Expect (ipsGetter (spec , k8snet .IPv6 )).To (BeEmpty ())
218
+ })
219
+ })
220
+ })
221
+ }
222
+
223
+ func testSetIP (initIPs func (* v1.EndpointSpec , []string ), ipsSetter func (* v1.EndpointSpec , string ),
224
+ ipsGetter func (* v1.EndpointSpec ) ([]string , string ),
225
+ ) {
226
+ var (
227
+ spec * v1.EndpointSpec
228
+ ipToSet string
229
+ initialIPs []string
230
+ )
231
+
232
+ BeforeEach (func () {
233
+ spec = & v1.EndpointSpec {}
234
+ initialIPs = []string {}
235
+ ipToSet = ""
236
+ })
237
+
238
+ JustBeforeEach (func () {
239
+ initIPs (spec , initialIPs )
240
+ ipsSetter (spec , ipToSet )
241
+ })
242
+
243
+ verifyIPs := func (ips []string , legacyV4 string ) {
244
+ actualIPs , actualLegacy := ipsGetter (spec )
245
+ Expect (actualIPs ).To (Equal (ips ))
246
+ Expect (actualLegacy ).To (Equal (legacyV4 ))
247
+ }
248
+
249
+ Context ("IPv4" , func () {
250
+ BeforeEach (func () {
251
+ ipToSet = ipV4Addr
252
+ })
253
+
254
+ When ("no addresses are present" , func () {
255
+ It ("should add the new address" , func () {
256
+ verifyIPs ([]string {ipToSet }, ipToSet )
257
+ })
258
+ })
259
+
260
+ When ("no IPv4 address is present" , func () {
261
+ BeforeEach (func () {
262
+ initialIPs = []string {ipV6Addr }
263
+ })
264
+
265
+ It ("should add the new address" , func () {
266
+ verifyIPs ([]string {ipV6Addr , ipToSet }, ipToSet )
267
+ })
268
+ })
269
+
270
+ When ("an IPv4 address is already present" , func () {
271
+ BeforeEach (func () {
272
+ initialIPs = []string {"11.22.33.44" }
273
+ })
274
+
275
+ It ("should update address" , func () {
276
+ verifyIPs ([]string {ipToSet }, ipToSet )
277
+ })
278
+ })
279
+ })
280
+
281
+ Context ("IPv6" , func () {
282
+ BeforeEach (func () {
283
+ ipToSet = ipV6Addr
284
+ })
285
+
286
+ When ("no addresses are present" , func () {
287
+ It ("should add the new address" , func () {
288
+ verifyIPs ([]string {ipToSet }, "" )
289
+ })
290
+ })
291
+
292
+ When ("no IPv6 address is present" , func () {
293
+ BeforeEach (func () {
294
+ initialIPs = []string {ipV4Addr }
295
+ })
296
+
297
+ It ("should add the new address" , func () {
298
+ verifyIPs ([]string {ipV4Addr , ipToSet }, "" )
299
+ })
300
+ })
301
+
302
+ When ("an IPv6 address is already present" , func () {
303
+ BeforeEach (func () {
304
+ initialIPs = []string {"1234:cb9:3333:4444:5555:6666:7777:8888" }
305
+ })
306
+
307
+ It ("should update address" , func () {
308
+ verifyIPs ([]string {ipToSet }, "" )
309
+ })
310
+ })
311
+ })
312
+ }
313
+
314
+ func testGetHealthCheckIP () {
315
+ testGetIP (func (s * v1.EndpointSpec , ips []string , ipv4IP string ) {
316
+ s .HealthCheckIPs = ips
317
+ s .HealthCheckIP = ipv4IP
318
+ }, func (s * v1.EndpointSpec , family k8snet.IPFamily ) string {
319
+ return s .GetHealthCheckIP (family )
320
+ })
321
+ }
322
+
323
+ func testSetHealthCheckIP () {
324
+ testSetIP (func (s * v1.EndpointSpec , ips []string ) {
325
+ s .HealthCheckIPs = ips
326
+ }, func (s * v1.EndpointSpec , ip string ) {
327
+ s .SetHealthCheckIP (ip )
328
+ }, func (s * v1.EndpointSpec ) ([]string , string ) {
329
+ return s .HealthCheckIPs , s .HealthCheckIP
330
+ })
331
+ }
332
+
333
+ func testGetPublicIP () {
334
+ testGetIP (func (s * v1.EndpointSpec , ips []string , ipv4IP string ) {
335
+ s .PublicIPs = ips
336
+ s .PublicIP = ipv4IP
337
+ }, func (s * v1.EndpointSpec , family k8snet.IPFamily ) string {
338
+ return s .GetPublicIP (family )
339
+ })
340
+ }
341
+
342
+ func testSetPublicIP () {
343
+ testSetIP (func (s * v1.EndpointSpec , ips []string ) {
344
+ s .PublicIPs = ips
345
+ }, func (s * v1.EndpointSpec , ip string ) {
346
+ s .SetPublicIP (ip )
347
+ }, func (s * v1.EndpointSpec ) ([]string , string ) {
348
+ return s .PublicIPs , s .PublicIP
349
+ })
350
+ }
351
+
352
+ func testGetPrivateIP () {
353
+ testGetIP (func (s * v1.EndpointSpec , ips []string , ipv4IP string ) {
354
+ s .PrivateIPs = ips
355
+ s .PrivateIP = ipv4IP
356
+ }, func (s * v1.EndpointSpec , family k8snet.IPFamily ) string {
357
+ return s .GetPrivateIP (family )
358
+ })
359
+ }
360
+
361
+ func testSetPrivateIP () {
362
+ testSetIP (func (s * v1.EndpointSpec , ips []string ) {
363
+ s .PrivateIPs = ips
364
+ }, func (s * v1.EndpointSpec , ip string ) {
365
+ s .SetPrivateIP (ip )
366
+ }, func (s * v1.EndpointSpec ) ([]string , string ) {
367
+ return s .PrivateIPs , s .PrivateIP
368
+ })
369
+ }
0 commit comments