Skip to content

Commit

Permalink
added unit test for updatePodsLoop
Browse files Browse the repository at this point in the history
  • Loading branch information
smritidahal653 committed Apr 6, 2023
1 parent 6a71dd9 commit 2be02d1
Showing 1 changed file with 78 additions and 0 deletions.
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

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")
}

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")
}

0 comments on commit 2be02d1

Please sign in to comment.