From 0bc818473d9a7eaab90dd9c80df796e6dd38b8b9 Mon Sep 17 00:00:00 2001 From: Smriti Dahal <93288516+smritidahal653@users.noreply.github.com> Date: Wed, 29 Mar 2023 17:48:10 -0700 Subject: [PATCH] test: added unit test for deleteContainerGroup (#506) --- pkg/provider/aci_test.go | 99 ++++++++++++++++++++++++++++++++++++++++ pkg/tests/utils.go | 9 ++++ 2 files changed, 108 insertions(+) diff --git a/pkg/provider/aci_test.go b/pkg/provider/aci_test.go index 8d1df0f0..5cda3afe 100644 --- a/pkg/provider/aci_test.go +++ b/pkg/provider/aci_test.go @@ -7,6 +7,7 @@ package provider import ( "context" "encoding/base64" + "errors" "fmt" "os" "strings" @@ -1264,3 +1265,101 @@ func TestFilterWindowsServiceAccountSecretVolume (t *testing.T) { }) } } + +func TestDeleteContainerGroup (t *testing.T) { + podName1 := "pod-" + uuid.New().String() + podName2 := "pod-" + uuid.New().String() + podNamespace := "ns-" + uuid.New().String() + + podNames:= []string{podName1, podName2} + fakePods:= testsutil.CreatePodsList(podNames, podNamespace) + + cases := []struct { + description string + podName string + cgDeleteExpectedError error + hasValidPodsTracker bool + }{ + { + description: "successfully deletes container group and updates pod status", + podName: podName1, + cgDeleteExpectedError: nil, + hasValidPodsTracker: true, + }, + { + description: "successfully deletes container group but fails to update pod status", + podName: "fakePod", + cgDeleteExpectedError: nil, + hasValidPodsTracker: false, + }, + { + description: "fails to delete container group", + podName: podName2, + cgDeleteExpectedError: errors.New("failed to delete container group"), + hasValidPodsTracker: false, + }, + } + + for _, tc := range cases { + t.Run(tc.description, func(t *testing.T) { + mockCtrl := gomock.NewController(t) + defer mockCtrl.Finish() + + aciMocks := createNewACIMock() + podLister := NewMockPodLister(mockCtrl) + + aciMocks.MockDeleteContainerGroup = func(ctx context.Context, resourceGroup, cgName string) error { + return tc.cgDeleteExpectedError + } + + provider, err := createTestProvider(aciMocks, NewMockConfigMapLister(mockCtrl), + NewMockSecretLister(mockCtrl), podLister) + if err != nil { + t.Fatal("failed to create the test provider", err) + } + + if tc.hasValidPodsTracker { + podsTracker:= &PodsTracker{ + pods: podLister, + updateCb: func(updatedPod *v1.Pod) { + for index, pod := range fakePods { + if (updatedPod.Name == pod.Name && updatedPod.Namespace == pod.Namespace) { + fakePods[index] = updatedPod + break + } + } + }, + } + podLister.EXPECT().List(gomock.Any()).Return(fakePods, nil) + + provider.tracker = podsTracker + } + + err = provider.deleteContainerGroup(context.Background(), podNamespace, tc.podName) + + if tc.cgDeleteExpectedError == nil { + assert.NilError(t, tc.cgDeleteExpectedError, err) + } else { + assert.Equal(t, tc.cgDeleteExpectedError.Error(), err.Error()) + } + + for _, pod := range fakePods { + if pod.Name == tc.podName { + for i := range pod.Status.ContainerStatuses { + if (tc.hasValidPodsTracker && tc.cgDeleteExpectedError == nil) { + assert.Check(t, pod.Status.ContainerStatuses[i].State.Terminated != nil, "Container should be terminated") + assert.Check(t, is.Nil((pod.Status.ContainerStatuses[i].State.Running)), "Container should not be running") + assert.Check(t, is.Equal((pod.Status.ContainerStatuses[i].State.Terminated.ExitCode), containerExitCodePodDeleted), "Status exit code should be set to pod deleted") + assert.Check(t, is.Equal((pod.Status.ContainerStatuses[i].State.Terminated.Reason), statusReasonPodDeleted), "Status reason should be set to pod deleted") + assert.Check(t, is.Equal((pod.Status.ContainerStatuses[i].State.Terminated.Message), statusMessagePodDeleted), "Status message code should be set to pod deleted") + } else { + assert.Check(t, pod.Status.ContainerStatuses[i].State.Running != nil, "Container should be running") + assert.Check(t, is.Nil((pod.Status.ContainerStatuses[i].State.Terminated)), "Container should not be terminated") + } + } + + } + } + }) + } +} diff --git a/pkg/tests/utils.go b/pkg/tests/utils.go index e615ff4b..f9b99bb2 100644 --- a/pkg/tests/utils.go +++ b/pkg/tests/utils.go @@ -355,6 +355,15 @@ func CreatePodsList(podNames []string, podNameSpace string) []*v12.Pod { }, Status: v12.PodStatus{ Phase: v12.PodRunning, + ContainerStatuses: []v12.ContainerStatus { + { + State: v12.ContainerState { + Running: &v12.ContainerStateRunning{ + StartedAt: v1.NewTime(time.Now()), + }, + }, + }, + }, }, } result = append(result, pod)