sal/src/rhaiexamples/nerdctl_test.rhai
2025-04-05 06:20:19 +02:00

188 lines
6.0 KiB
Plaintext

// nerdctl_test.rhai
// Tests the nerdctl wrapper functionality without requiring a running containerd daemon
// Check if nerdctl is installed
let nerdctl_exists = which("nerdctl");
println(`Nerdctl exists: ${nerdctl_exists}`);
// Test creating run options
println("\nTesting run options creation:");
let run_options = new_run_options();
println(`Default run options created: ${run_options}`);
println(`- name: ${run_options.name}`);
println(`- detach: ${run_options.detach}`);
println(`- ports: ${run_options.ports}`);
println(`- snapshotter: ${run_options.snapshotter}`);
// Modify run options
println("\nModifying run options:");
run_options.name = "test-container";
run_options.detach = false;
run_options.ports = ["8080:80", "8443:443"];
run_options.snapshotter = "overlayfs";
println(`Modified run options: ${run_options}`);
println(`- name: ${run_options.name}`);
println(`- detach: ${run_options.detach}`);
println(`- ports: ${run_options.ports}`);
println(`- snapshotter: ${run_options.snapshotter}`);
// Test function availability
println("\nTesting function availability:");
let functions = [
// Legacy functions
"nerdctl_run",
"nerdctl_run_with_name",
"nerdctl_run_with_port",
"nerdctl_exec",
"nerdctl_copy",
"nerdctl_stop",
"nerdctl_remove",
"nerdctl_list",
"nerdctl_images",
"nerdctl_image_remove",
"nerdctl_image_push",
"nerdctl_image_tag",
"nerdctl_image_pull",
"nerdctl_image_commit",
"nerdctl_image_build",
// Builder pattern functions
"nerdctl_container_new",
"nerdctl_container_from_image",
"with_port",
"with_volume",
"with_env",
"with_network",
"with_network_alias",
"with_cpu_limit",
"with_memory_limit",
"with_restart_policy",
"with_health_check",
"with_detach",
"build",
"start",
"stop",
"remove",
"exec",
"copy"
];
// Try to access each function (this will throw an error if the function doesn't exist)
for func in functions {
let exists = is_function_registered(func);
println(`Function ${func} registered: ${exists}`);
}
// Helper function to get current timestamp in seconds
fn timestamp() {
// Use the current time in seconds since epoch as a unique identifier
return now();
}
// Test the builder pattern with actual container creation and execution
println("\nTesting container builder pattern with actual container:");
try {
// Generate a unique container name based on timestamp
let container_name = "test-alpine-container";
// First, try to remove any existing container with this name
println(`Cleaning up any existing container named '${container_name}'...`);
try {
// Try to stop the container first (in case it's running)
nerdctl_stop(container_name);
println("Stopped existing container");
} catch(e) {
println("No running container to stop");
}
try {
// Try to remove the container
nerdctl_remove(container_name);
println("Removed existing container");
} catch(e) {
println("No container to remove");
}
// Create a container with builder pattern using Alpine image with a command that keeps it running
println("\nCreating new container from Alpine image...");
let container = nerdctl_container_from_image(container_name, "alpine:latest");
println(`Created container from image: ${container.name} (${container.image})`);
// Configure the container
container = container
.with_port("8080:80")
.with_volume("/tmp:/data")
.with_env("TEST_ENV", "test_value")
.with_detach(true);
// Print container properties before building
println("Container properties before building:");
println(`- name: ${container.name}`);
println(`- image: ${container.image}`);
println(`- ports: ${container.ports}`);
println(`- volumes: ${container.volumes}`);
println(`- detach: ${container.detach}`);
// Build the container
println("\nBuilding the container...");
container = container.build();
// Print container ID after building
println(`Container built successfully with ID: ${container.container_id}`);
// Start the container
println("\nStarting the container...");
let start_result = container.start();
println(`Start result: ${start_result.success}`);
// Execute a command in the running container
println("\nExecuting command in the container...");
let run_cmd = container.exec("echo 'Hello from Alpine container'");
println(`Command output: ${run_cmd.stdout}`);
// List all containers to verify it's running
println("\nListing all containers:");
let list_result = nerdctl_list(true);
println(`Container list: ${list_result.stdout}`);
// Stop and remove the container
println("\nStopping and removing the container...");
let stop_result = container.stop();
println(`Stop result: ${stop_result.success}`);
let remove_result = container.remove();
println(`Remove result: ${remove_result.success}`);
println("Container stopped and removed successfully");
// Return success message only if everything worked
return "Container builder pattern test completed successfully!";
} catch(e) {
// Print the error and return a failure message
println(`ERROR: ${e}`);
// Try to clean up if possible
try {
println("Attempting to clean up after error...");
nerdctl_stop("test-alpine-container");
nerdctl_remove("test-alpine-container");
println("Cleanup completed");
} catch(cleanup_error) {
println(`Cleanup failed: ${cleanup_error}`);
}
// Return failure message
return "Container builder pattern test FAILED!";
}
// Helper function to check if a function is registered
fn is_function_registered(name) {
try {
// This will throw an error if the function doesn't exist
eval(`${name}`);
return true;
} catch {
return false;
}
}
// The final result depends on the outcome of the container test