Skip to content

Commit 51f89c3

Browse files
authored
Merge pull request kubernetes#125383 from p0lyn0mial/upstream-unstructured-testchecklistconsistency
client-go/consistencydetector: refactor TestCheckListFromCacheDataConsistencyIfRequestedInternalHappyPath to work with unstructured data
2 parents 10d66ca + a2a48a4 commit 51f89c3

File tree

1 file changed

+76
-20
lines changed

1 file changed

+76
-20
lines changed

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

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

2525
v1 "k8s.io/api/core/v1"
2626
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
27+
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
28+
"k8s.io/apimachinery/pkg/runtime"
2729
"k8s.io/utils/ptr"
2830
)
2931

@@ -52,26 +54,80 @@ func TestCheckListFromCacheDataConsistencyIfRequestedInternalPanics(t *testing.T
5254
}
5355

5456
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")},
57+
scenarios := []struct {
58+
name string
59+
listResponse runtime.Object
60+
retrievedList runtime.Object
61+
retrievedListOptions metav1.ListOptions
62+
63+
expectedRequestOptions metav1.ListOptions
64+
}{
65+
{
66+
name: "list detector works with a typed list",
67+
listResponse: &v1.PodList{
68+
ListMeta: metav1.ListMeta{ResourceVersion: "2"},
69+
Items: []v1.Pod{*makePod("p1", "1"), *makePod("p2", "2")},
70+
},
71+
retrievedListOptions: metav1.ListOptions{TimeoutSeconds: ptr.To(int64(39))},
72+
retrievedList: &v1.PodList{
73+
ListMeta: metav1.ListMeta{ResourceVersion: "2"},
74+
Items: []v1.Pod{*makePod("p1", "1"), *makePod("p2", "2")},
75+
},
76+
expectedRequestOptions: metav1.ListOptions{
77+
ResourceVersion: "2",
78+
ResourceVersionMatch: metav1.ResourceVersionMatchExact,
79+
TimeoutSeconds: ptr.To(int64(39)),
80+
},
81+
},
82+
{
83+
name: "list detector works with a unstructured list",
84+
listResponse: &unstructured.UnstructuredList{
85+
Object: map[string]interface{}{
86+
"apiVersion": "vTest",
87+
"kind": "rTestList",
88+
"metadata": map[string]interface{}{
89+
"resourceVersion": "3",
90+
},
91+
},
92+
Items: []unstructured.Unstructured{
93+
*makeUnstructuredObject("vTest", "rTest", "item1"),
94+
*makeUnstructuredObject("vTest", "rTest", "item2"),
95+
*makeUnstructuredObject("vTest", "rTest", "item3"),
96+
},
97+
},
98+
retrievedListOptions: metav1.ListOptions{TimeoutSeconds: ptr.To(int64(39))},
99+
retrievedList: &unstructured.UnstructuredList{
100+
Object: map[string]interface{}{
101+
"apiVersion": "vTest",
102+
"kind": "rTestList",
103+
"metadata": map[string]interface{}{
104+
"resourceVersion": "3",
105+
},
106+
},
107+
Items: []unstructured.Unstructured{
108+
*makeUnstructuredObject("vTest", "rTest", "item1"),
109+
*makeUnstructuredObject("vTest", "rTest", "item2"),
110+
*makeUnstructuredObject("vTest", "rTest", "item3"),
111+
},
112+
},
113+
expectedRequestOptions: metav1.ListOptions{
114+
ResourceVersion: "3",
115+
ResourceVersionMatch: metav1.ResourceVersionMatchExact,
116+
TimeoutSeconds: ptr.To(int64(39)),
117+
},
118+
},
65119
}
66-
retrievedList := &v1.PodList{
67-
ListMeta: metav1.ListMeta{ResourceVersion: "2"},
68-
Items: []v1.Pod{*makePod("p1", "1"), *makePod("p2", "2")},
120+
for _, scenario := range scenarios {
121+
t.Run(scenario.name, func(t *testing.T) {
122+
ctx := context.TODO()
123+
listOptions := metav1.ListOptions{TimeoutSeconds: ptr.To(int64(39))}
124+
fakeLister := &listWrapper{response: scenario.listResponse}
125+
126+
checkListFromCacheDataConsistencyIfRequestedInternal(ctx, "", fakeLister.List, listOptions, scenario.retrievedList)
127+
128+
require.Equal(t, 1, fakeLister.counter)
129+
require.Equal(t, 1, len(fakeLister.requestOptions))
130+
require.Equal(t, fakeLister.requestOptions[0], scenario.expectedRequestOptions)
131+
})
69132
}
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)
77133
}

0 commit comments

Comments
 (0)