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 1 commit
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
78 changes: 78 additions & 0 deletions pkg/provider/podsTracker_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -263,3 +263,81 @@ func TestCleanupDanglingPods(t *testing.T) {
}
}
}

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

podsNames := []string{podName1, podName2, podName3}
k8sPods := testsutil.CreatePodsList(podsNames, podNamespace)

k8sPods[0].Status.Phase = v1.PodPending
k8sPods[1].Status.Phase = v1.PodRunning
k8sPods[2].Status.Phase = v1.PodPending
smritidahal653 marked this conversation as resolved.
Show resolved Hide resolved

podsCopy := make([]*v1.Pod, len(k8sPods))
copy(podsCopy, k8sPods)

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

aciMocks := createNewACIMock()

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

aciMocks.MockGetContainerGroupInfo = func(ctx context.Context, resourceGroup, namespace, name, nodeName string) (*azaciv2.ContainerGroup, error) {
if name == podName1 {
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)

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

podsTracker := &PodsTracker{
pods: k8sPodsLister,
updateCb: func(updatedPod *v1.Pod) {
for i := range k8sPods {
if k8sPods[i].Name == updatedPod.Name && k8sPods[i].Namespace == updatedPod.Namespace {
k8sPods[i] = updatedPod
break
}
}
},
handler: aciProvider,
}

podsTracker.updatePodsLoop(context.Background())

if equality.Semantic.DeepEqual(k8sPods[0], podsCopy[0]) {
t.Errorf("pod has been updated so it should not be deeply equal to the original copy")
}
if equality.Semantic.DeepEqual(k8sPods[1], podsCopy[1]) {
t.Errorf("pod has been updated so it should not be deeply equal to the original copy")
}
if !equality.Semantic.DeepEqual(k8sPods[2], podsCopy[2]) {
t.Errorf("pod has not been updated, so it should remain the same as the original copy")
}
smritidahal653 marked this conversation as resolved.
Show resolved Hide resolved

assert.Equal(t, k8sPods[0].Status.Phase, v1.PodSucceeded, "Pod should be updatd and phase should be set to succeeded")
assert.Check(t, k8sPods[0].Status.Conditions != nil, "Pod should be updated and podStatus conditions should be set")
assert.Check(t, k8sPods[0].Status.StartTime != nil, "Pod should be updated and podStatus start time should be set")
assert.Check(t, k8sPods[0].Status.ContainerStatuses != nil, "Pod should be updated and podStatus container statuses should be set")
assert.Check(t, is.Equal(len(k8sPods[0].Status.Conditions), 3), "Pod should be updated and 3 pod conditions should be present")

assert.Equal(t, k8sPods[1].Status.Phase, v1.PodFailed, "Pod status was not found so the pod should be updated and pod phase should be set to failed")
assert.Equal(t, k8sPods[1].Status.Reason, statusReasonNotFound, "Pod status was not found so the pod should be updated and pod reason should be set to not found")
assert.Equal(t, k8sPods[1].Status.Message, statusMessageNotFound, "Pod status was not found so the pod should be updated and pod message should be set to not found")
}