tests/rust: Extract a with_webserver_in helper wrapper
authorColin Walters <walters@verbum.org>
Thu, 4 Jun 2020 12:24:16 +0000 (12:24 +0000)
committerColin Walters <walters@verbum.org>
Thu, 4 Jun 2020 13:04:35 +0000 (13:04 +0000)
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.

tests/inst/src/repobin.rs
tests/inst/src/test.rs

index f45f913b38bdac4d77503206d3b27c2f9f9a973e..41fd1390522c53257e39d26fbae1fa20326e6a1c 100644 (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(())
 }
index 9e7d4b41177ec7e6f50c9eda54dfab7e4bcd0f9e..7178d7bb20ddbb7eee7a436c43b1876047b33ea2 100644 (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 {