From 9a6168b67d83673d7ef10ab0579812c872774aaa Mon Sep 17 00:00:00 2001 From: James Hodgkinson Date: Tue, 15 Aug 2023 15:42:15 +1000 Subject: [PATCH] Fixing test release (#1983) * Fixing cargo test --release * more tracing less dbg --- libs/client/src/lib.rs | 2 +- server/core/src/config.rs | 2 +- server/core/src/https/middleware/mod.rs | 25 ++++------------------ server/core/src/https/mod.rs | 3 ++- server/lib/src/plugins/namehistory.rs | 6 +++--- server/lib/src/server/mod.rs | 2 +- server/lib/src/server/modify.rs | 2 +- server/testkit/tests/oauth2_test.rs | 4 ++-- server/testkit/tests/proto_v1_test.rs | 4 ++-- unix_integration/tests/cache_layer_test.rs | 4 ++-- 10 files changed, 19 insertions(+), 35 deletions(-) diff --git a/libs/client/src/lib.rs b/libs/client/src/lib.rs index ee537f6f8..ffb72fd9b 100644 --- a/libs/client/src/lib.rs +++ b/libs/client/src/lib.rs @@ -530,7 +530,7 @@ impl KanidmClient { warn!(server_version = ?ver, client_version = ?EXPECT_VERSION, "Mismatched client and server version - features may not work, or other unforeseen errors may occur.") } - #[cfg(debug_assertions)] + #[cfg(any(test, debug_assertions))] if !matching { error!("You're in debug/dev mode, so we're going to quit here."); std::process::exit(1); diff --git a/server/core/src/config.rs b/server/core/src/config.rs index 4a8861a5a..a0ccb5ef0 100644 --- a/server/core/src/config.rs +++ b/server/core/src/config.rs @@ -275,7 +275,7 @@ impl Configuration { // Startup config action, used in kanidmd server etc pub fn update_config_for_server_mode(&mut self, sconfig: &ServerConfig) { - #[cfg(debug_assertions)] + #[cfg(any(test, debug_assertions))] debug!("update_config_for_server_mode {:?}", sconfig); self.update_tls(&sconfig.tls_chain, &sconfig.tls_key); self.update_bind(&sconfig.bindaddress); diff --git a/server/core/src/https/middleware/mod.rs b/server/core/src/https/middleware/mod.rs index aa1677622..3192372e1 100644 --- a/server/core/src/https/middleware/mod.rs +++ b/server/core/src/https/middleware/mod.rs @@ -5,8 +5,6 @@ use axum::{ response::Response, TypedHeader, }; -#[cfg(debug_assertions)] -use http::header::CONTENT_TYPE; use http::HeaderValue; use uuid::Uuid; @@ -34,21 +32,7 @@ pub struct KOpId { pub uat: Option, } -/// Ensure the status code is 200..=299 -#[cfg(debug_assertions)] -fn from_200_to_299(status: http::StatusCode) -> bool { - status.as_u16() >= 200 && status.as_u16() <= 299 -} - -#[test] -fn test_from_200_to_299() { - assert!(from_200_to_299(http::StatusCode::OK)); - assert!(from_200_to_299(http::StatusCode::IM_USED)); - assert!(!from_200_to_299(http::StatusCode::BAD_REQUEST)); - assert!(!from_200_to_299(http::StatusCode::INTERNAL_SERVER_ERROR)); -} - -#[cfg(debug_assertions)] +#[cfg(any(test, debug_assertions))] /// This is a debug middleware to ensure that /v1/ endpoints only return JSON #[instrument(name = "are_we_json_yet", skip_all)] pub async fn are_we_json_yet(request: Request, next: Next) -> Response { @@ -56,12 +40,11 @@ pub async fn are_we_json_yet(request: Request, next: Next) -> Response let response = next.run(request).await; - if uri.starts_with("/v1") && from_200_to_299(response.status()) { + if uri.starts_with("/v1") && response.status().is_success() { let headers = response.headers(); - assert!(headers.contains_key(CONTENT_TYPE)); - dbg!(headers.get(CONTENT_TYPE)); + assert!(headers.contains_key(http::header::CONTENT_TYPE)); assert!( - headers.get(CONTENT_TYPE) + headers.get(http::header::CONTENT_TYPE) == Some(&HeaderValue::from_static(crate::https::APPLICATION_JSON)) ); } diff --git a/server/core/src/https/mod.rs b/server/core/src/https/mod.rs index 953b9d27c..979b4c30d 100644 --- a/server/core/src/https/mod.rs +++ b/server/core/src/https/mod.rs @@ -255,7 +255,8 @@ pub async fn create_https_server( )); // layer which checks the responses have a content-type of JSON when we're in debug mode - #[cfg(debug_assertions)] + + #[cfg(any(test, debug_assertions))] let app = app.layer(from_fn(middleware::are_we_json_yet)); let app = app diff --git a/server/lib/src/plugins/namehistory.rs b/server/lib/src/plugins/namehistory.rs index ef1a379b0..490dd86f2 100644 --- a/server/lib/src/plugins/namehistory.rs +++ b/server/lib/src/plugins/namehistory.rs @@ -168,7 +168,7 @@ mod tests { let c = e .get_ava_set("name_history") .expect("failed to get primary cred."); - dbg!(c.clone()); + trace!("{:?}", c.clone()); assert!( c.contains(&PartialValue::new_utf8s("old_name")) && c.contains(&PartialValue::new_utf8s("new_name_1")) @@ -202,7 +202,7 @@ mod tests { let e = qs .internal_search_uuid(uuid!("d2b496bd-8493-47b7-8142-f568b5cf47e1")) .expect("failed to get entry"); - dbg!(e.get_ava()); + trace!("{:?}", e.get_ava()); let name_history = e .get_ava_set("name_history") .expect("failed to get name_history ava"); @@ -253,7 +253,7 @@ mod tests { let e = qs .internal_search_uuid(uuid!("d2b496bd-8493-47b7-8142-f568b5cf47ee")) .expect("failed to get entry"); - dbg!(e.get_ava()); + trace!("{:?}", e.get_ava()); let c = e .get_ava_set("name_history") .expect("failed to get name_history ava :/"); diff --git a/server/lib/src/server/mod.rs b/server/lib/src/server/mod.rs index 32844e711..1bf4b2527 100644 --- a/server/lib/src/server/mod.rs +++ b/server/lib/src/server/mod.rs @@ -1115,7 +1115,7 @@ impl QueryServer { pub async fn write(&self, curtime: Duration) -> QueryServerWriteTransaction<'_> { // Guarantee we are the only writer on the thread pool #[allow(clippy::expect_used)] - let write_ticket = if cfg!(debug_assertions) { + let write_ticket = if cfg!(test) { self.write_ticket .try_acquire() .expect("unable to acquire writer_ticket for qsw") diff --git a/server/lib/src/server/modify.rs b/server/lib/src/server/modify.rs index aa8e465e8..7ddae5ec7 100644 --- a/server/lib/src/server/modify.rs +++ b/server/lib/src/server/modify.rs @@ -313,7 +313,7 @@ impl<'a> QueryServerWriteTransaction<'a> { let norm_cand: Vec> = res?; - if cfg!(debug_assertions) { + if cfg!(debug_assertions) || cfg!(test) { pre_candidates .iter() .zip(norm_cand.iter()) diff --git a/server/testkit/tests/oauth2_test.rs b/server/testkit/tests/oauth2_test.rs index 92a1e941a..d2ac0a63c 100644 --- a/server/testkit/tests/oauth2_test.rs +++ b/server/testkit/tests/oauth2_test.rs @@ -327,7 +327,7 @@ async fn test_oauth2_openid_basic_flow(rsclient: KanidmClient) { .expect("Failed to send token introspect request."); assert!(response.status() == reqwest::StatusCode::OK); - dbg!(response.headers()); + tracing::trace!("{:?}", response.headers()); assert!( response.headers().get(CONTENT_TYPE) == Some(&HeaderValue::from_static(APPLICATION_JSON)) ); @@ -373,7 +373,7 @@ async fn test_oauth2_openid_basic_flow(rsclient: KanidmClient) { .await .expect("Failed to send userinfo request."); - dbg!(response.headers()); + tracing::trace!("{:?}", response.headers()); assert!( response.headers().get(CONTENT_TYPE) == Some(&HeaderValue::from_static(APPLICATION_JSON)) ); diff --git a/server/testkit/tests/proto_v1_test.rs b/server/testkit/tests/proto_v1_test.rs index 2313b34c8..9f1a886d8 100644 --- a/server/testkit/tests/proto_v1_test.rs +++ b/server/testkit/tests/proto_v1_test.rs @@ -647,7 +647,7 @@ async fn test_server_rest_posix_auth_lifecycle(rsclient: KanidmClient) { bad_json, ) .await; - dbg!(&res); + tracing::trace!("{:?}", &res); assert!(res.is_err()); // test sending a faulty JSON blob to the person unix update endpoint @@ -661,7 +661,7 @@ async fn test_server_rest_posix_auth_lifecycle(rsclient: KanidmClient) { bad_json, ) .await; - dbg!(&res); + tracing::trace!("{:?}", &res); assert!(res.is_err()); // attempt to verify (good, anon-conn) diff --git a/unix_integration/tests/cache_layer_test.rs b/unix_integration/tests/cache_layer_test.rs index 13353868f..6dfe3d57a 100644 --- a/unix_integration/tests/cache_layer_test.rs +++ b/unix_integration/tests/cache_layer_test.rs @@ -18,7 +18,7 @@ use kanidm_unix_common::unix_config::TpmPolicy; use kanidmd_core::config::{Configuration, IntegrationTestConfig, ServerRole}; use kanidmd_core::create_server_core; use tokio::task; -use tracing::log::debug; +use tracing::log::{debug, trace}; static PORT_ALLOC: AtomicU16 = AtomicU16::new(28080); const ADMIN_TEST_USER: &str = "admin"; @@ -137,7 +137,7 @@ async fn test_fixture(rsclient: KanidmClient) { .auth_simple_password("admin", ADMIN_TEST_PASSWORD) .await; debug!("auth_simple_password res: {:?}", res); - dbg!(&res); + trace!("{:?}", &res); assert!(res.is_ok()); // Not recommended in production! rsclient