Skip to content

Commit

Permalink
test: Added unit test for getRealTimeExtensionPodStats (#531)
Browse files Browse the repository at this point in the history
  • Loading branch information
nidhunge committed Apr 12, 2023
1 parent 9573d41 commit e24d23b
Show file tree
Hide file tree
Showing 2 changed files with 170 additions and 14 deletions.
170 changes: 163 additions & 7 deletions pkg/metrics/real_time_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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
}
14 changes: 7 additions & 7 deletions pkg/provider/aci_volumes_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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())
}
Expand Down Expand Up @@ -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())
}
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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())
Expand Down

0 comments on commit e24d23b

Please sign in to comment.