added test for everything container related

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

1
example.conf Normal file
View File

@ -0,0 +1 @@
EXAMPLE FILE TO TEST

8
example_Dockerfile Normal file
View File

@ -0,0 +1,8 @@
# syntax=docker/dockerfile:1
FROM node:lts-alpine
WORKDIR /app
COPY . .
RUN yarn install --production
CMD ["node", "src/index.js"]
EXPOSE 3000

View File

@ -20,51 +20,47 @@ pub fn run_buildah_example() -> Result<(), BuildahError> {
// Step 2: Run a command in the container // Step 2: Run a command in the container
println!("\n=== Installing nginx in container ==="); println!("\n=== Installing nginx in container ===");
// Use chroot isolation to avoid BPF issues // 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!("{:#?}", install_result);
println!("Installation output: {}", install_result.stdout); println!("Installation output: {}", install_result.stdout);
// // Step 3: Copy a file into the container // Step 3: Copy a file into the container
// println!("\n=== Copying configuration file to 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").unwrap();
// buildah::copy(container_id, "./example.conf", "/etc/example.conf")?;
// println!("For a real example, you would copy a configuration file here");
// // Step 4: Configure container metadata // Step 4: Configure container metadata
// println!("\n=== Configuring container metadata ==="); println!("\n=== Configuring container metadata ===");
// let mut config_options = HashMap::new(); let mut config_options = HashMap::new();
// config_options.insert("port".to_string(), "80".to_string()); config_options.insert("port".to_string(), "80".to_string());
// config_options.insert("label".to_string(), "maintainer=example@example.com".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()); config_options.insert("entrypoint".to_string(), "/usr/sbin/nginx".to_string());
// buildah::config(container_id, config_options)?; buildah::config(container_id, config_options)?;
// println!("Container configured"); println!("Container configured");
// // Step 5: Commit the container to create a new image // Step 5: Commit the container to create a new image
// println!("\n=== Committing container to create image ==="); println!("\n=== Committing container to create image ===");
// let image_name = "my-nginx:latest"; let image_name = "my-nginx:latest";
// buildah::image_commit(container_id, image_name, Some("docker"), true, true)?; buildah::image_commit(container_id, image_name, Some("docker"), true, true)?;
// println!("Created image: {}", image_name); println!("Created image: {}", image_name);
// // Step 6: List images to verify our new image exists // Step 6: List images to verify our new image exists
// println!("\n=== Listing images ==="); println!("\n=== Listing images ===");
// let images = buildah::images()?; let images = buildah::images()?;
// println!("Found {} images:", images.len()); println!("Found {} images:", images.len());
// for image in images { for image in images {
// println!(" ID: {}", image.id); println!(" ID: {}", image.id);
// println!(" Names: {}", image.names.join(", ")); println!(" Names: {}", image.names.join(", "));
// println!(" Size: {}", image.size); println!(" Size: {}", image.size);
// println!(" Created: {}", image.created); println!(" Created: {}", image.created);
// println!(); println!();
// } }
// // Step 7: Clean up (optional in a real workflow) // // Step 7: Clean up (optional in a real workflow)
// println!("\n=== Cleaning up ==="); println!("\n=== Cleaning up ===");
// println!("In a real workflow, you might want to keep the image"); buildah::image_remove(image_name).unwrap();
// println!("To remove the image, you would run:");
// println!("buildah::image_remove(\"{}\")", image_name);
// println!("\nBuildah example workflow completed successfully!"); println!("\nBuildah example workflow completed successfully!");
Ok(()) Ok(())
} }
@ -72,12 +68,8 @@ pub fn run_buildah_example() -> Result<(), BuildahError> {
pub fn build_image_example() -> Result<(), BuildahError> { pub fn build_image_example() -> Result<(), BuildahError> {
println!("Building an image from a Containerfile..."); println!("Building an image from a Containerfile...");
// This would typically use a command like: // Use the build function with tag, context directory, and isolation to avoid BPF issues
// buildah build -t my-app:latest . let result = buildah::build(Some("my-app:latest"), ".", "example_Dockerfile", Some("chroot"))?;
// 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)?;
println!("Build output: {}", result.stdout); println!("Build output: {}", result.stdout);
println!("Image built successfully!"); println!("Image built successfully!");
@ -100,9 +92,9 @@ pub fn registry_operations_example() -> Result<(), BuildahError> {
println!("Image tagged successfully"); println!("Image tagged successfully");
// Push an image (this would typically go to a real registry) // Push an image (this would typically go to a real registry)
println!("\n=== Pushing an image (example only) ==="); // println!("\n=== Pushing an image (example only) ===");
println!("In a real scenario, you would push to a registry with:"); // 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!("buildah::image_push(\"my-alpine:v1.0\", \"docker://registry.example.com/my-alpine:v1.0\", true)");
Ok(()) Ok(())
} }
@ -111,21 +103,13 @@ pub fn registry_operations_example() -> Result<(), BuildahError> {
pub fn run_all_examples() -> Result<(), BuildahError> { pub fn run_all_examples() -> Result<(), BuildahError> {
println!("=== BUILDAH MODULE EXAMPLES ===\n"); 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()?; run_buildah_example()?;
// build_image_example()?; build_image_example()?;
// registry_operations_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.");
Ok(()) Ok(())
} }
fn main() { 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();
}

View File

@ -52,3 +52,33 @@ pub fn remove(container: &str) -> Result<CommandResult, BuildahError> {
pub fn list() -> Result<CommandResult, BuildahError> { pub fn list() -> Result<CommandResult, BuildahError> {
execute_buildah_command(&["containers"]) execute_buildah_command(&["containers"])
} }
/// Build an image from a Containerfile/Dockerfile
///
/// # Arguments
///
/// * `tag` - Optional tag for the image (e.g., "my-app:latest")
/// * `context_dir` - The directory containing the Containerfile/Dockerfile (usually ".")
/// * `file` - Optional path to a specific Containerfile/Dockerfile
/// * `isolation` - Optional isolation method (e.g., "chroot", "rootless", "oci")
pub fn build(tag: Option<&str>, context_dir: &str, file: &str, isolation: Option<&str>) -> Result<CommandResult, BuildahError> {
let mut args = Vec::new();
args.push("build");
if let Some(tag_value) = tag {
args.push("-t");
args.push(tag_value);
}
if let Some(isolation_value) = isolation {
args.push("--isolation");
args.push(isolation_value);
}
args.push("-f");
args.push(file);
args.push(context_dir);
execute_buildah_command(&args)
}

View File

@ -91,6 +91,25 @@ mod tests {
test_execute_buildah_command(&["containers"]) test_execute_buildah_command(&["containers"])
} }
fn test_build(tag: Option<&str>, context_dir: &str, file: Option<&str>) -> Result<CommandResult, BuildahError> {
let mut args = Vec::new();
args.push("build");
if let Some(tag_value) = tag {
args.push("-t");
args.push(tag_value);
}
if let Some(file_path) = file {
args.push("-f");
args.push(file_path);
}
args.push(context_dir);
test_execute_buildah_command(&args)
}
// Tests for each function // Tests for each function
#[test] #[test]
fn test_from_function() { fn test_from_function() {
@ -188,6 +207,29 @@ mod tests {
assert_eq!(cmd, vec!["containers"]); assert_eq!(cmd, vec!["containers"]);
} }
#[test]
fn test_build_function() {
reset_test_state();
// Test with tag and context directory
let result = test_build(Some("my-app:latest"), ".", None);
assert!(result.is_ok());
let cmd = get_last_command();
assert_eq!(cmd, vec!["build", "-t", "my-app:latest", "."]);
// Test with tag, context directory, and file
let result = test_build(Some("my-app:latest"), ".", Some("Dockerfile.custom"));
assert!(result.is_ok());
let cmd = get_last_command();
assert_eq!(cmd, vec!["build", "-t", "my-app:latest", "-f", "Dockerfile.custom", "."]);
// Test with just context directory
let result = test_build(None, ".", None);
assert!(result.is_ok());
let cmd = get_last_command();
assert_eq!(cmd, vec!["build", "."]);
}
#[test] #[test]
fn test_error_handling() { fn test_error_handling() {
reset_test_state(); reset_test_state();

View File

@ -23,7 +23,7 @@ pub struct Image {
/// # Returns /// # Returns
/// * Result with array of Image objects on success or error details /// * Result with array of Image objects on success or error details
pub fn images() -> Result<Vec<Image>, BuildahError> { pub fn images() -> Result<Vec<Image>, BuildahError> {
let result = execute_buildah_command(&["images", "--format", "json"])?; let result = execute_buildah_command(&["images", "--json"])?;
// Try to parse the JSON output // Try to parse the JSON output
match serde_json::from_str::<serde_json::Value>(&result.stdout) { match serde_json::from_str::<serde_json::Value>(&result.stdout) {

View File

@ -1 +1,2 @@
pub mod buildah; pub mod buildah;
pub mod nerdctl;