From e24d23ba5d87f6b04c7f37b252d405d9cb27a608 Mon Sep 17 00:00:00 2001 From: nidhunge <43793123+nidhunge@users.noreply.github.com> Date: Wed, 12 Apr 2023 13:43:14 -0700 Subject: [PATCH] test: Added unit test for getRealTimeExtensionPodStats (#531) --- pkg/metrics/real_time_test.go | 170 +++++++++++++++++++++++++++++-- pkg/provider/aci_volumes_test.go | 14 +-- 2 files changed, 170 insertions(+), 14 deletions(-) diff --git a/pkg/metrics/real_time_test.go b/pkg/metrics/real_time_test.go index 881b6445..f76f8cda 100644 --- a/pkg/metrics/real_time_test.go +++ b/pkg/metrics/real_time_test.go @@ -5,10 +5,16 @@ Licensed under the Apache 2.0 license. package metrics import ( + "context" + "encoding/json" + "io/ioutil" + "net/http" + "strings" "testing" "github.com/google/uuid" - "github.com/stretchr/testify/assert" + "github.com/pkg/errors" + "gotest.tools/assert" corev1 "k8s.io/api/core/v1" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" ) @@ -150,10 +156,9 @@ func TestCalculateUsageNanoCores(t *testing.T) { for _, tc := range testCases { t.Run(tc.desc, func(t *testing.T) { nanoCoreUsage := calculateUsageNanoCores(tc.containerName, tc.lastPodStatus, tc.newPodStatus) - assert.EqualValues(t, tc.expectedUsage, nanoCoreUsage, tc.desc) + assert.DeepEqual(t, tc.expectedUsage, nanoCoreUsage) }) } - } func TestFilterOutContainerNotInPod(t *testing.T) { @@ -217,10 +222,161 @@ func TestFilterOutContainerNotInPod(t *testing.T) { testDescription := "Successfully filters out containerStats for containers not in pod.spec.containers" t.Run(testDescription, func(t *testing.T) { filterOutContainerNotInPod(podStats, pod) - assert.EqualValues(t, len(podStats.Containers), 3) - assert.EqualValues(t, podStats.Containers[0].Name, fake_container1) - assert.EqualValues(t, podStats.Containers[1].Name, fake_container2) - assert.EqualValues(t, podStats.Containers[2].Name, fake_container4) + assert.DeepEqual(t, 3, len(podStats.Containers)) + assert.DeepEqual(t, fake_container1, podStats.Containers[0].Name) + assert.DeepEqual(t, fake_container2, podStats.Containers[1].Name) + assert.DeepEqual(t, fake_container4, podStats.Containers[2].Name) }) +} + +func TestGetRealTimeExtensionPodStats(t *testing.T) { + + fake_container1 := "fake-container-name1" + fake_container2 := "fake-container-name2" + fake_container3 := "fake-infra-sidecar-container" + fake_container4 := "fake-container-name4" + podName := "pod-" + uuid.New().String() + podNamespace := "ns-" + uuid.New().String() + + pendingPod := &corev1.Pod{ + ObjectMeta: metav1.ObjectMeta{ + Name: podName, + Namespace: podNamespace, + }, + Spec: corev1.PodSpec{ + Containers: []corev1.Container{ + { + Name: fake_container1, + }, + { + Name: fake_container3, + }, + }, + }, + Status: corev1.PodStatus{ + Phase: corev1.PodPending, + }, + } + + runningPod := &corev1.Pod{ + ObjectMeta: metav1.ObjectMeta{ + Name: podName, + Namespace: podNamespace, + }, + Spec: corev1.PodSpec{ + Containers: []corev1.Container{ + { + Name: fake_container1, + }, + { + Name: fake_container2, + }, + { + Name: fake_container4, + }, + }, + }, + Status: corev1.PodStatus{ + Phase: corev1.PodRunning, + }, + } + + podStats := &realtimeMetricsExtensionPodStats{ + Timestamp: 1234560000, + Containers: []containerStats{ + { + Name: fake_container1, + CPU: cpuStats{ + UsageCoreNanoSeconds: 2345678900, + }, + }, + { + Name: fake_container2, + CPU: cpuStats{ + UsageCoreNanoSeconds: 1234567800, + }, + }, + { + Name: fake_container3, + CPU: cpuStats{ + UsageCoreNanoSeconds: 9876543210, + }, + }, + { + Name: fake_container4, + CPU: cpuStats{ + UsageCoreNanoSeconds: 1237894560, + }, + }, + }, + } + + respBodyJsonBytes, err := json.Marshal(podStats) + + if err != nil { + t.Fatal("Unable to Marshal JSON", err) + + } + + respBody := string(respBodyJsonBytes) + mockResp := &http.Response{ + StatusCode: http.StatusOK, + Body: ioutil.NopCloser(strings.NewReader(respBody)), + Header: make(http.Header), + } + + // create a new http.Client with the Transport field set to the mock round tripper + client := &http.Client{ + Transport: &mockRoundTripper{ + response: mockResp, + err: nil, + }, + } + + // replace the default client with the mocked one + originalClient := http.DefaultClient + http.DefaultClient = client + defer func() { http.DefaultClient = originalClient }() + + testCases := []struct { + desc string + pod *corev1.Pod + expectedError error + }{ + { + desc: "Pod.Status.Phase is not equal to Running", + pod: pendingPod, + expectedError: errors.Errorf("invalid parameter in getRealTimePodStats, only Running pod allow to query realtime statistics"), + }, + { + desc: "Pod.Status.Phase is equal to Running", + pod: runningPod, + expectedError: nil, + }, + } + + for _, tc := range testCases { + t.Run(tc.desc, func(t *testing.T) { + realTimeExtPodStats, err := getRealTimeExtensionPodStats(context.TODO(), tc.pod) + if tc.expectedError == nil { + assert.NilError(t, err) + assert.DeepEqual(t, 3, len(realTimeExtPodStats.Containers)) + assert.DeepEqual(t, fake_container1, realTimeExtPodStats.Containers[0].Name) + assert.DeepEqual(t, fake_container2, realTimeExtPodStats.Containers[1].Name) + assert.DeepEqual(t, fake_container4, realTimeExtPodStats.Containers[2].Name) + } else { + assert.DeepEqual(t, tc.expectedError.Error(), err.Error()) + } + + }) + } +} + +type mockRoundTripper struct { + response *http.Response + err error +} +func (m *mockRoundTripper) RoundTrip(req *http.Request) (*http.Response, error) { + return m.response, m.err } diff --git a/pkg/provider/aci_volumes_test.go b/pkg/provider/aci_volumes_test.go index 81ad144c..21b6abc8 100644 --- a/pkg/provider/aci_volumes_test.go +++ b/pkg/provider/aci_volumes_test.go @@ -193,7 +193,7 @@ func TestCreatedPodWithAzureFilesVolume(t *testing.T) { err = provider.CreatePod(context.Background(), pod) if tc.expectedError == nil { - assert.NilError(t, tc.expectedError, err) + assert.NilError(t, err) } else { assert.Equal(t, tc.expectedError.Error(), err.Error()) } @@ -484,7 +484,7 @@ func TestCreatePodWithCSIVolume(t *testing.T) { err = provider.CreatePod(context.Background(), pod) if tc.expectedError == nil { - assert.NilError(t, tc.expectedError, err) + assert.NilError(t, err) } else { assert.Equal(t, tc.expectedError.Error(), err.Error()) } @@ -598,7 +598,7 @@ func TestGetVolumesForSecretVolume(t *testing.T) { if tc.expectedError == nil { azureStorageAccountName := base64.StdEncoding.EncodeToString([]byte("azureFileStorageAccountName")) azureStorageAccountKey := base64.StdEncoding.EncodeToString([]byte("azureFileStorageAccountKey")) - assert.NilError(t, tc.expectedError, err) + assert.NilError(t, err) assert.DeepEqual(t, *volumes[1].Secret[azureFileStorageAccountName], azureStorageAccountName) assert.DeepEqual(t, *volumes[1].Secret[azureFileStorageAccountKey], azureStorageAccountKey) } else { @@ -710,7 +710,7 @@ func TestGetVolumesForConfigMapVolume(t *testing.T) { volumes, err := provider.getVolumes(context.Background(), pod) if tc.expectedError == nil { - assert.NilError(t, tc.expectedError, err) + assert.NilError(t, err) fakeCaConfigData := base64.StdEncoding.EncodeToString([]byte("fake-ca-data")) assert.DeepEqual(t, *volumes[1].Secret[configMapName], fakeCaConfigData) @@ -836,7 +836,7 @@ func TestGetVolumesProjectedVolSecretSource(t *testing.T) { if tc.expectedError == nil { azureStorageAccountName := base64.StdEncoding.EncodeToString([]byte("azureFileStorageAccountName")) azureStorageAccountKey := base64.StdEncoding.EncodeToString([]byte("azureFileStorageAccountKey")) - assert.NilError(t, tc.expectedError, err) + assert.NilError(t, err) assert.DeepEqual(t, *volumes[1].Secret[azureFileStorageAccountName], azureStorageAccountName) assert.DeepEqual(t, *volumes[1].Secret[azureFileStorageAccountKey], azureStorageAccountKey) } else { @@ -957,7 +957,7 @@ func TestGetVolumesProjectedVolConfMapSource(t *testing.T) { volumes, err := provider.getVolumes(context.Background(), pod) if tc.expectedError == nil { - assert.NilError(t, tc.expectedError, err) + assert.NilError(t, err) fakeCaConfigData := base64.StdEncoding.EncodeToString([]byte("fake-ca-data")) assert.DeepEqual(t, *volumes[1].Secret[configMapName], fakeCaConfigData) @@ -1074,7 +1074,7 @@ func TestGetVolumesProjectedVolSvcAcctTokenSource(t *testing.T) { if tc.expectedError == nil { fakeServiceAccountData := base64.StdEncoding.EncodeToString([]byte("fake-svc-acct-token-data")) - assert.NilError(t, tc.expectedError, err) + assert.NilError(t, err) assert.DeepEqual(t, *volumes[1].Secret[secretName], fakeServiceAccountData) } else { assert.Equal(t, tc.expectedError.Error(), err.Error())