Skip to content
/ kube-rs Public
forked from kube-rs/kube

Commit

Permalink
Add mock service example
Browse files Browse the repository at this point in the history
  • Loading branch information
kazk committed Feb 22, 2021
1 parent 72ae086 commit 2e8ac69
Showing 1 changed file with 47 additions and 0 deletions.
47 changes: 47 additions & 0 deletions kube/src/api/typed.rs
Original file line number Diff line number Diff line change
Expand Up @@ -339,3 +339,50 @@ impl<K> From<Api<K>> for Client {
api.client
}
}

#[cfg(test)]
mod tests {
use futures::pin_mut;
use http::{Request, Response};
use hyper::Body;
use k8s_openapi::api::core::v1::Pod;
use tower_test::mock;

use crate::{Api, Client, Service};

#[tokio::test]
async fn test_mock() {
let (mock_service, handle) = mock::pair::<Request<Body>, Response<Body>>();
let spawned = tokio::spawn(async move {
pin_mut!(handle);
let (request, send) = handle.next_request().await.expect("service not called");
assert_eq!(request.method(), http::Method::GET);
assert_eq!(request.uri().to_string(), "/api/v1/namespaces/default/pods/test");
let pod: Pod = serde_json::from_value(serde_json::json!({
"apiVersion": "v1",
"kind": "Pod",
"metadata": {
"name": "test",
"annotations": {
"kube-rs": "test",
},
},
"spec": {
"containers": [{ "name": "test", "image": "test-image" }],
}
}))
.unwrap();
send.send_response(
Response::builder()
.body(Body::from(serde_json::to_vec(&pod).unwrap()))
.unwrap(),
);
});

let service = Service::new(mock_service);
let pods: Api<Pod> = Api::namespaced(Client::new(service), "default");
let pod = pods.get("test").await.unwrap();
assert_eq!(pod.metadata.annotations.unwrap().get("kube-rs").unwrap(), "test");
spawned.await.unwrap();
}
}

0 comments on commit 2e8ac69

Please sign in to comment.