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 anyhow::{Context, Result};
use commandspec::{sh_command, sh_execute};
use tokio::runtime::Runtime;
use with_procspawn_tempdir::with_procspawn_tempdir;
#[itest]
@ -61,15 +60,16 @@ fn test_extensions() -> Result<()> {
Ok(())
}
async fn impl_test_pull_basicauth() -> Result<()> {
#[itest]
#[with_procspawn_tempdir]
fn test_pull_basicauth() -> Result<()> {
let opts = TestHttpServerOpts {
basicauth: true,
..Default::default()
};
let serverrepo = Path::new("server/repo");
std::fs::create_dir_all(&serverrepo)?;
let addr = http_server(&serverrepo, opts).await?;
tokio::task::spawn_blocking(move || -> Result<()> {
with_webserver_in(&serverrepo, &opts, move |addr| {
let baseuri = http::Uri::from_maybe_shared(format!("http://{}/", addr).into_bytes())?;
let unauthuri =
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"#,)?;
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(())
}

View File

@ -15,6 +15,7 @@ use futures_util::future;
use hyper::service::{make_service_fn, service_fn};
use hyper::{Body, Request, Response};
use hyper_staticfile::Static;
use tokio::runtime::Runtime;
pub(crate) type TestFn = fn() -> Result<()>;
@ -145,6 +146,23 @@ pub(crate) async fn http_server<P: AsRef<Path>>(
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
#[cfg(test)]
mod tests {