forked from kubernetes-sigs/cluster-api-provider-vsphere
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstatus_test.go
875 lines (757 loc) · 28.7 KB
/
status_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
/*
Copyright 2023 The Kubernetes Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package ipam
import (
"testing"
"github.com/onsi/gomega"
"gopkg.in/yaml.v2"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
apitypes "k8s.io/apimachinery/pkg/types"
ipamv1a1 "sigs.k8s.io/cluster-api/exp/ipam/api/v1alpha1"
infrav1 "sigs.k8s.io/cluster-api-provider-vsphere/apis/v1beta1"
"sigs.k8s.io/cluster-api-provider-vsphere/pkg/context"
"sigs.k8s.io/cluster-api-provider-vsphere/pkg/context/fake"
"sigs.k8s.io/cluster-api-provider-vsphere/pkg/util"
)
var (
devMAC = "0:0:0:0:a"
myAPIGroup = "my-pool-api-group"
)
func Test_buildIPAMDeviceConfigs(t *testing.T) {
var (
ctx context.VMContext
networkStatus []infrav1.NetworkStatus
claim1, claim2, claim3 *ipamv1a1.IPAddressClaim
address1, address2, address3 *ipamv1a1.IPAddress
g *gomega.WithT
)
before := func() {
ctx = *fake.NewVMContext(fake.NewControllerContext(fake.NewControllerManagerContext()))
networkStatus = []infrav1.NetworkStatus{
{Connected: true, MACAddr: devMAC},
}
g = gomega.NewWithT(t)
namespace := "my-namespace"
claim1 = &ipamv1a1.IPAddressClaim{
ObjectMeta: metav1.ObjectMeta{
Name: "vsphereVM1-0-0",
Namespace: namespace,
},
}
claim2 = &ipamv1a1.IPAddressClaim{
ObjectMeta: metav1.ObjectMeta{
Name: "vsphereVM1-0-1",
Namespace: namespace,
},
}
claim3 = &ipamv1a1.IPAddressClaim{
ObjectMeta: metav1.ObjectMeta{
Name: "vsphereVM1-0-2",
Namespace: namespace,
},
}
address1 = &ipamv1a1.IPAddress{
ObjectMeta: metav1.ObjectMeta{
Name: "vsphereVM1-0-0-address0",
Namespace: namespace,
},
}
address2 = &ipamv1a1.IPAddress{
ObjectMeta: metav1.ObjectMeta{
Name: "vsphereVM1-0-1-address1",
Namespace: namespace,
},
}
address3 = &ipamv1a1.IPAddress{
ObjectMeta: metav1.ObjectMeta{
Name: "vsphereVM1-0-2-address2",
Namespace: namespace,
},
}
}
t.Run("when a device has a IPAddressPool", func(_ *testing.T) {
before()
ctx.VSphereVM = &infrav1.VSphereVM{
ObjectMeta: metav1.ObjectMeta{
Name: "vsphereVM1",
Namespace: "my-namespace",
},
Spec: infrav1.VSphereVMSpec{
VirtualMachineCloneSpec: infrav1.VirtualMachineCloneSpec{
Network: infrav1.NetworkSpec{
Devices: []infrav1.NetworkDeviceSpec{
{
MACAddr: devMAC,
AddressesFromPools: []corev1.TypedLocalObjectReference{
{
APIGroup: &myAPIGroup,
Name: "my-pool-1",
Kind: "my-pool-kind",
},
{
APIGroup: &myAPIGroup,
Name: "my-pool-1",
Kind: "my-pool-kind",
},
{
APIGroup: &myAPIGroup,
Name: "my-pool-ipv6",
Kind: "my-pool-kind",
},
},
},
},
},
},
},
}
// Creates ip address claims
g.Expect(ctx.Client.Create(ctx, claim1)).NotTo(gomega.HaveOccurred())
g.Expect(ctx.Client.Create(ctx, claim2)).NotTo(gomega.HaveOccurred())
g.Expect(ctx.Client.Create(ctx, claim3)).NotTo(gomega.HaveOccurred())
// IP provider has not provided Addresses yet
_, err := buildIPAMDeviceConfigs(ctx, networkStatus)
g.Expect(err).To(gomega.Equal(ErrWaitingForIPAddr))
// Simulate IP provider reconciling one claim
g.Expect(ctx.Client.Create(ctx, address3)).NotTo(gomega.HaveOccurred())
ipAddrClaim := &ipamv1a1.IPAddressClaim{}
ipAddrClaimKey := apitypes.NamespacedName{
Namespace: ctx.VSphereVM.Namespace,
Name: "vsphereVM1-0-2",
}
g.Expect(ctx.Client.Get(ctx, ipAddrClaimKey, ipAddrClaim)).NotTo(gomega.HaveOccurred())
ipAddrClaim.Status.AddressRef.Name = "vsphereVM1-0-2-address2"
g.Expect(ctx.Client.Update(ctx, ipAddrClaim)).NotTo(gomega.HaveOccurred())
// Only the last claim has been bound
_, err = buildIPAMDeviceConfigs(ctx, networkStatus)
g.Expect(err).To(gomega.Equal(ErrWaitingForIPAddr))
// Simulate IP provider reconciling remaining claims
g.Expect(ctx.Client.Create(ctx, address1)).NotTo(gomega.HaveOccurred())
g.Expect(ctx.Client.Create(ctx, address2)).NotTo(gomega.HaveOccurred())
ipAddrClaimKey = apitypes.NamespacedName{
Namespace: ctx.VSphereVM.Namespace,
Name: "vsphereVM1-0-0",
}
g.Expect(ctx.Client.Get(ctx, ipAddrClaimKey, ipAddrClaim)).NotTo(gomega.HaveOccurred())
ipAddrClaim.Status.AddressRef.Name = "vsphereVM1-0-0-address0"
g.Expect(ctx.Client.Update(ctx, ipAddrClaim)).NotTo(gomega.HaveOccurred())
ipAddrClaimKey = apitypes.NamespacedName{
Namespace: ctx.VSphereVM.Namespace,
Name: "vsphereVM1-0-1",
}
g.Expect(ctx.Client.Get(ctx, ipAddrClaimKey, ipAddrClaim)).NotTo(gomega.HaveOccurred())
ipAddrClaim.Status.AddressRef.Name = "vsphereVM1-0-1-address1"
g.Expect(ctx.Client.Update(ctx, ipAddrClaim)).NotTo(gomega.HaveOccurred())
// Now that claims are fulfilled, reconciling should update
// ipAddrs on network spec
configs, err := buildIPAMDeviceConfigs(ctx, networkStatus)
g.Expect(err).NotTo(gomega.HaveOccurred())
g.Expect(configs).To(gomega.HaveLen(1))
config := configs[0]
g.Expect(config.MACAddress).To(gomega.Equal(devMAC))
g.Expect(config.DeviceIndex).To(gomega.Equal(0))
g.Expect(config.IPAMAddresses).To(gomega.HaveLen(3))
})
t.Run("when a device has no pools", func(_ *testing.T) {
before()
ctx.VSphereVM = &infrav1.VSphereVM{
ObjectMeta: metav1.ObjectMeta{
Name: "vsphereVM1",
Namespace: "my-namespace",
},
Spec: infrav1.VSphereVMSpec{
VirtualMachineCloneSpec: infrav1.VirtualMachineCloneSpec{
Network: infrav1.NetworkSpec{
Devices: []infrav1.NetworkDeviceSpec{
{
MACAddr: devMAC,
DHCP4: true,
},
},
},
},
},
}
// The IPAddressClaimed condition should not be added
config, err := buildIPAMDeviceConfigs(ctx, networkStatus)
g.Expect(err).NotTo(gomega.HaveOccurred())
g.Expect(config[0].IPAMAddresses).To(gomega.HaveLen(0))
})
}
func Test_BuildState(t *testing.T) {
var (
ctx context.VMContext
networkStatus []infrav1.NetworkStatus
claim1, claim2, claim3 *ipamv1a1.IPAddressClaim
address1, address2, address3 *ipamv1a1.IPAddress
g *gomega.WithT
)
type nameservers struct {
Addresses []string `json:"addresses"`
}
type ethernet struct {
Addresses []string `json:"addresses"`
DHCP4 bool `json:"dhcp4"`
DHCP6 bool `json:"dhcp6"`
Gateway4 string `json:"gateway4"`
Match map[string]string `json:"match"`
Nameservers nameservers `json:"nameservers"`
}
type network struct {
Ethernets map[string]ethernet `json:"ethernets"`
}
type vmMetadata struct {
Network network `json:"network"`
}
before := func() {
ctx = *fake.NewVMContext(fake.NewControllerContext(fake.NewControllerManagerContext()))
networkStatus = []infrav1.NetworkStatus{
{Connected: true, MACAddr: devMAC},
}
g = gomega.NewWithT(t)
claim1 = &ipamv1a1.IPAddressClaim{
ObjectMeta: metav1.ObjectMeta{
Name: "vsphereVM1-0-0",
Namespace: "my-namespace",
},
}
claim2 = &ipamv1a1.IPAddressClaim{
ObjectMeta: metav1.ObjectMeta{
Name: "vsphereVM1-0-1",
Namespace: "my-namespace",
},
}
claim3 = &ipamv1a1.IPAddressClaim{
ObjectMeta: metav1.ObjectMeta{
Name: "vsphereVM1-0-2",
Namespace: "my-namespace",
},
}
address1 = &ipamv1a1.IPAddress{
ObjectMeta: metav1.ObjectMeta{
Name: "vsphereVM1-0-0-address0",
Namespace: "my-namespace",
},
Spec: ipamv1a1.IPAddressSpec{
Address: "10.0.0.50",
Prefix: 24,
Gateway: "10.0.0.1",
},
}
address2 = &ipamv1a1.IPAddress{
ObjectMeta: metav1.ObjectMeta{
Name: "vsphereVM1-0-1-address1",
Namespace: "my-namespace",
},
Spec: ipamv1a1.IPAddressSpec{
Address: "10.0.1.50",
Prefix: 30,
Gateway: "10.0.0.1",
},
}
address3 = &ipamv1a1.IPAddress{
ObjectMeta: metav1.ObjectMeta{
Name: "vsphereVM1-0-2-address2",
Namespace: "my-namespace",
},
Spec: ipamv1a1.IPAddressSpec{
Address: "fe80::cccc:12",
Prefix: 64,
Gateway: "fe80::cccc:1",
},
}
}
t.Run("when a device has a IPAddressPool", func(_ *testing.T) {
before()
ctx.VSphereVM = &infrav1.VSphereVM{
ObjectMeta: metav1.ObjectMeta{
Name: "vsphereVM1",
Namespace: "my-namespace",
},
Spec: infrav1.VSphereVMSpec{
VirtualMachineCloneSpec: infrav1.VirtualMachineCloneSpec{
Network: infrav1.NetworkSpec{
Devices: []infrav1.NetworkDeviceSpec{
{
MACAddr: devMAC,
AddressesFromPools: []corev1.TypedLocalObjectReference{
{
APIGroup: &myAPIGroup,
Name: "my-pool-1",
Kind: "my-pool-kind",
},
{
APIGroup: &myAPIGroup,
Name: "my-pool-1",
Kind: "my-pool-kind",
},
{
APIGroup: &myAPIGroup,
Name: "my-pool-ipv6",
Kind: "my-pool-kind",
},
},
},
},
},
},
},
}
// Creates ip address claims
g.Expect(ctx.Client.Create(ctx, claim1)).NotTo(gomega.HaveOccurred())
g.Expect(ctx.Client.Create(ctx, claim2)).NotTo(gomega.HaveOccurred())
g.Expect(ctx.Client.Create(ctx, claim3)).NotTo(gomega.HaveOccurred())
// IP provider has not provided Addresses yet
_, err := BuildState(ctx, networkStatus)
g.Expect(err).To(gomega.Equal(ErrWaitingForIPAddr))
// Simulate IP provider reconciling one claim
g.Expect(ctx.Client.Create(ctx, address3)).NotTo(gomega.HaveOccurred())
ipAddrClaim := &ipamv1a1.IPAddressClaim{}
ipAddrClaimKey := apitypes.NamespacedName{
Namespace: ctx.VSphereVM.Namespace,
Name: "vsphereVM1-0-2",
}
g.Expect(ctx.Client.Get(ctx, ipAddrClaimKey, ipAddrClaim)).NotTo(gomega.HaveOccurred())
ipAddrClaim.Status.AddressRef.Name = "vsphereVM1-0-2-address2"
g.Expect(ctx.Client.Update(ctx, ipAddrClaim)).NotTo(gomega.HaveOccurred())
// Only the last claim has been bound
_, err = BuildState(ctx, networkStatus)
g.Expect(err).To(gomega.Equal(ErrWaitingForIPAddr))
// Simulate IP provider reconciling remaining claims
g.Expect(ctx.Client.Create(ctx, address1)).NotTo(gomega.HaveOccurred())
g.Expect(ctx.Client.Create(ctx, address2)).NotTo(gomega.HaveOccurred())
ipAddrClaimKey = apitypes.NamespacedName{
Namespace: ctx.VSphereVM.Namespace,
Name: "vsphereVM1-0-0",
}
g.Expect(ctx.Client.Get(ctx, ipAddrClaimKey, ipAddrClaim)).NotTo(gomega.HaveOccurred())
ipAddrClaim.Status.AddressRef.Name = "vsphereVM1-0-0-address0"
g.Expect(ctx.Client.Update(ctx, ipAddrClaim)).NotTo(gomega.HaveOccurred())
ipAddrClaimKey = apitypes.NamespacedName{
Namespace: ctx.VSphereVM.Namespace,
Name: "vsphereVM1-0-1",
}
g.Expect(ctx.Client.Get(ctx, ipAddrClaimKey, ipAddrClaim)).NotTo(gomega.HaveOccurred())
ipAddrClaim.Status.AddressRef.Name = "vsphereVM1-0-1-address1"
g.Expect(ctx.Client.Update(ctx, ipAddrClaim)).NotTo(gomega.HaveOccurred())
// Now that claims are fulfilled, reconciling should update
// ipAddrs on network spec
state, err := BuildState(ctx, networkStatus)
g.Expect(err).NotTo(gomega.HaveOccurred())
g.Expect(state).To(gomega.HaveLen(1))
g.Expect(state[devMAC].IPAddrs).To(gomega.HaveLen(3))
g.Expect(state[devMAC].IPAddrs[0]).To(gomega.Equal("10.0.0.50/24"))
g.Expect(state[devMAC].Gateway4).To(gomega.Equal("10.0.0.1"))
g.Expect(state[devMAC].IPAddrs[1]).To(gomega.Equal("10.0.1.50/30"))
g.Expect(state[devMAC].Gateway4).To(gomega.Equal("10.0.0.1"))
g.Expect(state[devMAC].IPAddrs[2]).To(gomega.Equal("fe80::cccc:12/64"))
g.Expect(state[devMAC].Gateway6).To(gomega.Equal("fe80::cccc:1"))
})
t.Run("when a device has no pools", func(_ *testing.T) {
before()
ctx.VSphereVM = &infrav1.VSphereVM{
ObjectMeta: metav1.ObjectMeta{
Name: "vsphereVM1",
Namespace: "my-namespace",
},
Spec: infrav1.VSphereVMSpec{
VirtualMachineCloneSpec: infrav1.VirtualMachineCloneSpec{
Network: infrav1.NetworkSpec{
Devices: []infrav1.NetworkDeviceSpec{
{
MACAddr: devMAC,
DHCP4: true,
},
},
},
},
},
}
state, err := BuildState(ctx, networkStatus)
g.Expect(err).NotTo(gomega.HaveOccurred())
g.Expect(state).To(gomega.HaveLen(0))
})
t.Run("when one device has no pool and is DHCP true, and one device has a IPAddressPool", func(_ *testing.T) {
before()
devMAC0 := "0:0:0:0:a"
devMAC1 := "0:0:0:0:b"
claim := &ipamv1a1.IPAddressClaim{
ObjectMeta: metav1.ObjectMeta{
Name: "vsphereVM1-1-0",
Namespace: "my-namespace",
},
}
address := &ipamv1a1.IPAddress{
ObjectMeta: metav1.ObjectMeta{
Name: "vsphereVM1-1-0-address",
Namespace: "my-namespace",
},
Spec: ipamv1a1.IPAddressSpec{
Address: "10.0.0.50",
Prefix: 24,
Gateway: "10.0.0.1",
},
}
ctx.VSphereVM = &infrav1.VSphereVM{
ObjectMeta: metav1.ObjectMeta{
Name: "vsphereVM1",
Namespace: "my-namespace",
},
Spec: infrav1.VSphereVMSpec{
VirtualMachineCloneSpec: infrav1.VirtualMachineCloneSpec{
Network: infrav1.NetworkSpec{
Devices: []infrav1.NetworkDeviceSpec{
{
DHCP4: true,
},
{
AddressesFromPools: []corev1.TypedLocalObjectReference{
{
APIGroup: &myAPIGroup,
Name: "my-pool-1",
Kind: "my-pool-kind",
},
},
Nameservers: []string{"1.1.1.1"},
},
},
},
},
},
}
networkStatus = []infrav1.NetworkStatus{
{Connected: true},
{Connected: true},
}
// Creates ip address claims
g.Expect(ctx.Client.Create(ctx, claim)).NotTo(gomega.HaveOccurred())
// VSphere has not yet assigned MAC addresses to the machine's devices
_, err := BuildState(ctx, networkStatus)
g.Expect(err).To(gomega.MatchError("waiting for devices to have MAC address set"))
networkStatus = []infrav1.NetworkStatus{
{Connected: true, MACAddr: devMAC0},
{Connected: true, MACAddr: devMAC1},
}
// IP provider has not provided Addresses yet
_, err = BuildState(ctx, networkStatus)
g.Expect(err).To(gomega.MatchError("waiting for IP address claims to be bound"))
// Simulate IP provider reconciling one claim
g.Expect(ctx.Client.Create(ctx, address)).NotTo(gomega.HaveOccurred())
ipAddrClaim := &ipamv1a1.IPAddressClaim{}
ipAddrClaimKey := apitypes.NamespacedName{
Namespace: ctx.VSphereVM.Namespace,
Name: "vsphereVM1-1-0",
}
g.Expect(ctx.Client.Get(ctx, ipAddrClaimKey, ipAddrClaim)).NotTo(gomega.HaveOccurred())
ipAddrClaim.Status.AddressRef.Name = "vsphereVM1-1-0-address"
g.Expect(ctx.Client.Update(ctx, ipAddrClaim)).NotTo(gomega.HaveOccurred())
// Now that claims are fulfilled, reconciling should update
// ipAddrs on network spec
ipamState, err := BuildState(ctx, networkStatus)
g.Expect(err).NotTo(gomega.HaveOccurred())
g.Expect(ipamState).To(gomega.HaveLen(1))
_, found := ipamState[devMAC0]
g.Expect(found).To(gomega.BeFalse())
g.Expect(ipamState[devMAC1].IPAddrs).To(gomega.HaveLen(1))
g.Expect(ipamState[devMAC1].IPAddrs[0]).To(gomega.Equal("10.0.0.50/24"))
g.Expect(ipamState[devMAC1].Gateway4).To(gomega.Equal("10.0.0.1"))
// Compute the new metadata from the context to see if the addresses are rendered correctly
metadataBytes, err := util.GetMachineMetadata(ctx.VSphereVM.Name, *ctx.VSphereVM, ipamState, networkStatus...)
g.Expect(err).NotTo(gomega.HaveOccurred())
metadata := vmMetadata{}
g.Expect(yaml.Unmarshal(metadataBytes, &metadata)).To(gomega.Succeed())
g.Expect(metadata.Network.Ethernets["id0"].Addresses).To(gomega.BeNil())
g.Expect(metadata.Network.Ethernets["id0"].DHCP4).To(gomega.BeTrue())
g.Expect(metadata.Network.Ethernets["id1"].Addresses).To(gomega.ConsistOf("10.0.0.50/24"))
g.Expect(metadata.Network.Ethernets["id1"].DHCP4).To(gomega.BeFalse())
g.Expect(metadata.Network.Ethernets["id1"].Gateway4).To(gomega.Equal("10.0.0.1"))
g.Expect(metadata.Network.Ethernets["id1"].Nameservers.Addresses).To(gomega.ConsistOf("1.1.1.1"))
})
t.Run("when realized IP addresses are incorrect", func(t *testing.T) {
var (
devMAC0 = "0:0:0:0:a"
devMAC1 = "0:0:0:0:b"
)
beforeWithClaimsAndAddressCreated := func() {
before()
claim1 = &ipamv1a1.IPAddressClaim{
ObjectMeta: metav1.ObjectMeta{
Name: "vsphereVM1-0-0",
Namespace: "my-namespace",
},
Status: ipamv1a1.IPAddressClaimStatus{
AddressRef: corev1.LocalObjectReference{
Name: "vsphereVM1-0-0",
},
},
}
claim2 = &ipamv1a1.IPAddressClaim{
ObjectMeta: metav1.ObjectMeta{
Name: "vsphereVM1-0-1",
Namespace: "my-namespace",
},
Status: ipamv1a1.IPAddressClaimStatus{
AddressRef: corev1.LocalObjectReference{
Name: "vsphereVM1-0-1",
},
},
}
claim3 = &ipamv1a1.IPAddressClaim{
ObjectMeta: metav1.ObjectMeta{
Name: "vsphereVM1-1-0",
Namespace: "my-namespace",
},
Status: ipamv1a1.IPAddressClaimStatus{
AddressRef: corev1.LocalObjectReference{
Name: "vsphereVM1-1-0",
},
},
}
address1 = &ipamv1a1.IPAddress{
ObjectMeta: metav1.ObjectMeta{
Name: "vsphereVM1-0-0",
Namespace: "my-namespace",
},
Spec: ipamv1a1.IPAddressSpec{
Address: "10.0.1.50",
Prefix: 24,
Gateway: "10.0.0.1",
},
}
address2 = &ipamv1a1.IPAddress{
ObjectMeta: metav1.ObjectMeta{
Name: "vsphereVM1-0-1",
Namespace: "my-namespace",
},
Spec: ipamv1a1.IPAddressSpec{
Address: "10.0.1.51",
Prefix: 24,
Gateway: "10.0.0.1",
},
}
address3 = &ipamv1a1.IPAddress{
ObjectMeta: metav1.ObjectMeta{
Name: "vsphereVM1-1-0",
Namespace: "my-namespace",
},
Spec: ipamv1a1.IPAddressSpec{
Address: "11.0.1.50",
Prefix: 24,
Gateway: "11.0.0.1",
},
}
ctx.VSphereVM = &infrav1.VSphereVM{
ObjectMeta: metav1.ObjectMeta{
Name: "vsphereVM1",
Namespace: "my-namespace",
},
Spec: infrav1.VSphereVMSpec{
VirtualMachineCloneSpec: infrav1.VirtualMachineCloneSpec{
Network: infrav1.NetworkSpec{
Devices: []infrav1.NetworkDeviceSpec{
{
MACAddr: devMAC0,
AddressesFromPools: []corev1.TypedLocalObjectReference{
{
APIGroup: &myAPIGroup,
Name: "my-pool-1",
Kind: "my-pool-kind",
},
{
APIGroup: &myAPIGroup,
Name: "my-pool-2",
Kind: "my-pool-kind",
},
},
},
{
MACAddr: devMAC1,
AddressesFromPools: []corev1.TypedLocalObjectReference{
{
APIGroup: &myAPIGroup,
Name: "my-pool-3",
Kind: "my-pool-kind",
},
},
},
},
},
},
},
}
networkStatus = []infrav1.NetworkStatus{
{Connected: true, MACAddr: devMAC0},
{Connected: true, MACAddr: devMAC1},
}
g.Expect(ctx.Client.Create(ctx, claim1)).NotTo(gomega.HaveOccurred())
g.Expect(ctx.Client.Create(ctx, claim2)).NotTo(gomega.HaveOccurred())
g.Expect(ctx.Client.Create(ctx, claim3)).NotTo(gomega.HaveOccurred())
g.Expect(ctx.Client.Create(ctx, address1)).NotTo(gomega.HaveOccurred())
g.Expect(ctx.Client.Create(ctx, address2)).NotTo(gomega.HaveOccurred())
g.Expect(ctx.Client.Create(ctx, address3)).NotTo(gomega.HaveOccurred())
}
t.Run("when a provider assigns an IPAddress without an Address field", func(_ *testing.T) {
beforeWithClaimsAndAddressCreated()
address1.Spec.Address = ""
g.Expect(ctx.Client.Update(ctx, address1)).NotTo(gomega.HaveOccurred())
_, err := BuildState(ctx, networkStatus)
g.Expect(err).To(gomega.HaveOccurred())
g.Expect(err).To(gomega.MatchError("IPAddress my-namespace/vsphereVM1-0-0 has invalid ip address: \"/24\""))
})
t.Run("when a provider assigns an IPAddress with an invalid IP in the Address field", func(_ *testing.T) {
beforeWithClaimsAndAddressCreated()
// Simulate an invalid ip address was provided: the address is not a valid ip
address1.Spec.Address = "invalid-ip"
g.Expect(ctx.Client.Update(ctx, address1)).NotTo(gomega.HaveOccurred())
_, err := BuildState(ctx, networkStatus)
g.Expect(err).To(gomega.HaveOccurred())
g.Expect(err).To(gomega.MatchError("IPAddress my-namespace/vsphereVM1-0-0 has invalid ip address: \"invalid-ip/24\""))
})
t.Run("when a provider assigns an IPAddress with an invalid value in the Prefix field", func(_ *testing.T) {
beforeWithClaimsAndAddressCreated()
// Simulate an invalid prefix address was provided: the prefix is out of bounds
address1.Spec.Prefix = 200
g.Expect(ctx.Client.Update(ctx, address1)).NotTo(gomega.HaveOccurred())
_, err := BuildState(ctx, networkStatus)
g.Expect(err).To(gomega.HaveOccurred())
g.Expect(err).To(gomega.MatchError("IPAddress my-namespace/vsphereVM1-0-0 has invalid ip address: \"10.0.1.50/200\""))
})
t.Run("when a provider assigns an IPv4 IPAddress without a Gateway field", func(_ *testing.T) {
beforeWithClaimsAndAddressCreated()
address1.Spec.Gateway = ""
g.Expect(ctx.Client.Update(ctx, address1)).NotTo(gomega.HaveOccurred())
_, err := BuildState(ctx, networkStatus)
g.Expect(err).NotTo(gomega.HaveOccurred())
})
t.Run("when a provider assigns an IPv6 IPAddress without a Gateway field", func(_ *testing.T) {
beforeWithClaimsAndAddressCreated()
address1.Spec.Address = "fd00:dddd::1"
address1.Spec.Gateway = ""
g.Expect(ctx.Client.Update(ctx, address1)).NotTo(gomega.HaveOccurred())
_, err := BuildState(ctx, networkStatus)
g.Expect(err).NotTo(gomega.HaveOccurred())
})
t.Run("when a provider assigns an IPAddress with an invalid value in the Gateway field", func(_ *testing.T) {
beforeWithClaimsAndAddressCreated()
// Simulate an invalid gateway was provided: the gateway is an invalid ip
address1.Spec.Gateway = "invalid-gateway"
g.Expect(ctx.Client.Update(ctx, address1)).NotTo(gomega.HaveOccurred())
_, err := BuildState(ctx, networkStatus)
g.Expect(err).To(gomega.HaveOccurred())
g.Expect(err).To(gomega.MatchError("IPAddress my-namespace/vsphereVM1-0-0 has invalid gateway: \"invalid-gateway\""))
})
t.Run("when a provider assigns an IPAddress where the Gateway and Address fields are mismatched", func(_ *testing.T) {
beforeWithClaimsAndAddressCreated()
// Simulate mismatch address and gateways were provided
address1.Spec.Address = "10.0.1.50"
address1.Spec.Gateway = "fd01::1"
g.Expect(ctx.Client.Update(ctx, address1)).NotTo(gomega.HaveOccurred())
_, err := BuildState(ctx, networkStatus)
g.Expect(err).To(gomega.HaveOccurred())
g.Expect(err).To(gomega.MatchError("IPAddress my-namespace/vsphereVM1-0-0 has mismatched gateway and address IP families"))
// Simulate mismatch address and gateways were provided
address1.Spec.Address = "fd00:cccc::1"
address1.Spec.Gateway = "10.0.0.1"
g.Expect(ctx.Client.Update(ctx, address1)).NotTo(gomega.HaveOccurred())
_, err = BuildState(ctx, networkStatus)
g.Expect(err).To(gomega.HaveOccurred())
g.Expect(err).To(gomega.MatchError("IPAddress my-namespace/vsphereVM1-0-0 has mismatched gateway and address IP families"))
})
t.Run("when there are multiple IPAddresses for a device with different Gateways", func(_ *testing.T) {
beforeWithClaimsAndAddressCreated()
// Simulate multiple gateways were provided
address1.Spec.Address = "10.0.1.50"
address1.Spec.Gateway = "10.0.0.2"
g.Expect(ctx.Client.Update(ctx, address1)).NotTo(gomega.HaveOccurred())
address2.Spec.Address = "10.0.1.51"
address2.Spec.Gateway = "10.0.0.3"
g.Expect(ctx.Client.Update(ctx, address2)).NotTo(gomega.HaveOccurred())
_, err := BuildState(ctx, networkStatus)
g.Expect(err).To(gomega.HaveOccurred())
g.Expect(err).To(gomega.MatchError("the IPv4 IPAddresses assigned to the same device (index 0) do not have the same gateway"))
// Simulate multiple gateways were provided
address1.Spec.Address = "fd00:cccc::2"
address1.Spec.Gateway = "fd00::1"
g.Expect(ctx.Client.Update(ctx, address1)).NotTo(gomega.HaveOccurred())
address2.Spec.Address = "fd00:cccc::3"
address2.Spec.Gateway = "fd00::2"
g.Expect(ctx.Client.Update(ctx, address2)).NotTo(gomega.HaveOccurred())
_, err = BuildState(ctx, networkStatus)
g.Expect(err).To(gomega.HaveOccurred())
g.Expect(err).To(gomega.MatchError("the IPv6 IPAddresses assigned to the same device (index 0) do not have the same gateway"))
})
t.Run("when a user specified gateway does not match the gateway provided by IPAM", func(_ *testing.T) {
beforeWithClaimsAndAddressCreated()
ctx.VSphereVM.Spec.VirtualMachineCloneSpec.Network.Devices[0].Gateway4 = "10.10.10.1"
ctx.VSphereVM.Spec.VirtualMachineCloneSpec.Network.Devices[0].Gateway6 = "fd00::2"
address2.Spec.Address = "fd00:cccc::1"
address2.Spec.Gateway = "fd00::1"
g.Expect(ctx.Client.Update(ctx, address2)).NotTo(gomega.HaveOccurred())
_, err := BuildState(ctx, networkStatus)
g.Expect(err).To(gomega.HaveOccurred())
g.Expect(err).To(gomega.MatchError(gomega.ContainSubstring("the IPv4 Gateway for IPAddress vsphereVM1-0-0 does not match the Gateway4 already configured on device (index 0)")))
// Fix the Gateway4 for dev0
ctx.VSphereVM.Spec.VirtualMachineCloneSpec.Network.Devices[0].Gateway4 = "10.0.0.1"
_, err = BuildState(ctx, networkStatus)
g.Expect(err).To(gomega.HaveOccurred())
g.Expect(err).To(gomega.MatchError("the IPv6 Gateway for IPAddress vsphereVM1-0-1 does not match the Gateway6 already configured on device (index 0)"))
})
t.Run("when there are multiple IPAM ip configuration issues on one vm, it notes all of the problems", func(_ *testing.T) {
beforeWithClaimsAndAddressCreated()
beforeWithClaimsAndAddressCreated()
address1.Spec.Address = "10.10.10.10.10"
address2.Spec.Address = "11.11.11.11.11"
address3.Spec.Address = "12.12.12.12.12"
g.Expect(ctx.Client.Update(ctx, address1)).NotTo(gomega.HaveOccurred())
g.Expect(ctx.Client.Update(ctx, address2)).NotTo(gomega.HaveOccurred())
g.Expect(ctx.Client.Update(ctx, address3)).NotTo(gomega.HaveOccurred())
_, err := BuildState(ctx, networkStatus)
g.Expect(err).To(gomega.HaveOccurred())
g.Expect(err).To(gomega.MatchError(
gomega.ContainSubstring("IPAddress my-namespace/vsphereVM1-0-0 has invalid ip address: \"10.10.10.10.10/24\"")))
g.Expect(err).To(gomega.MatchError(
gomega.ContainSubstring("IPAddress my-namespace/vsphereVM1-0-1 has invalid ip address: \"11.11.11.11.11/24\"")))
g.Expect(err).To(gomega.MatchError(
gomega.ContainSubstring("IPAddress my-namespace/vsphereVM1-1-0 has invalid ip address: \"12.12.12.12.12/24\"")))
})
t.Run("when there are multiple IPAM gateway configuration issues on one vm, it notes all of the problems", func(_ *testing.T) {
beforeWithClaimsAndAddressCreated()
address1.Spec.Gateway = "10.10.10.10.10"
address2.Spec.Gateway = "11.11.11.11.11"
address3.Spec.Gateway = "12.12.12.12.12"
g.Expect(ctx.Client.Update(ctx, address1)).NotTo(gomega.HaveOccurred())
g.Expect(ctx.Client.Update(ctx, address2)).NotTo(gomega.HaveOccurred())
g.Expect(ctx.Client.Update(ctx, address3)).NotTo(gomega.HaveOccurred())
_, err := BuildState(ctx, networkStatus)
g.Expect(err).To(gomega.HaveOccurred())
g.Expect(err).To(gomega.MatchError(
gomega.ContainSubstring("IPAddress my-namespace/vsphereVM1-0-0 has invalid gateway: \"10.10.10.10.10\"")))
g.Expect(err).To(gomega.MatchError(
gomega.ContainSubstring("IPAddress my-namespace/vsphereVM1-0-1 has invalid gateway: \"11.11.11.11.11\"")))
g.Expect(err).To(gomega.MatchError(
gomega.ContainSubstring("IPAddress my-namespace/vsphereVM1-1-0 has invalid gateway: \"12.12.12.12.12\"")))
})
t.Run("when there are duplicate IPAddresses", func(_ *testing.T) {
beforeWithClaimsAndAddressCreated()
address1.Spec.Address = "10.0.0.50"
address2.Spec.Address = "10.0.0.50"
g.Expect(ctx.Client.Update(ctx, address1)).NotTo(gomega.HaveOccurred())
g.Expect(ctx.Client.Update(ctx, address2)).NotTo(gomega.HaveOccurred())
_, err := BuildState(ctx, networkStatus)
g.Expect(err).To(gomega.HaveOccurred())
g.Expect(err).To(gomega.MatchError(
gomega.ContainSubstring("IPAddress my-namespace/vsphereVM1-0-1 is a duplicate of another address: \"10.0.0.50/24\"")))
})
})
}