Skip to content

Commit

Permalink
Added unit test for watcher
Browse files Browse the repository at this point in the history
Signed-off-by: ayushrakesh <[email protected]>
  • Loading branch information
ayushrakesh committed Jun 22, 2024
1 parent f7aca40 commit b5fdfd1
Showing 1 changed file with 56 additions and 9 deletions.
65 changes: 56 additions & 9 deletions kube-runtime/src/watcher.rs
Original file line number Diff line number Diff line change
Expand Up @@ -157,16 +157,63 @@ enum State<K> {

/// Used to control whether the watcher receives the full object, or only the
/// metadata
#[async_trait]
trait ApiMode {
type Value: Clone;
// #[async_trait]
// trait ApiMode {
// type Value: Clone;

// async fn list(&self, lp: &ListParams) -> kube_client::Result<ObjectList<Self::Value>>;
// async fn watch(
// &self,
// wp: &WatchParams,
// version: &str,
// ) -> kube_client::Result<BoxStream<'static, kube_client::Result<WatchEvent<Self::Value>>>>;
// }

#[cfg(test)]
mod tests {
use super::*;
use async_trait::async_trait;
use futures::stream::{self, BoxStream};
use kube_client::{Result, ListParams, WatchParams, ObjectList, WatchEvent};

struct TestApiMode {
list_response: ObjectList<TestResource>,
watch_response: Vec<Result<WatchEvent<TestResource>>>,
}

async fn list(&self, lp: &ListParams) -> kube_client::Result<ObjectList<Self::Value>>;
async fn watch(
&self,
wp: &WatchParams,
version: &str,
) -> kube_client::Result<BoxStream<'static, kube_client::Result<WatchEvent<Self::Value>>>>;
impl TestApiMode {
fn new(list_response: ObjectList<TestResource>, watch_response: Vec<Result<WatchEvent<TestResource>>>) -> Self {
TestApiMode {
list_response,
watch_response,
}
}
}

#[async_trait]
impl ApiMode for TestApiMode {
type Value = TestResource;

async fn list(&self, _lp: &ListParams) -> Result<ObjectList<Self::Value>> {
Ok(self.list_response.clone())
}

async fn watch(&self, _wp: &WatchParams, _version: &str) -> Result<BoxStream<'static, Result<WatchEvent<Self::Value>>>> {
Ok(stream::iter(self.watch_response.clone()).boxed())
}
}

#[tokio::test]
async fn test_watcher_behavior() {
let list_response = ObjectList::<TestResource> { items: vec![/* ... */], metadata: /* ... */ };

Check failure on line 208 in kube-runtime/src/watcher.rs

View workflow job for this annotation

GitHub Actions / msrv

expected expression, found `}`
let watch_response = vec![Ok(WatchEvent::Added(TestResource { /* ... */ }))];

let api_mode = TestApiMode::new(list_response, watch_response);

// Test the watcher behavior using the TestApiMode
// E.g., verify that the watcher calls list() and watch() correctly,
// handles pagination, desynchronization, etc.
}
}

/// A wrapper around the `Api` of a `Resource` type that when used by the
Expand Down

0 comments on commit b5fdfd1

Please sign in to comment.