Skip to content

Commit 3f07a43

Browse files
kkumardamyan
authored andcommitted
Add few more tests
1 parent 1c611d4 commit 3f07a43

File tree

2 files changed

+96
-81
lines changed

2 files changed

+96
-81
lines changed

plugins/ipam/plugin_test.go

Lines changed: 95 additions & 81 deletions
Original file line numberDiff line numberDiff line change
@@ -42,36 +42,15 @@ var _ = Describe("IPAM Plugin", func() {
4242
err = os.WriteFile(testConfigPath, configData, 0644)
4343
Expect(err).NotTo(HaveOccurred())
4444

45-
mac := machineWithIPAddressMACAddress
46-
m, err := net.ParseMAC(mac)
47-
Expect(err).NotTo(HaveOccurred())
48-
i := net.ParseIP(linkLocalIPV6Prefix)
49-
linkLocalIPV6Addr, err = eui64.ParseMAC(i, m)
50-
Expect(err).NotTo(HaveOccurred())
45+
ipv6, linkLocalIPV6Addr = createTestIPAMIP(machineWithIPAddressMACAddress, *ns)
46+
Expect(ipv6).NotTo(BeNil())
47+
Expect(linkLocalIPV6Addr).NotTo(BeNil())
5148

52-
sanitizedMAC := strings.Replace(mac, ":", "", -1)
53-
ipv6Addr, err := ipamv1alpha1.IPAddrFromString(linkLocalIPV6Addr.String())
54-
Expect(err).NotTo(HaveOccurred())
55-
ipv6 = &ipamv1alpha1.IP{
56-
ObjectMeta: metav1.ObjectMeta{
57-
Namespace: ns.Name,
58-
GenerateName: "test-",
59-
Labels: map[string]string{
60-
"mac": sanitizedMAC,
61-
},
62-
},
63-
Spec: ipamv1alpha1.IPSpec{
64-
Subnet: corev1.LocalObjectReference{
65-
Name: "foo",
66-
},
67-
IP: ipv6Addr,
68-
},
69-
}
7049
Expect(k8sClientTest.Create(ctx, ipv6)).To(Succeed())
7150
DeferCleanup(k8sClientTest.Delete, ipv6)
7251

7352
Eventually(UpdateStatus(ipv6, func() {
74-
ipv6.Status.Reserved = ipv6Addr
53+
ipv6.Status.Reserved = ipv6.Spec.IP
7554
})).Should(Succeed())
7655
})
7756

@@ -150,85 +129,92 @@ var _ = Describe("IPAM Plugin", func() {
150129
})
151130

152131
It("should successfully handle request", func() {
153-
// //sanitizedMAC := strings.Replace(mac, ":", "", -1)
154-
// ipv6Addr, _ := ipamv1alpha1.IPAddrFromString(linkLocalIPV6Addr.String())
155-
156-
// ipv6 := &ipamv1alpha1.IP{
157-
// ObjectMeta: metav1.ObjectMeta{
158-
// Namespace: ns.Name,
159-
// GenerateName: "test-",
160-
// },
161-
// Spec: ipamv1alpha1.IPSpec{
162-
// Subnet: corev1.LocalObjectReference{
163-
// Name: "foo",
164-
// },
165-
// IP: ipv6Addr,
166-
// },
167-
// }
168-
169-
// err3 := k8sClientTest.Create(context.TODO(), ipv6)
170-
// Expect(err3).To(BeNil())
171-
172-
// createdSubnet := &ipamv1alpha1.Subnet{
173-
// ObjectMeta: metav1.ObjectMeta{
174-
// Name: "foo",
175-
// Namespace: ns.Name,
176-
// },
177-
// }
178-
179-
// err5 := k8sClientTest.Create(context.TODO(), createdSubnet)
180-
// Expect(err5).To(BeNil())
181-
182-
// subnet := &ipamv1alpha1.Subnet{
183-
// ObjectMeta: metav1.ObjectMeta{
184-
// Name: "foo",
185-
// Namespace: ns.Name,
186-
// },
187-
// }
188-
// existingSubnet := subnet.DeepCopy()
189-
// err4 := k8sClientTest.Get(context.TODO(), client.ObjectKeyFromObject(subnet), existingSubnet)
190-
// Expect(err4).To(BeNil())
191-
192-
//Expect(k8sClientTest.Create(context.TODO(), ipv6)).To(Succeed())
193-
// clientset, err2 := ipam.NewForConfig(cfg)
194-
// Expect(err2).NotTo(HaveOccurred())
195-
// createdSubnet, err1 := clientset.IpamV1alpha1().Subnets(ns.Name).Create(context.TODO(), subnet, v1.CreateOptions{})
196-
// Expect(err1).NotTo(HaveOccurred())
197-
// Expect(createdSubnet).NotTo(BeNil())
198-
199-
//fmt.Printf("createdSubnet: %v", createdSubnet)
200-
201-
// mac, _ := net.ParseMAC(machineWithIPAddressMACAddress)
202-
// ip := net.ParseIP(linkLocalIPV6Prefix)
203-
// linkLocalIPV6Addr, _ := eui64.ParseMAC(ip, mac)
204-
205132
req, _ := dhcpv6.NewMessage()
206133
req.MessageType = dhcpv6.MessageTypeRequest
207134
relayedRequest, _ := dhcpv6.EncapsulateRelay(req, dhcpv6.MessageTypeRelayForward, net.IPv6loopback, linkLocalIPV6Addr)
208135

209136
stub, err := dhcpv6.NewMessage()
210-
Expect(err).To(BeNil())
137+
Expect(err).ToNot(HaveOccurred())
211138
resp, stop := handler6(relayedRequest, stub)
212139
Expect(stop).To(BeFalse())
213140
Expect(resp).NotTo(BeNil())
214141
})
215142
})
216143

