Skip to content

Commit c590442

Browse files
committed
client-go/util/consistencydetector: refactor TestCheckListFromCacheDataConsistencyIfRequestedInternalHappyPath to work with unstructured data
1 parent 10d66ca commit c590442

File tree

1 file changed

+38
-20
lines changed

1 file changed

+38
-20
lines changed

staging/src/k8s.io/client-go/util/consistencydetector/list_data_consistency_detector_test.go

Lines changed: 38 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ import (
2424

2525
v1 "k8s.io/api/core/v1"
2626
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
27+
"k8s.io/apimachinery/pkg/runtime"
2728
"k8s.io/utils/ptr"
2829
)
2930

@@ -52,26 +53,43 @@ func TestCheckListFromCacheDataConsistencyIfRequestedInternalPanics(t *testing.T
5253
}
5354

5455
func TestCheckListFromCacheDataConsistencyIfRequestedInternalHappyPath(t *testing.T) {
55-
ctx := context.TODO()
56-
listOptions := metav1.ListOptions{TimeoutSeconds: ptr.To(int64(39))}
57-
expectedRequestOptions := metav1.ListOptions{
58-
ResourceVersion: "2",
59-
ResourceVersionMatch: metav1.ResourceVersionMatchExact,
60-
TimeoutSeconds: ptr.To(int64(39)),
61-
}
62-
listResponse := &v1.PodList{
63-
ListMeta: metav1.ListMeta{ResourceVersion: "2"},
64-
Items: []v1.Pod{*makePod("p1", "1"), *makePod("p2", "2")},
56+
scenarios := []struct {
57+
name string
58+
listResponse runtime.Object
59+
retrievedList runtime.Object
60+
retrievedListOptions metav1.ListOptions
61+
62+
expectedRequestOptions metav1.ListOptions
63+
}{
64+
{
65+
name: "list detector works with a typed list",
66+
listResponse: &v1.PodList{
67+
ListMeta: metav1.ListMeta{ResourceVersion: "2"},
68+
Items: []v1.Pod{*makePod("p1", "1"), *makePod("p2", "2")},
69+
},
70+
retrievedListOptions: metav1.ListOptions{TimeoutSeconds: ptr.To(int64(39))},
71+
retrievedList: &v1.PodList{
72+
ListMeta: metav1.ListMeta{ResourceVersion: "2"},
73+
Items: []v1.Pod{*makePod("p1", "1"), *makePod("p2", "2")},
74+
},
75+
expectedRequestOptions: metav1.ListOptions{
76+
ResourceVersion: "2",
77+
ResourceVersionMatch: metav1.ResourceVersionMatchExact,
78+
TimeoutSeconds: ptr.To(int64(39)),
79+
},
80+
},
6581
}
66-
retrievedList := &v1.PodList{
67-
ListMeta: metav1.ListMeta{ResourceVersion: "2"},
68-
Items: []v1.Pod{*makePod("p1", "1"), *makePod("p2", "2")},
82+
for _, scenario := range scenarios {
83+
t.Run(scenario.name, func(t *testing.T) {
84+
ctx := context.TODO()
85+
listOptions := metav1.ListOptions{TimeoutSeconds: ptr.To(int64(39))}
86+
fakeLister := &listWrapper{response: scenario.listResponse}
87+
88+
checkListFromCacheDataConsistencyIfRequestedInternal(ctx, "", fakeLister.List, listOptions, scenario.retrievedList)
89+
90+
require.Equal(t, 1, fakeLister.counter)
91+
require.Equal(t, 1, len(fakeLister.requestOptions))
92+
require.Equal(t, fakeLister.requestOptions[0], scenario.expectedRequestOptions)
93+
})
6994
}
70-
fakeLister := &listWrapper{response: listResponse}
71-
72-
checkListFromCacheDataConsistencyIfRequestedInternal(ctx, "", fakeLister.List, listOptions, retrievedList)
73-
74-
require.Equal(t, 1, fakeLister.counter)
75-
require.Equal(t, 1, len(fakeLister.requestOptions))
76-
require.Equal(t, fakeLister.requestOptions[0], expectedRequestOptions)
7795
}

0 commit comments

Comments
 (0)