diff --git a/src/cli/client.rs b/src/cli/client.rs index f33b869d..c16cf9b3 100644 --- a/src/cli/client.rs +++ b/src/cli/client.rs @@ -443,7 +443,7 @@ mod tests { use super::*; use crate::cli::options::KrillInitDetails; - use crate::commons::util::test; + use crate::test; #[test] fn init_config_file() { diff --git a/src/commons/api/ca.rs b/src/commons/api/ca.rs index ab1149c5..fa24a0d7 100644 --- a/src/commons/api/ca.rs +++ b/src/commons/api/ca.rs @@ -1910,7 +1910,7 @@ mod test { use rpki::crypto::PublicKeyFormat; use crate::commons::util::softsigner::OpenSslSigner; - use crate::commons::util::test; + use crate::test; use super::*; diff --git a/src/commons/eventsourcing/mod.rs b/src/commons/eventsourcing/mod.rs index 250ecf94..53a310e1 100644 --- a/src/commons/eventsourcing/mod.rs +++ b/src/commons/eventsourcing/mod.rs @@ -35,7 +35,7 @@ mod tests { use serde::Serialize; use crate::commons::api::Handle; - use crate::commons::util::test; + use crate::test; use super::*; diff --git a/src/commons/remote/rfc8181.rs b/src/commons/remote/rfc8181.rs index 8b8b6b91..35d0e649 100644 --- a/src/commons/remote/rfc8181.rs +++ b/src/commons/remote/rfc8181.rs @@ -782,7 +782,7 @@ mod tests { use crate::commons::api::{ HexEncodedHash, ListElement, ListReply, Publish, PublishDeltaBuilder, Update, Withdraw, }; - use crate::commons::util::test::rsync; + use crate::test::rsync; struct ListReplyBuilder { elements: Vec, diff --git a/src/commons/remote/rfc8183.rs b/src/commons/remote/rfc8183.rs index b1c0ceb6..9e24448d 100644 --- a/src/commons/remote/rfc8183.rs +++ b/src/commons/remote/rfc8183.rs @@ -797,7 +797,7 @@ mod tests { use rpki::x509::Time; use crate::commons::remote::id::tests::test_id_certificate; - use crate::commons::util::test; + use crate::test; use super::*; diff --git a/src/commons/util/file.rs b/src/commons/util/file.rs index d9303f63..d19d702b 100644 --- a/src/commons/util/file.rs +++ b/src/commons/util/file.rs @@ -320,7 +320,7 @@ impl From for io::Error { #[cfg(test)] mod tests { - use crate::commons::util::test; + use crate::test; use super::*; @@ -357,5 +357,4 @@ mod tests { assert!(files.contains(&file_4)); }); } - } diff --git a/src/commons/util/mod.rs b/src/commons/util/mod.rs index 5d0cb28c..07c90f09 100644 --- a/src/commons/util/mod.rs +++ b/src/commons/util/mod.rs @@ -6,7 +6,6 @@ pub mod ext_serde; pub mod file; pub mod httpclient; pub mod softsigner; -pub mod test; pub mod xml; pub fn sha256(object: &[u8]) -> Bytes { diff --git a/src/commons/util/softsigner.rs b/src/commons/util/softsigner.rs index ddbda21d..a2fef9af 100644 --- a/src/commons/util/softsigner.rs +++ b/src/commons/util/softsigner.rs @@ -242,7 +242,7 @@ impl From for SignerError { #[cfg(test)] pub mod tests { - use crate::commons::util::test; + use crate::test; use super::*; diff --git a/src/commons/util/test.rs b/src/commons/util/test.rs deleted file mode 100644 index b88c1214..00000000 --- a/src/commons/util/test.rs +++ /dev/null @@ -1,78 +0,0 @@ -use std::fs; -use std::fs::File; -use std::io::Write; -use std::path::PathBuf; -use std::str::FromStr; - -use bytes::Bytes; -use rand::{thread_rng, Rng}; - -use rpki::uri; - -/// This method sets up a test directory with a random name (a number) -/// under 'work', relative to where cargo is running. It then runs the -/// test provided in the closure, and finally it cleans up the test -/// directory. -/// -/// Note that if your test fails the directory is not cleaned up. -pub fn test_under_tmp(op: F) -where - F: FnOnce(PathBuf) -> (), -{ - let dir = sub_dir(&PathBuf::from("work")); - let path = PathBuf::from(&dir); - - op(dir); - - let _result = fs::remove_dir_all(path); -} - -pub async fn test_under_tmp_async(op: F) -where - F: FnOnce(PathBuf) -> (), -{ - let dir = tmp_dir(); - let path = PathBuf::from(&dir); - - op(dir); - - let _result = fs::remove_dir_all(path); -} - -pub fn tmp_dir() -> PathBuf { - sub_dir(&PathBuf::from("work")) -} - -/// This method sets up a random subdirectory and returns it. It is -/// assumed that the caller will clean this directory themselves. -pub fn sub_dir(base_dir: &PathBuf) -> PathBuf { - let mut rng = thread_rng(); - let rnd: u32 = rng.gen(); - - let mut dir = base_dir.clone(); - dir.push(PathBuf::from(format!("{}", rnd))); - - let full_path = PathBuf::from(&dir); - fs::create_dir_all(&full_path).unwrap(); - - full_path -} - -pub fn rsync(s: &str) -> uri::Rsync { - uri::Rsync::from_str(s).unwrap() -} - -pub fn https(s: &str) -> uri::Https { - uri::Https::from_str(s).unwrap() -} - -pub fn as_bytes(s: &str) -> Bytes { - Bytes::copy_from_slice(s.as_bytes()) -} - -pub fn save_file(base_dir: &PathBuf, file_name: &str, content: &[u8]) { - let mut full_name = base_dir.clone(); - full_name.push(PathBuf::from(file_name)); - let mut f = File::create(full_name).unwrap(); - f.write_all(content).unwrap(); -} diff --git a/src/daemon/ca/certauth.rs b/src/daemon/ca/certauth.rs index b19567f1..80930a3b 100644 --- a/src/daemon/ca/certauth.rs +++ b/src/daemon/ca/certauth.rs @@ -1588,7 +1588,7 @@ impl CertAuth { mod tests { use super::*; use crate::commons::util::softsigner::OpenSslSigner; - use crate::commons::util::test; + use crate::test; #[test] fn generate_id_cert() { diff --git a/src/daemon/ca/server.rs b/src/daemon/ca/server.rs index 3c38e5ea..af041ef1 100644 --- a/src/daemon/ca/server.rs +++ b/src/daemon/ca/server.rs @@ -1028,7 +1028,7 @@ mod tests { use crate::commons::api::RepoInfo; use crate::commons::util::softsigner::OpenSslSigner; - use crate::commons::util::test; + use crate::test; #[test] fn add_ta() { diff --git a/src/daemon/http/server.rs b/src/daemon/http/server.rs index 9c950013..e7d43119 100644 --- a/src/daemon/http/server.rs +++ b/src/daemon/http/server.rs @@ -977,10 +977,12 @@ async fn refresh_all(req: Request) -> RoutingResult { #[cfg(test)] mod tests { - use super::*; - use crate::commons::util::test; use std::path::PathBuf; + use crate::test; + + use super::*; + #[tokio::test] async fn start_tls_server() { let dir = test::sub_dir(&PathBuf::from("work")); @@ -993,6 +995,6 @@ mod tests { tokio::spawn(super::start(server_conf)); - assert!(crate::daemon::test::primary_server_ready().await); + assert!(test::primary_server_ready().await); } } diff --git a/src/daemon/http/tls_keys.rs b/src/daemon/http/tls_keys.rs index af0ad52c..055da355 100644 --- a/src/daemon/http/tls_keys.rs +++ b/src/daemon/http/tls_keys.rs @@ -300,7 +300,7 @@ impl std::error::Error for Error {} #[cfg(test)] mod tests { // use actix_web::*; - use crate::commons::util::test; + use crate::test; use super::*; diff --git a/src/daemon/mod.rs b/src/daemon/mod.rs index e54cd7e6..1255ad7b 100644 --- a/src/daemon/mod.rs +++ b/src/daemon/mod.rs @@ -4,6 +4,5 @@ pub mod config; pub mod http; pub mod krillserver; pub mod scheduler; -pub mod test; mod mq; diff --git a/src/lib.rs b/src/lib.rs index 7481a616..bd569555 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -33,3 +33,4 @@ pub mod constants; pub mod daemon; pub mod pubd; pub mod publish; +pub mod test; diff --git a/src/pubd/pubserver.rs b/src/pubd/pubserver.rs index 0758e19d..e97594fc 100644 --- a/src/pubd/pubserver.rs +++ b/src/pubd/pubserver.rs @@ -278,8 +278,8 @@ mod tests { use crate::commons::remote::builder::IdCertBuilder; use crate::commons::remote::id::IdCert; use crate::commons::util::file::CurrentFile; - use crate::commons::util::test; use crate::pubd::Publisher; + use crate::test; use super::*; diff --git a/src/daemon/test.rs b/src/test.rs similarity index 85% rename from src/daemon/test.rs rename to src/test.rs index 95ec7b75..587893d7 100644 --- a/src/daemon/test.rs +++ b/src/test.rs @@ -1,11 +1,19 @@ -//! Support for tests in other modules using a running krill server +//! Helper functions for testing Krill. +use std::fs; +use std::fs::File; +use std::io::Write; use std::path::PathBuf; +use std::str::FromStr; use std::time::Duration; +use bytes::Bytes; +use rand::{thread_rng, Rng}; + use hyper::StatusCode; use tokio::time::{delay_for, timeout}; +use rpki::uri; use rpki::uri::Rsync; use crate::cli::options::{BulkCaCommand, CaCommand, Command, Options, PublishersCommand}; @@ -19,8 +27,7 @@ use crate::commons::api::{ }; use crate::commons::remote::rfc8183; use crate::commons::remote::rfc8183::ChildRequest; -use crate::commons::util::test::{sub_dir, tmp_dir}; -use crate::commons::util::{httpclient, test}; +use crate::commons::util::httpclient; use crate::daemon::ca::ta_handle; use crate::daemon::config::Config; use crate::daemon::http::server; @@ -76,7 +83,7 @@ pub async fn start_krill() -> PathBuf { } pub async fn start_secondary_krill(base_dir: &PathBuf) { - let data_dir = test::sub_dir(base_dir); + let data_dir = sub_dir(base_dir); let server_conf = Config::pubd_test(&data_dir); tokio::spawn(server::start(server_conf)); @@ -86,7 +93,7 @@ pub async fn start_secondary_krill(base_dir: &PathBuf) { pub async fn krill_admin(command: Command) -> ApiResponse { let krillc_opts = Options::new( - test::https("https://localhost:3000/"), + https("https://localhost:3000/"), "secret", ReportFormat::Json, command, @@ -99,7 +106,7 @@ pub async fn krill_admin(command: Command) -> ApiResponse { pub async fn krill_admin_secondary(command: Command) -> ApiResponse { let krillc_opts = Options::new( - test::https("https://localhost:3001/"), + https("https://localhost:3001/"), "secret", ReportFormat::Json, command, @@ -119,7 +126,7 @@ pub async fn krill_pubd_admin(command: Command, server: PubdTestContext) -> ApiR pub async fn krill_admin_expect_error(command: Command) -> Error { let krillc_opts = Options::new( - test::https("https://localhost:3000/"), + https("https://localhost:3000/"), "secret", ReportFormat::Json, command, @@ -440,3 +447,71 @@ pub async fn will_publish_objects(publisher: &PublisherHandle, objects: &[&str]) false } + +/// This method sets up a test directory with a random name (a number) +/// under 'work', relative to where cargo is running. It then runs the +/// test provided in the closure, and finally it cleans up the test +/// directory. +/// +/// Note that if your test fails the directory is not cleaned up. +pub fn test_under_tmp(op: F) +where + F: FnOnce(PathBuf) -> (), +{ + let dir = sub_dir(&PathBuf::from("work")); + let path = PathBuf::from(&dir); + + op(dir); + + let _result = fs::remove_dir_all(path); +} + +pub async fn test_under_tmp_async(op: F) +where + F: FnOnce(PathBuf) -> (), +{ + let dir = tmp_dir(); + let path = PathBuf::from(&dir); + + op(dir); + + let _result = fs::remove_dir_all(path); +} + +pub fn tmp_dir() -> PathBuf { + sub_dir(&PathBuf::from("work")) +} + +/// This method sets up a random subdirectory and returns it. It is +/// assumed that the caller will clean this directory themselves. +pub fn sub_dir(base_dir: &PathBuf) -> PathBuf { + let mut rng = thread_rng(); + let rnd: u32 = rng.gen(); + + let mut dir = base_dir.clone(); + dir.push(PathBuf::from(format!("{}", rnd))); + + let full_path = PathBuf::from(&dir); + fs::create_dir_all(&full_path).unwrap(); + + full_path +} + +pub fn rsync(s: &str) -> uri::Rsync { + uri::Rsync::from_str(s).unwrap() +} + +pub fn https(s: &str) -> uri::Https { + uri::Https::from_str(s).unwrap() +} + +pub fn as_bytes(s: &str) -> Bytes { + Bytes::copy_from_slice(s.as_bytes()) +} + +pub fn save_file(base_dir: &PathBuf, file_name: &str, content: &[u8]) { + let mut full_name = base_dir.clone(); + full_name.push(PathBuf::from(file_name)); + let mut f = File::create(full_name).unwrap(); + f.write_all(content).unwrap(); +} diff --git a/tests/ca_embedded.rs b/tests/ca_embedded.rs index ca137aac..e37efc3e 100644 --- a/tests/ca_embedded.rs +++ b/tests/ca_embedded.rs @@ -1,9 +1,10 @@ extern crate krill; +use std::fs; + use krill::commons::api::{Handle, ParentCaReq, ResourceSet}; use krill::daemon::ca::ta_handle; -use krill::daemon::test::*; -use std::fs; +use krill::test::*; #[tokio::test] async fn ca_embedded() { diff --git a/tests/ca_grandchildren.rs b/tests/ca_grandchildren.rs index f6ae8237..e341d7b2 100644 --- a/tests/ca_grandchildren.rs +++ b/tests/ca_grandchildren.rs @@ -1,9 +1,10 @@ extern crate krill; +use std::fs; + use krill::commons::api::{Handle, ObjectName, ParentCaReq, ResourceClassName, ResourceSet}; use krill::daemon::ca::ta_handle; -use krill::daemon::test::*; -use std::fs; +use krill::test::*; #[tokio::test] /// Test that we can delegate from normal CAs to child CAs, and that these child CAs diff --git a/tests/ca_keyroll_rfc6492.rs b/tests/ca_keyroll_rfc6492.rs index d1ebd4c5..f4fff043 100644 --- a/tests/ca_keyroll_rfc6492.rs +++ b/tests/ca_keyroll_rfc6492.rs @@ -1,9 +1,10 @@ extern crate krill; +use std::fs; + use krill::commons::api::{Handle, ParentCaReq, ResourceSet}; use krill::daemon::ca::ta_handle; -use krill::daemon::test::*; -use std::fs; +use krill::test::*; #[tokio::test] async fn ca_keyroll_rfc6492() { diff --git a/tests/ca_rfc6492.rs b/tests/ca_rfc6492.rs index 33df0bb1..92c9c87b 100644 --- a/tests/ca_rfc6492.rs +++ b/tests/ca_rfc6492.rs @@ -1,9 +1,10 @@ extern crate krill; +use std::fs; + use krill::commons::api::{Handle, ParentCaReq, ResourceSet}; use krill::daemon::ca::ta_handle; -use krill::daemon::test::*; -use std::fs; +use krill::test::*; #[tokio::test] async fn ca_rfc6492() { diff --git a/tests/ca_roas.rs b/tests/ca_roas.rs index 4665aced..e04a0edf 100644 --- a/tests/ca_roas.rs +++ b/tests/ca_roas.rs @@ -1,13 +1,13 @@ extern crate krill; +use std::fs; use std::str::FromStr; use krill::commons::api::{ Handle, ObjectName, ParentCaReq, ResourceSet, RoaDefinition, RoaDefinitionUpdates, }; use krill::daemon::ca::ta_handle; -use krill::daemon::test::*; -use std::fs; +use krill::test::*; #[tokio::test] /// Test the CAs can issue and publish ROAs for their resources, and that diff --git a/tests/remote_publication.rs b/tests/remote_publication.rs index 4f034f1b..a5a2b8cc 100644 --- a/tests/remote_publication.rs +++ b/tests/remote_publication.rs @@ -19,7 +19,7 @@ use krill::commons::api::{ }; use krill::commons::remote::rfc8183; use krill::daemon::ca::ta_handle; -use krill::daemon::test::{ +use krill::test::{ add_child_to_ta_embedded, add_parent_to_ca, ca_gets_resources, ca_route_authorizations_update, init_child_with_embedded_repo, krill_admin, krill_pubd_admin, start_krill, start_secondary_krill, PubdTestContext, diff --git a/tests/upgrades.rs b/tests/upgrades.rs index 08fe3b37..e3adeb05 100644 --- a/tests/upgrades.rs +++ b/tests/upgrades.rs @@ -1,12 +1,13 @@ extern crate krill; +use std::path::PathBuf; + use krill::commons::api::Handle; use krill::commons::eventsourcing::{AggregateStore, DiskAggregateStore}; use krill::commons::util::softsigner::OpenSslSigner; use krill::constants::PUBSERVER_DFLT; use krill::daemon::ca::CertAuth; use krill::pubd::Repository; -use std::path::PathBuf; /// This tests that we can understand all events as they have been implemented since 0.4.0, /// in order to guarantee that upgrades will work.