@@ -56,6 +56,23 @@ func Test_WaitForCacheToBeUpToDate(t *testing.T) {
5656 {
5757 name : "no-op if no objects are passed in" ,
5858 },
59+ {
60+ name : "error if passed in objects have no resourceVersion set" ,
61+ objs : []client.Object {
62+ machine ("machine-1" , "" , nil ),
63+ machine ("machine-2" , "" , nil ),
64+ },
65+ clientResponses : map [client.ObjectKey ][]client.Object {
66+ {Namespace : metav1 .NamespaceDefault , Name : "machine-1" }: {
67+ machine ("machine-1" , "1" , nil ),
68+ },
69+ {Namespace : metav1 .NamespaceDefault , Name : "machine-2" }: {
70+ machine ("machine-2" , "2" , nil ),
71+ },
72+ },
73+ wantErr : "failed to wait for up-to-date Machine objects in the cache after Machine update: " +
74+ "default/machine-1: cannot compare with invalid resourceVersion: resourceVersion not set" ,
75+ },
5976 {
6077 name : "error if passed in objects have invalid resourceVersion" ,
6178 objs : []client.Object {
@@ -70,7 +87,7 @@ func Test_WaitForCacheToBeUpToDate(t *testing.T) {
7087 machine ("machine-2" , "2" , nil ),
7188 },
7289 },
73- wantErr : "failed to wait for up-to-date Machine objects in the cache after Machine creation : " +
90+ wantErr : "failed to wait for up-to-date Machine objects in the cache after Machine update : " +
7491 "default/machine-1: cannot compare with invalid resourceVersion: current: 1, expected to be >= invalidResourceVersion: resource version is not well formed: invalidResourceVersion" ,
7592 },
7693 {
@@ -87,35 +104,30 @@ func Test_WaitForCacheToBeUpToDate(t *testing.T) {
87104 machine ("machine-2" , "invalidResourceVersion" , nil ),
88105 },
89106 },
90- wantErr : "failed to wait for up-to-date Machine objects in the cache after Machine creation : " +
107+ wantErr : "failed to wait for up-to-date Machine objects in the cache after Machine update : " +
91108 "default/machine-1: cannot compare with invalid resourceVersion: current: invalidResourceVersion, expected to be >= 1: resource version is not well formed: invalidResourceVersion" ,
92109 },
93110 {
94- name : "error if objects never show up in the cache" ,
111+ name : "success if objects are never visible in the cache (deleted before WaitForCacheToBeUpToDate is called) " ,
95112 objs : []client.Object {
96113 machine ("machine-1" , "1" , nil ),
97114 machine ("machine-2" , "2" , nil ),
98115 machine ("machine-3" , "3" , nil ),
99116 machine ("machine-4" , "4" , nil ),
100117 },
101118 clientResponses : map [client.ObjectKey ][]client.Object {},
102- wantErr : "failed to wait for up-to-date Machine objects in the cache after Machine creation: timed out: [" +
103- "machines.cluster.x-k8s.io \" machine-1\" not found, " +
104- "machines.cluster.x-k8s.io \" machine-2\" not found, " +
105- "machines.cluster.x-k8s.io \" machine-3\" not found, " +
106- "machines.cluster.x-k8s.io \" machine-4\" not found]" ,
107119 },
108120 {
109121 name : "success if objects are instantly up-to-date" ,
110122 objs : []client.Object {
111- machine ("machine-1" , "" , nil ),
123+ machine ("machine-1" , "1 " , nil ),
112124 machine ("machine-2" , "2" , nil ),
113125 machine ("machine-3" , "3" , nil ),
114126 machine ("machine-4" , "4" , nil ),
115127 },
116128 clientResponses : map [client.ObjectKey ][]client.Object {
117129 {Namespace : metav1 .NamespaceDefault , Name : "machine-1" }: {
118- // For this object it's enough if it shows up, exact resourceVersion doesn't matter .
130+ // This object has an even newer resourceVersion.
119131 machine ("machine-1" , "5" , nil ),
120132 },
121133 {Namespace : metav1 .NamespaceDefault , Name : "machine-2" }: {
@@ -133,14 +145,13 @@ func Test_WaitForCacheToBeUpToDate(t *testing.T) {
133145 {
134146 name : "success if objects are up-to-date after a few tries" ,
135147 objs : []client.Object {
136- machine ("machine-1" , "" , nil ),
148+ machine ("machine-1" , "1 " , nil ),
137149 machine ("machine-2" , "10" , nil ),
138150 machine ("machine-3" , "11" , nil ),
139151 machine ("machine-4" , "12" , nil ),
140152 },
141153 clientResponses : map [client.ObjectKey ][]client.Object {
142154 {Namespace : metav1 .NamespaceDefault , Name : "machine-1" }: {
143- // For this object it's enough if it shows up, exact resourceVersion doesn't matter.
144155 machine ("machine-1" , "4" , nil ),
145156 },
146157 {Namespace : metav1 .NamespaceDefault , Name : "machine-2" }: {
@@ -195,7 +206,109 @@ func Test_WaitForCacheToBeUpToDate(t *testing.T) {
195206 },
196207 })
197208
198- err := WaitForCacheToBeUpToDate (t .Context (), fakeClient , "Machine creation" , tt .objs ... )
209+ err := WaitForCacheToBeUpToDate (t .Context (), fakeClient , "Machine update" , tt .objs ... )
210+ if tt .wantErr != "" {
211+ g .Expect (err ).To (HaveOccurred ())
212+ g .Expect (err .Error ()).To (Equal (tt .wantErr ))
213+ } else {
214+ g .Expect (err ).ToNot (HaveOccurred ())
215+ }
216+ })
217+ }
218+ }
219+
220+ func Test_WaitForObjectsToBeAddedToTheCache (t * testing.T ) {
221+ // Modify timeout to speed up test
222+ waitBackoff = wait.Backoff {
223+ Duration : 25 * time .Microsecond ,
224+ Cap : 2 * time .Second ,
225+ Factor : 1.2 ,
226+ Steps : 5 ,
227+ }
228+
229+ tests := []struct {
230+ name string
231+ objs []client.Object
232+ clientResponses map [client.ObjectKey ][]client.Object
233+ wantErr string
234+ }{
235+ {
236+ name : "no-op if no objects are passed in" ,
237+ },
238+ {
239+ name : "error if objects never show up in the cache" ,
240+ objs : []client.Object {
241+ machine ("machine-1" , "1" , nil ),
242+ machine ("machine-2" , "2" , nil ),
243+ machine ("machine-3" , "3" , nil ),
244+ machine ("machine-4" , "4" , nil ),
245+ },
246+ clientResponses : map [client.ObjectKey ][]client.Object {},
247+ wantErr : "failed to wait for up-to-date Machine objects in the cache after Machine creation: timed out: [" +
248+ "machines.cluster.x-k8s.io \" machine-1\" not found, " +
249+ "machines.cluster.x-k8s.io \" machine-2\" not found, " +
250+ "machines.cluster.x-k8s.io \" machine-3\" not found, " +
251+ "machines.cluster.x-k8s.io \" machine-4\" not found]" ,
252+ },
253+ {
254+ name : "success if objects instantly show up in the cache" ,
255+ objs : []client.Object {
256+ machine ("machine-1" , "1" , nil ),
257+ machine ("machine-2" , "2" , nil ),
258+ machine ("machine-3" , "3" , nil ),
259+ machine ("machine-4" , "4" , nil ),
260+ },
261+ clientResponses : map [client.ObjectKey ][]client.Object {
262+ {Namespace : metav1 .NamespaceDefault , Name : "machine-1" }: {
263+ // This object has an even newer resourceVersion.
264+ machine ("machine-1" , "5" , nil ),
265+ },
266+ {Namespace : metav1 .NamespaceDefault , Name : "machine-2" }: {
267+ machine ("machine-2" , "2" , nil ),
268+ },
269+ {Namespace : metav1 .NamespaceDefault , Name : "machine-3" }: {
270+ machine ("machine-3" , "3" , nil ),
271+ },
272+ {Namespace : metav1 .NamespaceDefault , Name : "machine-4" }: {
273+ // This object has an even newer resourceVersion.
274+ machine ("machine-4" , "6" , nil ),
275+ },
276+ },
277+ },
278+ }
279+
280+ for _ , tt := range tests {
281+ t .Run (tt .name , func (t * testing.T ) {
282+ g := NewWithT (t )
283+
284+ scheme := runtime .NewScheme ()
285+ _ = clusterv1 .AddToScheme (scheme )
286+
287+ callCounter := map [client.ObjectKey ]int {}
288+ fakeClient := interceptor .NewClient (fake .NewClientBuilder ().WithScheme (scheme ).Build (), interceptor.Funcs {
289+ Get : func (ctx context.Context , _ client.WithWatch , key client.ObjectKey , obj client.Object , _ ... client.GetOption ) error {
290+ if len (tt .clientResponses ) == 0 || len (tt .clientResponses [key ]) == 0 {
291+ return apierrors .NewNotFound (schema.GroupResource {
292+ Group : clusterv1 .GroupVersion .Group ,
293+ Resource : "machines" ,
294+ }, key .Name )
295+ }
296+
297+ currentCall := callCounter [key ]
298+ currentCall = min (currentCall , len (tt .clientResponses [key ])- 1 )
299+
300+ // Write back the modified object so callers can access the patched object.
301+ if err := scheme .Convert (tt.clientResponses [key ][currentCall ], obj , ctx ); err != nil {
302+ return errors .Wrapf (err , "unexpected error: failed to get" )
303+ }
304+
305+ callCounter [key ]++
306+
307+ return nil
308+ },
309+ })
310+
311+ err := WaitForObjectsToBeAddedToTheCache (t .Context (), fakeClient , "Machine creation" , tt .objs ... )
199312 if tt .wantErr != "" {
200313 g .Expect (err ).To (HaveOccurred ())
201314 g .Expect (err .Error ()).To (Equal (tt .wantErr ))
0 commit comments