added test for everything container related
This commit is contained in:
@@ -20,51 +20,47 @@ pub fn run_buildah_example() -> Result<(), BuildahError> {
|
||||
// Step 2: Run a command in the container
|
||||
println!("\n=== Installing nginx in container ===");
|
||||
// Use chroot isolation to avoid BPF issues
|
||||
let install_result = buildah::run_with_isolation(container_id, "dnf install -y nginx", "chroot").unwrap();
|
||||
let install_result = buildah::run_with_isolation(container_id, "dnf install -y nginx", "chroot")?;
|
||||
println!("{:#?}", install_result);
|
||||
println!("Installation output: {}", install_result.stdout);
|
||||
|
||||
// // Step 3: Copy a file into the container
|
||||
// println!("\n=== Copying configuration file to container ===");
|
||||
// // Note: This would require an actual file to exist
|
||||
// buildah::copy(container_id, "./example.conf", "/etc/example.conf")?;
|
||||
// println!("For a real example, you would copy a configuration file here");
|
||||
// Step 3: Copy a file into the container
|
||||
println!("\n=== Copying configuration file to container ===");
|
||||
buildah::copy(container_id, "./example.conf", "/etc/example.conf").unwrap();
|
||||
|
||||
// // Step 4: Configure container metadata
|
||||
// println!("\n=== Configuring container metadata ===");
|
||||
// let mut config_options = HashMap::new();
|
||||
// config_options.insert("port".to_string(), "80".to_string());
|
||||
// config_options.insert("label".to_string(), "maintainer=example@example.com".to_string());
|
||||
// config_options.insert("entrypoint".to_string(), "/usr/sbin/nginx".to_string());
|
||||
// Step 4: Configure container metadata
|
||||
println!("\n=== Configuring container metadata ===");
|
||||
let mut config_options = HashMap::new();
|
||||
config_options.insert("port".to_string(), "80".to_string());
|
||||
config_options.insert("label".to_string(), "maintainer=example@example.com".to_string());
|
||||
config_options.insert("entrypoint".to_string(), "/usr/sbin/nginx".to_string());
|
||||
|
||||
// buildah::config(container_id, config_options)?;
|
||||
// println!("Container configured");
|
||||
buildah::config(container_id, config_options)?;
|
||||
println!("Container configured");
|
||||
|
||||
// // Step 5: Commit the container to create a new image
|
||||
// println!("\n=== Committing container to create image ===");
|
||||
// let image_name = "my-nginx:latest";
|
||||
// buildah::image_commit(container_id, image_name, Some("docker"), true, true)?;
|
||||
// println!("Created image: {}", image_name);
|
||||
// Step 5: Commit the container to create a new image
|
||||
println!("\n=== Committing container to create image ===");
|
||||
let image_name = "my-nginx:latest";
|
||||
buildah::image_commit(container_id, image_name, Some("docker"), true, true)?;
|
||||
println!("Created image: {}", image_name);
|
||||
|
||||
// // Step 6: List images to verify our new image exists
|
||||
// println!("\n=== Listing images ===");
|
||||
// let images = buildah::images()?;
|
||||
// println!("Found {} images:", images.len());
|
||||
// for image in images {
|
||||
// println!(" ID: {}", image.id);
|
||||
// println!(" Names: {}", image.names.join(", "));
|
||||
// println!(" Size: {}", image.size);
|
||||
// println!(" Created: {}", image.created);
|
||||
// println!();
|
||||
// }
|
||||
// Step 6: List images to verify our new image exists
|
||||
println!("\n=== Listing images ===");
|
||||
let images = buildah::images()?;
|
||||
println!("Found {} images:", images.len());
|
||||
for image in images {
|
||||
println!(" ID: {}", image.id);
|
||||
println!(" Names: {}", image.names.join(", "));
|
||||
println!(" Size: {}", image.size);
|
||||
println!(" Created: {}", image.created);
|
||||
println!();
|
||||
}
|
||||
|
||||
// // Step 7: Clean up (optional in a real workflow)
|
||||
// println!("\n=== Cleaning up ===");
|
||||
// println!("In a real workflow, you might want to keep the image");
|
||||
// println!("To remove the image, you would run:");
|
||||
// println!("buildah::image_remove(\"{}\")", image_name);
|
||||
println!("\n=== Cleaning up ===");
|
||||
buildah::image_remove(image_name).unwrap();
|
||||
|
||||
// println!("\nBuildah example workflow completed successfully!");
|
||||
println!("\nBuildah example workflow completed successfully!");
|
||||
Ok(())
|
||||
}
|
||||
|
||||
@@ -72,12 +68,8 @@ pub fn run_buildah_example() -> Result<(), BuildahError> {
|
||||
pub fn build_image_example() -> Result<(), BuildahError> {
|
||||
println!("Building an image from a Containerfile...");
|
||||
|
||||
// This would typically use a command like:
|
||||
// buildah build -t my-app:latest .
|
||||
|
||||
// For our example, we'll just show the command structure
|
||||
let build_args = &["build", "-t", "my-app:latest", "."];
|
||||
let result = buildah::execute_buildah_command(build_args)?;
|
||||
// Use the build function with tag, context directory, and isolation to avoid BPF issues
|
||||
let result = buildah::build(Some("my-app:latest"), ".", "example_Dockerfile", Some("chroot"))?;
|
||||
|
||||
println!("Build output: {}", result.stdout);
|
||||
println!("Image built successfully!");
|
||||
@@ -100,9 +92,9 @@ pub fn registry_operations_example() -> Result<(), BuildahError> {
|
||||
println!("Image tagged successfully");
|
||||
|
||||
// Push an image (this would typically go to a real registry)
|
||||
println!("\n=== Pushing an image (example only) ===");
|
||||
println!("In a real scenario, you would push to a registry with:");
|
||||
println!("buildah::image_push(\"my-alpine:v1.0\", \"docker://registry.example.com/my-alpine:v1.0\", true)");
|
||||
// println!("\n=== Pushing an image (example only) ===");
|
||||
// println!("In a real scenario, you would push to a registry with:");
|
||||
// println!("buildah::image_push(\"my-alpine:v1.0\", \"docker://registry.example.com/my-alpine:v1.0\", true)");
|
||||
|
||||
Ok(())
|
||||
}
|
||||
@@ -111,21 +103,13 @@ pub fn registry_operations_example() -> Result<(), BuildahError> {
|
||||
pub fn run_all_examples() -> Result<(), BuildahError> {
|
||||
println!("=== BUILDAH MODULE EXAMPLES ===\n");
|
||||
|
||||
// Note: In a real application, you might want to run these
|
||||
// examples individually or have proper error handling
|
||||
|
||||
// Uncomment these to run the examples
|
||||
run_buildah_example()?;
|
||||
// build_image_example()?;
|
||||
// registry_operations_example()?;
|
||||
|
||||
println!("\nTo run these examples, uncomment the function calls in run_all_examples()");
|
||||
println!("Note that these examples require buildah to be installed on your system");
|
||||
println!("and may require root/sudo privileges depending on your setup.");
|
||||
|
||||
build_image_example()?;
|
||||
registry_operations_example()?;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn main() {
|
||||
run_all_examples();
|
||||
let _ = run_all_examples().unwrap();
|
||||
}
|
||||
Reference in New Issue
Block a user