added test for everything container related

This commit is contained in:
2025-04-02 16:12:31 +02:00
parent 5f6420a421
commit d6b53a72bb
8 changed files with 206 additions and 58 deletions

View File

@@ -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();
}

82
examples/nerdctl.rs Normal file
View File

@@ -0,0 +1,82 @@
//! Example usage of the nerdctl module
//!
//! This file demonstrates how to use the nerdctl module to perform
//! common container operations like creating containers, running commands,
//! and managing images.
use sal::virt::nerdctl::{self, NerdctlError};
/// Run a complete nerdctl workflow example
pub fn run_nerdctl_example() -> Result<(), NerdctlError> {
println!("Starting nerdctl example workflow...");
// Step 1: Pull an image
println!("\n=== Pulling nginx:latest image ===");
let pull_result = nerdctl::image_pull("nginx:latest")?;
println!("Pull output: {}", pull_result.stdout);
// Step 2: Create a container from the image
println!("\n=== Creating container from nginx:latest ===");
let run_result = nerdctl::run("nginx:latest", Some("my-nginx"), true, Some(&["8080:80"]))?;
println!("Container created: {}", run_result.stdout.trim());
let container_id = "my-nginx"; // Using the name we specified
// Step 3: Execute a command in the container
println!("\n=== Installing curl in container ===");
let update_result = nerdctl::exec(container_id, "apt-get update")?;
println!("Update output: {}", update_result.stdout);
let install_result = nerdctl::exec(container_id, "apt-get install -y curl")?;
println!("Installation output: {}", install_result.stdout);
// Step 4: Copy a file into the container (assuming nginx.conf exists)
println!("\n=== Copying configuration file to container ===");
nerdctl::copy("./nginx.conf", format!("{}:/etc/nginx/nginx.conf", container_id).as_str())?;
// Step 5: Commit the container to create a new image
println!("\n=== Committing container to create image ===");
let image_name = "my-custom-nginx:latest";
nerdctl::image_commit(container_id, image_name)?;
println!("Created image: {}", image_name);
// Step 6: Stop and remove the container
println!("\n=== Stopping and removing container ===");
nerdctl::stop(container_id)?;
nerdctl::remove(container_id)?;
println!("Container stopped and removed");
// Step 7: Create a new container from our custom image
println!("\n=== Creating container from custom image ===");
nerdctl::run(image_name, Some("nginx-custom"), true, Some(&["8081:80"]))?;
println!("Custom container created");
// Step 8: List images
println!("\n=== Listing images ===");
let images_result = nerdctl::images()?;
println!("Images: \n{}", images_result.stdout);
// Step 9: Clean up (optional in a real workflow)
println!("\n=== Cleaning up ===");
nerdctl::stop("nginx-custom")?;
nerdctl::remove("nginx-custom")?;
nerdctl::image_remove(image_name)?;
println!("\nNerdctl example workflow completed successfully!");
Ok(())
}
/// Main function to run all examples
pub fn run_all_examples() -> Result<(), NerdctlError> {
println!("=== NERDCTL MODULE EXAMPLES ===\n");
run_nerdctl_example()?;
println!("\nNote that these examples require nerdctl to be installed on your system");
println!("and may require root/sudo privileges depending on your setup.");
Ok(())
}
fn main() {
run_all_examples();
}