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

Support local buildpacks and meta-buildpacks in libcnb-test #666

Merged
merged 9 commits into from
Sep 20, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,4 @@
Cargo.lock
**/fixtures/*/target/
**/fixtures/*/packaged/
**/test-fixtures/buildpacks/*/target/
colincasey marked this conversation as resolved.
Show resolved Hide resolved
2 changes: 1 addition & 1 deletion libcnb-test/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@ readme = "README.md"
include = ["src/**/*", "LICENSE", "README.md"]

[dependencies]
cargo_metadata = "0.17.0"
fastrand = "2.0.0"
fs_extra = "1.3.0"
libcnb-common.workspace = true
libcnb-data.workspace = true
libcnb-package.workspace = true
tempfile = "3.7.1"
Expand Down
134 changes: 105 additions & 29 deletions libcnb-test/src/build.rs
Original file line number Diff line number Diff line change
@@ -1,62 +1,138 @@
use cargo_metadata::MetadataCommand;
use libcnb_package::build::{build_buildpack_binaries, BuildBinariesError};
use libcnb_common::toml_file::{read_toml_file, TomlFileError};
use libcnb_data::buildpack::{BuildpackDescriptor, BuildpackId};
use libcnb_package::buildpack_dependency_graph::{
build_libcnb_buildpacks_dependency_graph, BuildBuildpackDependencyGraphError,
};
use libcnb_package::cross_compile::{cross_compile_assistance, CrossCompileAssistance};
use libcnb_package::{assemble_buildpack_directory, CargoProfile};
use libcnb_package::dependency_graph::{get_dependencies, GetDependenciesError};
use libcnb_package::output::create_packaged_buildpack_dir_resolver;
use libcnb_package::package::PackageBuildpackError;
use libcnb_package::{find_cargo_workspace_root_dir, CargoProfile, FindCargoWorkspaceRootError};
use std::collections::BTreeMap;
use std::fs;
use std::path::PathBuf;
use tempfile::{tempdir, TempDir};

