From 2d885a2ed5f7a375f321c0c37d618f7f4b823e13 Mon Sep 17 00:00:00 2001 From: Lukasz Szaszkiewicz Date: Fri, 7 Jun 2024 10:43:28 +0200 Subject: [PATCH] client-go/util/consistencydetector: refactor TestCheckListFromCacheDataConsistencyIfRequestedInternalHappyPath to work with unstructured data Kubernetes-commit: c5904424b2b03cec1befaf16c55bb97df57669bc --- .../list_data_consistency_detector_test.go | 58 ++++++++++++------- 1 file changed, 38 insertions(+), 20 deletions(-) diff --git a/util/consistencydetector/list_data_consistency_detector_test.go b/util/consistencydetector/list_data_consistency_detector_test.go index 655d9c99..0a0debd7 100644 --- a/util/consistencydetector/list_data_consistency_detector_test.go +++ b/util/consistencydetector/list_data_consistency_detector_test.go @@ -24,6 +24,7 @@ import ( v1 "k8s.io/api/core/v1" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" + "k8s.io/apimachinery/pkg/runtime" "k8s.io/utils/ptr" ) @@ -52,26 +53,43 @@ func TestCheckListFromCacheDataConsistencyIfRequestedInternalPanics(t *testing.T } func TestCheckListFromCacheDataConsistencyIfRequestedInternalHappyPath(t *testing.T) { - ctx := context.TODO() - listOptions := metav1.ListOptions{TimeoutSeconds: ptr.To(int64(39))} - expectedRequestOptions := metav1.ListOptions{ - ResourceVersion: "2", - ResourceVersionMatch: metav1.ResourceVersionMatchExact, - TimeoutSeconds: ptr.To(int64(39)), - } - listResponse := &v1.PodList{ - ListMeta: metav1.ListMeta{ResourceVersion: "2"}, - Items: []v1.Pod{*makePod("p1", "1"), *makePod("p2", "2")}, - } - retrievedList := &v1.PodList{ - ListMeta: metav1.ListMeta{ResourceVersion: "2"}, - Items: []v1.Pod{*makePod("p1", "1"), *makePod("p2", "2")}, - } - fakeLister := &listWrapper{response: listResponse} + scenarios := []struct { + name string + listResponse runtime.Object + retrievedList runtime.Object + retrievedListOptions metav1.ListOptions - checkListFromCacheDataConsistencyIfRequestedInternal(ctx, "", fakeLister.List, listOptions, retrievedList) + expectedRequestOptions metav1.ListOptions + }{ + { + name: "list detector works with a typed list", + listResponse: &v1.PodList{ + ListMeta: metav1.ListMeta{ResourceVersion: "2"}, + Items: []v1.Pod{*makePod("p1", "1"), *makePod("p2", "2")}, + }, + retrievedListOptions: metav1.ListOptions{TimeoutSeconds: ptr.To(int64(39))}, + retrievedList: &v1.PodList{ + ListMeta: metav1.ListMeta{ResourceVersion: "2"}, + Items: []v1.Pod{*makePod("p1", "1"), *makePod("p2", "2")}, + }, + expectedRequestOptions: metav1.ListOptions{ + ResourceVersion: "2", + ResourceVersionMatch: metav1.ResourceVersionMatchExact, + TimeoutSeconds: ptr.To(int64(39)), + }, + }, + } + for _, scenario := range scenarios { + t.Run(scenario.name, func(t *testing.T) { + ctx := context.TODO() + listOptions := metav1.ListOptions{TimeoutSeconds: ptr.To(int64(39))} + fakeLister := &listWrapper{response: scenario.listResponse} - require.Equal(t, 1, fakeLister.counter) - require.Equal(t, 1, len(fakeLister.requestOptions)) - require.Equal(t, fakeLister.requestOptions[0], expectedRequestOptions) + checkListFromCacheDataConsistencyIfRequestedInternal(ctx, "", fakeLister.List, listOptions, scenario.retrievedList) + + require.Equal(t, 1, fakeLister.counter) + require.Equal(t, 1, len(fakeLister.requestOptions)) + require.Equal(t, fakeLister.requestOptions[0], scenario.expectedRequestOptions) + }) + } }