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 updatePodsLoop #521

Merged
merged 5 commits into from
Apr 10, 2023
Merged
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
98 changes: 98 additions & 0 deletions pkg/provider/podsTracker_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -263,3 +263,101 @@ func TestCleanupDanglingPods(t *testing.T) {
}
}
}

func TestUpdatePodsLoop(t *testing.T) {
podName := "pod-" + uuid.New().String()
podNamespace := "ns-" + uuid.New().String()

mockCtrl := gomock.NewController(t)
defer mockCtrl.Finish()

aciMocks := createNewACIMock()

aciProvider, err := createTestProvider(aciMocks, NewMockConfigMapLister(mockCtrl),
NewMockSecretLister(mockCtrl), NewMockPodLister(mockCtrl))
if err != nil {
t.Fatal("failed to create the test provider", err)
}

containersList := testsutil.CreateACIContainersListObj(runningState, "Initializing",
testsutil.CgCreationTime.Add(time.Second*2), testsutil.CgCreationTime.Add(time.Second*3),
true, true, true)

cases := []struct {
description string
podPhase v1.PodPhase
expectedAssertions func(podToCheck *v1.Pod) bool
}{
{
description: "Pod is updated after retrieving the pod status from the provider",
podPhase: v1.PodPending,
expectedAssertions: func(podToCheck *v1.Pod) bool {
return (assert.Check(t, is.Equal(podToCheck.Status.Phase, v1.PodSucceeded), "Pod should be updated and phase should be set to succeeded") &&
assert.Check(t, podToCheck.Status.Conditions != nil, "Pod should be updated and podStatus conditions should be set") &&
assert.Check(t, podToCheck.Status.StartTime != nil, "Pod should be updated and podStatus start time should be set") &&
assert.Check(t, podToCheck.Status.ContainerStatuses != nil, "Pod should be updated and podStatus container statuses should be set") &&
assert.Check(t, is.Equal(len(podToCheck.Status.Conditions), 3), "Pod should be updated and 3 pod conditions should be present"))
},
},
{
description: "Pod is updated after provider cannot retrieve the pod status but the pod is in a running state",
podPhase: v1.PodRunning,
expectedAssertions: func(podToCheck *v1.Pod) bool {
return (assert.Check(t, is.Equal(podToCheck.Status.Phase, v1.PodFailed), "Pod status was not found so the pod should be updated and pod phase should be set to failed") &&
assert.Check(t, is.Equal(podToCheck.Status.Reason, statusReasonNotFound), "Pod status was not found so the pod should be updated and pod reason should be set to not found") &&
assert.Check(t, is.Equal(podToCheck.Status.Message, statusMessageNotFound), "Pod status was not found so the pod should be updated and pod message should be set to not found"))
},
},
{
description: "Pod status update is skipped because pod has reached a failed phase",
podPhase: v1.PodFailed,
expectedAssertions: func(podToCheck *v1.Pod) bool {
return (assert.Check(t, is.Equal(podToCheck.Status.Phase, v1.PodFailed), "Pod was not updated, so the status should not change") &&
assert.Check(t, is.Nil(podToCheck.Status.Conditions), "Pod was not updated, so the podStatus conditions should not be set") &&
assert.Check(t, is.Nil(podToCheck.Status.StartTime), "Pod was not updated, so the podStatus start time should not be set"))
},
},
{
description: "Pod status update is skipped because pod is in a succeeded phase",
podPhase: v1.PodSucceeded,
expectedAssertions: func(podToCheck *v1.Pod) bool {
return (assert.Check(t, is.Equal(podToCheck.Status.Phase, v1.PodSucceeded), "Pod was not updated, so the status should not change") &&
assert.Check(t, is.Nil(podToCheck.Status.Conditions), "Pod was not updated, so the podStatus conditions should not be set") &&
assert.Check(t, is.Nil(podToCheck.Status.StartTime), "Pod was not updated, so the podStatus start time should not be set"))
},
},
}

for _, tc := range cases {
t.Run(tc.description, func(t *testing.T) {
pod := testsutil.CreatePodObj(podName, podNamespace)
pod.Status.Phase = tc.podPhase
k8sPods := []*v1.Pod{pod}

aciMocks.MockGetContainerGroupInfo = func(ctx context.Context, resourceGroup, namespace, name, nodeName string) (*azaciv2.ContainerGroup, error) {
if tc.podPhase == v1.PodPending {
return testsutil.CreateContainerGroupObj(podName, podNamespace, "Succeeded", containersList, "Succeeded"), nil
} else {
return nil, errdefs.NotFound("cg is not found")
}
}

k8sPodsLister := NewMockPodLister(mockCtrl)
k8sPodsLister.EXPECT().List(gomock.Any()).Return(k8sPods, nil)

podsTracker := &PodsTracker{
pods: k8sPodsLister,
updateCb: func(updatedPod *v1.Pod) {
pod = updatedPod
},
handler: aciProvider,
}

podsTracker.updatePodsLoop(context.Background())

if !tc.expectedAssertions(pod) {
t.Error("Expected assertions failed")
}
})
}
}