@@ -24,6 +24,8 @@ import (
24
24
25
25
v1 "k8s.io/api/core/v1"
26
26
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
27
+ "k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
28
+ "k8s.io/apimachinery/pkg/runtime"
27
29
"k8s.io/utils/ptr"
28
30
)
29
31
@@ -52,26 +54,80 @@ func TestCheckListFromCacheDataConsistencyIfRequestedInternalPanics(t *testing.T
52
54
}
53
55
54
56
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
+ },
65
119
}
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
+ })
69
132
}
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 )
77
133
}
0 commit comments