@@ -32,69 +32,165 @@ func genAllocation(indexes []int) []backend.Allocation {
3232}
3333
3434var _ = Describe ("Test Allocator" , func () {
35- initIndexes := []int {1 , 2 , 3 , 8 , 13 , 18 }
36- allocations := genAllocation (initIndexes )
3735
38- It ("find simple next available index" , func () {
39- indexes := []int {1 , 2 , 3 , 8 , 13 , 18 }
40- nextIndex := FindAvailableIndex (indexes , 0 )
41- Expect (nextIndex ).To (Equal (4 ))
42- })
36+ Context ("Allocate" , func () {
37+ initIndexes := []int {1 , 2 , 3 , 8 , 13 , 18 }
38+ allocations := genAllocation (initIndexes )
4339
44- It ("find next available index with exclude range over consecutive order" , func () {
45- excludes := []ExcludeRange {
46- ExcludeRange {
47- MinIndex : 4 ,
48- MaxIndex : 6 ,
40+ DescribeTable ("FindAvailableIndex" ,
41+ func (excludes []ExcludeRange , expectedIndex []int , expected int ) {
42+ indexes := GenerateAllocateIndexes (allocations , 20 , excludes )
43+ Expect (indexes ).To (Equal (expectedIndex ))
44+ nextIndex := FindAvailableIndex (indexes , 0 )
45+ Expect (nextIndex ).To (Equal (expected ))
4946 },
50- }
51- indexes := GenerateAllocateIndexes (allocations , 20 , excludes )
52- Expect (indexes ).To (Equal ([]int {1 , 2 , 3 , 4 , 5 , 6 , 8 , 13 , 18 }))
53- nextIndex := FindAvailableIndex (indexes , 0 )
54- Expect (nextIndex ).To (Equal (7 ))
55- })
56- It ("find next available index with exclude range over non-consecutive order" , func () {
57- excludes := []ExcludeRange {
58- ExcludeRange {
59- MinIndex : 4 ,
60- MaxIndex : 7 ,
47+ Entry ("no excludes" , []ExcludeRange {}, []int {1 , 2 , 3 , 8 , 13 , 18 }, 4 ),
48+ Entry ("excludes consecutive order" , []ExcludeRange {
49+ ExcludeRange {
50+ MinIndex : 4 ,
51+ MaxIndex : 6 ,
52+ },
6153 },
62- }
63-
64- indexes := GenerateAllocateIndexes (allocations , 20 , excludes )
65- Expect (indexes ).To (Equal ([]int {1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 , 13 , 18 }))
66- nextIndex := FindAvailableIndex (indexes , 0 )
67- Expect (nextIndex ).To (Equal (9 ))
68- })
69-
70- It ("find next available index with exclude range over non-consecutive and then consecutive order" , func () {
71- excludes := []ExcludeRange {
72- ExcludeRange {
73- MinIndex : 4 ,
74- MaxIndex : 7 ,
54+ []int {1 , 2 , 3 , 4 , 5 , 6 , 8 , 13 , 18 },
55+ 7 ,
56+ ),
57+ Entry ("excludes non-consecutive order" , []ExcludeRange {
58+ ExcludeRange {
59+ MinIndex : 4 ,
60+ MaxIndex : 7 ,
61+ },
7562 },
76- ExcludeRange {
77- MinIndex : 9 ,
78- MaxIndex : 12 ,
63+ []int {1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 , 13 , 18 },
64+ 9 ,
65+ ),
66+ Entry ("excludes non-consecutive and then consecutive order" , []ExcludeRange {
67+ ExcludeRange {
68+ MinIndex : 4 ,
69+ MaxIndex : 7 ,
70+ },
71+ ExcludeRange {
72+ MinIndex : 9 ,
73+ MaxIndex : 12 ,
74+ },
7975 },
80- }
81-
82- indexes := GenerateAllocateIndexes (allocations , 20 , excludes )
83- Expect (indexes ).To (Equal ([]int {1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 , 9 , 10 , 11 , 12 , 13 , 18 }))
84- nextIndex := FindAvailableIndex (indexes , 0 )
85- Expect (nextIndex ).To (Equal (14 ))
76+ []int {1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 , 9 , 10 , 11 , 12 , 13 , 18 },
77+ 14 ,
78+ ),
79+ )
80+
81+ DescribeTable ("getAddressByIndex" , func (cidr string , index int , expectedIP string ) {
82+ result := getAddressByIndex (cidr , index )
83+ Expect (result ).To (Equal (expectedIP ))
84+ },
85+ Entry ("zero index" , "10.0.0.0/16" , 0 , "10.0.0.0" ),
86+ Entry ("first index" , "10.0.0.0/16" , 1 , "10.0.0.1" ),
87+ Entry ("shifted index" , "10.0.0.0/16" , 256 , "10.0.1.0" ),
88+ )
89+
90+ DescribeTable ("getExcludeRanges" , func (cidr string , excludes []string , expected []ExcludeRange ) {
91+ result := getExcludeRanges (cidr , excludes )
92+ Expect (result ).To (BeEquivalentTo (expected ))
93+ },
94+ Entry ("empty" , "10.0.0.0/16" , []string {}, []ExcludeRange {}),
95+ Entry ("inner exclude" , "10.0.0.0/16" , []string {"10.0.0.0/24" },
96+ []ExcludeRange {
97+ ExcludeRange {
98+ MinIndex : 0 ,
99+ MaxIndex : 255 ,
100+ },
101+ },
102+ ),
103+ Entry ("multiple inner excludes" , "10.0.0.0/16" , []string {"10.0.0.0/24" , "10.0.1.1/32" },
104+ []ExcludeRange {
105+ ExcludeRange {
106+ MinIndex : 0 ,
107+ MaxIndex : 255 ,
108+ },
109+ ExcludeRange {
110+ MinIndex : 257 ,
111+ MaxIndex : 257 ,
112+ },
113+ },
114+ ),
115+ Entry ("outer exclude" , "10.0.0.0/24" , []string {"10.0.0.0/23" },
116+ []ExcludeRange {
117+ ExcludeRange {
118+ MinIndex : 0 ,
119+ MaxIndex : 511 ,
120+ },
121+ },
122+ ),
123+ )
124+
125+ DescribeTable ("allocateIP" , func (interfaceNames []string , ippoolSpecMap map [string ]backend.IPPoolType , expectedAddress map [string ]string ) {
126+ newAllocations := allocateIP ("test-pod" , "test-namespace" , interfaceNames , 1 , ippoolSpecMap )
127+ Expect (newAllocations ).To (HaveLen (len (expectedAddress )))
128+ for ippoolName , allocation := range newAllocations {
129+ address , found := expectedAddress [ippoolName ]
130+ Expect (found ).To (BeTrue ())
131+ Expect (allocation .Address ).To (BeEquivalentTo (address ))
132+ }
133+ },
134+ Entry ("no interface name" , []string {}, map [string ]backend.IPPoolType {
135+ "eth0" : backend.IPPoolType {InterfaceName : "eth0" , PodCIDR : "192.168.0.0/24" },
136+ }, map [string ]string {}),
137+ Entry ("no ippool" , []string {"eth0" }, map [string ]backend.IPPoolType {}, map [string ]string {}),
138+ Entry ("first allocation" , []string {"eth0" }, map [string ]backend.IPPoolType {
139+ "eth0" : backend.IPPoolType {InterfaceName : "eth0" , PodCIDR : "192.168.0.0/24" },
140+ }, map [string ]string {
141+ "eth0" : "192.168.0.1" ,
142+ }),
143+ Entry ("second allocation" , []string {"eth0" }, map [string ]backend.IPPoolType {
144+ "eth0" : backend.IPPoolType {
145+ InterfaceName : "eth0" ,
146+ PodCIDR : "192.168.0.0/24" ,
147+ Allocations : []backend.Allocation {
148+ {
149+ Pod : "dummy" ,
150+ Namespace : "test-namespace" ,
151+ Index : 1 ,
152+ Address : "192.168.0.1" },
153+ },
154+ },
155+ }, map [string ]string {
156+ "eth0" : "192.168.0.2" ,
157+ }),
158+ Entry ("reuse allocation" , []string {"eth0" }, map [string ]backend.IPPoolType {
159+ "eth0" : backend.IPPoolType {
160+ InterfaceName : "eth0" ,
161+ PodCIDR : "192.168.0.0/24" ,
162+ Allocations : []backend.Allocation {
163+ {
164+ Pod : "dummy" ,
165+ Namespace : "test-namespace" ,
166+ Index : 255 ,
167+ Address : "192.168.0.255" },
168+ },
169+ },
170+ }, map [string ]string {
171+ "eth0" : "192.168.0.1" ,
172+ }),
173+ )
86174 })
87175
88- It ("force expired" , func () {
89- podName := "A"
90- deallocateHistory [podName ] = & allocateRecord {
91- Time : time .Now (),
92- LastOffset : 1 ,
93- }
94- Expect (deallocateHistory [podName ].Expired ()).To (Equal (false ))
95- deallocateHistory [podName ].Time = deallocateHistory [podName ].Time .Add (time .Duration (- HISTORY_TIMEOUT - 1 ) * time .Second )
96- Expect (deallocateHistory [podName ].Expired ()).To (Equal (true ))
176+ Context ("Deallocate" , func () {
177+
178+ It ("force expired" , func () {
179+ podName := "A"
180+ deallocateHistory [podName ] = & allocateRecord {
181+ Time : time .Now (),
182+ LastOffset : 1 ,
183+ }
184+ Expect (deallocateHistory [podName ].Expired ()).To (Equal (false ))
185+ deallocateHistory [podName ].Time = deallocateHistory [podName ].Time .Add (time .Duration (- HISTORY_TIMEOUT - 1 ) * time .Second )
186+ Expect (deallocateHistory [podName ].Expired ()).To (Equal (true ))
187+ FlushExpiredHistory ()
188+ _ , found := deallocateHistory [podName ]
189+ Expect (found ).To (BeFalse ())
190+ })
191+
97192 })
193+
98194})
99195
100196var _ = Describe ("Test VF/PF Interface Mapping" , func () {
@@ -210,7 +306,6 @@ var _ = Describe("Test VF/PF Interface Mapping", func() {
210306 Expect (result ).To (Equal ("ens9f0np0" ))
211307 })
212308
213-
214309 })
215310
216311 Describe ("Interface matching logic" , func () {
@@ -226,7 +321,7 @@ var _ = Describe("Test VF/PF Interface Mapping", func() {
226321 // Mock the VF detection functions
227322 originalIsVF := isVF
228323 originalGetPF := getPFInterfaceName
229- defer func () {
324+ defer func () {
230325 isVF = originalIsVF
231326 getPFInterfaceName = originalGetPF
232327 }()
@@ -377,4 +472,3 @@ var _ = Describe("Test VF/PF Interface Mapping", func() {
377472 })
378473 })
379474})
380-
0 commit comments