From bd38f9e98af51e542c58baf79ec731e951c5c2ed Mon Sep 17 00:00:00 2001 From: Ryan Levick Date: Fri, 19 Jul 2024 14:34:18 +0200 Subject: [PATCH] Improve errors related to services Signed-off-by: Ryan Levick --- crates/test-environment/src/services.rs | 16 ++++++++++------ crates/test-environment/src/services/docker.rs | 9 +++++++-- crates/test-environment/src/services/python.rs | 2 +- 3 files changed, 18 insertions(+), 9 deletions(-) diff --git a/crates/test-environment/src/services.rs b/crates/test-environment/src/services.rs index 3add414..3d88762 100644 --- a/crates/test-environment/src/services.rs +++ b/crates/test-environment/src/services.rs @@ -126,7 +126,7 @@ fn get_builtin_service_definitions( return Ok(Vec::new()); } - std::fs::read_dir(service_definitions_path) + let result = std::fs::read_dir(service_definitions_path) .with_context(|| { format!( "no service definitions found at '{}'", @@ -147,7 +147,7 @@ fn get_builtin_service_definitions( Ok((file_name.to_owned(), file_extension.to_owned())) }) .filter(|r| !matches!(r, Ok((_, extension)) if extension == "lock")) - .filter(move |r| match r { + .filter(|r| match r { Ok((service, _)) => builtins.remove(service.as_str()), _ => false, }) @@ -157,18 +157,22 @@ fn get_builtin_service_definitions( name: name.clone(), kind: match extension.as_str() { "py" => ServiceKind::Python { - script: service_definitions_path.join(format!("{}.py", name)), + script: service_definitions_path.join(format!("{name}.py")), }, "Dockerfile" => ServiceKind::Docker { image: docker::DockerImage::FromDockerfile( - service_definitions_path.join(format!("{}.Dockerfile", name)), + service_definitions_path.join(format!("{name}.Dockerfile")), ), }, - _ => bail!("unsupported service definition extension '{}'", extension), + _ => bail!("unsupported service definition extension '{extension}'"), }, }) }) - .collect() + .collect(); + if !builtins.is_empty() { + bail!("no service definitions found for: {builtins:?}",); + } + result } /// A service definition. diff --git a/crates/test-environment/src/services/docker.rs b/crates/test-environment/src/services/docker.rs index 7a769a1..0aa2fcb 100644 --- a/crates/test-environment/src/services/docker.rs +++ b/crates/test-environment/src/services/docker.rs @@ -164,7 +164,12 @@ fn build_image(dockerfile_path: &Path, image_name: &String) -> anyhow::Result<() .stdout(Stdio::null()) .stderr(Stdio::piped()) .output() - .context("service failed to spawn")?; + .with_context(|| { + format!( + "docker build failed to spawn for Dockerfile '{}'", + dockerfile_path.display() + ) + })?; if !output.status.success() { let stderr = std::str::from_utf8(&output.stderr).unwrap_or(""); @@ -196,7 +201,7 @@ fn run_container(image_name: &str) -> anyhow::Result { .arg("--health-start-period=1s") .arg(image_name) .output() - .context("service failed to spawn")?; + .with_context(|| format!("docker run failed to spawn for image '{image_name}'"))?; if !output.status.success() { bail!("failed to run docker image"); } diff --git a/crates/test-environment/src/services/python.rs b/crates/test-environment/src/services/python.rs index e0d5713..ba9be99 100644 --- a/crates/test-environment/src/services/python.rs +++ b/crates/test-environment/src/services/python.rs @@ -34,7 +34,7 @@ impl PythonService { .arg(script_path.display().to_string()) .stdout(Stdio::piped()) .spawn() - .context("service failed to spawn")?; + .with_context(|| format!("python failed to spawn for '{}'", script_path.display()))?; std::thread::sleep(std::time::Duration::from_millis(1000)); Ok(Self { stdout: OutputStream::new(