@@ -141,82 +141,6 @@ var _ = Describe("IPAM Plugin", func() {
141141 })
142142 })
143143
144- Describe ("K8s Client tests" , func () {
145- var (
146- linkLocalIPV6Addr net.IP
147- ipv6 * ipamv1alpha1.IP
148- k8sClient * K8sClient
149- )
150-
151- BeforeEach (func () {
152- By ("creating an IPAM IP" )
153- ipv6 , linkLocalIPV6Addr = createTestIPAMIP (machineWithMacAddress , * ns )
154- Expect (ipv6 ).NotTo (BeNil ())
155- Expect (linkLocalIPV6Addr ).NotTo (BeNil ())
156-
157- k8sClient , err = NewK8sClient (ns .Name , []string {"foo" })
158- Expect (err ).NotTo (HaveOccurred ())
159- Expect (k8sClient ).NotTo (BeNil ())
160- })
161-
162- It ("should successfully match the subnet" , func () {
163- subnet , err := k8sClient .getMatchingSubnet ("foo" , linkLocalIPV6Addr )
164- Expect (err ).NotTo (HaveOccurred ())
165- Expect (subnet ).To (BeNil ())
166- })
167-
168- // It("should return error if subnet not matched", func() {
169- // subnet, err := k8sClient.getMatchingSubnet("random-subnet", linkLocalIPV6Addr)
170- // Expect(err).To(HaveOccurred())
171- // Expect(subnet).To(BeNil())
172- // })
173-
174- It ("should successfully return IPAM IP for machine with mac address" , func () {
175- ip , err := k8sClient .prepareCreateIpamIP ("foo" , linkLocalIPV6Addr , net .HardwareAddr (machineWithIPAddressMACAddress ))
176- Expect (err ).NotTo (HaveOccurred ())
177- Expect (ip ).NotTo (BeNil ())
178- })
179-
180- It ("should successfully create IPAM IP for machine with mac address" , func () {
181- createIPError := k8sClient .doCreateIpamIP (ipv6 )
182- Expect (createIPError ).NotTo (HaveOccurred ())
183- })
184-
185- It ("should successfully create IPAM IP for machine" , func () {
186- createIPError := k8sClient .createIpamIP (linkLocalIPV6Addr , net .HardwareAddr (machineWithIPAddressMACAddress ))
187- Expect (createIPError ).NotTo (HaveOccurred ())
188- })
189-
190- // It("should return error, if create same IPAM IP for machine with same ip and same mac address", func() {
191- // err := k8sClient.doCreateIpamIP(ipv6)
192- // Expect(err).NotTo(HaveOccurred())
193- // //Expect(err.Error()).To(ContainSubstring("resourceVersion should not be set on objects to be created"))
194- // })
195-
196- It ("should return timeout error, if IPAM IP not deleted" , func () {
197- err := k8sClient .waitForDeletion (ipv6 )
198- Expect (err ).To (HaveOccurred ())
199- Expect (err .Error ()).To (ContainSubstring ("timeout reached, IP not deleted" ))
200- })
201-
202- // It("should return timeout error, if IPAM IP not deleted", func() {
203- // ipv6 := createTestIPAMIP(machineWithMacAddress, *ns)
204-
205- // createIPError := k8sClient.doCreateIpamIP(ipv6)
206- // Expect(createIPError).NotTo(HaveOccurred())
207-
208- // err = k8sClientTest.Get(context.TODO(), client.ObjectKeyFromObject(ipv6), ipv6)
209- // Expect(err).NotTo(HaveOccurred())
210-
211- // err = k8sClient.waitForDeletion(ipv6)
212- // Expect(err).To(HaveOccurred())
213- // err1 := k8sClientTest.Delete(context.TODO(), ipv6)
214- // Expect(err1).NotTo(HaveOccurred())
215- // Expect(err.Error()).To(ContainSubstring("timeout reached, IP not deleted"))
216- // })
217-
218- })
219-
220144 Describe ("Common tests" , func () {
221145 It ("return true checks the ip in CIDR" , func () {
222146 checkIP := checkIPv6InCIDR (linkLocalIPV6Addr , "fe80::/64" )
@@ -256,6 +180,100 @@ var _ = Describe("IPAM Plugin", func() {
256180 })
257181})
258182
183+ var _ = Describe ("K8s Client tests" , func () {
184+ var (
185+ linkLocalIPV6Addr net.IP
186+ ipv6 * ipamv1alpha1.IP
187+ k8sClient * K8sClient
188+ err error
189+ )
190+
191+ ns := SetupTest ()
192+
193+ BeforeEach (func () {
194+ By ("creating an IPAM IP" )
195+ ipv6 , linkLocalIPV6Addr = createTestIPAMIP (machineWithMacAddress , * ns )
196+ Expect (ipv6 ).NotTo (BeNil ())
197+ Expect (linkLocalIPV6Addr ).NotTo (BeNil ())
198+
199+ k8sClient , err = NewK8sClient (ns .Name , []string {"foo" })
200+ Expect (err ).NotTo (HaveOccurred ())
201+ Expect (k8sClient ).NotTo (BeNil ())
202+ })
203+
204+ It ("should successfully match the subnet" , func () {
205+ subnet , err := k8sClient .getMatchingSubnet ("foo" , linkLocalIPV6Addr )
206+ Expect (err ).NotTo (HaveOccurred ())
207+ Expect (subnet ).To (BeNil ())
208+ })
209+
210+ It ("should return error if subnet not matched" , func () {
211+ subnet , err := k8sClient .getMatchingSubnet ("random-subnet" , linkLocalIPV6Addr )
212+ Expect (err ).ToNot (HaveOccurred ())
213+ Expect (subnet ).To (BeNil ())
214+ })
215+
216+ It ("should return (nil, nil) if CIDR mismatch" , func () {
217+ subnet , err := k8sClient .getMatchingSubnet ("foo" , net .IP ("11:22:33:44" ))
218+ Expect (err ).ToNot (HaveOccurred ())
219+ Expect (subnet ).To (BeNil ())
220+ })
221+
222+ It ("should successfully return IPAM IP for machine with mac address" , func () {
223+ ip , err := k8sClient .prepareCreateIpamIP ("foo" , linkLocalIPV6Addr , net .HardwareAddr (machineWithIPAddressMACAddress ))
224+ Expect (err ).NotTo (HaveOccurred ())
225+ Expect (ip ).NotTo (BeNil ())
226+ })
227+
228+ It ("should return error failed to parse IP if invalid ip prefix" , func () {
229+ m , err := net .ParseMAC (machineWithIPAddressMACAddress )
230+ Expect (err ).NotTo (HaveOccurred ())
231+ i := net .ParseIP ("fe80??::" )
232+ linkLocalIPV6Addr , err := eui64 .ParseMAC (i , m )
233+ Expect (err ).To (HaveOccurred ())
234+
235+ ip , err := k8sClient .prepareCreateIpamIP ("foo" , linkLocalIPV6Addr , net .HardwareAddr (machineWithIPAddressMACAddress ))
236+ Expect (err ).To (HaveOccurred ())
237+ Expect (err .Error ()).To (ContainSubstring ("failed to parse IP" ))
238+ Expect (ip ).To (BeNil ())
239+ })
240+
241+ It ("should successfully return with (nil, nil) if IP already exists in subnet" , func () {
242+ m , err := net .ParseMAC (machineWithIPAddressMACAddress )
243+ Expect (err ).NotTo (HaveOccurred ())
244+ i := net .ParseIP ("fe80::" )
245+ linkLocalIPV6Addr , err := eui64 .ParseMAC (i , m )
246+ Expect (err ).NotTo (HaveOccurred ())
247+
248+ ip , err := k8sClient .prepareCreateIpamIP ("foo" , linkLocalIPV6Addr , net .HardwareAddr (machineWithIPAddressMACAddress ))
249+ Expect (err ).NotTo (HaveOccurred ())
250+ Expect (ip ).NotTo (BeNil ())
251+
252+ createIPError := k8sClient .doCreateIpamIP (ip )
253+ Expect (createIPError ).NotTo (HaveOccurred ())
254+
255+ sameip , err := k8sClient .prepareCreateIpamIP ("foo" , linkLocalIPV6Addr , net .HardwareAddr (machineWithIPAddressMACAddress ))
256+ Expect (err ).ToNot (HaveOccurred ())
257+ Expect (sameip ).To (BeNil ())
258+ })
259+
260+ It ("should successfully create IPAM IP for machine with mac address" , func () {
261+ createIPError := k8sClient .doCreateIpamIP (ipv6 )
262+ Expect (createIPError ).NotTo (HaveOccurred ())
263+ })
264+
265+ It ("should successfully create IPAM IP for machine" , func () {
266+ createIPError := k8sClient .createIpamIP (linkLocalIPV6Addr , net .HardwareAddr (machineWithIPAddressMACAddress ))
267+ Expect (createIPError ).NotTo (HaveOccurred ())
268+ })
269+
270+ It ("should return timeout error, if IPAM IP not deleted" , func () {
271+ err := k8sClient .waitForDeletion (ipv6 )
272+ Expect (err ).To (HaveOccurred ())
273+ Expect (err .Error ()).To (ContainSubstring ("timeout reached, IP not deleted" ))
274+ })
275+ })
276+
259277func createTestIPAMIP (mac string , ns corev1.Namespace ) (* ipamv1alpha1.IP , net.IP ) {
260278 m , err := net .ParseMAC (mac )
261279 Expect (err ).NotTo (HaveOccurred ())
0 commit comments