k8s.io/client-go@v0.31.1/util/consistencydetector/list_data_consistency_detector_test.go (about) 1 /* 2 Copyright 2024 The Kubernetes Authors. 3 4 Licensed under the Apache License, Version 2.0 (the "License"); 5 you may not use this file except in compliance with the License. 6 You may obtain a copy of the License at 7 8 http://www.apache.org/licenses/LICENSE-2.0 9 10 Unless required by applicable law or agreed to in writing, software 11 distributed under the License is distributed on an "AS IS" BASIS, 12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 See the License for the specific language governing permissions and 14 limitations under the License. 15 */ 16 17 package consistencydetector 18 19 import ( 20 "context" 21 "testing" 22 23 "github.com/stretchr/testify/require" 24 25 v1 "k8s.io/api/core/v1" 26 metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" 27 "k8s.io/apimachinery/pkg/apis/meta/v1/unstructured" 28 "k8s.io/apimachinery/pkg/runtime" 29 "k8s.io/utils/ptr" 30 ) 31 32 var ( 33 emptyListFunc = func(_ context.Context, opts metav1.ListOptions) (*v1.PodList, error) { 34 return &v1.PodList{}, nil 35 } 36 emptyListOptions = metav1.ListOptions{} 37 ) 38 39 func TestDriveCheckWatchListFromCacheDataConsistencyIfRequested(t *testing.T) { 40 ctx := context.TODO() 41 42 CheckWatchListFromCacheDataConsistencyIfRequested(ctx, "", emptyListFunc, emptyListOptions, &v1.PodList{}) 43 } 44 45 func TestDriveCheckListFromCacheDataConsistencyIfRequested(t *testing.T) { 46 ctx := context.TODO() 47 48 CheckListFromCacheDataConsistencyIfRequested(ctx, "", emptyListFunc, emptyListOptions, &v1.PodList{}) 49 } 50 51 func TestCheckListFromCacheDataConsistencyIfRequestedInternalPanics(t *testing.T) { 52 ctx := context.TODO() 53 pod := makePod("p1", "1") 54 55 wrappedTarget := func() { 56 checkListFromCacheDataConsistencyIfRequestedInternal(ctx, "", emptyListFunc, emptyListOptions, pod) 57 } 58 59 require.PanicsWithError(t, "object does not implement the List interfaces", wrappedTarget) 60 } 61 62 func TestCheckListFromCacheDataConsistencyIfRequestedInternalHappyPath(t *testing.T) { 63 scenarios := []struct { 64 name string 65 listResponse runtime.Object 66 retrievedList runtime.Object 67 retrievedListOptions metav1.ListOptions 68 69 expectedRequestOptions metav1.ListOptions 70 }{ 71 { 72 name: "list detector works with a typed list", 73 listResponse: &v1.PodList{ 74 ListMeta: metav1.ListMeta{ResourceVersion: "2"}, 75 Items: []v1.Pod{*makePod("p1", "1"), *makePod("p2", "2")}, 76 }, 77 retrievedListOptions: metav1.ListOptions{TimeoutSeconds: ptr.To(int64(39))}, 78 retrievedList: &v1.PodList{ 79 ListMeta: metav1.ListMeta{ResourceVersion: "2"}, 80 Items: []v1.Pod{*makePod("p1", "1"), *makePod("p2", "2")}, 81 }, 82 expectedRequestOptions: metav1.ListOptions{ 83 ResourceVersion: "2", 84 ResourceVersionMatch: metav1.ResourceVersionMatchExact, 85 TimeoutSeconds: ptr.To(int64(39)), 86 }, 87 }, 88 { 89 name: "list detector works with a unstructured list", 90 listResponse: &unstructured.UnstructuredList{ 91 Object: map[string]interface{}{ 92 "apiVersion": "vTest", 93 "kind": "rTestList", 94 "metadata": map[string]interface{}{ 95 "resourceVersion": "3", 96 }, 97 }, 98 Items: []unstructured.Unstructured{ 99 *makeUnstructuredObject("vTest", "rTest", "item1"), 100 *makeUnstructuredObject("vTest", "rTest", "item2"), 101 *makeUnstructuredObject("vTest", "rTest", "item3"), 102 }, 103 }, 104 retrievedListOptions: metav1.ListOptions{TimeoutSeconds: ptr.To(int64(39))}, 105 retrievedList: &unstructured.UnstructuredList{ 106 Object: map[string]interface{}{ 107 "apiVersion": "vTest", 108 "kind": "rTestList", 109 "metadata": map[string]interface{}{ 110 "resourceVersion": "3", 111 }, 112 }, 113 Items: []unstructured.Unstructured{ 114 *makeUnstructuredObject("vTest", "rTest", "item1"), 115 *makeUnstructuredObject("vTest", "rTest", "item2"), 116 *makeUnstructuredObject("vTest", "rTest", "item3"), 117 }, 118 }, 119 expectedRequestOptions: metav1.ListOptions{ 120 ResourceVersion: "3", 121 ResourceVersionMatch: metav1.ResourceVersionMatchExact, 122 TimeoutSeconds: ptr.To(int64(39)), 123 }, 124 }, 125 } 126 for _, scenario := range scenarios { 127 t.Run(scenario.name, func(t *testing.T) { 128 ctx := context.TODO() 129 listOptions := metav1.ListOptions{TimeoutSeconds: ptr.To(int64(39))} 130 fakeLister := &listWrapper{response: scenario.listResponse} 131 132 checkListFromCacheDataConsistencyIfRequestedInternal(ctx, "", fakeLister.List, listOptions, scenario.retrievedList) 133 134 require.Equal(t, 1, fakeLister.counter) 135 require.Len(t, fakeLister.requestOptions, 1) 136 require.Equal(t, fakeLister.requestOptions[0], scenario.expectedRequestOptions) 137 }) 138 } 139 }