Skip to content

Commit

Permalink
add ut
Browse files Browse the repository at this point in the history
Signed-off-by: zhuanlan <[email protected]>
  • Loading branch information
Longchuanzheng committed Jul 27, 2024
1 parent c1a00ec commit 61ec648
Showing 1 changed file with 56 additions and 3 deletions.
59 changes: 56 additions & 3 deletions pkg/controller/pod_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,16 @@ package controller
import (
"context"
"encoding/json"
"errors"
"fmt"
k8stesting "k8s.io/client-go/testing"
"os"
"path"
"testing"
"time"

"k8s.io/apimachinery/pkg/runtime"

. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"

Expand Down Expand Up @@ -77,7 +81,7 @@ var _ = Describe("Dynamic Attachment controller", func() {
cniArgs := &map[string]string{"foo": "bar"}
var (
eventRecorder *record.FakeRecorder
k8sClient k8sclient.Interface
k8sClient *fake.Clientset
pod *corev1.Pod
networkToAdd string
stopChannel chan struct{}
Expand Down Expand Up @@ -323,7 +327,7 @@ var _ = Describe("Dynamic Attachment controller", func() {
Expect(err).NotTo(HaveOccurred())
})

It("an `AddedInterface` event and then and `RemovedInterface` event are seen in the event recorded", func() {
It("an `AddedInterface` event and then a `RemovedInterface` event are seen in the event recorded", func() {
expectedEventPayload := fmt.Sprintf(
"Normal AddedInterface pod [%s]: added interface %s to network: %s",
annotations.NamespacedName(namespace, podName),
Expand Down Expand Up @@ -399,7 +403,6 @@ var _ = Describe("Dynamic Attachment controller", func() {
)
Eventually(<-eventRecorder.Events).Should(Equal(expectedEventPayload))


// reconciliation requeued without adding the next interface (net1).
expectedEventPayload = fmt.Sprintf(
"Warning FailedAddingInterface pod [%s]: failed adding interface %s to network: %s",
Expand Down Expand Up @@ -625,7 +628,57 @@ var _ = Describe("Dynamic Attachment controller", func() {
}).Should(ConsistOf(defaultNet))
})
})
When("an attachment is added with update status failed", func() {
JustBeforeEach(func() {
var err error
_, err = k8sClient.CoreV1().Pods(namespace).UpdateStatus(
context.TODO(),
updatePodSpec(pod, networkName, networkToAdd),
metav1.UpdateOptions{})
Expect(err).NotTo(HaveOccurred())
})

It("an `AddedInterface` and then a `FailedRemovingInterface` event are seen in the event recorded ", func() {

expectedError := errors.New("someerror")
k8sClient.PrependReactor("update", "pods", func(_ k8stesting.Action) (bool, runtime.Object, error) {
return true, nil, expectedError
})

expectedEventPayload := fmt.Sprintf(
"Normal AddedInterface pod [%s]: added interface %s to network: %s",
annotations.NamespacedName(namespace, podName),
"net1",
networkToAdd,
)

Eventually(<-eventRecorder.Events).Should(Equal(expectedEventPayload))

expectedEventPayload = fmt.Sprintf(
"Warning FailedRemovingInterface pod [%s]: failed removing interface %s from network: %s",
annotations.NamespacedName(namespace, podName),
"net1",
networkToAdd,
)

Eventually(<-eventRecorder.Events).Should(Equal(expectedEventPayload))
})

It("the pod network-status dosen't change", func() {
Eventually(func() ([]nad.NetworkStatus, error) {
updatedPod, err := k8sClient.CoreV1().Pods(namespace).Get(context.TODO(), podName, metav1.GetOptions{})
if err != nil {
return nil, err
}
status, err := annotations.PodDynamicNetworkStatus(updatedPod)
if err != nil {
return nil, err
}
return status, nil
}).Should(ConsistOf(
ifaceStatusForDefaultNamespace(networkName, "net0", "")))
})
})
})
})
})
Expand Down

0 comments on commit 61ec648

Please sign in to comment.