Skip to content

Commit 266585b

Browse files
committed
tests: Genericize test_error method
Signed-off-by: Jakob Naucke <jnaucke@redhat.com>
1 parent e807d1b commit 266585b

6 files changed

Lines changed: 43 additions & 39 deletions

File tree

operator/src/attestation_key_register.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -381,7 +381,9 @@ pub async fn launch_secret_ak_controller(client: Client) {
381381
#[cfg(test)]
382382
mod tests {
383383
use super::*;
384+
use http::{Method, Request, StatusCode};
384385
use trusted_cluster_operator_test_utils::mock_client::*;
386+
use trusted_cluster_operator_test_utils::test_error_method;
385387

386388
#[tokio::test]
387389
async fn test_create_ak_register_depl_success() {
@@ -396,7 +398,7 @@ mod tests {
396398
let clos = |client| {
397399
create_attestation_key_register_deployment(client, Default::default(), "image")
398400
};
399-
test_create_error(clos).await;
401+
test_error_method!(clos, Method::POST);
400402
}
401403

402404
#[tokio::test]
@@ -410,6 +412,6 @@ mod tests {
410412
async fn test_create_ak_register_svc_error() {
411413
let clos =
412414
|client| create_attestation_key_register_service(client, Default::default(), Some(80));
413-
test_create_error(clos).await;
415+
test_error_method!(clos, Method::POST);
414416
}
415417
}

operator/src/reference_values.rs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -413,10 +413,11 @@ pub async fn disallow_image(ctx: RvContextData, resource_name: &str) -> Result<(
413413
mod tests {
414414
use super::*;
415415
use crate::test_utils::*;
416-
use http::{Method, Request};
416+
use http::{Method, Request, StatusCode};
417417
use k8s_openapi::api::batch::v1::JobStatus;
418418
use k8s_openapi::apimachinery::pkg::apis::meta::v1::Time;
419419
use trusted_cluster_operator_test_utils::mock_client::*;
420+
use trusted_cluster_operator_test_utils::test_error_method;
420421

421422
#[tokio::test]
422423
async fn test_create_pcrs_cm_success() {
@@ -433,7 +434,7 @@ mod tests {
433434
#[tokio::test]
434435
async fn test_create_pcrs_cm_error() {
435436
let clos = |client| create_pcrs_config_map(client, Default::default());
436-
test_create_error(clos).await;
437+
test_error_method!(clos, Method::POST);
437438
}
438439

439440
fn dummy_job() -> Job {
@@ -509,7 +510,7 @@ mod tests {
509510
#[tokio::test]
510511
async fn test_compute_fresh_pcrs_error() {
511512
let clos = |client| compute_fresh_pcrs(generate_rv_ctx(client), "image", "registry");
512-
test_create_error(clos).await;
513+
test_error_method!(clos, Method::POST);
513514
}
514515

515516
// handle_new_image is an inherently online function and not tested here.

operator/src/register_server.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -200,7 +200,9 @@ pub async fn launch_keygen_controller(client: Client) {
200200
#[cfg(test)]
201201
mod tests {
202202
use super::*;
203+
use http::{Method, Request, StatusCode};
203204
use trusted_cluster_operator_test_utils::mock_client::*;
205+
use trusted_cluster_operator_test_utils::test_error_method;
204206

205207
#[tokio::test]
206208
async fn test_create_reg_server_depl_success() {
@@ -211,7 +213,7 @@ mod tests {
211213
#[tokio::test]
212214
async fn test_create_reg_server_depl_error() {
213215
let clos = |client| create_register_server_deployment(client, Default::default(), "image");
214-
test_create_error(clos).await;
216+
test_error_method!(clos, Method::POST);
215217
}
216218

217219
#[tokio::test]
@@ -223,6 +225,6 @@ mod tests {
223225
#[tokio::test]
224226
async fn test_create_reg_server_svc_error() {
225227
let clos = |client| create_register_server_service(client, Default::default(), Some(80));
226-
test_create_error(clos).await;
228+
test_error_method!(clos, Method::POST);
227229
}
228230
}

operator/src/trustee.rs

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -550,6 +550,7 @@ mod tests {
550550
use http::{Method, Request, StatusCode};
551551
use kube::client::Body;
552552
use trusted_cluster_operator_test_utils::mock_client::*;
553+
use trusted_cluster_operator_test_utils::test_error_method;
553554

554555
#[test]
555556
fn test_get_image_pcrs_success() {
@@ -792,7 +793,7 @@ mod tests {
792793
#[tokio::test]
793794
async fn test_generate_att_policy_error() {
794795
let clos = |client| generate_attestation_policy(client, Default::default());
795-
test_create_error(clos).await;
796+
test_error_method!(clos, Method::POST);
796797
}
797798

798799
#[tokio::test]
@@ -810,7 +811,7 @@ mod tests {
810811
#[tokio::test]
811812
async fn test_generate_secret_error() {
812813
let clos = |client| generate_secret(client, "id", Default::default());
813-
test_create_error(clos).await;
814+
test_error_method!(clos, Method::POST);
814815
}
815816

816817
#[tokio::test]
@@ -828,7 +829,7 @@ mod tests {
828829
#[tokio::test]
829830
async fn test_generate_trustee_data_error() {
830831
let clos = |client| generate_trustee_data(client, Default::default());
831-
test_create_error(clos).await;
832+
test_error_method!(clos, Method::POST);
832833
}
833834

834835
#[tokio::test]
@@ -840,7 +841,7 @@ mod tests {
840841
#[tokio::test]
841842
async fn test_generate_kbs_service_error() {
842843
let clos = |client| generate_kbs_service(client, Default::default(), Some(80));
843-
test_create_error(clos).await;
844+
test_error_method!(clos, Method::POST);
844845
}
845846

846847
#[tokio::test]
@@ -852,6 +853,6 @@ mod tests {
852853
#[tokio::test]
853854
async fn test_generate_kbs_depl_error() {
854855
let clos = |client| generate_kbs_deployment(client, Default::default(), "image");
855-
test_create_error(clos).await;
856+
test_error_method!(clos, Method::POST);
856857
}
857858
}

register-server/src/main.rs

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -215,10 +215,15 @@ async fn main() {
215215

216216
#[cfg(test)]
217217
mod tests {
218-
use super::*;
218+
use super::{create_machine, EndpointInfo, Machine};
219+
use http::{Method, Request, StatusCode};
220+
use k8s_openapi::apimachinery::pkg::apis::meta::v1::OwnerReference;
219221
use kube::api::ObjectList;
222+
use kube::api::ObjectMeta;
223+
use trusted_cluster_operator_lib::MachineSpec;
220224
use trusted_cluster_operator_lib::TrustedExecutionCluster;
221225
use trusted_cluster_operator_test_utils::mock_client::*;
226+
use trusted_cluster_operator_test_utils::test_error_method;
222227

223228
fn dummy_clusters() -> ObjectList<TrustedExecutionCluster> {
224229
ObjectList {
@@ -280,7 +285,8 @@ mod tests {
280285

281286
#[tokio::test]
282287
async fn test_get_public_trustee_error() {
283-
test_get_error(async |c| EndpointInfo::create(c).await.map(|_| ())).await;
288+
let clos = async |c| EndpointInfo::create(c).await.map(|_| ());
289+
test_error_method!(clos, Method::GET);
284290
}
285291

286292
fn dummy_machine() -> Machine {
@@ -319,11 +325,10 @@ mod tests {
319325

320326
#[tokio::test]
321327
async fn test_create_machine_error() {
322-
test_create_error(async |c| {
323-
create_machine(c, "test", dummy_owner_reference())
324-
.await
325-
.map(|_| ())
326-
})
327-
.await;
328+
let clos = async |c| {
329+
let machine = create_machine(c, "test", dummy_owner_reference());
330+
machine.await.map(|_| ())
331+
};
332+
test_error_method!(clos, Method::POST);
328333
}
329334
}

test_utils/src/mock_client.rs

Lines changed: 12 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,17 @@ macro_rules! count_check {
4343
}
4444
}
4545

46+
#[macro_export]
47+
macro_rules! test_error_method {
48+
($action:ident, $method:path) => {
49+
let clos = async |req: Request<_>, _| match req.method() {
50+
&$method => Err(StatusCode::INTERNAL_SERVER_ERROR),
51+
_ => panic!("unexpected API interaction: {req:?}"),
52+
};
53+
test_error($action, clos).await;
54+
};
55+
}
56+
4657
pub use count_check;
4758

4859
async fn create_response<T: Future<Output = Result<String, StatusCode>>>(
@@ -141,7 +152,7 @@ pub async fn test_create_already_exists<
141152
});
142153
}
143154

144-
async fn test_error<
155+
pub async fn test_error<
145156
F: Fn(Client) -> S,
146157
S: Future<Output = anyhow::Result<T>>,
147158
T: Debug,
@@ -158,24 +169,6 @@ async fn test_error<
158169
});
159170
}
160171

161-
pub async fn test_create_error<F: Fn(Client) -> S, S: Future<Output = anyhow::Result<()>>>(
162-
create: F,
163-
) {
164-
let clos = async |req: Request<_>, _| match req.method() {
165-
&Method::POST => Err(StatusCode::INTERNAL_SERVER_ERROR),
166-
_ => panic!("unexpected API interaction: {req:?}"),
167-
};
168-
test_error(create, clos).await;
169-
}
170-
171-
pub async fn test_get_error<F: Fn(Client) -> S, S: Future<Output = anyhow::Result<()>>>(get: F) {
172-
let clos = async |req: Request<_>, _| match req.method() {
173-
&Method::GET => Err(StatusCode::INTERNAL_SERVER_ERROR),
174-
_ => panic!("unexpected API interaction: {req:?}"),
175-
};
176-
test_error(get, clos).await;
177-
}
178-
179172
pub fn dummy_cluster() -> TrustedExecutionCluster {
180173
TrustedExecutionCluster {
181174
metadata: ObjectMeta {

0 commit comments

Comments
 (0)