-
Notifications
You must be signed in to change notification settings - Fork 10
Initial unit tests for Trustee interaction #53
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
alicefr
merged 12 commits into
trusted-execution-clusters:main
from
Jakob-Naucke:initial-unittest
Oct 13, 2025
Merged
Changes from all commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
99ca65b
update_reference_values: open API only once
Jakob-Naucke 0b7ecd9
Run mount_secret from reconciler
Jakob-Naucke 0e8b7f1
Create MockClient for testing
alicefr 67dbcab
test: Add for trustee::generate_attestation_policy
6-dehan 66dd3a8
test: Add unit tests for trustee::get_image_pcrs
6-dehan e00bcc5
test: test_generate_luks_key_returns_correct_size
6-dehan 90916e1
test: Response closure in MockClient
Jakob-Naucke 219fbfd
test: Add unit tests for update_reference_values
Jakob-Naucke 891ff76
test: Genericize tests on creation functions
Jakob-Naucke 1343b0b
test: Create success/existing/error tests
Jakob-Naucke 29e26b6
test: Add unit test for recompute_reference_values
Jakob-Naucke 293c368
test: Add unit tests for mount_secret
Jakob-Naucke File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,124 @@ | ||
| // SPDX-FileCopyrightText: Alice Frosi <afrosi@redhat.com> | ||
| // SPDX-FileCopyrightText: Jakob Naucke <jnaucke@redhat.com> | ||
| // | ||
| // SPDX-License-Identifier: MIT | ||
|
|
||
| use http::{Request, Response, StatusCode}; | ||
| use kube::{Client, client::Body, error::ErrorResponse}; | ||
| use serde::{Deserialize, Serialize}; | ||
| use std::convert::Infallible; | ||
| use tower::service_fn; | ||
|
|
||
| macro_rules! assert_kube_api_error { | ||
| ($err:expr, $code:expr, $reason:expr, $message:expr, $status:expr) => {{ | ||
| let kube_error = $err | ||
| .downcast_ref::<kube::Error>() | ||
| .expect(&format!("Expected kube::Error, got: {:?}", $err)); | ||
|
|
||
| if let kube::Error::Api(error_response) = kube_error { | ||
| assert_eq!(error_response.code, $code); | ||
| assert_eq!(error_response.reason, $reason); | ||
| assert_eq!(error_response.message, $message); | ||
| assert_eq!(error_response.status, $status); | ||
| } else { | ||
| assert!(false, "Expected kube::Error::Api, got: {:?}", kube_error); | ||
| } | ||
| }}; | ||
| } | ||
|
|
||
| pub(crate) use assert_kube_api_error; | ||
|
|
||
| pub struct MockClient<F, T> | ||
| where | ||
| F: Fn(&Option<Request<Body>>) -> Result<T, StatusCode> + Send + 'static, | ||
| T: Default + Serialize + for<'de> Deserialize<'de>, | ||
| { | ||
| response_closure: F, | ||
| namespace: String, | ||
| } | ||
|
|
||
| impl<F, T> MockClient<F, T> | ||
| where | ||
| F: Fn(&Option<Request<Body>>) -> Result<T, StatusCode> + Send + 'static, | ||
| T: Clone + Default + Send + Serialize + for<'de> Deserialize<'de> + 'static, | ||
| { | ||
| pub fn new(response_closure: F, namespace: String) -> Self { | ||
| Self { | ||
| response_closure, | ||
| namespace, | ||
| } | ||
| } | ||
|
|
||
| pub fn into_client(self) -> Client { | ||
| let response_data = (self.response_closure)(&None).unwrap_or_default(); | ||
| let response_json = serde_json::to_string(&response_data).unwrap(); | ||
| let (kind, name) = serde_json::from_str::<serde_json::Value>(&response_json) | ||
| .map(|json_value| { | ||
| let kind = json_value | ||
| .get("kind") | ||
| .and_then(|v| v.as_str()) | ||
| .unwrap_or("Unknown") | ||
| .to_string(); | ||
| let name = json_value | ||
| .get("metadata") | ||
| .and_then(|m| m.get("name")) | ||
| .and_then(|n| n.as_str()) | ||
| .unwrap_or("Unknown") | ||
| .to_string(); | ||
| (kind, name) | ||
| }) | ||
| .unwrap_or(("Unknown".to_string(), "Unknown".to_string())); | ||
| let plural = kind.to_lowercase() + "s"; | ||
| let namespace = self.namespace.clone(); | ||
|
|
||
| let mock_svc = service_fn(move |req: Request<Body>| { | ||
| let mut status_code = StatusCode::OK; | ||
| let response = (self.response_closure)(&Some(req)); | ||
| let body = if let Ok(response_data) = response { | ||
| let response_json = serde_json::to_string(&response_data).unwrap(); | ||
| Body::from(response_json.into_bytes()) | ||
| } else { | ||
| status_code = response.err().unwrap(); | ||
| let code = status_code.as_u16(); | ||
| let error_response = match status_code { | ||
| StatusCode::CONFLICT => ErrorResponse { | ||
| status: "Failure".to_string(), | ||
| message: format!("{plural} \"{name}\" already exists"), | ||
| reason: "AlreadyExists".to_string(), | ||
| code, | ||
| }, | ||
| StatusCode::INTERNAL_SERVER_ERROR => ErrorResponse { | ||
| status: "Failure".to_string(), | ||
| message: "internal server error".to_string(), | ||
| reason: "ServerTimeout".to_string(), | ||
| code, | ||
| }, | ||
| StatusCode::NOT_FOUND => ErrorResponse { | ||
| status: "Failure".to_string(), | ||
| message: "resource not found".to_string(), | ||
| reason: "NotFound".to_string(), | ||
| code, | ||
| }, | ||
| StatusCode::BAD_REQUEST => ErrorResponse { | ||
| status: "Failure".to_string(), | ||
| message: "bad request".to_string(), | ||
| reason: "BadRequest".to_string(), | ||
| code, | ||
| }, | ||
| _ => ErrorResponse { | ||
| status: "Failure".to_string(), | ||
| message: format!("error with status code {status_code}"), | ||
| reason: "Unknown".to_string(), | ||
| code, | ||
| }, | ||
| }; | ||
| let error_json = serde_json::to_string(&error_response).unwrap(); | ||
| Body::from(error_json.into_bytes()) | ||
| }; | ||
|
|
||
| let response = Response::builder().status(status_code).body(body).unwrap(); | ||
| async move { Ok::<_, Infallible>(response) } | ||
| }); | ||
| Client::new(mock_svc, namespace) | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.