Skip to content

Commit

Permalink
Migrate examples and test buildpacks to struct layer API (#833)
Browse files Browse the repository at this point in the history
  • Loading branch information
Malax authored Jun 18, 2024
1 parent fa568d4 commit aa3268b
Show file tree
Hide file tree
Showing 8 changed files with 102 additions and 231 deletions.
35 changes: 0 additions & 35 deletions examples/execd/src/layer.rs

This file was deleted.

24 changes: 15 additions & 9 deletions examples/execd/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,18 +1,12 @@
// This example uses the older trait Layer API. The example will be updated to the newer API
// before the next libcnb.rs release.
#![allow(deprecated)]

mod layer;

use crate::layer::ExecDLayer;
use libcnb::build::{BuildContext, BuildResult, BuildResultBuilder};
use libcnb::data::layer_name;
use libcnb::detect::{DetectContext, DetectResult, DetectResultBuilder};
use libcnb::generic::{GenericError, GenericMetadata, GenericPlatform};
use libcnb::{buildpack_main, Buildpack};
use libcnb::{additional_buildpack_binary_path, buildpack_main, Buildpack};

// Suppress warnings due to the `unused_crate_dependencies` lint not handling integration tests well.
use fastrand as _;
use libcnb::layer::UncachedLayerDefinition;
#[cfg(test)]
use libcnb_test as _;

Expand All @@ -28,7 +22,19 @@ impl Buildpack for ExecDBuildpack {
}

fn build(&self, context: BuildContext<Self>) -> libcnb::Result<BuildResult, Self::Error> {
context.handle_layer(layer_name!("layer_name"), ExecDLayer)?;
let layer_ref = context.uncached_layer(
layer_name!("layer_name"),
UncachedLayerDefinition {
build: false,
launch: true,
},
)?;

layer_ref.write_exec_d_programs([(
"dice_roller",
additional_buildpack_binary_path!("dice_roller"),
)])?;

BuildResultBuilder::new().build()
}
}
Expand Down
54 changes: 0 additions & 54 deletions test-buildpacks/readonly-layer-files/src/layer.rs

This file was deleted.

33 changes: 25 additions & 8 deletions test-buildpacks/readonly-layer-files/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,15 +1,12 @@
// This test buildpack uses the older trait Layer API. It will be updated to the newer API
// before the next libcnb.rs release.
#![allow(deprecated)]

mod layer;

use crate::layer::TestLayer;
use libcnb::build::{BuildContext, BuildResult, BuildResultBuilder};
use libcnb::data::layer_name;
use libcnb::detect::{DetectContext, DetectResult, DetectResultBuilder};
use libcnb::generic::{GenericMetadata, GenericPlatform};
use libcnb::layer::{CachedLayerDefinition, InvalidMetadataAction, RestoredLayerAction};
use libcnb::{buildpack_main, Buildpack};
use std::fs;
use std::fs::Permissions;
use std::os::unix::fs::PermissionsExt;

// Suppress warnings due to the `unused_crate_dependencies` lint not handling integration tests well.
#[cfg(test)]
Expand All @@ -27,7 +24,27 @@ impl Buildpack for TestBuildpack {
}

fn build(&self, context: BuildContext<Self>) -> libcnb::Result<BuildResult, Self::Error> {
context.handle_layer(layer_name!("test"), TestLayer)?;
let layer_ref = context.cached_layer(
layer_name!("test"),
CachedLayerDefinition {
build: true,
launch: true,
invalid_metadata_action: &|_| InvalidMetadataAction::DeleteLayer,
restored_layer_action: &|_: &GenericMetadata, _| RestoredLayerAction::DeleteLayer,
},
)?;

let directory = layer_ref.path().join("sub_directory");
fs::create_dir_all(&directory).expect("Couldn't create subdirectory");

fs::write(directory.join("foo.txt"), "hello world!").expect("Couldn't write file");

// By making the sub-directory read-only, files inside it cannot be deleted. This would
// cause issues when libcnb.rs tries to delete a cached layer directory unless libcnb.rs
// handles this case explicitly.
fs::set_permissions(&directory, Permissions::from_mode(0o555))
.expect("Couldn't set permissions to read-only");

BuildResultBuilder::new().build()
}
}
Expand Down
72 changes: 61 additions & 11 deletions test-buildpacks/sbom/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,17 +1,11 @@
// This test buildpack uses the older trait Layer API. It will be updated to the newer API
// before the next libcnb.rs release.
#![allow(deprecated)]

