Skip to content

Commit 85b675c

Browse files
committed
tests: Genericize test_error method
Signed-off-by: Jakob Naucke <jnaucke@redhat.com>
1 parent 9a60d5a commit 85b675c

6 files changed

Lines changed: 44 additions & 41 deletions

File tree

operator/src/attestation_key_register.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -390,7 +390,9 @@ pub async fn launch_secret_ak_controller(client: Client) {
390390
#[cfg(test)]
391391
mod tests {
392392
use super::*;
393+
use http::{Method, Request, StatusCode};
393394
use trusted_cluster_operator_test_utils::mock_client::*;
395+
use trusted_cluster_operator_test_utils::test_error_method;
394396

395397
#[tokio::test]
396398
async fn test_create_ak_register_depl_success() {
@@ -405,7 +407,7 @@ mod tests {
405407
let clos = |client| {
406408
create_attestation_key_register_deployment(client, Default::default(), "image", &None)
407409
};
408-
test_create_error(clos).await;
410+
test_error_method!(clos, Method::POST);
409411
}
410412

411413
#[tokio::test]
@@ -419,6 +421,6 @@ mod tests {
419421
async fn test_create_ak_register_svc_error() {
420422
let clos =
421423
|client| create_attestation_key_register_service(client, Default::default(), Some(80));
422-
test_create_error(clos).await;
424+
test_error_method!(clos, Method::POST);
423425
}
424426
}

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: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -208,7 +208,9 @@ pub async fn launch_keygen_controller(client: Client) {
208208
#[cfg(test)]
209209
mod tests {
210210
use super::*;
211+
use http::{Method, Request, StatusCode};
211212
use trusted_cluster_operator_test_utils::mock_client::*;
213+
use trusted_cluster_operator_test_utils::test_error_method;
212214

213215
#[tokio::test]
214216
async fn test_create_reg_server_depl_success() {
@@ -219,9 +221,8 @@ mod tests {
219221

220222
#[tokio::test]
221223
async fn test_create_reg_server_depl_error() {
222-
let clos =
223-
|client| create_register_server_deployment(client, Default::default(), "image", &None);
224-
test_create_error(clos).await;
224+
let clos = |client| create_register_server_deployment(client, Default::default(), "image", &None);
225+
test_error_method!(clos, Method::POST);
225226
}
226227

227228
#[tokio::test]
@@ -233,6 +234,6 @@ mod tests {
233234
#[tokio::test]
234235
async fn test_create_reg_server_svc_error() {
235236
let clos = |client| create_register_server_service(client, Default::default(), Some(80));
236-
test_create_error(clos).await;
237+
test_error_method!(clos, Method::POST);
237238
}
238239
}

operator/src/trustee.rs

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -582,6 +582,7 @@ mod tests {
582582
use http::{Method, Request, StatusCode};
583583
use kube::client::Body;
584584
use trusted_cluster_operator_test_utils::mock_client::*;
585+
use trusted_cluster_operator_test_utils::test_error_method;
585586

586587
#[test]
587588
fn test_get_image_pcrs_success() {
@@ -824,7 +825,7 @@ mod tests {
824825
#[tokio::test]
825826
async fn test_generate_att_policy_error() {
826827
let clos = |client| generate_attestation_policy(client, Default::default());
827-
test_create_error(clos).await;
828+
test_error_method!(clos, Method::POST);
828829
}
829830

830831
#[tokio::test]
@@ -842,7 +843,7 @@ mod tests {
842843
#[tokio::test]
843844
async fn test_generate_secret_error() {
844845
let clos = |client| generate_secret(client, "id", Default::default());
845-
test_create_error(clos).await;
846+
test_error_method!(clos, Method::POST);
846847
}
847848

848849
#[tokio::test]
@@ -860,7 +861,7 @@ mod tests {
860861
#[tokio::test]
861862
async fn test_generate_trustee_data_error() {
862863
let clos = |client| generate_trustee_data(client, Default::default(), &None);
863-
test_create_error(clos).await;
864+
test_error_method!(clos, Method::POST);
864865
}
865866

866867
#[tokio::test]
@@ -872,7 +873,7 @@ mod tests {
872873
#[tokio::test]
873874
async fn test_generate_kbs_service_error() {
874875
let clos = |client| generate_kbs_service(client, Default::default(), Some(80));
875-
test_create_error(clos).await;
876+
test_error_method!(clos, Method::POST);
876877
}
877878

878879
#[tokio::test]
@@ -884,6 +885,6 @@ mod tests {
884885
#[tokio::test]
885886
async fn test_generate_kbs_depl_error() {
886887
let clos = |client| generate_kbs_deployment(client, Default::default(), "image", &None);
887-
test_create_error(clos).await;
888+
test_error_method!(clos, Method::POST);
888889
}
889890
}

register-server/src/main.rs

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

264264
#[cfg(test)]
265265
mod tests {
266-
use super::*;
266+
use super::{create_machine, EndpointInfo, Machine};
267+
use http::{Method, Request, StatusCode};
268+
use k8s_openapi::apimachinery::pkg::apis::meta::v1::OwnerReference;
267269
use kube::api::ObjectList;
270+
use kube::api::ObjectMeta;
271+
use trusted_cluster_operator_lib::MachineSpec;
268272
use trusted_cluster_operator_lib::TrustedExecutionCluster;
269273
use trusted_cluster_operator_test_utils::mock_client::*;
274+
use trusted_cluster_operator_test_utils::test_error_method;
270275

271276
fn dummy_clusters() -> ObjectList<TrustedExecutionCluster> {
272277
ObjectList {
@@ -328,7 +333,8 @@ mod tests {
328333

329334
#[tokio::test]
330335
async fn test_get_public_trustee_error() {
331-
test_get_error(async |c| EndpointInfo::create(c).await.map(|_| ())).await;
336+
let clos = async |c| EndpointInfo::create(c).await.map(|_| ());
337+
test_error_method!(clos, Method::GET);
332338
}
333339

334340
fn dummy_machine() -> Machine {
@@ -367,11 +373,10 @@ mod tests {
367373

368374
#[tokio::test]
369375
async fn test_create_machine_error() {
370-
test_create_error(async |c| {
371-
create_machine(c, "test", dummy_owner_reference())
372-
.await
373-
.map(|_| ())
374-
})
375-
.await;
376+
let clos = async |c| {
377+
let machine = create_machine(c, "test", dummy_owner_reference());
378+
machine.await.map(|_| ())
379+
};
380+
test_error_method!(clos, Method::POST);
376381
}
377382
}

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)