Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test: added unit test for deleteContainerGroup #506

Merged
merged 5 commits into from
Mar 30, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
99 changes: 99 additions & 0 deletions pkg/provider/aci_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ package provider
import (
"context"
"encoding/base64"
"errors"
"fmt"
"os"
"strings"
Expand Down Expand Up @@ -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")
}
}

}
}
})
}
}
9 changes: 9 additions & 0 deletions pkg/tests/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down