@@ -153,8 +153,9 @@ impl TestKubeClient {
153
153
} )
154
154
}
155
155
156
- /// Verifies that the given pod condition becomes true within the specified timeout.
157
- pub fn verify_pod_condition ( & self , pod : & Pod , condition_type : & str ) {
156
+ /// Verifies that the given pod condition becomes true within the
157
+ /// specified timeout.
158
+ pub fn verify_pod_condition ( & self , pod : & Pod , condition_type : & str ) -> Pod {
158
159
self . runtime . block_on ( async {
159
160
self . kube_client
160
161
. verify_pod_condition ( pod, condition_type)
@@ -163,6 +164,36 @@ impl TestKubeClient {
163
164
} )
164
165
}
165
166
167
+ /// Verifies that the status of a resource fulfills the given
168
+ /// predicate within the specified timeout.
169
+ pub fn verify_status < K , P > ( & self , resource : & K , predicate : P ) -> K
170
+ where
171
+ P : Fn ( & K ) -> bool ,
172
+ K : Clone + Debug + DeserializeOwned + Resource ,
173
+ <K as Resource >:: DynamicType : Default ,
174
+ {
175
+ self . runtime . block_on ( async {
176
+ self . kube_client
177
+ . verify_status ( resource, predicate)
178
+ . await
179
+ . expect ( "Resource did not reach the expected status" )
180
+ } )
181
+ }
182
+
183
+ /// Returns the given resource with an updated status.
184
+ pub fn get_status < K > ( & self , resource : & K ) -> K
185
+ where
186
+ K : DeserializeOwned + Resource ,
187
+ <K as Resource >:: DynamicType : Default ,
188
+ {
189
+ self . runtime . block_on ( async {
190
+ self . kube_client
191
+ . get_status ( resource)
192
+ . await
193
+ . expect ( "Status could not be retrieved" )
194
+ } )
195
+ }
196
+
166
197
/// Returns the logs for the given pod.
167
198
pub fn get_logs ( & self , pod : & Pod , params : & LogParams ) -> Vec < String > {
168
199
self . runtime . block_on ( async {
@@ -197,7 +228,7 @@ pub struct Timeouts {
197
228
pub create : Duration ,
198
229
pub delete : Duration ,
199
230
pub get_annotation : Duration ,
200
- pub verify_pod_condition : Duration ,
231
+ pub verify_status : Duration ,
201
232
}
202
233
203
234
impl Default for Timeouts {
@@ -207,7 +238,7 @@ impl Default for Timeouts {
207
238
create : Duration :: from_secs ( 10 ) ,
208
239
delete : Duration :: from_secs ( 10 ) ,
209
240
get_annotation : Duration :: from_secs ( 10 ) ,
210
- verify_pod_condition : Duration :: from_secs ( 30 ) ,
241
+ verify_status : Duration :: from_secs ( 30 ) ,
211
242
}
212
243
}
213
244
}
@@ -248,6 +279,11 @@ impl KubeClient {
248
279
let timeout_secs = self . timeouts . apply_crd . as_secs ( ) as u32 ;
249
280
let crds: Api < CustomResourceDefinition > = Api :: all ( self . client . clone ( ) ) ;
250
281
282
+ let lp = ListParams :: default ( )
283
+ . fields ( & format ! ( "metadata.name={}" , crd. name( ) ) )
284
+ . timeout ( timeout_secs) ;
285
+ let mut stream = crds. watch ( & lp, "0" ) . await ?. boxed ( ) ;
286
+
251
287
let apply_params = PatchParams :: apply ( "agent_integration_test" ) . force ( ) ;
252
288
crds. patch ( & crd. name ( ) , & apply_params, & Patch :: Apply ( crd) )
253
289
. await ?;
@@ -256,11 +292,6 @@ impl KubeClient {
256
292
return Ok ( ( ) ) ;
257
293
}
258
294
259
- let lp = ListParams :: default ( )
260
- . fields ( & format ! ( "metadata.name={}" , crd. name( ) ) )
261
- . timeout ( timeout_secs) ;
262
- let mut stream = crds. watch ( & lp, "0" ) . await ?. boxed ( ) ;
263
-
264
295
while let Some ( status) = stream. try_next ( ) . await ? {
265
296
if let WatchEvent :: Modified ( crd) = status {
266
297
if is_ready ( & crd) {
@@ -320,14 +351,15 @@ impl KubeClient {
320
351
let timeout_secs = self . timeouts . create . as_secs ( ) as u32 ;
321
352
let api: Api < K > = Api :: namespaced ( self . client . clone ( ) , & self . namespace ) ;
322
353
323
- let resource = from_yaml ( spec) ;
324
- api. create ( & PostParams :: default ( ) , & resource) . await ?;
354
+ let resource: K = from_yaml ( spec) ;
325
355
326
356
let list_params = ListParams :: default ( )
327
357
. fields ( & format ! ( "metadata.name={}" , resource. name( ) ) )
328
358
. timeout ( timeout_secs) ;
329
359
let mut stream = api. watch ( & list_params, "0" ) . await ?. boxed ( ) ;
330
360
361
+ api. create ( & PostParams :: default ( ) , & resource) . await ?;
362
+
331
363
while let Some ( status) = stream. try_next ( ) . await ? {
332
364
if let WatchEvent :: Added ( resource) = status {
333
365
return Ok ( resource) ;
@@ -350,6 +382,11 @@ impl KubeClient {
350
382
let timeout_secs = self . timeouts . delete . as_secs ( ) as u32 ;
351
383
let api: Api < K > = Api :: namespaced ( self . client . clone ( ) , & self . namespace ) ;
352
384
385
+ let list_params = ListParams :: default ( )
386
+ . fields ( & format ! ( "metadata.name={}" , resource. name( ) ) )
387
+ . timeout ( timeout_secs) ;
388
+ let mut stream = api. watch ( & list_params, "0" ) . await ?. boxed ( ) ;
389
+
353
390
let result = api
354
391
. delete ( & resource. name ( ) , & DeleteParams :: default ( ) )
355
392
. await ?;
@@ -358,11 +395,6 @@ impl KubeClient {
358
395
return Ok ( ( ) ) ;
359
396
}
360
397
361
- let list_params = ListParams :: default ( )
362
- . fields ( & format ! ( "metadata.name={}" , resource. name( ) ) )
363
- . timeout ( timeout_secs) ;
364
- let mut stream = api. watch ( & list_params, "0" ) . await ?. boxed ( ) ;
365
-
366
398
while let Some ( status) = stream. try_next ( ) . await ? {
367
399
if let WatchEvent :: Deleted ( _) = status {
368
400
return Ok ( ( ) ) ;
@@ -419,42 +451,62 @@ impl KubeClient {
419
451
}
420
452
421
453
/// Verifies that the given pod condition becomes true within the specified timeout.
422
- pub async fn verify_pod_condition ( & self , pod : & Pod , condition_type : & str ) -> Result < ( ) > {
454
+ pub async fn verify_pod_condition ( & self , pod : & Pod , condition_type : & str ) -> Result < Pod > {
423
455
let is_condition_true = |pod : & Pod | {
424
456
get_pod_conditions ( pod)
425
457
. iter ( )
426
458
. any ( |condition| condition. type_ == condition_type && condition. status == "True" )
427
459
} ;
460
+ self . verify_status ( pod, is_condition_true) . await
461
+ }
428
462
429
- let timeout_secs = self . timeouts . verify_pod_condition . as_secs ( ) as u32 ;
430
- let pods: Api < Pod > = Api :: namespaced ( self . client . clone ( ) , & self . namespace ) ;
463
+ /// Verifies that the status of a resource fulfills the given
464
+ /// predicate within the specified timeout.
465
+ pub async fn verify_status < K , P > ( & self , resource : & K , predicate : P ) -> Result < K >
466
+ where
467
+ P : Fn ( & K ) -> bool ,
468
+ K : Clone + Debug + DeserializeOwned + Resource ,
469
+ <K as Resource >:: DynamicType : Default ,
470
+ {
471
+ let timeout_secs = self . timeouts . verify_status . as_secs ( ) as u32 ;
472
+ let api: Api < K > = Api :: namespaced ( self . client . clone ( ) , & self . namespace ) ;
431
473
432
474
let lp = ListParams :: default ( )
433
- . fields ( & format ! ( "metadata.name={}" , pod . name( ) ) )
475
+ . fields ( & format ! ( "metadata.name={}" , resource . name( ) ) )
434
476
. timeout ( timeout_secs) ;
435
- let mut stream = pods . watch ( & lp, "0" ) . await ?. boxed ( ) ;
477
+ let mut stream = api . watch ( & lp, "0" ) . await ?. boxed ( ) ;
436
478
437
- let pod = pods . get_status ( & pod . name ( ) ) . await ?;
479
+ let resource = api . get_status ( & resource . name ( ) ) . await ?;
438
480
439
- if is_condition_true ( & pod ) {
440
- return Ok ( ( ) ) ;
481
+ if predicate ( & resource ) {
482
+ return Ok ( resource ) ;
441
483
}
442
484
443
485
while let Some ( status) = stream. try_next ( ) . await ? {
444
- if let WatchEvent :: Modified ( pod ) = status {
445
- if is_condition_true ( & pod ) {
446
- return Ok ( ( ) ) ;
486
+ if let WatchEvent :: Modified ( resource ) = status {
487
+ if predicate ( & resource ) {
488
+ return Ok ( resource ) ;
447
489
}
448
490
}
449
491
}
450
492
451
493
Err ( anyhow ! (
452
- "Pod condition [{}] was not satisfied within {} seconds" ,
453
- condition_type ,
494
+ "Resource [{}] did not reach the expected status within {} seconds. " ,
495
+ resource . name ( ) ,
454
496
timeout_secs
455
497
) )
456
498
}
457
499
500
+ /// Returns the given resource with an updated status.
501
+ pub async fn get_status < K > ( & self , resource : & K ) -> Result < K >
502
+ where
503
+ K : DeserializeOwned + Resource ,
504
+ <K as Resource >:: DynamicType : Default ,
505
+ {
506
+ let api: Api < K > = Api :: namespaced ( self . client . clone ( ) , & self . namespace ) ;
507
+ Ok ( api. get_status ( & resource. name ( ) ) . await ?)
508
+ }
509
+
458
510
/// Returns the logs for the given pod.
459
511
pub async fn get_logs ( & self , pod : & Pod , params : & LogParams ) -> Result < Vec < String > > {
460
512
let pods: Api < Pod > = Api :: namespaced ( self . client . clone ( ) , & self . namespace ) ;
0 commit comments