diff --git a/staging/src/k8s.io/client-go/util/consistencydetector/list_data_consistency_detector_test.go b/staging/src/k8s.io/client-go/util/consistencydetector/list_data_consistency_detector_test.go index 655d9c99829..945e0731025 100644 --- a/staging/src/k8s.io/client-go/util/consistencydetector/list_data_consistency_detector_test.go +++ b/staging/src/k8s.io/client-go/util/consistencydetector/list_data_consistency_detector_test.go @@ -24,6 +24,8 @@ import ( v1 "k8s.io/api/core/v1" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" + "k8s.io/apimachinery/pkg/apis/meta/v1/unstructured" + "k8s.io/apimachinery/pkg/runtime" "k8s.io/utils/ptr" ) @@ -52,26 +54,80 @@ 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)), + }, + }, + { + name: "list detector works with a unstructured list", + listResponse: &unstructured.UnstructuredList{ + Object: map[string]interface{}{ + "apiVersion": "vTest", + "kind": "rTestList", + "metadata": map[string]interface{}{ + "resourceVersion": "3", + }, + }, + Items: []unstructured.Unstructured{ + *makeUnstructuredObject("vTest", "rTest", "item1"), + *makeUnstructuredObject("vTest", "rTest", "item2"), + *makeUnstructuredObject("vTest", "rTest", "item3"), + }, + }, + retrievedListOptions: metav1.ListOptions{TimeoutSeconds: ptr.To(int64(39))}, + retrievedList: &unstructured.UnstructuredList{ + Object: map[string]interface{}{ + "apiVersion": "vTest", + "kind": "rTestList", + "metadata": map[string]interface{}{ + "resourceVersion": "3", + }, + }, + Items: []unstructured.Unstructured{ + *makeUnstructuredObject("vTest", "rTest", "item1"), + *makeUnstructuredObject("vTest", "rTest", "item2"), + *makeUnstructuredObject("vTest", "rTest", "item3"), + }, + }, + expectedRequestOptions: metav1.ListOptions{ + ResourceVersion: "3", + 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) + }) + } }