From 789a3640369e22fa4e58f2c423a81d9b45f8e915 Mon Sep 17 00:00:00 2001 From: Catherine Fang Date: Tue, 30 Apr 2024 15:38:21 -0400 Subject: [PATCH] Update test --- .../adapter/coreprovider/coreprovider_test.go | 54 ++++++++++--------- 1 file changed, 29 insertions(+), 25 deletions(-) diff --git a/custom-metrics-stackdriver-adapter/pkg/adapter/coreprovider/coreprovider_test.go b/custom-metrics-stackdriver-adapter/pkg/adapter/coreprovider/coreprovider_test.go index 2424435c3..b4084385f 100644 --- a/custom-metrics-stackdriver-adapter/pkg/adapter/coreprovider/coreprovider_test.go +++ b/custom-metrics-stackdriver-adapter/pkg/adapter/coreprovider/coreprovider_test.go @@ -639,9 +639,9 @@ func TestCoreprovider_GetNodeMetrics_Many(t *testing.T) { func TestCoreprovider_GetPodMetrics_Many(t *testing.T) { podObjects := []*metav1.PartialObjectMetadata{ - {ObjectMeta: metav1.ObjectMeta{Namespace: "namespace1", Name: "pod2"}}, {ObjectMeta: metav1.ObjectMeta{Namespace: "namespace1", Name: "pod1"}}, - {ObjectMeta: metav1.ObjectMeta{Namespace: "namespace2", Name: "pod1"}}, + {ObjectMeta: metav1.ObjectMeta{Namespace: "namespace2", Name: "pod2"}}, + {ObjectMeta: metav1.ObjectMeta{Namespace: "namespace3", Name: "pod3"}}, } pods := []apitypes.NamespacedName{} @@ -654,19 +654,19 @@ func TestCoreprovider_GetPodMetrics_Many(t *testing.T) { time3, _ := time.Parse(time.RFC3339, "2017-01-02T13:01:20Z") contCpuRes := map[string]map[string]resource.Quantity{ - "namespace1:pod1": {"cont1": fromDouble(0.1), "cont2": fromDouble(0.2)}, - "namespace1:pod2": {"cont1": fromDouble(0.3), "cont2": fromDouble(0.4)}, - "namespace2:pod1": {"cont1": fromDouble(0.5), "cont2": fromDouble(0.6)}, + "namespace1:pod1": {"cont1": fromDouble(0.3), "cont2": fromDouble(0.4)}, + "namespace2:pod2": {"cont3": fromDouble(0.1), "cont4": fromDouble(0.2)}, + "namespace3:pod3": {"cont5": fromDouble(0.5), "cont6": fromDouble(0.6)}, } contRamRes := map[string]map[string]resource.Quantity{ - "namespace1:pod1": {"cont1": fromInt(1000), "cont2": fromInt(1001)}, - "namespace1:pod2": {"cont1": fromInt(1002), "cont2": fromInt(1003)}, - "namespace2:pod1": {"cont1": fromInt(1004), "cont2": fromInt(1005)}, + "namespace1:pod1": {"cont1": fromInt(1002), "cont2": fromInt(1003)}, + "namespace2:pod2": {"cont3": fromInt(1000), "cont4": fromInt(1001)}, + "namespace3:pod3": {"cont5": fromInt(1004), "cont6": fromInt(1005)}, } timeRes := map[string]api.TimeInfo{ - "namespace1:pod1": {time1, time.Minute}, - "namespace1:pod2": {time2, time.Minute}, - "namespace2:pod1": {time3, time.Minute}, + "namespace1:pod1": {time2, time.Minute}, + "namespace2:pod2": {time1, time.Minute}, + "namespace3:pod3": {time3, time.Minute}, } var provider = CoreProvider{podClient([]string{doubleQuote(pods[0].Name), doubleQuote(pods[1].Name), doubleQuote(pods[2].Name)}, contCpuRes, timeRes, contRamRes, timeRes, t)} podMetrics, err := provider.GetPodMetrics(podObjects...) @@ -674,12 +674,13 @@ func TestCoreprovider_GetPodMetrics_Many(t *testing.T) { t.Fatalf("Provider error: %s", err) } + expectedPodMetrics := []metrics.PodMetrics{ metrics.PodMetrics{ ObjectMeta: metav1.ObjectMeta{ Name: "pod1", - Namespace: "namespace2", + Namespace: "namespace1", CreationTimestamp: metav1.Now(), }, Timestamp: metav1.NewTime(time2), @@ -704,22 +705,22 @@ func TestCoreprovider_GetPodMetrics_Many(t *testing.T) { metrics.PodMetrics{ ObjectMeta: metav1.ObjectMeta{ - Name: "pod1", - Namespace: "namespace1", + Name: "pod2", + Namespace: "namespace2", CreationTimestamp: metav1.Now(), }, Timestamp: metav1.NewTime(time1), Window: metav1.Duration{Duration: time.Minute}, Containers: []metrics.ContainerMetrics{ { - Name: "cont1", + Name: "cont3", Usage: corev1.ResourceList{ corev1.ResourceCPU: fromDouble(0.1), corev1.ResourceMemory: fromInt(1000), }, }, { - Name: "cont2", + Name: "cont4", Usage: corev1.ResourceList{ corev1.ResourceCPU: fromDouble(0.2), corev1.ResourceMemory: fromInt(1001), @@ -730,22 +731,22 @@ func TestCoreprovider_GetPodMetrics_Many(t *testing.T) { metrics.PodMetrics{ ObjectMeta: metav1.ObjectMeta{ - Name: "pod2", - Namespace: "namespace1", + Name: "pod3", + Namespace: "namespace3", CreationTimestamp: metav1.Now(), }, Timestamp: metav1.NewTime(time3), Window: metav1.Duration{Duration: time.Minute}, Containers: []metrics.ContainerMetrics{ { - Name: "cont1", + Name: "cont5", Usage: corev1.ResourceList{ corev1.ResourceCPU: fromDouble(0.5), corev1.ResourceMemory: fromInt(1004), }, }, { - Name: "cont2", + Name: "cont6", Usage: corev1.ResourceList{ corev1.ResourceCPU: fromDouble(0.6), corev1.ResourceMemory: fromInt(1005), @@ -755,12 +756,15 @@ func TestCoreprovider_GetPodMetrics_Many(t *testing.T) { }, } - if len(podObjects) != len(podMetrics) { - t.Fatalf("Unexpected result. Expected len: \n%v,\n received: \n%v", len(pods), len(podMetrics)) + if len(expectedPodMetrics) != len(podMetrics) { + t.Fatalf("Unexpected result. Expected len: \n%v,\n received: \n%v", len(expectedPodMetrics), len(podMetrics)) } - - if diff := cmp.Diff(expectedPodMetrics, podMetrics, cmpopts.IgnoreFields(metrics.PodMetrics{}, "ObjectMeta", "CreationTimestamp")); diff != "" { - t.Errorf("Has a diff, (-want, +got): %s", diff) + + for i := range pods { + expectedPodMetrics[i].CreationTimestamp = podMetrics[i].CreationTimestamp + if !reflect.DeepEqual(podMetrics[i], expectedPodMetrics[i]) { + t.Errorf("Unexpected result. Expected: \n%v,\n received: \n%v", expectedPodMetrics[i], podMetrics[i]) + } } }