Skip to content

Commit e934941

Browse files
authored
Merge pull request #290 from sunya-ch/unit-test
fix: verify cidr,ippool in controller test
2 parents d7c3bab + bc9e84b commit e934941

4 files changed

Lines changed: 116 additions & 138 deletions

File tree

controllers/cidr_handler_test.go

Lines changed: 26 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,7 @@ import (
44
"context"
55
"encoding/json"
66
"fmt"
7-
"reflect"
87
"time"
9-
"unsafe"
108

119
multinicv1 "github.com/foundation-model-stack/multi-nic-cni/api/v1"
1210
. "github.com/foundation-model-stack/multi-nic-cni/controllers"
@@ -18,9 +16,6 @@ import (
1816
"k8s.io/apimachinery/pkg/api/errors"
1917
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
2018
"k8s.io/apimachinery/pkg/runtime"
21-
"k8s.io/apimachinery/pkg/runtime/schema"
22-
dynamicfake "k8s.io/client-go/dynamic/fake"
23-
k8sfake "k8s.io/client-go/kubernetes/fake"
2419
)
2520

2621
var (
@@ -29,36 +24,6 @@ var (
2924
networkAddresses []string = []string{"10.242.0.0/24", "10.242.1.0/24", "10.242.2.0/24", "10.242.3.0/24"}
3025
)
3126

32-
func newTestNetAttachDefHandler(scheme *runtime.Scheme) *plugin.NetAttachDefHandler {
33-
// Create a fake clientset
34-
fakeClient := k8sfake.NewSimpleClientset()
35-
36-
// Since we can't directly convert types, we'll create a new NetAttachDefHandler
37-
// that embeds the DynamicHandler and uses the fake client
38-
handler := &plugin.NetAttachDefHandler{
39-
DynamicHandler: &plugin.DynamicHandler{
40-
DYN: dynamicfake.NewSimpleDynamicClient(scheme),
41-
GVR: schema.GroupVersionResource{
42-
Group: "k8s.cni.cncf.io",
43-
Version: "v1",
44-
Resource: "network-attachment-definitions",
45-
},
46-
},
47-
}
48-
49-
// Use reflection to set the Clientset field
50-
clientsetValue := reflect.ValueOf(handler).Elem()
51-
clientsetField := clientsetValue.FieldByName("Clientset")
52-
if clientsetField.IsValid() && clientsetField.CanSet() {
53-
// Convert the fake client to an unsafe pointer and back to the expected type
54-
ptr := unsafe.Pointer(fakeClient)
55-
converted := reflect.NewAt(clientsetField.Type(), ptr).Elem()
56-
clientsetField.Set(converted)
57-
}
58-
59-
return handler
60-
}
61-
6227
var _ = Describe("Test CIDR Handler ", func() {
6328
cniVersion := "0.3.0"
6429
cniType := "ipvlan"
@@ -378,11 +343,15 @@ var _ = Describe("Test CIDR Handler ", func() {
378343
handler = newCIDRHandler(make(chan struct{}))
379344
scheme = runtime.NewScheme()
380345
Expect(multinicv1.AddToScheme(scheme)).To(Succeed())
381-
defHandler = newTestNetAttachDefHandler(scheme)
346+
var err error
347+
defHandler, err = plugin.GetNetAttachDefHandler(Cfg)
348+
Expect(err).To(BeNil())
382349
})
383350

384351
It("should sync network attachments and update internal state", func() {
385352
testCIDRName := "test-network"
353+
testPodCIDR := "10.0.0.1/30"
354+
testIPPoolName := handler.GetIPPoolName(testCIDRName, testPodCIDR)
386355
testCIDRSpec := multinicv1.CIDRSpec{
387356
Config: multinicv1.PluginConfig{
388357
Name: "test-network",
@@ -403,7 +372,7 @@ var _ = Describe("Test CIDR Handler ", func() {
403372
HostName: "host1",
404373
InterfaceName: "eth1",
405374
HostIP: "10.0.0.1",
406-
PodCIDR: "10.0.0.1/30",
375+
PodCIDR: testPodCIDR,
407376
IPPool: "ippool1",
408377
},
409378
},
@@ -412,6 +381,11 @@ var _ = Describe("Test CIDR Handler ", func() {
412381
}
413382

414383
// Set up the test state
384+
By("Creating MultiNicNetwork")
385+
multinicnetwork := GetMultiNicCNINetwork(testCIDRName, cniVersion, cniType, cniArgs)
386+
err := handler.Client.Create(context.TODO(), multinicnetwork)
387+
Expect(err).NotTo(HaveOccurred())
388+
415389
By("Setting up test CIDR in cache")
416390
handler.SetCache(testCIDRName, testCIDRSpec)
417391

@@ -422,35 +396,39 @@ var _ = Describe("Test CIDR Handler ", func() {
422396
},
423397
Spec: testCIDRSpec,
424398
}
425-
err := K8sClient.Create(context.TODO(), cidrObj)
399+
err = K8sClient.Create(context.TODO(), cidrObj)
426400
Expect(err).NotTo(HaveOccurred())
427401

428402
By("Setting up IPPool in cache")
429403
mockIPPool := multinicv1.IPPool{
430404
ObjectMeta: metav1.ObjectMeta{
431-
Name: testCIDRName,
405+
Name: testIPPoolName,
432406
},
433407
Spec: multinicv1.IPPoolSpec{
434-
PodCIDR: "10.0.0.1/30",
408+
PodCIDR: testPodCIDR,
435409
},
436410
}
437-
handler.IPPoolHandler.SafeCache.SetCache(testCIDRName, mockIPPool.Spec)
411+
handler.IPPoolHandler.SafeCache.SetCache(testIPPoolName, mockIPPool.Spec)
438412

439413
// Act
440414
By("Calling SyncAllPendingCustomCR")
441415
handler.SyncAllPendingCustomCR(defHandler)
442416

443417
// Assert
444418
By("Verifying CIDR state")
445-
cidr, err := handler.GetCIDR(testCIDRName)
446-
Expect(err).NotTo(HaveOccurred())
447-
Expect(cidr).NotTo(BeNil())
448-
Expect(cidr.Spec.Config.Name).To(Equal(testCIDRName))
449-
Expect(cidr.Spec.Config.Subnet).To(Equal("10.0.0.0/16"))
419+
Eventually(func(g Gomega) {
420+
cidr, err := handler.GetCIDR(testCIDRName)
421+
g.Expect(err).NotTo(HaveOccurred())
422+
Expect(cidr).NotTo(BeNil())
423+
Expect(cidr.Spec.Config.Name).To(Equal(testCIDRName))
424+
Expect(cidr.Spec.Config.Subnet).To(Equal("10.0.0.0/16"))
425+
}).WithTimeout(30 * time.Second).WithPolling(2 * time.Second).Should(Succeed())
450426

451427
By("Verifying IPPool state")
452-
ippool := handler.IPPoolHandler.SafeCache.GetCache(testCIDRName)
453-
Expect(ippool).NotTo(BeNil())
428+
Eventually(func(g Gomega) {
429+
_, err := handler.IPPoolHandler.GetIPPool(testIPPoolName)
430+
g.Expect(err).NotTo(HaveOccurred())
431+
}).WithTimeout(30 * time.Second).WithPolling(2 * time.Second).Should(Succeed())
454432
})
455433

456434
It("should delete CIDR when no corresponding MultiNicNetwork exists", func() {

controllers/testing/unit-test

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
<?xml version="1.0" encoding="UTF-8"?>
22
<testsuite name="Controller Suite" tests="36" failures="36" errors="0" time="0.063">
33
<testcase name="BeforeSuite" classname="Controller Suite" time="0.06222375">
4-
<failure type="Failure">/Users/aa404681/Documents/internal_ws/cni/multi-nic-cni-operator/controllers/suite_test.go:82&#xA;Unexpected error:&#xA; &lt;*fmt.wrapError | 0xc000514ac0&gt;: &#xA; unable to start control plane itself: failed to start the controlplane. retried 5 times: fork/exec /usr/local/kubebuilder/bin/etcd: no such file or directory&#xA; {&#xA; msg: &#34;unable to start control plane itself: failed to start the controlplane. retried 5 times: fork/exec /usr/local/kubebuilder/bin/etcd: no such file or directory&#34;,&#xA; err: &lt;*fmt.wrapError | 0xc000514aa0&gt;{&#xA; msg: &#34;failed to start the controlplane. retried 5 times: fork/exec /usr/local/kubebuilder/bin/etcd: no such file or directory&#34;,&#xA; err: &lt;*fs.PathError | 0xc0006aeb40&gt;{&#xA; Op: &#34;fork/exec&#34;,&#xA; Path: &#34;/usr/local/kubebuilder/bin/etcd&#34;,&#xA; Err: &lt;syscall.Errno&gt;0x2,&#xA; },&#xA; },&#xA; }&#xA;occurred&#xA;/Users/aa404681/Documents/internal_ws/cni/multi-nic-cni-operator/controllers/suite_test.go:99</failure>
4+
<failure type="Failure">./controllers/suite_test.go:82&#xA;Unexpected error:&#xA; &lt;*fmt.wrapError | 0xc000514ac0&gt;: &#xA; unable to start control plane itself: failed to start the controlplane. retried 5 times: fork/exec /usr/local/kubebuilder/bin/etcd: no such file or directory&#xA; {&#xA; msg: &#34;unable to start control plane itself: failed to start the controlplane. retried 5 times: fork/exec /usr/local/kubebuilder/bin/etcd: no such file or directory&#34;,&#xA; err: &lt;*fmt.wrapError | 0xc000514aa0&gt;{&#xA; msg: &#34;failed to start the controlplane. retried 5 times: fork/exec /usr/local/kubebuilder/bin/etcd: no such file or directory&#34;,&#xA; err: &lt;*fs.PathError | 0xc0006aeb40&gt;{&#xA; Op: &#34;fork/exec&#34;,&#xA; Path: &#34;/usr/local/kubebuilder/bin/etcd&#34;,&#xA; Err: &lt;syscall.Errno&gt;0x2,&#xA; },&#xA; },&#xA; }&#xA;occurred&#xA;./controllers/suite_test.go:99</failure>
55
</testcase>
66
<testcase name="AfterSuite" classname="Controller Suite" time="0.000188583">
7-
<failure type="Panic">/Users/aa404681/Documents/internal_ws/cni/multi-nic-cni-operator/controllers/suite_test.go:314&#xA;Test Panicked&#xA;/usr/local/go/src/runtime/panic.go:261</failure>
7+
<failure type="Panic">./controllers/suite_test.go:314&#xA;Test Panicked&#xA;/usr/local/go/src/runtime/panic.go:261</failure>
88
</testcase>
99
</testsuite>

testing/coverage.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ File | Function | Coverage
2020
| github.com/foundation-model-stack/multi-nic-cni/controllers/cidr_handler.go:277: | NewCIDR | 75.0% |
2121
| github.com/foundation-model-stack/multi-nic-cni/controllers/cidr_handler.go:327: | Run | 100.0% |
2222
| github.com/foundation-model-stack/multi-nic-cni/controllers/cidr_handler.go:336: | UpdateCIDRs | 100.0% |
23-
| github.com/foundation-model-stack/multi-nic-cni/controllers/cidr_handler.go:344: | ProcessUpdateRequest | 60.0% |
23+
| github.com/foundation-model-stack/multi-nic-cni/controllers/cidr_handler.go:344: | ProcessUpdateRequest | 70.0% |
2424
| github.com/foundation-model-stack/multi-nic-cni/controllers/cidr_handler.go:361: | NewCIDRWithNewConfig | 80.0% |
2525
| github.com/foundation-model-stack/multi-nic-cni/controllers/cidr_handler.go:371: | UpdateEntries | 80.9% |
2626
| github.com/foundation-model-stack/multi-nic-cni/controllers/cidr_handler.go:529: | updateCIDR | 52.0% |

0 commit comments

Comments
 (0)