Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

libcnb-test: Check the exit code of pack sbom download #520

Merged
merged 1 commit into from
Oct 18, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@ separate changelogs for each crate were used. If you need to refer to these old

## [Unreleased]

### Fixed

- libcnb-test: `TestContext::download_sbom_files` now checks the exit code of the `pack sbom download` command it runs. ([#520](https://github.com/heroku/libcnb.rs/pull/520))

### Changed

- libcnb: Drop the use of the `stacker` crate when recursively removing layer directories. ([#517](https://github.com/heroku/libcnb.rs/pull/517))
Expand Down
42 changes: 31 additions & 11 deletions libcnb-test/src/pack.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use crate::PackResult;
use std::collections::BTreeMap;
use std::path::PathBuf;
use std::process::{Command, Output, Stdio};
use std::process::{Command, Output};
use tempfile::TempDir;

/// Represents a `pack build` command.
Expand Down Expand Up @@ -175,22 +176,41 @@ impl From<PackSbomDownloadCommand> for Command {
}
}

/// Runs the given pack command, panicking with user-friendly error messages when errors occur and
/// pipes through stdout and stderr.
pub(crate) fn run_pack_command<C: Into<Command>>(command: C) -> Output {
command.into()
.stdout(Stdio::piped())
.stderr(Stdio::piped())
.spawn()
/// Runs the given pack command, panicking with user-friendly error messages when errors occur.
pub(crate) fn run_pack_command<C: Into<Command>>(
command: C,
expected_result: &PackResult,
) -> Output {
let output = command.into()
.output()
edmorley marked this conversation as resolved.
Show resolved Hide resolved
.unwrap_or_else(|io_error| {
if io_error.kind() == std::io::ErrorKind::NotFound {
panic!("External `pack` command not found. Install Pack CLI and ensure it is on PATH: https://buildpacks.io/docs/install-pack");
} else {
panic!("Could not spawn external `pack` process: {io_error}");
};
})
.wait_with_output()
.expect("Error while waiting on external `pack` process")
});

if (expected_result == &PackResult::Success && !output.status.success())
|| (expected_result == &PackResult::Failure && output.status.success())
{
panic!(
"pack command unexpectedly {} with exit-code {}!\n\npack stdout:\n{}\n\npack stderr:\n{}",
if output.status.success() {
"succeeded"
} else {
"failed"
},
output
.status
.code()
.map_or(String::from("<unknown>"), |exit_code| exit_code.to_string()),
String::from_utf8_lossy(&output.stdout),
String::from_utf8_lossy(&output.stderr)
);
}

output
}

#[cfg(test)]
Expand Down
4 changes: 2 additions & 2 deletions libcnb-test/src/test_context.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use crate::pack::{run_pack_command, PackSbomDownloadCommand};
use crate::{
container_port_mapping, util, BuildConfig, ContainerConfig, ContainerContext, LogOutput,
TestRunner,
PackResult, TestRunner,
};
use bollard::container::{Config, CreateContainerOptions, StartContainerOptions};
use bollard::image::RemoveImageOptions;
Expand Down Expand Up @@ -202,7 +202,7 @@ impl<'a> TestContext<'a> {
let mut command = PackSbomDownloadCommand::new(&self.image_name);
command.output_dir(temp_dir.path());

run_pack_command(command);
run_pack_command(command, &PackResult::Success);

f(SbomFiles {
sbom_files_directory: temp_dir.path().into(),
Expand Down
21 changes: 3 additions & 18 deletions libcnb-test/src/test_runner.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use crate::pack::{run_pack_command, PackBuildCommand};
use crate::{app, build, util, BuildConfig, BuildpackReference, PackResult, TestContext};
use crate::{app, build, util, BuildConfig, BuildpackReference, TestContext};
use bollard::Docker;
use std::borrow::Borrow;
use std::env;
Expand Down Expand Up @@ -156,7 +156,7 @@ impl TestRunner {
};
}

let output = run_pack_command(pack_command);
let output = run_pack_command(pack_command, &config.expected_pack_result);

let test_context = TestContext {
pack_stdout: String::from_utf8_lossy(&output.stdout).into_owned(),
Expand All @@ -166,21 +166,6 @@ impl TestRunner {
runner: self,
};

if (config.expected_pack_result == PackResult::Failure && output.status.success())
|| (config.expected_pack_result == PackResult::Success && !output.status.success())
{
panic!(
"pack command unexpectedly {} with exit-code {}!\n\npack stdout:\n{}\n\npack stderr:\n{}",
if output.status.success() { "succeeded" } else { "failed" },
output
.status
.code()
.map_or(String::from("<unknown>"), |exit_code| exit_code.to_string()),
test_context.pack_stdout,
test_context.pack_stderr
);
} else {
f(test_context);
}
f(test_context);
}
}