From 2be02d1414b5955f0ef8fc4405153a06f53071d3 Mon Sep 17 00:00:00 2001 From: Smriti Dahal Date: Thu, 6 Apr 2023 16:55:52 -0700 Subject: [PATCH] added unit test for updatePodsLoop --- pkg/provider/podsTracker_test.go | 78 ++++++++++++++++++++++++++++++++ 1 file changed, 78 insertions(+) diff --git a/pkg/provider/podsTracker_test.go b/pkg/provider/podsTracker_test.go index 4418fdb4..7fefb37a 100644 --- a/pkg/provider/podsTracker_test.go +++ b/pkg/provider/podsTracker_test.go @@ -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") +}