-
Notifications
You must be signed in to change notification settings - Fork 42
/
prefix_test.go
1805 lines (1615 loc) · 55.2 KB
/
prefix_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
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
package ipam
import (
"context"
"errors"
"fmt"
"net/netip"
"reflect"
"strings"
"sync"
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"golang.org/x/sync/errgroup"
)
// getHostAddresses will return all possible ipadresses a host can get in the given prefix.
// The IPs will be acquired by this method, so that the prefix has no free IPs afterwards.
func (i *ipamer) getHostAddresses(ctx context.Context, prefix string) ([]string, error) {
hostAddresses := []string{}
p, err := i.NewPrefix(ctx, prefix)
if err != nil {
return hostAddresses, err
}
// loop till AcquireIP signals that it has no ips left
for {
ip, err := i.AcquireIP(ctx, p.Cidr)
if errors.Is(err, ErrNoIPAvailable) {
return hostAddresses, nil
}
if err != nil {
return nil, err
}
hostAddresses = append(hostAddresses, ip.IP.String())
}
}
func TestIPRangeOverlapping(t *testing.T) {
ctx := context.Background()
i := New(ctx)
cidr := "10.10.10.0/24"
_, err := i.NewPrefix(ctx, cidr)
require.NoError(t, err)
cidr = "10.10.10.1/24"
_, err = i.NewPrefix(ctx, cidr)
require.Error(t, err)
}
func TestIpamer_AcquireIP(t *testing.T) {
ctx := context.Background()
type fields struct {
prefixCIDR string
namespace string
existingips []string
}
tests := []struct {
name string
fields fields
want *IP
}{
{
name: "Acquire next IP regularly",
fields: fields{
prefixCIDR: "192.168.1.0/24",
existingips: []string{},
},
want: &IP{IP: netip.MustParseAddr("192.168.1.1"), ParentPrefix: "192.168.1.0/24"},
},
{
name: "Acquire next namespaced IP regularly",
fields: fields{
prefixCIDR: "192.168.1.0/24",
namespace: "my-namespace",
existingips: []string{},
},
want: &IP{IP: netip.MustParseAddr("192.168.1.1"), ParentPrefix: "192.168.1.0/24"},
},
{
name: "Acquire next IPv6 regularly",
fields: fields{
prefixCIDR: "2001:0db8:85a3::/124",
existingips: []string{},
},
want: &IP{IP: netip.MustParseAddr("2001:0db8:85a3::1"), ParentPrefix: "2001:0db8:85a3::/124"},
},
{
name: "Acquire next namespaced IPv6 regularly",
fields: fields{
prefixCIDR: "2001:0db8:85a3::/124",
namespace: "my-namespace",
existingips: []string{},
},
want: &IP{IP: netip.MustParseAddr("2001:0db8:85a3::1"), ParentPrefix: "2001:0db8:85a3::/124"},
},
{
name: "Want next IP, network already occupied a little",
fields: fields{
prefixCIDR: "192.168.2.0/30",
existingips: []string{"192.168.2.1"},
},
want: &IP{IP: netip.MustParseAddr("192.168.2.2"), ParentPrefix: "192.168.2.0/30"},
},
{
name: "Want next IPv6, network already occupied a little",
fields: fields{
prefixCIDR: "2001:0db8:85a3::/124",
existingips: []string{"2001:db8:85a3::1"},
},
want: &IP{IP: netip.MustParseAddr("2001:db8:85a3::2"), ParentPrefix: "2001:0db8:85a3::/124"},
},
{
name: "Want next IP, but network is full",
fields: fields{
prefixCIDR: "192.168.3.0/30",
existingips: []string{"192.168.3.1", "192.168.3.2"},
},
want: nil,
},
{
name: "Want next IP, but network is full",
fields: fields{
prefixCIDR: "192.168.4.0/32",
},
want: nil,
},
{
name: "Want next IPv6, but network is full",
fields: fields{
prefixCIDR: "2001:0db8:85a3::/126",
existingips: []string{"2001:db8:85a3::1", "2001:db8:85a3::2", "2001:db8:85a3::3"},
},
want: nil,
},
{
name: "Want next IPv6, but network is full",
fields: fields{
prefixCIDR: "2001:0db8:85a3::/128",
},
want: nil,
},
}
for _, tt := range tests {
test := tt
testWithBackends(t, func(t *testing.T, ipam *ipamer) {
p, err := ipam.NewPrefix(ctx, test.fields.prefixCIDR)
if err != nil {
t.Errorf("Could not create prefix: %v", err)
}
t.Logf("Prefix:%#v", p)
for _, ipString := range test.fields.existingips {
p.ips[ipString] = true
}
var updatedPrefix Prefix
updatedPrefix, err = ipam.storage.UpdatePrefix(ctx, *p, defaultNamespace)
if err != nil {
t.Errorf("Could not update prefix: %v", err)
}
got, _ := ipam.AcquireIP(ctx, updatedPrefix.Cidr)
if test.want == nil || got == nil {
if !reflect.DeepEqual(got, test.want) {
t.Errorf("Ipamer.AcquireIP() want or got is nil, got %v, want %v", got, test.want)
}
} else {
if test.want.IP.Compare(got.IP) != 0 {
t.Errorf("Ipamer.AcquireIP() got %v, want %v", got, test.want)
}
}
})
}
}
func TestIpamer_ReleaseIPFromPrefixIPv4(t *testing.T) {
ctx := context.Background()
testWithBackends(t, func(t *testing.T, ipam *ipamer) {
prefix, err := ipam.NewPrefix(ctx, "192.168.0.0/24")
require.NoError(t, err)
require.NotNil(t, prefix)
err = ipam.ReleaseIPFromPrefix(ctx, prefix.Cidr, "1.2.3.4")
require.Error(t, err)
require.ErrorIs(t, err, ErrNotFound)
require.Equal(t, "NotFound: unable to release ip:1.2.3.4 because it is not allocated in prefix:192.168.0.0/24", err.Error())
err = ipam.ReleaseIPFromPrefix(ctx, "4.5.6.7/23", "1.2.3.4")
require.Error(t, err)
require.ErrorIs(t, err, ErrNotFound)
require.Contains(t, err.Error(), "NotFound: unable to find prefix for cidr:4.5.6.7/23")
})
}
func TestIpamer_ReleaseIPFromPrefixIPv6(t *testing.T) {
ctx := context.Background()
testWithBackends(t, func(t *testing.T, ipam *ipamer) {
prefix, err := ipam.NewPrefix(ctx, "2001:0db8:85a3::/120")
require.NoError(t, err)
require.NotNil(t, prefix)
err = ipam.ReleaseIPFromPrefix(ctx, prefix.Cidr, "1.2.3.4")
require.Error(t, err)
require.ErrorIs(t, err, ErrNotFound)
require.Equal(t, "NotFound: unable to release ip:1.2.3.4 because it is not allocated in prefix:2001:db8:85a3::/120", err.Error())
err = ipam.ReleaseIPFromPrefix(ctx, prefix.Cidr, "1001:0db8:85a3::1")
require.Error(t, err)
require.ErrorIs(t, err, ErrNotFound)
require.Equal(t, "NotFound: unable to release ip:1001:0db8:85a3::1 because it is not allocated in prefix:2001:db8:85a3::/120", err.Error())
err = ipam.ReleaseIPFromPrefix(ctx, "1001:0db8:85a3::/120", "1.2.3.4")
require.Error(t, err)
require.ErrorIs(t, err, ErrNotFound)
require.Contains(t, err.Error(), "NotFound: unable to find prefix for cidr:1001:0db8:85a3::/120")
})
}
func TestIpamer_AcquireSpecificIP(t *testing.T) {
ctx := context.Background()
testWithBackends(t, func(t *testing.T, ipam *ipamer) {
// IPv4
prefix, err := ipam.NewPrefix(ctx, "192.168.99.0/24")
require.NoError(t, err)
require.Equal(t, uint64(256), prefix.availableips())
// network an broadcast are blocked
require.Equal(t, uint64(2), prefix.acquiredips())
ip1, err := ipam.AcquireSpecificIP(ctx, prefix.Cidr, "192.168.99.1")
require.NoError(t, err)
require.NotNil(t, ip1)
prefix, err = ipam.PrefixFrom(ctx, prefix.Cidr)
require.NoError(t, err)
require.Equal(t, uint64(256), prefix.availableips())
require.Equal(t, uint64(3), prefix.acquiredips())
ip2, err := ipam.AcquireSpecificIP(ctx, prefix.Cidr, "192.168.99.2")
require.NoError(t, err)
require.NotEqual(t, ip1, ip2)
prefix, err = ipam.PrefixFrom(ctx, prefix.Cidr)
require.NoError(t, err)
require.Equal(t, uint64(256), prefix.availableips())
require.Equal(t, uint64(4), prefix.acquiredips())
require.Equal(t, "192.168.99.1", ip1.IP.String())
require.Equal(t, "192.168.99.2", ip2.IP.String())
// Wish IP out of prefix
ip3, err := ipam.AcquireSpecificIP(ctx, prefix.Cidr, "192.168.98.2")
require.Nil(t, ip3)
require.Error(t, err)
require.Equal(t, "given ip:192.168.98.2 is not in 192.168.99.0/24", err.Error())
// Wish IP is invalid
ip4, err := ipam.AcquireSpecificIP(ctx, prefix.Cidr, "192.168.99.1.invalid")
require.Nil(t, ip4)
require.Error(t, err)
require.Equal(t, "given ip:192.168.99.1.invalid in not valid", err.Error())
// Cidr is invalid
ip5, err := ipam.AcquireSpecificIP(ctx, "3.4.5.6/27", "192.168.99.1.invalid")
require.Nil(t, ip5)
require.Error(t, err)
require.ErrorIs(t, err, ErrNotFound)
require.Contains(t, err.Error(), "NotFound: unable to find prefix for cidr:3.4.5.6/27")
prefix, err = ipam.ReleaseIP(ctx, ip1)
require.NoError(t, err)
require.Equal(t, uint64(256), prefix.availableips())
require.Equal(t, uint64(3), prefix.acquiredips())
prefix, err = ipam.ReleaseIP(ctx, ip2)
require.NoError(t, err)
require.Equal(t, uint64(256), prefix.availableips())
require.Equal(t, uint64(2), prefix.acquiredips())
// IPv6
prefix, err = ipam.NewPrefix(ctx, "2001:0db8:85a3::/120")
require.NoError(t, err)
require.Equal(t, uint64(256), prefix.availableips())
// network is blocked
require.Equal(t, uint64(1), prefix.acquiredips())
ip1, err = ipam.AcquireSpecificIP(ctx, prefix.Cidr, "2001:db8:85a3::1")
require.NoError(t, err)
require.NotNil(t, ip1)
prefix, err = ipam.PrefixFrom(ctx, prefix.Cidr)
require.NoError(t, err)
require.Equal(t, uint64(256), prefix.availableips())
require.Equal(t, uint64(2), prefix.acquiredips())
ip2, err = ipam.AcquireSpecificIP(ctx, prefix.Cidr, "2001:0db8:85a3::2")
require.NoError(t, err)
require.NotEqual(t, ip1, ip2)
prefix, err = ipam.PrefixFrom(ctx, prefix.Cidr)
require.NoError(t, err)
require.Equal(t, uint64(256), prefix.availableips())
require.Equal(t, uint64(3), prefix.acquiredips())
require.Equal(t, "2001:db8:85a3::1", ip1.IP.String())
require.Equal(t, "2001:db8:85a3::2", ip2.IP.String())
// Wish IP out of prefix
ip3, err = ipam.AcquireSpecificIP(ctx, prefix.Cidr, "2001:0db8:85a4::1")
require.Nil(t, ip3)
require.Error(t, err)
require.Equal(t, "given ip:2001:0db8:85a4::1 is not in 2001:db8:85a3::/120", err.Error())
// Cidr is invalid
ip5, err = ipam.AcquireSpecificIP(ctx, "2001:0db8:95a3::/120", "2001:0db8:95a3::invalid")
require.Nil(t, ip5)
require.Error(t, err)
require.ErrorIs(t, err, ErrNotFound)
require.Contains(t, err.Error(), "NotFound: unable to find prefix for cidr:2001:0db8:95a3::/120", err.Error())
prefix, err = ipam.ReleaseIP(ctx, ip1)
require.NoError(t, err)
require.Equal(t, uint64(256), prefix.availableips())
require.Equal(t, uint64(2), prefix.acquiredips())
prefix, err = ipam.ReleaseIP(ctx, ip2)
require.NoError(t, err)
require.Equal(t, uint64(256), prefix.availableips())
require.Equal(t, uint64(1), prefix.acquiredips())
})
}
func TestIpamer_AcquireIPCountsIPv4(t *testing.T) {
ctx := context.Background()
testWithBackends(t, func(t *testing.T, ipam *ipamer) {
prefix, err := ipam.NewPrefix(ctx, "192.168.0.0/24")
require.NoError(t, err)
require.Equal(t, uint64(256), prefix.availableips())
// network and broadcast are blocked
require.Equal(t, uint64(2), prefix.acquiredips())
ip1, err := ipam.AcquireIP(ctx, prefix.Cidr)
require.NoError(t, err)
require.NotNil(t, ip1)
prefix, err = ipam.PrefixFrom(ctx, prefix.Cidr)
require.NoError(t, err)
require.Equal(t, uint64(256), prefix.availableips())
require.Equal(t, uint64(3), prefix.acquiredips())
ip2, err := ipam.AcquireIP(ctx, prefix.Cidr)
require.NoError(t, err)
require.NotEqual(t, ip1, ip2)
prefix, err = ipam.PrefixFrom(ctx, prefix.Cidr)
require.NoError(t, err)
require.Equal(t, uint64(256), prefix.availableips())
require.Equal(t, uint64(4), prefix.acquiredips())
require.True(t, strings.HasPrefix(ip1.IP.String(), "192.168.0"))
require.True(t, strings.HasPrefix(ip2.IP.String(), "192.168.0"))
prefix, err = ipam.ReleaseIP(ctx, ip1)
require.NoError(t, err)
require.Equal(t, uint64(256), prefix.availableips())
require.Equal(t, uint64(3), prefix.acquiredips())
prefix, err = ipam.ReleaseIP(ctx, ip2)
require.NoError(t, err)
require.Equal(t, uint64(256), prefix.availableips())
require.Equal(t, uint64(2), prefix.acquiredips())
})
}
func TestIpamer_AcquireIPCountsIPv6(t *testing.T) {
ctx := context.Background()
testWithBackends(t, func(t *testing.T, ipam *ipamer) {
prefix, err := ipam.NewPrefix(ctx, "2001:0db8:85a3::/120")
require.NoError(t, err)
require.Equal(t, uint64(256), prefix.availableips())
// network is blocked
require.Equal(t, uint64(1), prefix.acquiredips())
ip1, err := ipam.AcquireIP(ctx, prefix.Cidr)
require.NoError(t, err)
require.NotNil(t, ip1)
prefix, err = ipam.PrefixFrom(ctx, prefix.Cidr)
require.NoError(t, err)
require.Equal(t, uint64(256), prefix.availableips())
require.Equal(t, uint64(2), prefix.acquiredips())
ip2, err := ipam.AcquireIP(ctx, prefix.Cidr)
require.NoError(t, err)
require.NotEqual(t, ip1, ip2)
prefix, err = ipam.PrefixFrom(ctx, prefix.Cidr)
require.NoError(t, err)
require.Equal(t, uint64(256), prefix.availableips())
require.Equal(t, uint64(3), prefix.acquiredips())
require.True(t, strings.HasPrefix(ip1.IP.String(), "2001:db8:85a3::"))
require.True(t, strings.HasPrefix(ip2.IP.String(), "2001:db8:85a3::"))
prefix, err = ipam.ReleaseIP(ctx, ip1)
require.NoError(t, err)
require.Equal(t, uint64(256), prefix.availableips())
require.Equal(t, uint64(2), prefix.acquiredips())
prefix, err = ipam.ReleaseIP(ctx, ip2)
require.NoError(t, err)
require.Equal(t, uint64(256), prefix.availableips())
require.Equal(t, uint64(1), prefix.acquiredips())
})
}
func TestIpamer_AcquireChildPrefixFragmented(t *testing.T) {
ctx := context.Background()
testWithBackends(t, func(t *testing.T, ipam *ipamer) {
allPrefixes, err := ipam.storage.ReadAllPrefixes(ctx, defaultNamespace)
require.NoError(t, err)
require.Empty(t, allPrefixes)
// Create Prefix with /20
prefix, err := ipam.NewPrefix(ctx, "192.168.0.0/20")
require.NoError(t, err)
s, _ := prefix.availablePrefixes()
require.Equal(t, uint64(1024), s)
require.Empty(t, prefix.acquiredPrefixes())
require.Empty(t, prefix.Usage().AcquiredPrefixes)
// Acquire first half 192.168.0.0/21 = 192.168.0.0 - 192.168.7.254
c1, err := ipam.AcquireChildPrefix(ctx, prefix.Cidr, 21)
require.NoError(t, err)
require.NotNil(t, c1)
prefix, err = ipam.PrefixFrom(ctx, prefix.Cidr)
require.NoError(t, err)
s, _ = prefix.availablePrefixes()
require.Equal(t, uint64(512), s)
require.Equal(t, uint64(1), prefix.acquiredPrefixes())
require.Equal(t, uint64(1), prefix.Usage().AcquiredPrefixes)
// acquire 1/4the of the rest 192.168.8.0/22 = 192.168.8.0 - 192.168.11.254
c2, err := ipam.AcquireChildPrefix(ctx, prefix.Cidr, 22)
require.NoError(t, err)
require.NotNil(t, c2)
prefix, err = ipam.PrefixFrom(ctx, prefix.Cidr)
require.NoError(t, err)
s, a := prefix.availablePrefixes()
// Next free must be 192.168.12.0/22
require.Equal(t, []string{"192.168.12.0/22"}, a)
require.Equal(t, uint64(256), s)
require.Equal(t, uint64(2), prefix.acquiredPrefixes())
require.Equal(t, uint64(2), prefix.Usage().AcquiredPrefixes)
// acquire impossible size
_, err = ipam.AcquireChildPrefix(ctx, prefix.Cidr, 21)
require.EqualError(t, err, "no prefix found in 192.168.0.0/20 with length:21, but 192.168.12.0/22 is available")
// Release small, first half acquired
err = ipam.ReleaseChildPrefix(ctx, c2)
require.NoError(t, err)
// acquire /28
c3, err := ipam.AcquireChildPrefix(ctx, prefix.Cidr, 28)
require.NoError(t, err)
require.NotNil(t, c3)
prefix, err = ipam.PrefixFrom(ctx, prefix.Cidr)
require.NoError(t, err)
s, a = prefix.availablePrefixes()
require.Equal(t, []string{"192.168.8.16/28", "192.168.8.32/27", "192.168.8.64/26", "192.168.8.128/25", "192.168.9.0/24", "192.168.10.0/23", "192.168.12.0/22"}, a)
require.Equal(t, uint64(508), s)
require.Equal(t, uint64(2), prefix.acquiredPrefixes())
require.Equal(t, uint64(2), prefix.Usage().AcquiredPrefixes)
// acquire impossible size
_, err = ipam.AcquireChildPrefix(ctx, prefix.Cidr, 21)
require.EqualError(t, err, "no prefix found in 192.168.0.0/20 with length:21, but 192.168.8.16/28,192.168.8.32/27,192.168.8.64/26,192.168.8.128/25,192.168.9.0/24,192.168.10.0/23,192.168.12.0/22 are available")
// acquire a /22 which must be possible
c4, err := ipam.AcquireChildPrefix(ctx, prefix.Cidr, 22)
require.NoError(t, err)
require.NotNil(t, c4)
require.Equal(t, "192.168.12.0/22", c4.String())
})
}
func TestIpamer_AcquireChildPrefixCounts(t *testing.T) {
ctx := context.Background()
testWithBackends(t, func(t *testing.T, ipam *ipamer) {
allPrefixes, err := ipam.storage.ReadAllPrefixes(ctx, defaultNamespace)
require.NoError(t, err)
require.Empty(t, allPrefixes)
prefix, err := ipam.NewPrefix(ctx, "192.168.0.0/20")
require.NoError(t, err)
s, _ := prefix.availablePrefixes()
require.Equal(t, uint64(1024), s)
require.Equal(t, uint64(0), prefix.acquiredPrefixes())
require.Equal(t, uint64(0), prefix.Usage().AcquiredPrefixes)
usage := prefix.Usage()
require.Equal(t, "ip:2/4096", usage.String())
allPrefixes, err = ipam.storage.ReadAllPrefixes(ctx, defaultNamespace)
require.NoError(t, err)
require.Len(t, allPrefixes, 1)
c1, err := ipam.AcquireChildPrefix(ctx, prefix.Cidr, 22)
require.NoError(t, err)
require.NotNil(t, c1)
prefix, err = ipam.PrefixFrom(ctx, prefix.Cidr)
require.NoError(t, err)
s, _ = prefix.availablePrefixes()
require.Equal(t, uint64(768), s)
require.Equal(t, uint64(1), prefix.acquiredPrefixes())
require.Equal(t, uint64(1), prefix.Usage().AcquiredPrefixes)
usage = prefix.Usage()
require.Equal(t, "ip:2/4096 prefixes alloc:1 avail:768", usage.String())
allPrefixes, err = ipam.storage.ReadAllPrefixes(ctx, defaultNamespace)
require.NoError(t, err)
require.Len(t, allPrefixes, 2)
c2, err := ipam.AcquireChildPrefix(ctx, prefix.Cidr, 22)
require.NoError(t, err)
require.NotNil(t, c2)
prefix, err = ipam.PrefixFrom(ctx, prefix.Cidr)
require.NoError(t, err)
s, _ = prefix.availablePrefixes()
require.Equal(t, uint64(512), s)
require.Equal(t, uint64(2), prefix.acquiredPrefixes())
require.Equal(t, uint64(2), prefix.Usage().AcquiredPrefixes)
require.True(t, strings.HasSuffix(c1.Cidr, "/22"))
require.True(t, strings.HasSuffix(c2.Cidr, "/22"))
require.True(t, strings.HasPrefix(c1.Cidr, "192.168."))
require.True(t, strings.HasPrefix(c2.Cidr, "192.168."))
allPrefixes, err = ipam.storage.ReadAllPrefixes(ctx, defaultNamespace)
require.NoError(t, err)
require.Len(t, allPrefixes, 3)
err = ipam.ReleaseChildPrefix(ctx, c1)
require.NoError(t, err)
prefix, err = ipam.PrefixFrom(ctx, prefix.Cidr)
require.NoError(t, err)
s, _ = prefix.availablePrefixes()
require.Equal(t, uint64(768), s)
require.Equal(t, uint64(1), prefix.acquiredPrefixes())
allPrefixes, err = ipam.storage.ReadAllPrefixes(ctx, defaultNamespace)
require.NoError(t, err)
require.Len(t, allPrefixes, 2)
err = ipam.ReleaseChildPrefix(ctx, c2)
require.NoError(t, err)
prefix, err = ipam.PrefixFrom(ctx, prefix.Cidr)
require.NoError(t, err)
s, _ = prefix.availablePrefixes()
require.Equal(t, uint64(1024), s)
require.Equal(t, uint64(0), prefix.acquiredPrefixes())
require.Equal(t, uint64(0), prefix.Usage().AcquiredPrefixes)
allPrefixes, err = ipam.storage.ReadAllPrefixes(ctx, defaultNamespace)
require.NoError(t, err)
require.Len(t, allPrefixes, 1)
err = ipam.ReleaseChildPrefix(ctx, c1)
require.Errorf(t, err, "unable to release prefix %s:delete prefix:%s not found", prefix.Cidr, c1.Cidr)
c3, err := ipam.AcquireChildPrefix(ctx, prefix.Cidr, 22)
require.NoError(t, err)
require.NotNil(t, c2)
ip1, err := ipam.AcquireIP(ctx, c3.Cidr)
require.NoError(t, err)
require.NotNil(t, ip1)
err = ipam.ReleaseChildPrefix(ctx, c3)
require.Errorf(t, err, "prefix %s has ips, deletion not possible", c3.Cidr)
c3, err = ipam.ReleaseIP(ctx, ip1)
require.NoError(t, err)
err = ipam.ReleaseChildPrefix(ctx, c3)
require.NoError(t, err)
allPrefixes, err = ipam.storage.ReadAllPrefixes(ctx, defaultNamespace)
require.NoError(t, err)
require.Len(t, allPrefixes, 1)
})
}
func TestIpamer_AcquireChildPrefixIPv4(t *testing.T) {
ctx := context.Background()
testWithBackends(t, func(t *testing.T, ipam *ipamer) {
prefix, err := ipam.NewPrefix(ctx, "192.168.0.0/20")
require.NoError(t, err)
s, _ := prefix.availablePrefixes()
require.Equal(t, uint64(1024), s)
require.Equal(t, uint64(0), prefix.acquiredPrefixes())
// Same length
cp, err := ipam.AcquireChildPrefix(ctx, prefix.Cidr, 20)
require.Error(t, err)
require.Equal(t, "given length:20 must be greater than prefix length:20", err.Error())
require.Nil(t, cp)
// working length
cp, err = ipam.AcquireChildPrefix(ctx, prefix.Cidr, 21)
require.NoError(t, err)
require.NotNil(t, cp)
require.True(t, strings.HasPrefix(cp.Cidr, "192.168."))
require.True(t, strings.HasSuffix(cp.Cidr, "/21"))
require.Equal(t, prefix.Cidr, cp.ParentCidr)
// No more ChildPrefixes
cp, err = ipam.AcquireChildPrefix(ctx, prefix.Cidr, 21)
require.NoError(t, err)
require.NotNil(t, cp)
cp, err = ipam.AcquireChildPrefix(ctx, prefix.Cidr, 21)
require.Error(t, err)
require.Equal(t, "no prefix found in 192.168.0.0/20 with length:21", err.Error())
require.Nil(t, cp)
// Prefix has ips
p2, err := ipam.NewPrefix(ctx, "10.0.0.0/24")
require.NoError(t, err)
s, _ = p2.availablePrefixes()
require.Equal(t, uint64(64), s)
require.Equal(t, uint64(0), p2.acquiredPrefixes())
ip, err := ipam.AcquireIP(ctx, p2.Cidr)
require.NoError(t, err)
require.NotNil(t, ip)
cp2, err := ipam.AcquireChildPrefix(ctx, p2.Cidr, 25)
require.Error(t, err)
require.Equal(t, "prefix 10.0.0.0/24 has ips, acquire child prefix not possible", err.Error())
require.Nil(t, cp2)
// Prefix has Children, AcquireIP wont work
p3, err := ipam.NewPrefix(ctx, "172.17.0.0/24")
require.NoError(t, err)
s, _ = p3.availablePrefixes()
require.Equal(t, uint64(64), s)
require.Equal(t, uint64(0), p3.acquiredPrefixes())
cp3, err := ipam.AcquireChildPrefix(ctx, p3.Cidr, 25)
require.NoError(t, err)
require.NotNil(t, cp3)
p3, err = ipam.PrefixFrom(ctx, p3.Cidr)
require.NoError(t, err)
ip, err = ipam.AcquireIP(ctx, p3.Cidr)
require.Error(t, err)
require.Equal(t, "prefix 172.17.0.0/24 has childprefixes, acquire ip not possible", err.Error())
require.Nil(t, ip)
// Release Parent Prefix must not work
err = ipam.ReleaseChildPrefix(ctx, p3)
require.ErrorIs(t, err, ErrNotFound)
require.Error(t, err)
require.Contains(t, err.Error(), "NotFound: unable to find prefix for cidr")
})
}
func TestIpamer_AcquireChildPrefixIPv6(t *testing.T) {
ctx := context.Background()
testWithBackends(t, func(t *testing.T, ipam *ipamer) {
prefix, err := ipam.NewPrefix(ctx, "2001:0db8:85a3::/116")
require.NoError(t, err)
s, _ := prefix.availablePrefixes()
require.Equal(t, uint64(1024), s)
require.Equal(t, uint64(0), prefix.acquiredPrefixes())
// Same length
cp, err := ipam.AcquireChildPrefix(ctx, prefix.Cidr, 116)
require.Error(t, err)
require.Equal(t, "given length:116 must be greater than prefix length:116", err.Error())
require.Nil(t, cp)
// working length
cp, err = ipam.AcquireChildPrefix(ctx, prefix.Cidr, 117)
require.NoError(t, err)
require.NotNil(t, cp)
require.True(t, strings.HasPrefix(cp.Cidr, "2001:db8:85a3:"))
require.True(t, strings.HasSuffix(cp.Cidr, "/117"))
require.Equal(t, prefix.Cidr, cp.ParentCidr)
// No more ChildPrefixes
cp, err = ipam.AcquireChildPrefix(ctx, prefix.Cidr, 117)
require.NoError(t, err)
require.NotNil(t, cp)
cp, err = ipam.AcquireChildPrefix(ctx, prefix.Cidr, 117)
require.Error(t, err)
require.Equal(t, "no prefix found in 2001:db8:85a3::/116 with length:117", err.Error())
require.Nil(t, cp)
// Prefix has ips
p2, err := ipam.NewPrefix(ctx, "2001:0db8:95a3::/120")
require.NoError(t, err)
s, _ = p2.availablePrefixes()
require.Equal(t, uint64(64), s)
require.Equal(t, uint64(0), p2.acquiredPrefixes())
ip, err := ipam.AcquireIP(ctx, p2.Cidr)
require.NoError(t, err)
require.NotNil(t, ip)
cp2, err := ipam.AcquireChildPrefix(ctx, p2.Cidr, 121)
require.Error(t, err)
require.Equal(t, "prefix 2001:db8:95a3::/120 has ips, acquire child prefix not possible", err.Error())
require.Nil(t, cp2)
// Prefix has Children, AcquireIP wont work
p3, err := ipam.NewPrefix(ctx, "2001:0db8:75a3::/120")
require.NoError(t, err)
s, _ = p3.availablePrefixes()
require.Equal(t, uint64(64), s)
require.Equal(t, uint64(0), p3.acquiredPrefixes())
cp3, err := ipam.AcquireChildPrefix(ctx, p3.Cidr, 121)
require.NoError(t, err)
require.NotNil(t, cp3)
p3, err = ipam.PrefixFrom(ctx, p3.Cidr)
require.NoError(t, err)
ip, err = ipam.AcquireIP(ctx, p3.Cidr)
require.Error(t, err)
require.Equal(t, "prefix 2001:db8:75a3::/120 has childprefixes, acquire ip not possible", err.Error())
require.Nil(t, ip)
// Release Parent Prefix must not work
err = ipam.ReleaseChildPrefix(ctx, p3)
require.ErrorIs(t, err, ErrNotFound)
require.Error(t, err)
require.Contains(t, err.Error(), "NotFound: unable to find prefix for cidr")
})
}
func TestIpamer_AcquireSpecificChildPrefixIPv4(t *testing.T) {
ctx := context.Background()
testWithBackends(t, func(t *testing.T, ipam *ipamer) {
prefix, err := ipam.NewPrefix(ctx, "192.168.0.0/20")
require.NoError(t, err)
s, _ := prefix.availablePrefixes()
require.Equal(t, uint64(1024), s)
require.Equal(t, uint64(0), prefix.acquiredPrefixes())
// Same length
cp, err := ipam.AcquireSpecificChildPrefix(ctx, prefix.Cidr, "192.168.0.0/20")
require.Error(t, err)
require.Equal(t, "given length:20 must be greater than prefix length:20", err.Error())
require.Nil(t, cp)
// working length
cp, err = ipam.AcquireSpecificChildPrefix(ctx, prefix.Cidr, "192.168.0.0/21")
require.NoError(t, err)
require.NotNil(t, cp)
require.Equal(t, "192.168.0.0/21", cp.Cidr)
require.Equal(t, prefix.Cidr, cp.ParentCidr)
// specific prefix not available
cp, err = ipam.AcquireSpecificChildPrefix(ctx, prefix.Cidr, "192.168.8.0/21")
require.NoError(t, err)
require.NotNil(t, cp)
cp, err = ipam.AcquireSpecificChildPrefix(ctx, prefix.Cidr, "192.168.8.0/21")
require.Error(t, err)
require.Equal(t, "specific prefix 192.168.8.0/21 is not available in prefix 192.168.0.0/20", err.Error())
require.Nil(t, cp)
// Prefix has ips
p2, err := ipam.NewPrefix(ctx, "10.0.0.0/24")
require.NoError(t, err)
s, _ = p2.availablePrefixes()
require.Equal(t, uint64(64), s)
require.Equal(t, uint64(0), p2.acquiredPrefixes())
ip, err := ipam.AcquireIP(ctx, p2.Cidr)
require.NoError(t, err)
require.NotNil(t, ip)
cp2, err := ipam.AcquireSpecificChildPrefix(ctx, p2.Cidr, "10.0.0.0/25")
require.Error(t, err)
require.Equal(t, "prefix 10.0.0.0/24 has ips, acquire child prefix not possible", err.Error())
require.Nil(t, cp2)
// Prefix has Children, AcquireIP wont work
p3, err := ipam.NewPrefix(ctx, "172.17.0.0/24")
require.NoError(t, err)
s, _ = p3.availablePrefixes()
require.Equal(t, uint64(64), s)
require.Equal(t, uint64(0), p3.acquiredPrefixes())
cp3, err := ipam.AcquireSpecificChildPrefix(ctx, p3.Cidr, "172.17.0.0/25")
require.NoError(t, err)
require.NotNil(t, cp3)
p3, err = ipam.PrefixFrom(ctx, p3.Cidr)
require.NoError(t, err)
ip, err = ipam.AcquireIP(ctx, p3.Cidr)
require.Error(t, err)
require.Equal(t, "prefix 172.17.0.0/24 has childprefixes, acquire ip not possible", err.Error())
require.Nil(t, ip)
})
}
func TestIpamer_AcquireSpecificChildPrefixIPv6(t *testing.T) {
ctx := context.Background()
testWithBackends(t, func(t *testing.T, ipam *ipamer) {
prefix, err := ipam.NewPrefix(ctx, "2001:0db8:85a3::/116")
require.NoError(t, err)
s, _ := prefix.availablePrefixes()
require.Equal(t, uint64(1024), s)
require.Equal(t, uint64(0), prefix.acquiredPrefixes())
// Same length
cp, err := ipam.AcquireSpecificChildPrefix(ctx, prefix.Cidr, "2001:0db8:85a3::/116")
require.Error(t, err)
require.Equal(t, "given length:116 must be greater than prefix length:116", err.Error())
require.Nil(t, cp)
// working length
cp, err = ipam.AcquireSpecificChildPrefix(ctx, prefix.Cidr, "2001:0db8:85a3::/117")
require.NoError(t, err)
require.NotNil(t, cp)
require.Equal(t, "2001:db8:85a3::/117", cp.Cidr)
require.Equal(t, prefix.Cidr, cp.ParentCidr)
// specific prefix not available
cp, err = ipam.AcquireSpecificChildPrefix(ctx, prefix.Cidr, "2001:0db8:85a3::0800/117")
require.NoError(t, err)
require.NotNil(t, cp)
require.Equal(t, "2001:db8:85a3::800/117", cp.Cidr)
cp, err = ipam.AcquireSpecificChildPrefix(ctx, prefix.Cidr, "2001:0db8:85a3::0800/117")
require.Error(t, err)
require.Equal(t, "specific prefix 2001:0db8:85a3::0800/117 is not available in prefix 2001:db8:85a3::/116", err.Error())
require.Nil(t, cp)
// Prefix has ips
p2, err := ipam.NewPrefix(ctx, "2001:0db8:95a3::/120")
require.NoError(t, err)
s, _ = p2.availablePrefixes()
require.Equal(t, uint64(64), s)
require.Equal(t, uint64(0), p2.acquiredPrefixes())
ip, err := ipam.AcquireIP(ctx, p2.Cidr)
require.NoError(t, err)
require.NotNil(t, ip)
cp2, err := ipam.AcquireSpecificChildPrefix(ctx, p2.Cidr, "2001:0db8:95a3::/121")
require.Error(t, err)
require.Equal(t, "prefix 2001:db8:95a3::/120 has ips, acquire child prefix not possible", err.Error())
require.Nil(t, cp2)
// Prefix has Children, AcquireIP wont work
p3, err := ipam.NewPrefix(ctx, "2001:0db8:75a3::/120")
require.NoError(t, err)
s, _ = p3.availablePrefixes()
require.Equal(t, uint64(64), s)
require.Equal(t, uint64(0), p3.acquiredPrefixes())
cp3, err := ipam.AcquireSpecificChildPrefix(ctx, p3.Cidr, "2001:0db8:75a3::/121")
require.NoError(t, err)
require.NotNil(t, cp3)
p3, err = ipam.PrefixFrom(ctx, p3.Cidr)
require.NoError(t, err)
ip, err = ipam.AcquireIP(ctx, p3.Cidr)
require.Error(t, err)
require.Equal(t, "prefix 2001:db8:75a3::/120 has childprefixes, acquire ip not possible", err.Error())
require.Nil(t, ip)
// Release Parent Prefix must not work
err = ipam.ReleaseChildPrefix(ctx, p3)
require.ErrorIs(t, err, ErrNotFound)
require.Error(t, err)
require.Contains(t, err.Error(), "NotFound: unable to find prefix for cidr")
})
}
func TestIpamer_AcquireChildPrefixNoDuplicatesUntilFullIPv6(t *testing.T) {
ctx := context.Background()
testWithBackends(t, func(t *testing.T, ipam *ipamer) {
prefix, err := ipam.NewPrefix(ctx, "2001:0db8:85a3::/112")
require.NoError(t, err)
s, _ := prefix.availablePrefixes()
require.Equal(t, uint64(16384), s)
require.Equal(t, uint64(0), prefix.acquiredPrefixes())
uniquePrefixes := make(map[string]bool)
// acquire all /120 prefixes (2^8 = 256)
for range 256 {
cp, err := ipam.AcquireChildPrefix(ctx, prefix.Cidr, 120)
require.NoError(t, err)
require.NotNil(t, cp)
require.True(t, strings.HasPrefix(cp.Cidr, "2001:db8:85a3:"))
require.True(t, strings.HasSuffix(cp.Cidr, "/120"))
require.Equal(t, prefix.Cidr, cp.ParentCidr)
_, ok := uniquePrefixes[cp.String()]
require.False(t, ok)
uniquePrefixes[cp.String()] = true
}
prefix, err = ipam.PrefixFrom(ctx, prefix.Cidr)
require.NoError(t, err)
require.Len(t, uniquePrefixes, 256)
s, _ = prefix.availablePrefixes()
require.Equal(t, uint64(0), s)
require.Equal(t, uint64(256), prefix.acquiredPrefixes())
})
}
func TestIpamer_AcquireChildPrefixNoDuplicatesUntilFullIPv4(t *testing.T) {
ctx := context.Background()
testWithBackends(t, func(t *testing.T, ipam *ipamer) {
prefix, err := ipam.NewPrefix(ctx, "192.168.0.0/16")
require.NoError(t, err)
s, _ := prefix.availablePrefixes()
require.Equal(t, uint64(16384), s)
require.Equal(t, uint64(0), prefix.acquiredPrefixes())
uniquePrefixes := make(map[string]bool)
// acquire all /24 prefixes (2^8 = 256)
for range 256 {
cp, err := ipam.AcquireChildPrefix(ctx, prefix.Cidr, 24)
require.NoError(t, err)
require.NotNil(t, cp)
require.True(t, strings.HasPrefix(cp.Cidr, "192.168."))
require.True(t, strings.HasSuffix(cp.Cidr, "/24"))
require.Equal(t, prefix.Cidr, cp.ParentCidr)
_, ok := uniquePrefixes[cp.String()]
require.False(t, ok)
uniquePrefixes[cp.String()] = true
}
prefix, err = ipam.PrefixFrom(ctx, prefix.Cidr)
require.NoError(t, err)
require.Len(t, uniquePrefixes, 256)
s, _ = prefix.availablePrefixes()
require.Equal(t, uint64(0), s)
require.Equal(t, uint64(256), prefix.acquiredPrefixes())
})
}
func TestPrefix_Availableips(t *testing.T) {
tests := []struct {
name string
Cidr string
want uint64
}{
{
name: "large",
Cidr: "192.168.0.0/20",
want: 4096,
},
{
name: "small",
Cidr: "192.168.0.0/24",
want: 256,
},
{
name: "smaller",
Cidr: "192.168.0.0/25",
want: 128,
},
{
name: "smaller",
Cidr: "192.168.0.0/30",
want: 4,
},
{
name: "smaller IPv6",
Cidr: "2001:0db8:85a3::/126",
want: 4,
},
{
name: "large IPv6",
Cidr: "2001:0db8:85a3::/116",
want: 4096,
},
}
for _, tt := range tests {
test := tt
t.Run(tt.name, func(t *testing.T) {
p := &Prefix{
Cidr: test.Cidr,
}
if got := p.availableips(); got != test.want {
t.Errorf("Prefix.Availableips() = %v, want %v", got, test.want)
}
})
}
}
func TestIpamer_PrefixesOverlapping(t *testing.T) {
ctx := context.Background()
tests := []struct {
name string
existingPrefixes []string
newPrefixes []string
wantErr bool
errorString string
}{
{
name: "simple",
existingPrefixes: []string{"192.168.0.0/24"},
newPrefixes: []string{"192.168.1.0/24"},
wantErr: false,
errorString: "",
},
{
name: "simple IPv6",
existingPrefixes: []string{"2001:0db8:85a3::/126"},
newPrefixes: []string{"2001:0db8:85a4::/126"},
wantErr: false,
errorString: "",
},
{
name: "one overlap IPv6",
existingPrefixes: []string{"2001:0db8:85a3::/126", "2001:0db8:85a4::/126"},
newPrefixes: []string{"2001:0db8:85a4::/126"},
wantErr: true,
errorString: "2001:db8:85a4::/126 overlaps 2001:db8:85a4::/126",
},
{
name: "one overlap",