Skip to content

Commit af39eb4

Browse files
author
kkumar
committed
Setup test suite
1 parent 1c611d4 commit af39eb4

File tree

2 files changed

+133
-102
lines changed

2 files changed

+133
-102
lines changed

plugins/ipam/plugin_test.go

Lines changed: 130 additions & 98 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

@@ -109,7 +88,7 @@ var _ = Describe("IPAM Plugin", func() {
10988
Expect(handler).NotTo(BeNil())
11089
})
11190

112-
It("should not return an error for empty ipam config", func() {
91+
It("should not return an error for empty IPAM config", func() {
11392
invalidConfig := &api.IPAMConfig{}
11493
invalidConfigData, err := yaml.Marshal(invalidConfig)
11594
Expect(err).NotTo(HaveOccurred())
@@ -150,87 +129,18 @@ 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

217-
Describe("K8s Client tests", func() {
218-
It("should successfully match the subnet", func() {
219-
k8sClient, err := NewK8sClient(ns.Name, []string{"foo"})
220-
Expect(err).NotTo(HaveOccurred())
221-
Expect(k8sClient).NotTo(BeNil())
222-
223-
subnet, err := k8sClient.getMatchingSubnet("foo", linkLocalIPV6Addr)
224-
Expect(err).NotTo(HaveOccurred())
225-
Expect(subnet).To(BeNil())
226-
})
227-
228-
// It("should successfully match the subnet", func() {
229-
// err := k8sClient.doCreateIpamIP(ipv6)
230-
// Expect(err).NotTo(HaveOccurred())
231-
// })
232-
})
233-
234144
Describe("Common tests", func() {
235145
It("return true checks the ip in CIDR", func() {
236146
checkIP := checkIPv6InCIDR(linkLocalIPV6Addr, "fe80::/64")
@@ -242,7 +152,7 @@ var _ = Describe("IPAM Plugin", func() {
242152
Expect(checkIP).To(BeFalse())
243153
})
244154

245-
It("return formrted string, if valid ipv6", func() {
155+
It("return formatted string, if valid ipv6", func() {
246156
longIP := getLongIPv6(net.ParseIP("fe80::"))
247157
Expect(longIP).To(Equal("fe80-0000-0000-0000-0000-0000-0000-0000"))
248158
})
@@ -253,7 +163,7 @@ var _ = Describe("IPAM Plugin", func() {
253163
}).To(Panic())
254164
})
255165

256-
It("return pretty fromated string for ipamv1alpha1.IPSpec", func() {
166+
It("return pretty formatted string for ipamv1alpha1.IPSpec", func() {
257167
ipv6Addr, err := ipamv1alpha1.IPAddrFromString(linkLocalIPV6Addr.String())
258168
Expect(err).NotTo(HaveOccurred())
259169
ipv6 := &ipamv1alpha1.IP{
@@ -269,3 +179,125 @@ var _ = Describe("IPAM Plugin", func() {
269179
})
270180
})
271181
})
182+
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+
277+
func createTestIPAMIP(mac string, ns corev1.Namespace) (*ipamv1alpha1.IP, net.IP) {
278+
m, err := net.ParseMAC(mac)
279+
Expect(err).NotTo(HaveOccurred())
280+
i := net.ParseIP(linkLocalIPV6Prefix)
281+
linkLocalIPV6Addr, err := eui64.ParseMAC(i, m)
282+
Expect(err).NotTo(HaveOccurred())
283+
284+
sanitizedMAC := strings.Replace(mac, ":", "", -1)
285+
ipv6Addr, err := ipamv1alpha1.IPAddrFromString(linkLocalIPV6Addr.String())
286+
Expect(err).NotTo(HaveOccurred())
287+
ipv6 := &ipamv1alpha1.IP{
288+
ObjectMeta: metav1.ObjectMeta{
289+
Namespace: ns.Name,
290+
Name: "test-ip",
291+
Labels: map[string]string{
292+
"mac": sanitizedMAC,
293+
},
294+
},
295+
Spec: ipamv1alpha1.IPSpec{
296+
Subnet: corev1.LocalObjectReference{
297+
Name: "foo",
298+
},
299+
IP: ipv6Addr,
300+
},
301+
}
302+
return ipv6, linkLocalIPV6Addr
303+
}

plugins/ipam/suite_test.go

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -34,11 +34,9 @@ const (
3434
pollingInterval = 50 * time.Millisecond
3535
eventuallyTimeout = 3 * time.Second
3636
consistentlyDuration = 1 * time.Second
37-
unknownMachineMACAddress = "11:11:11:11:11:11"
3837
linkLocalIPV6Prefix = "fe80::"
39-
subnetLabel = "subnet=dhcp"
4038
machineWithIPAddressMACAddress = "11:22:33:44:55:66"
41-
privateIPV4Address = "192.168.47.11"
39+
machineWithMacAddress = "11:22:33:44:55:77"
4240
ipamConfigFile = "config.yaml"
4341
)
4442

@@ -73,7 +71,7 @@ var _ = BeforeSuite(func() {
7371
// Note that you must have the required binaries setup under the bin directory to perform
7472
// the tests directly. When we run make test it will be setup and used automatically.
7573
BinaryAssetsDirectory: filepath.Join("..", "..", "bin", "k8s",
76-
fmt.Sprintf("1.30.0-%s-%s", runtime.GOOS, runtime.GOARCH)),
74+
fmt.Sprintf("1.32.0-%s-%s", runtime.GOOS, runtime.GOARCH)),
7775
}
7876

7977
var err error
@@ -132,6 +130,7 @@ func SetupTest() *corev1.Namespace {
132130
Expect(err).NotTo(HaveOccurred())
133131
Expect(config.Namespace).To(Equal(ns.Name))
134132
Expect(config.Subnets[0]).To(Equal("ipam-subnet1"))
133+
Expect(config.Subnets[1]).To(Equal("ipam-subnet2"))
135134
})
136135

137136
return ns

0 commit comments

Comments
 (0)