mod test_layer;
mod test_layer_2;

use crate::test_layer::TestLayer;
use crate::test_layer_2::TestLayer2;
use libcnb::build::{BuildContext, BuildResult, BuildResultBuilder};
use libcnb::data::layer_name;
use libcnb::data::sbom::SbomFormat;
use libcnb::detect::{DetectContext, DetectResult, DetectResultBuilder};
use libcnb::generic::{GenericMetadata, GenericPlatform};
use libcnb::layer::{
CachedLayerDefinition, InvalidMetadataAction, LayerState, RestoredLayerAction,
};
use libcnb::sbom::Sbom;
use libcnb::{buildpack_main, Buildpack};

Expand All @@ -31,8 +25,64 @@ impl Buildpack for TestBuildpack {
}

fn build(&self, context: BuildContext<Self>) -> libcnb::Result<BuildResult, Self::Error> {
context.handle_layer(layer_name!("test"), TestLayer)?;
context.handle_layer(layer_name!("test2"), TestLayer2)?;
let first_layer_ref = context.cached_layer(
layer_name!("test"),
CachedLayerDefinition {
build: true,
launch: true,
invalid_metadata_action: &|_| InvalidMetadataAction::DeleteLayer,
restored_layer_action: &|_: &GenericMetadata, _| RestoredLayerAction::KeepLayer,
},
)?;

match first_layer_ref.state {
LayerState::Restored { .. } => {
first_layer_ref.write_sboms(&[])?;
}
LayerState::Empty { .. } => {
first_layer_ref.write_sboms(&[
Sbom::from_bytes(
SbomFormat::CycloneDxJson,
*include_bytes!("../etc/cyclonedx_3.sbom.json"),
),
Sbom::from_bytes(
SbomFormat::SpdxJson,
*include_bytes!("../etc/spdx_3.sbom.json"),
),
Sbom::from_bytes(
SbomFormat::SyftJson,
*include_bytes!("../etc/syft_3.sbom.json"),
),
])?;
}
}

let second_layer_ref = context.cached_layer(
layer_name!("test2"),
CachedLayerDefinition {
build: true,
launch: true,
invalid_metadata_action: &|_| InvalidMetadataAction::DeleteLayer,
restored_layer_action: &|_: &GenericMetadata, _| RestoredLayerAction::KeepLayer,
},
)?;

if let LayerState::Empty { .. } = second_layer_ref.state {
second_layer_ref.write_sboms(&[
Sbom::from_bytes(
SbomFormat::CycloneDxJson,
*include_bytes!("../etc/cyclonedx_2.sbom.json"),
),
Sbom::from_bytes(
SbomFormat::SpdxJson,
*include_bytes!("../etc/spdx_2.sbom.json"),
),
Sbom::from_bytes(
SbomFormat::SyftJson,
*include_bytes!("../etc/syft_2.sbom.json"),
),
])?;
}

BuildResultBuilder::new()
.launch_sbom(Sbom::from_bytes(
Expand Down
60 changes: 0 additions & 60 deletions test-buildpacks/sbom/src/test_layer.rs

This file was deleted.

52 changes: 0 additions & 52 deletions test-buildpacks/sbom/src/test_layer_2.rs

This file was deleted.

3 changes: 1 addition & 2 deletions test-buildpacks/sbom/tests/integration_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -113,8 +113,7 @@ fn test() {

context.rebuild(&build_config, |context| {
context.download_sbom_files(|sbom_files| {
// The 'test' layer does not return any SBOM files in its update implementation. This
// must result in no SBOM files being used from previous build.
// The buildpack removes restored SBOMs from the 'test' layer.
assert!(!sbom_files
.path_for(
&buildpack_id,
Expand Down

0 comments on commit aa3268b

Please sign in to comment.