217144
Describe("K8s Client tests", func() {
218-
It("should successfully match the subnet", func() {
219-
k8sClient, err := NewK8sClient(ns.Name, []string{"foo"})
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"})
220158
Expect(err).NotTo(HaveOccurred())
221159
Expect(k8sClient).NotTo(BeNil())
160+
})
222161

162+
It("should successfully match the subnet", func() {
223163
subnet, err := k8sClient.getMatchingSubnet("foo", linkLocalIPV6Addr)
224164
Expect(err).NotTo(HaveOccurred())
225165
Expect(subnet).To(BeNil())
226166
})
227167

228-
// It("should successfully match the subnet", func() {
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() {
229191
// err := k8sClient.doCreateIpamIP(ipv6)
230192
// 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"))
231216
// })
217+
232218
})
233219

234220
Describe("Common tests", func() {
@@ -269,3 +255,31 @@ var _ = Describe("IPAM Plugin", func() {
269255
})
270256
})
271257
})
258+
259+
func createTestIPAMIP(mac string, ns corev1.Namespace) (*ipamv1alpha1.IP, net.IP) {
260+
m, err := net.ParseMAC(mac)
261+
Expect(err).NotTo(HaveOccurred())
262+
i := net.ParseIP(linkLocalIPV6Prefix)
263+
linkLocalIPV6Addr, err := eui64.ParseMAC(i, m)
264+
Expect(err).NotTo(HaveOccurred())
265+
266+
sanitizedMAC := strings.Replace(mac, ":", "", -1)
267+
ipv6Addr, err := ipamv1alpha1.IPAddrFromString(linkLocalIPV6Addr.String())
268+
Expect(err).NotTo(HaveOccurred())
269+
ipv6 := &ipamv1alpha1.IP{
270+
ObjectMeta: metav1.ObjectMeta{
271+
Namespace: ns.Name,
272+
Name: "test-ip",
273+
Labels: map[string]string{
274+
"mac": sanitizedMAC,
275+
},
276+
},
277+
Spec: ipamv1alpha1.IPSpec{
278+
Subnet: corev1.LocalObjectReference{
279+
Name: "foo",
280+
},
281+
IP: ipv6Addr,
282+
},
283+
}
284+
return ipv6, linkLocalIPV6Addr
285+
}

plugins/ipam/suite_test.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ const (
3838
linkLocalIPV6Prefix = "fe80::"
3939
subnetLabel = "subnet=dhcp"
4040
machineWithIPAddressMACAddress = "11:22:33:44:55:66"
41+
machineWithMacAddress = "11:22:33:44:55:77"
4142
privateIPV4Address = "192.168.47.11"
4243
ipamConfigFile = "config.yaml"
4344
)

0 commit comments

Comments
 (0)