Skip to content

Commit 19b8c6b

Browse files
author
kkumar
committed
Extend tests
1 parent 08987e8 commit 19b8c6b

File tree

4 files changed

+143
-70
lines changed

4 files changed

+143
-70
lines changed

plugins/oob/k8s.go

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ package oob
55

66
import (
77
"context"
8+
"encoding/hex"
89
"encoding/json"
910
"fmt"
1011
"net"
@@ -89,7 +90,7 @@ func (k K8sClient) getIp(
8990
if len(subnetNames) == 0 {
9091
return nil, errors.New("No OOB subnets found")
9192
} else {
92-
log.Debugf("%d OOB subnets found: %s", len(subnetNames), strings.Join(subnetNames, " "))
93+
log.Infof("%d OOB subnets found: %s", len(subnetNames), strings.Join(subnetNames, " "))
9394
subnetMatch := false
9495
for _, subnetName := range subnetNames {
9596
subnet, err := k.getMatchingSubnet(subnetName, ipaddr)
@@ -99,7 +100,7 @@ func (k K8sClient) getIp(
99100
if subnet == nil {
100101
continue
101102
}
102-
log.Debugf("Selecting subnet %s/%s", k.Namespace, subnetName)
103+
log.Infof("Selecting subnet %s/%s", k.Namespace, subnetName)
103104
subnetMatch = true
104105

105106
ipamIP, err = k.prepareCreateIpamIP(subnetName, macKey)
@@ -363,7 +364,14 @@ func (k K8sClient) getMatchingSubnet(subnetName string, ipaddr net.IP) (*ipamv1a
363364
return nil, nil
364365
}
365366
if !checkIPInCIDR(ipaddr, existingSubnet.Status.Reserved.String()) && ipaddr.String() != UNKNOWN_IP {
366-
log.Infof("Cannot select subnet %s/%s, CIDR mismatch", k.Namespace, subnetName)
367+
log.Infof("Cannot select subnet %s/%s, CIDR mismatch, subnet reserved: %s, ipaddr: %s", k.Namespace, subnetName, existingSubnet.Status.Reserved.String(), ipaddr.String())
368+
log.Infof("IP Type: %T, Value: %v", ipaddr, ipaddr)
369+
decodedIP, err := hex.DecodeString(ipaddr.String())
370+
if err != nil {
371+
log.Errorf("Failed to decode IP: %v", err)
372+
} else {
373+
log.Infof("Decoded IP Address: %s", string(decodedIP))
374+
}
367375
return nil, nil
368376
}
369377

@@ -416,7 +424,7 @@ func checkIPInCIDR(ip net.IP, cidrStr string) bool {
416424
// Parse the CIDR string
417425
_, cidrNet, err := net.ParseCIDR(cidrStr)
418426
if err != nil {
419-
log.Errorf("Error parsing CIDR: %v\n", err)
427+
log.Infof("Error parsing CIDR: %v\n", err)
420428
return false
421429
}
422430

plugins/oob/plugin.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -175,7 +175,7 @@ func handler4(req, resp *dhcpv4.DHCPv4) (*dhcpv4.DHCPv4, bool) {
175175
// ack requested address
176176
exactIP = true
177177
ipaddr = clientIP
178-
log.Infof("IP client: %v", ipaddr)
178+
log.Infof("IP client test: %s", ipaddr)
179179
} else if requestedIP != nil {
180180
// ack requested address
181181
exactIP = true

plugins/oob/plugin_test.go

Lines changed: 51 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ var _ = Describe("OOB Plugin", func() {
2828
Expect(err).NotTo(HaveOccurred())
2929
Expect(config).NotTo(BeNil())
3030
Expect(config.Namespace).To(Equal(ns.Name))
31-
Expect(config.SubnetLabel).To(Equal(subnetLabel))
31+
Expect(config.SubnetLabel).To(Equal("subnet=foo"))
3232
})
3333

3434
It("should return an error if the configuration file is missing", func() {
@@ -106,6 +106,48 @@ var _ = Describe("OOB Plugin", func() {
106106
})
107107
})
108108

109+
Describe("K8s Client tests", func() {
110+
It("should successfully match the subnet", func() {
111+
subnets := k8sClient.getOOBNetworks(ipamv1alpha1.CIPv6SubnetType)
112+
Expect(subnets).NotTo(BeNil())
113+
Expect(subnets).To(HaveLen(1))
114+
})
115+
116+
It("should match the subnet", func() {
117+
subnet, err := k8sClient.getMatchingSubnet("foo-v6", linkLocalIPV6Addr)
118+
Expect(err).NotTo(HaveOccurred())
119+
Expect(subnet).NotTo(BeNil())
120+
})
121+
122+
It("should return (nil, nil) and not match the subnet if random subnet passed", func() {
123+
subnet, err := k8sClient.getMatchingSubnet("randomfoo", linkLocalIPV6Addr)
124+
Expect(err).ToNot(HaveOccurred())
125+
Expect(subnet).To(BeNil())
126+
})
127+
128+
It("should not match the subnet", func() {
129+
m, err := net.ParseMAC(unknownMachineMACAddress)
130+
Expect(err).NotTo(HaveOccurred())
131+
i := net.ParseIP("fe90::")
132+
unknownIPV6Addr, err := eui64.ParseMAC(i, m)
133+
Expect(err).NotTo(HaveOccurred())
134+
135+
subnet, err := k8sClient.getMatchingSubnet("foo", unknownIPV6Addr)
136+
Expect(err).ToNot(HaveOccurred())
137+
Expect(subnet).To(BeNil())
138+
})
139+
140+
It("return true checks the ip in CIDR", func() {
141+
checkIP := checkIPInCIDR(linkLocalIPV6Addr, "fe80::/64")
142+
Expect(checkIP).To(BeTrue())
143+
})
144+
145+
It("return false, if invalid CIDR", func() {
146+
checkIP := checkIPInCIDR(linkLocalIPV6Addr, "fe80::")
147+
Expect(checkIP).To(BeFalse())
148+
})
149+
})
150+
109151
Describe("Plugin Setup4", func() {
110152
It("should return an error for invalid subnetLabel in the config", func() {
111153
invalidConfig := &api.OOBConfig{
@@ -151,57 +193,15 @@ var _ = Describe("OOB Plugin", func() {
151193
Expect(resp).To(BeNil())
152194
})
153195

154-
// It("should successfully handle request", func() {
155-
// req, _ := dhcpv4.New()
156-
// resp, _ := dhcpv4.NewReplyFromRequest(req)
157-
158-
// resp, stop := handler4(req, resp)
159-
// Expect(stop).To(BeFalse())
160-
// Expect(resp).NotTo(BeNil())
161-
// })
162-
})
163-
164-
Describe("K8s Client tests", func() {
165-
It("should successfully match the subnet", func() {
166-
subnets := k8sClient.getOOBNetworks(ipamv1alpha1.CIPv6SubnetType)
167-
Expect(subnets).NotTo(BeNil())
168-
Expect(subnets).To(HaveLen(1))
169-
})
170-
171-
It("should match the subnet", func() {
172-
subnet, err := k8sClient.getMatchingSubnet("foo", linkLocalIPV6Addr)
173-
Expect(err).NotTo(HaveOccurred())
174-
Expect(subnet).NotTo(BeNil())
175-
})
176-
177-
It("should return (nil, nil) and not match the subnet if random subnet passed", func() {
178-
subnet, err := k8sClient.getMatchingSubnet("randomfoo", linkLocalIPV6Addr)
179-
Expect(err).ToNot(HaveOccurred())
180-
Expect(subnet).To(BeNil())
181-
})
182-
183-
It("should not match the subnet", func() {
184-
m, err := net.ParseMAC(unknownMachineMACAddress)
185-
Expect(err).NotTo(HaveOccurred())
186-
i := net.ParseIP("fe90::")
187-
unknownIPV6Addr, err := eui64.ParseMAC(i, m)
188-
Expect(err).NotTo(HaveOccurred())
189-
190-
subnet, err := k8sClient.getMatchingSubnet("foo", unknownIPV6Addr)
191-
Expect(err).ToNot(HaveOccurred())
192-
Expect(subnet).To(BeNil())
193-
})
194-
195-
It("return true checks the ip in CIDR", func() {
196-
checkIP := checkIPInCIDR(linkLocalIPV6Addr, "fe80::/64")
197-
Expect(checkIP).To(BeTrue())
198-
})
196+
It("should successfully handle request", func() {
197+
req, _ := dhcpv4.New()
198+
req.ClientHWAddr, _ = net.ParseMAC(machineWithIPAddressMACAddress)
199+
req.ClientIPAddr = net.ParseIP(privateIPV4Address)
200+
resp, _ := dhcpv4.NewReplyFromRequest(req)
199201

200-
It("return false, if invalid CIDR", func() {
201-
checkIP := checkIPInCIDR(linkLocalIPV6Addr, "fe80::")
202-
Expect(checkIP).To(BeFalse())
202+
_, stop := handler4(req, resp)
203+
Expect(stop).To(BeFalse())
204+
Expect(resp).NotTo(BeNil())
203205
})
204-
205206
})
206-
207207
})

plugins/oob/suite_test.go

Lines changed: 79 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -44,9 +44,8 @@ const (
4444
oobConfigFile = "config.yaml"
4545
unknownMachineMACAddress = "11:11:11:11:11:11"
4646
linkLocalIPV6Prefix = "fe80::"
47-
subnetLabel = "subnet=foo"
4847
machineWithIPAddressMACAddress = "11:22:33:44:55:66"
49-
privateIPV4Address = "192.168.47.11"
48+
privateIPV4Address = "192.168.1.11"
5049
)
5150

5251
var (
@@ -111,22 +110,22 @@ var _ = BeforeSuite(func() {
111110
kubernetes.SetClient(&k8sClientTest)
112111
kubernetes.SetConfig(cfg)
113112

114-
//fmt.Printf("config: %v", cfg)
115-
SetupTest()
113+
setupTest6()
114+
setupTest4()
116115
})
117116

118-
func SetupTest() {
117+
func setupTest6() {
119118
ns = corev1.Namespace{
120119
ObjectMeta: metav1.ObjectMeta{
121-
GenerateName: "test-",
120+
GenerateName: "test-6",
122121
},
123122
}
124123
Expect(k8sClientTest.Create(context.Background(), &ns)).To(Succeed(), "failed to create test namespace")
125124

126125
configFile := oobConfigFile
127126
data := &api.OOBConfig{
128127
Namespace: ns.Name,
129-
SubnetLabel: subnetLabel,
128+
SubnetLabel: "subnet=foo",
130129
}
131130

132131
configData, err := yaml.Marshal(data)
@@ -144,14 +143,10 @@ func SetupTest() {
144143
Expect(err).NotTo(HaveOccurred())
145144
Expect(handler).NotTo(BeNil())
146145

147-
k8sClient, err = NewK8sClient(ns.Name, "subnet=foo")
148-
Expect(err).NotTo(HaveOccurred())
149-
Expect(k8sClient).NotTo(BeNil())
150-
151146
subnet6 := &ipamv1alpha1.Subnet{
152147
ObjectMeta: metav1.ObjectMeta{
153148
Namespace: ns.Name,
154-
Name: "foo",
149+
Name: "foo-v6",
155150
Labels: map[string]string{
156151
"subnet": "foo",
157152
},
@@ -182,14 +177,14 @@ func SetupTest() {
182177
ipv6 := &ipamv1alpha1.IP{
183178
ObjectMeta: metav1.ObjectMeta{
184179
Namespace: ns.Name,
185-
Name: "test-ip",
180+
Name: "test-ipv6",
186181
Labels: map[string]string{
187182
"mac": sanitizedMAC,
188183
},
189184
},
190185
Spec: ipamv1alpha1.IPSpec{
191186
Subnet: corev1.LocalObjectReference{
192-
Name: "foo",
187+
Name: "foo-v6",
193188
},
194189
IP: ipv6Addr,
195190
},
@@ -202,3 +197,73 @@ func SetupTest() {
202197
ipv6.Status.Reserved = ipv6.Spec.IP
203198
})).Should(Succeed())
204199
}
200+
201+
func setupTest4() {
202+
configFile := oobConfigFile
203+
data := &api.OOBConfig{
204+
Namespace: ns.Name,
205+
SubnetLabel: "subnet=foo",
206+
}
207+
208+
configData, err := yaml.Marshal(data)
209+
Expect(err).NotTo(HaveOccurred())
210+
211+
file, err := os.CreateTemp(GinkgoT().TempDir(), configFile)
212+
Expect(err).NotTo(HaveOccurred())
213+
defer func() {
214+
_ = file.Close()
215+
}()
216+
217+
Expect(os.WriteFile(file.Name(), configData, 0644)).To(Succeed())
218+
219+
handler, err := setup4(file.Name())
220+
Expect(err).NotTo(HaveOccurred())
221+
Expect(handler).NotTo(BeNil())
222+
subnet4 := &ipamv1alpha1.Subnet{
223+
ObjectMeta: metav1.ObjectMeta{
224+
Namespace: ns.Name,
225+
Name: "foo-v4",
226+
Labels: map[string]string{
227+
"subnet": "foo",
228+
},
229+
},
230+
}
231+
232+
cidr := &ipamv1alpha1.CIDR{
233+
Net: netip.MustParsePrefix("192.168.1.0/24"),
234+
}
235+
Expect(k8sClientTest.Create(context.Background(), subnet4)).To(Succeed())
236+
DeferCleanup(k8sClientTest.Delete, subnet4)
237+
238+
Eventually(UpdateStatus(subnet4, func() {
239+
subnet4.Status.Type = ipamv1alpha1.CIPv4SubnetType
240+
subnet4.Status.Reserved = cidr
241+
})).Should(Succeed())
242+
243+
By("creating an IPAM IPv4")
244+
sanitizedMAC := strings.Replace(machineWithIPAddressMACAddress, ":", "", -1)
245+
ipv4Addr, err := ipamv1alpha1.IPAddrFromString(privateIPV4Address)
246+
Expect(err).NotTo(HaveOccurred())
247+
ipv4 := &ipamv1alpha1.IP{
248+
ObjectMeta: metav1.ObjectMeta{
249+
Namespace: ns.Name,
250+
Name: "test-ipv4",
251+
Labels: map[string]string{
252+
"mac": sanitizedMAC,
253+
},
254+
},
255+
Spec: ipamv1alpha1.IPSpec{
256+
Subnet: corev1.LocalObjectReference{
257+
Name: "foo-v4",
258+
},
259+
IP: ipv4Addr,
260+
},
261+
}
262+
263+
Expect(k8sClientTest.Create(context.Background(), ipv4)).To(Succeed())
264+
DeferCleanup(k8sClientTest.Delete, ipv4)
265+
266+
Eventually(UpdateStatus(ipv4, func() {
267+
ipv4.Status.Reserved = ipv4.Spec.IP
268+
})).Should(Succeed())
269+
}

0 commit comments

Comments
 (0)