Skip to content

Commit

Permalink
Use wiremock to match k8s API request body
Browse files Browse the repository at this point in the history
  • Loading branch information
garryod committed Aug 20, 2024
1 parent 89c3e9d commit 88399c7
Show file tree
Hide file tree
Showing 3 changed files with 83 additions and 70 deletions.
99 changes: 55 additions & 44 deletions sessionspaces/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 3 additions & 1 deletion sessionspaces/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ kube = { version = "0.93.1" }
ldap3 = { version = "0.11.5", default-features = false, features = [
"tls-rustls",
] }
mockito = { version = "1.5.0" }
serde_json = { version = "1.0.125" }
sqlx = { version = "0.8.0", features = [
"runtime-tokio",
Expand All @@ -31,3 +30,6 @@ tower = { version = "0.4.13", features = ["limit", "util"] }
tracing = { version = "0.1.40" }
tracing-subscriber = { version = "0.3.18" }
url = { version = "2.5.2" }

[dev-dependencies]
wiremock = "0.6.1"
50 changes: 25 additions & 25 deletions sessionspaces/src/resources/namespace.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,36 +61,36 @@ pub async fn create_namespace(

#[cfg(test)]
mod tests {
use super::*;
use kube::Client;
use kube::Config;
use mockito::Server;
use super::create_namespace;
use k8s_openapi::api::core::v1::Namespace;
use kube::{api::ObjectMeta, Client, Config};
use wiremock::{
matchers::{body_partial_json, method, path, query_param},
Mock, MockServer, ResponseTemplate,
};

#[tokio::test]
async fn create_new_namespace() {
let mut server = Server::new_async().await;
let mock_patch_test_namespace = server
.mock(
"PATCH",
"/api/v1/namespaces/test?&fieldManager=sessionspaces",
)
.with_status(201)
.with_header("content-type", "application/json")
.with_body(
r#"{
"apiVersion": "v1",
"kind": "Namespace",
"metadata": {
"name": "test"
}
}"#,
)
.create();
let config = Config::new(server.url().parse().unwrap());
let server = MockServer::start().await;
let namespace = Namespace {
metadata: ObjectMeta {
name: Some("cm37235-3".to_string()),
..Default::default()
},
..Default::default()
};
let _mock = Mock::given(method("PATCH"))
.and(path("/api/v1/namespaces/cm37235-3"))
.and(query_param("fieldManager", "sessionspaces"))
.and(body_partial_json(namespace.clone()))
.respond_with(ResponseTemplate::new(201).set_body_json(namespace))
.expect(1)
.mount(&server)
.await;
let config = Config::new(server.uri().parse().unwrap());
let k8s_client = Client::try_from(config).unwrap();
create_namespace("test".to_string(), k8s_client)
create_namespace("cm37235-3".to_string(), k8s_client)
.await
.unwrap();
mock_patch_test_namespace.assert();
}
}

0 comments on commit 88399c7

Please sign in to comment.