/// Packages the current crate as a buildpack into a temporary directory.
pub(crate) fn package_crate_buildpack(
cargo_profile: CargoProfile,
target_triple: impl AsRef<str>,
) -> Result<TempDir, PackageCrateBuildpackError> {
) -> Result<PackagedBuildpackDir, PackageTestBuildpackError> {
colincasey marked this conversation as resolved.
Show resolved Hide resolved
let cargo_manifest_dir = std::env::var("CARGO_MANIFEST_DIR")
.map(PathBuf::from)
.map_err(PackageCrateBuildpackError::CannotDetermineCrateDirectory)?;
.map_err(PackageTestBuildpackError::CannotDetermineCrateDirectory)?;

let cargo_metadata = MetadataCommand::new()
.manifest_path(&cargo_manifest_dir.join("Cargo.toml"))
.exec()
.map_err(PackageCrateBuildpackError::CargoMetadataError)?;
let buildpack_toml = cargo_manifest_dir.join("buildpack.toml");

let cargo_env = match cross_compile_assistance(target_triple.as_ref()) {
assert!(
buildpack_toml.exists(),
"Could not package directory as buildpack! No `buildpack.toml` file exists at {}",
cargo_manifest_dir.display()
);

let buildpack_descriptor: BuildpackDescriptor =
read_toml_file(buildpack_toml).map_err(PackageTestBuildpackError::CannotReadBuildpack)?;

package_buildpack(
&buildpack_descriptor.buildpack().id,
cargo_profile,
target_triple,
)
}

pub(crate) fn package_buildpack(
buildpack_id: &BuildpackId,
cargo_profile: CargoProfile,
target_triple: impl AsRef<str>,
) -> Result<PackagedBuildpackDir, PackageTestBuildpackError> {
let cargo_manifest_dir = std::env::var("CARGO_MANIFEST_DIR")
.map(PathBuf::from)
.map_err(PackageTestBuildpackError::CannotDetermineCrateDirectory)?;

let cargo_build_env = match cross_compile_assistance(target_triple.as_ref()) {
CrossCompileAssistance::HelpText(help_text) => {
return Err(PackageCrateBuildpackError::CrossCompileConfigurationError(
return Err(PackageTestBuildpackError::CrossCompileConfigurationError(
help_text,
));
}
CrossCompileAssistance::NoAssistance => Vec::new(),
CrossCompileAssistance::Configuration { cargo_env } => cargo_env,
};

let buildpack_dir =
tempdir().map_err(PackageCrateBuildpackError::CannotCreateBuildpackTempDirectory)?;
let workspace_root_path = find_cargo_workspace_root_dir(&cargo_manifest_dir)
.map_err(PackageTestBuildpackError::FindCargoWorkspace)?;

let buildpack_binaries = build_buildpack_binaries(
&cargo_manifest_dir,
&cargo_metadata,
let package_dir =
tempdir().map_err(PackageTestBuildpackError::CannotCreateBuildpackTempDirectory)?;

let buildpack_dir_resolver = create_packaged_buildpack_dir_resolver(
package_dir.as_ref(),
cargo_profile,
&cargo_env,
target_triple.as_ref(),
)
.map_err(PackageCrateBuildpackError::BuildBinariesError)?;
);

// TODO: this could accidentally detect a packaged meta-buildpack twice. how should we ignore directories
colincasey marked this conversation as resolved.
Show resolved Hide resolved
// containing packaged buildpacks that might get detected now that the user controls --package-dir?
// - support .gitignore or some other persistent configuration?
// - during packaging should we create some type of file that indicates the output is a packaged buildpack?
let buildpack_dependency_graph =
build_libcnb_buildpacks_dependency_graph(&workspace_root_path, &[])
.map_err(PackageTestBuildpackError::CreateBuildpackDependencyGraph)?;

assemble_buildpack_directory(
buildpack_dir.path(),
cargo_manifest_dir.join("buildpack.toml"),
&buildpack_binaries,
let root_node = buildpack_dependency_graph
.node_weights()
.find(|node| node.buildpack_id == buildpack_id.clone());

assert!(
root_node.is_some(),
"Could not package directory as buildpack! No buildpack with id `{buildpack_id}` exists in the workspace at {}",
workspace_root_path.display()
);

let build_order = get_dependencies(
&buildpack_dependency_graph,
&[root_node.expect("The root node should exist")],
)
.map_err(PackageCrateBuildpackError::CannotAssembleBuildpackDirectory)?;
.map_err(PackageTestBuildpackError::GetDependencies)?;

let mut packaged_buildpack_dirs = BTreeMap::new();
for node in &build_order {
let buildpack_destination_dir = buildpack_dir_resolver(&node.buildpack_id);

fs::create_dir_all(&buildpack_destination_dir).unwrap();

libcnb_package::package::package_buildpack(
&node.path,
cargo_profile,
target_triple.as_ref(),
&cargo_build_env,
&buildpack_destination_dir,
&packaged_buildpack_dirs,
)
.map_err(PackageTestBuildpackError::PackageBuildpack)?;

packaged_buildpack_dirs.insert(node.buildpack_id.clone(), buildpack_destination_dir);
}

Ok(PackagedBuildpackDir {
_root: package_dir,
path: buildpack_dir_resolver(buildpack_id),
})
}

Ok(buildpack_dir)
pub(crate) struct PackagedBuildpackDir {
_root: TempDir,
pub(crate) path: PathBuf,
colincasey marked this conversation as resolved.
Show resolved Hide resolved
}

#[derive(Debug)]
pub(crate) enum PackageCrateBuildpackError {
BuildBinariesError(BuildBinariesError),
CannotAssembleBuildpackDirectory(std::io::Error),
pub(crate) enum PackageTestBuildpackError {
CannotCreateBuildpackTempDirectory(std::io::Error),
CannotDetermineCrateDirectory(std::env::VarError),
CargoMetadataError(cargo_metadata::Error),
CannotReadBuildpack(TomlFileError),
colincasey marked this conversation as resolved.
Show resolved Hide resolved
CreateBuildpackDependencyGraph(BuildBuildpackDependencyGraphError),
colincasey marked this conversation as resolved.
Show resolved Hide resolved
CrossCompileConfigurationError(String),
FindCargoWorkspace(FindCargoWorkspaceRootError),
colincasey marked this conversation as resolved.
Show resolved Hide resolved
GetDependencies(GetDependenciesError<BuildpackId>),
PackageBuildpack(PackageBuildpackError),
}
3 changes: 3 additions & 0 deletions libcnb-test/src/build_config.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use libcnb_data::buildpack::BuildpackId;
use std::collections::HashMap;
use std::path::{Path, PathBuf};
use std::rc::Rc;
Expand Down Expand Up @@ -251,6 +252,8 @@ impl BuildConfig {
pub enum BuildpackReference {
colincasey marked this conversation as resolved.
Show resolved Hide resolved
/// References the buildpack in the Rust Crate currently being tested.
Crate,
/// References a libcnb.rs buildpack within the Rust Workspace that needs to be packaged into a buildpack
LibCnbRs(BuildpackId),
/// References another buildpack by id, local directory or tarball.
Other(String),
colincasey marked this conversation as resolved.
Show resolved Hide resolved
}
Expand Down
11 changes: 11 additions & 0 deletions libcnb-test/src/pack.rs
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,17 @@ impl From<PackSbomDownloadCommand> for Command {
}
}

#[derive(Clone, Debug)]
pub(crate) struct PackVersionCommand;

impl From<PackVersionCommand> for Command {
fn from(_: PackVersionCommand) -> Self {
let mut command = Self::new("pack");
command.arg("--version");
command
}
}

#[cfg(test)]
mod tests {
use super::*;
Expand Down
61 changes: 49 additions & 12 deletions libcnb-test/src/test_runner.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use crate::docker::DockerRemoveImageCommand;
use crate::pack::PackBuildCommand;
use crate::pack::{PackBuildCommand, PackVersionCommand};
use crate::util::CommandError;
use crate::{app, build, util, BuildConfig, BuildpackReference, PackResult, TestContext};
use std::borrow::Borrow;
Expand Down Expand Up @@ -56,6 +56,8 @@ impl TestRunner {
config: C,
f: F,
) {
check_required_pack_version();
colincasey marked this conversation as resolved.
Show resolved Hide resolved

let config = config.borrow();

let app_dir = {
Expand Down Expand Up @@ -88,14 +90,7 @@ impl TestRunner {
}
};

let temp_crate_buildpack_dir =
config
.buildpacks
.contains(&BuildpackReference::Crate)
.then(|| {
build::package_crate_buildpack(config.cargo_profile, &config.target_triple)
.expect("Could not package current crate as buildpack")
});
let mut buildpack_dirs = vec![];

let mut pack_command = PackBuildCommand::new(&config.builder_name, &app_dir, &image_name);

Expand All @@ -106,10 +101,22 @@ impl TestRunner {
for buildpack in &config.buildpacks {
match buildpack {
BuildpackReference::Crate => {
pack_command.buildpack(temp_crate_buildpack_dir.as_ref()
.expect("Test references crate buildpack, but crate wasn't packaged as a buildpack. This is an internal libcnb-test error, please report any occurrences."))
let crate_buildpack_dir = build::package_crate_buildpack(config.cargo_profile, &config.target_triple)
.expect("Test references crate buildpack, but crate wasn't packaged as a buildpack. This is an internal libcnb-test error, please report any occurrences");
pack_command.buildpack(crate_buildpack_dir.path.clone());
buildpack_dirs.push(crate_buildpack_dir);
}

BuildpackReference::LibCnbRs(builpack_id) => {
let buildpack_dir = build::package_buildpack(builpack_id, config.cargo_profile, &config.target_triple)
.unwrap_or_else(|_| panic!("Test references buildpack `{builpack_id}`, but this directory wasn't packaged as a buildpack. This is an internal libcnb-test error, please report any occurrences"));
pack_command.buildpack(buildpack_dir.path.clone());
buildpack_dirs.push(buildpack_dir);
}

BuildpackReference::Other(id) => {
pack_command.buildpack(id.clone());
}
BuildpackReference::Other(id) => pack_command.buildpack(id.clone()),
};
}

Expand Down Expand Up @@ -146,3 +153,33 @@ impl TestRunner {
f(test_context);
}
}

fn check_required_pack_version() {
match util::run_command(PackVersionCommand) {
Ok(output) => {
let version = output.stdout.trim();
let mut split = version.split('.');
let major: u32 = split
.next()
.map(|value| {
value
.parse::<u32>()
.expect("Major coordinate should be a number")
})
.expect("Major coordinate should be present in pack --version output");
let minor: u32 = split
.next()
.map(|value| {
value
.parse::<u32>()
.expect("Minor coordinate should be a number")
})
.expect("Minor coordinate should be present in pack --version output");
let is_min_version = major == 0 && minor >= 30 || major >= 1;
assert!(is_min_version, "Pack version 0.30+ is required but {version} is installed. Please upgrade your pack CLI.");
}
Err(error) => {
panic!("Error determinging pack version:\n\n{error}");
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
[package]
name = "one"
version = "0.0.0"

[workspace]
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
api = "0.8"

[buildpack]
id = "libcnb-test/a"
version = "0.0.0"

[[stacks]]
id = "*"
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
fn main() {
println!("Buildpack A");
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
[package]
name = "two"
version = "0.0.0"

[workspace]
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
api = "0.8"

[buildpack]
id = "libcnb-test/b"
version = "0.0.0"

[[stacks]]
id = "*"
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
fn main() {
println!("Buildpack B");
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
api = "0.8"

[buildpack]
id = "libcnb-test/meta"
name = "Meta-buildpack Test"
version = "0.0.0"
homepage = "https://example.com"
description = "Official test example"
keywords = ["test"]

[[buildpack.licenses]]
type = "BSD-3-Clause"

[[order]]

[[order.group]]
id = "libcnb-test/a"
version = "0.0.0"

[[order.group]]
id = "libcnb-test/b"
version = "0.0.0"
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
[buildpack]
uri = "."

[[dependencies]]
uri = "libcnb:libcnb-test/a"

[[dependencies]]
uri = "libcnb:libcnb-test/b"

Loading