2022-10-24 01:50:31 +02:00
|
|
|
#![deny(warnings)]
|
|
|
|
#![warn(unused_extern_crates)]
|
|
|
|
#![deny(clippy::todo)]
|
|
|
|
#![deny(clippy::unimplemented)]
|
|
|
|
#![deny(clippy::unwrap_used)]
|
|
|
|
#![deny(clippy::expect_used)]
|
|
|
|
#![deny(clippy::panic)]
|
|
|
|
#![deny(clippy::unreachable)]
|
|
|
|
#![deny(clippy::await_holding_lock)]
|
|
|
|
#![deny(clippy::needless_pass_by_value)]
|
|
|
|
#![deny(clippy::trivially_copy_pass_by_ref)]
|
|
|
|
|
2020-12-05 08:07:04 +01:00
|
|
|
use std::net::TcpStream;
|
|
|
|
use std::sync::atomic::{AtomicU16, Ordering};
|
2020-04-30 16:54:46 +02:00
|
|
|
|
2022-04-29 05:23:46 +02:00
|
|
|
use kanidm_client::{KanidmClient, KanidmClientBuilder};
|
2022-10-05 01:48:48 +02:00
|
|
|
use kanidmd_core::config::{Configuration, IntegrationTestConfig, ServerRole};
|
2022-11-23 11:10:43 +01:00
|
|
|
use kanidmd_core::{create_server_core, CoreHandle};
|
2022-04-29 05:03:21 +02:00
|
|
|
use tokio::task;
|
2020-04-30 16:54:46 +02:00
|
|
|
|
2021-04-25 03:35:56 +02:00
|
|
|
pub const ADMIN_TEST_USER: &str = "admin";
|
2020-04-30 16:54:46 +02:00
|
|
|
pub const ADMIN_TEST_PASSWORD: &str = "integration test admin password";
|
2022-05-06 06:20:52 +02:00
|
|
|
pub static PORT_ALLOC: AtomicU16 = AtomicU16::new(18080);
|
2020-12-05 08:07:04 +01:00
|
|
|
|
2022-10-24 01:50:31 +02:00
|
|
|
pub use testkit_macros::test;
|
|
|
|
|
2022-05-26 06:58:53 +02:00
|
|
|
pub fn is_free_port(port: u16) -> bool {
|
2021-08-19 03:04:24 +02:00
|
|
|
// TODO: Refactor to use `Result::is_err` in a future PR
|
2020-12-05 08:07:04 +01:00
|
|
|
match TcpStream::connect(("0.0.0.0", port)) {
|
|
|
|
Ok(_) => false,
|
|
|
|
Err(_) => true,
|
|
|
|
}
|
|
|
|
}
|
2020-04-30 16:54:46 +02:00
|
|
|
|
|
|
|
// Test external behaviours of the service.
|
2022-05-26 06:58:53 +02:00
|
|
|
|
|
|
|
// allowed because the use of this function is behind a test gate
|
|
|
|
#[allow(dead_code)]
|
2022-11-23 11:10:43 +01:00
|
|
|
pub async fn setup_async_test() -> (KanidmClient, CoreHandle) {
|
2023-01-28 04:52:44 +01:00
|
|
|
sketching::test_init();
|
2020-06-05 06:01:20 +02:00
|
|
|
|
2020-12-08 02:06:13 +01:00
|
|
|
let mut counter = 0;
|
2020-12-05 08:07:04 +01:00
|
|
|
let port = loop {
|
|
|
|
let possible_port = PORT_ALLOC.fetch_add(1, Ordering::SeqCst);
|
|
|
|
if is_free_port(possible_port) {
|
|
|
|
break possible_port;
|
|
|
|
}
|
2020-12-08 02:06:13 +01:00
|
|
|
counter += 1;
|
|
|
|
if counter >= 5 {
|
|
|
|
eprintln!("Unable to allocate port!");
|
|
|
|
assert!(false);
|
|
|
|
}
|
2020-12-05 08:07:04 +01:00
|
|
|
};
|
2020-04-30 16:54:46 +02:00
|
|
|
|
|
|
|
let int_config = Box::new(IntegrationTestConfig {
|
2021-04-25 03:35:56 +02:00
|
|
|
admin_user: ADMIN_TEST_USER.to_string(),
|
2020-04-30 16:54:46 +02:00
|
|
|
admin_password: ADMIN_TEST_PASSWORD.to_string(),
|
|
|
|
});
|
|
|
|
|
2022-07-30 14:10:24 +02:00
|
|
|
let addr = format!("http://localhost:{}", port);
|
|
|
|
|
2020-06-10 04:07:43 +02:00
|
|
|
// Setup the config ...
|
2020-04-30 16:54:46 +02:00
|
|
|
let mut config = Configuration::new();
|
|
|
|
config.address = format!("127.0.0.1:{}", port);
|
|
|
|
config.secure_cookies = false;
|
|
|
|
config.integration_test_config = Some(int_config);
|
2022-10-24 01:50:31 +02:00
|
|
|
config.role = ServerRole::WriteReplica;
|
2022-07-30 14:10:24 +02:00
|
|
|
config.domain = "localhost".to_string();
|
|
|
|
config.origin = addr.clone();
|
2020-12-02 02:12:07 +01:00
|
|
|
// config.log_level = Some(LogLevel::Verbose as u32);
|
2021-04-25 03:35:56 +02:00
|
|
|
// config.log_level = Some(LogLevel::FullTrace as u32);
|
2020-09-06 00:44:35 +02:00
|
|
|
config.threads = 1;
|
|
|
|
|
2022-11-23 11:10:43 +01:00
|
|
|
let core_handle = create_server_core(config, false)
|
2022-04-29 05:03:21 +02:00
|
|
|
.await
|
|
|
|
.expect("failed to start server core");
|
|
|
|
// We have to yield now to guarantee that the tide elements are setup.
|
|
|
|
task::yield_now().await;
|
2020-04-30 16:54:46 +02:00
|
|
|
|
|
|
|
let rsclient = KanidmClientBuilder::new()
|
2022-10-24 01:50:31 +02:00
|
|
|
.address(addr.clone())
|
2021-05-19 23:58:11 +02:00
|
|
|
.no_proxy()
|
2022-04-29 05:23:46 +02:00
|
|
|
.build()
|
2020-04-30 16:54:46 +02:00
|
|
|
.expect("Failed to build client");
|
|
|
|
|
2022-10-24 01:50:31 +02:00
|
|
|
tracing::info!("Testkit server setup complete - {}", addr);
|
|
|
|
|
2022-11-23 11:10:43 +01:00
|
|
|
(rsclient, core_handle)
|
2020-04-30 16:54:46 +02:00
|
|
|
}
|