tests/rust: Extract a with_webserver_in helper wrapper

It's much cleaner if the Tokio stuff stays in `test.rs`, and
easier to write tests if the function is synchronous.

Prep for further tests.
This commit is contained in:
Colin Walters 2020-06-04 12:24:16 +00:00
parent 25986126c7
commit 1cd902cd1a
2 changed files with 23 additions and 14 deletions

View File

@ -6,7 +6,6 @@ use std::path::Path;
use crate::test::*; use crate::test::*;
use anyhow::{Context, Result}; use anyhow::{Context, Result};
use commandspec::{sh_command, sh_execute}; use commandspec::{sh_command, sh_execute};
use tokio::runtime::Runtime;
use with_procspawn_tempdir::with_procspawn_tempdir; use with_procspawn_tempdir::with_procspawn_tempdir;
#[itest] #[itest]
@ -61,15 +60,16 @@ fn test_extensions() -> Result<()> {
Ok(()) Ok(())
} }
async fn impl_test_pull_basicauth() -> Result<()> { #[itest]
#[with_procspawn_tempdir]
fn test_pull_basicauth() -> Result<()> {
let opts = TestHttpServerOpts { let opts = TestHttpServerOpts {
basicauth: true, basicauth: true,
..Default::default() ..Default::default()
}; };
let serverrepo = Path::new("server/repo"); let serverrepo = Path::new("server/repo");
std::fs::create_dir_all(&serverrepo)?; std::fs::create_dir_all(&serverrepo)?;
let addr = http_server(&serverrepo, opts).await?; with_webserver_in(&serverrepo, &opts, move |addr| {
tokio::task::spawn_blocking(move || -> Result<()> {
let baseuri = http::Uri::from_maybe_shared(format!("http://{}/", addr).into_bytes())?; let baseuri = http::Uri::from_maybe_shared(format!("http://{}/", addr).into_bytes())?;
let unauthuri = let unauthuri =
http::Uri::from_maybe_shared(format!("http://unknown:badpw@{}/", addr).into_bytes())?; http::Uri::from_maybe_shared(format!("http://unknown:badpw@{}/", addr).into_bytes())?;
@ -107,15 +107,6 @@ async fn impl_test_pull_basicauth() -> Result<()> {
} }
sh_execute!(r#"ostree --repo=client/repo pull origin-goodauth os >/dev/null"#,)?; sh_execute!(r#"ostree --repo=client/repo pull origin-goodauth os >/dev/null"#,)?;
Ok(()) Ok(())
}) })?;
.await??;
Ok(())
}
#[itest]
#[with_procspawn_tempdir]
fn test_pull_basicauth() -> Result<()> {
let mut rt = Runtime::new()?;
rt.block_on(async move { impl_test_pull_basicauth().await })?;
Ok(()) Ok(())
} }

View File

@ -15,6 +15,7 @@ use futures_util::future;
use hyper::service::{make_service_fn, service_fn}; use hyper::service::{make_service_fn, service_fn};
use hyper::{Body, Request, Response}; use hyper::{Body, Request, Response};
use hyper_staticfile::Static; use hyper_staticfile::Static;
use tokio::runtime::Runtime;
pub(crate) type TestFn = fn() -> Result<()>; pub(crate) type TestFn = fn() -> Result<()>;
@ -145,6 +146,23 @@ pub(crate) async fn http_server<P: AsRef<Path>>(
Ok(addr) Ok(addr)
} }
pub(crate) fn with_webserver_in<P: AsRef<Path>, F>(
path: P,
opts: &TestHttpServerOpts,
f: F) -> Result<()>
where
F: FnOnce(&std::net::SocketAddr) -> Result<()>,
F: Send + 'static,
{
let path = path.as_ref();
let mut rt = Runtime::new()?;
rt.block_on(async move {
let addr = http_server(path, opts.clone()).await?;
tokio::task::spawn_blocking(move || f(&addr)).await?
})?;
Ok(())
}
// I put tests in your tests so you can test while you test // I put tests in your tests so you can test while you test
#[cfg(test)] #[cfg(test)]
mod tests { mod tests {