diff --git a/pkg/controller/pod_test.go b/pkg/controller/pod_test.go index 38205363..26f2b21e 100644 --- a/pkg/controller/pod_test.go +++ b/pkg/controller/pod_test.go @@ -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" @@ -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{} @@ -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), @@ -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", @@ -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", ""))) + }) + }) }) }) })