- Added documentation for Nerdctl module tests, detailing test structure, running instructions, and individual test cases. - Added documentation for RFS module tests, covering test structure, running instructions, and individual test cases. These tests verify remote filesystem operations and filesystem layer management.
4.7 KiB
Nerdctl Module Tests
This document describes the test scripts for the Nerdctl module in the SAL library. These tests verify the functionality of the Nerdctl module's container and image operations.
Test Structure
The tests are organized into three main scripts:
- Container Operations (
01_container_operations.rhai
): Tests for basic container operations like creating, running, executing commands, and removing containers. - Image Operations (
02_image_operations.rhai
): Tests for image-related operations like pulling, tagging, listing, building, and removing images. - Container Builder Pattern (
03_container_builder.rhai
): Tests for the Container Builder pattern, which provides a fluent interface for configuring and running containers.
Additionally, there's a runner script (run_all_tests.rhai
) that executes all tests and reports results. The runner script contains simplified versions of the individual tests to avoid dependency issues.
Running the Tests
To run all tests, execute the following command from the project root:
herodo --path src/rhai_tests/nerdctl/run_all_tests.rhai
To run individual test scripts:
herodo --path src/rhai_tests/nerdctl/01_container_operations.rhai
Test Details
Container Operations Test
The Container Operations test (01_container_operations.rhai
) verifies the following functions:
nerdctl_container_new
: Creating a new Container- Container properties:
name
,container_id
,image
,detach
with_image
: Setting the container imagewith_detach
: Setting detach modewith_env
andwith_envs
: Setting environment variableswith_port
andwith_ports
: Setting port mappingswith_volume
: Setting volume mountswith_cpu_limit
andwith_memory_limit
: Setting resource limitsrun
: Running the containerexec
: Executing commands in the containerlogs
: Getting container logsstop
: Stopping the containerremove
: Removing the container
Image Operations Test
The Image Operations test (02_image_operations.rhai
) verifies the following functions:
nerdctl_image_pull
: Pulling images from registriesnerdctl_images
: Listing imagesnerdctl_image_tag
: Tagging imagesnerdctl_image_build
: Building images from Dockerfilesnerdctl_run_with_name
: Running containers from imagesnerdctl_stop
andnerdctl_remove
: Stopping and removing containersnerdctl_image_remove
: Removing images
The test creates a temporary directory with a Dockerfile for testing the build functionality.
Container Builder Pattern Test
The Container Builder Pattern test (03_container_builder.rhai
) verifies the following functions:
nerdctl_container_from_image
: Creating a container from an imagereset
: Resetting container configurationwith_detach
: Setting detach modewith_ports
: Setting multiple port mappingswith_volumes
: Setting multiple volume mountswith_envs
: Setting multiple environment variableswith_network
: Setting networkwith_cpu_limit
andwith_memory_limit
: Setting resource limitsrun
: Running the containerexec
: Executing commands in the containerstop
: Stopping the containerremove
: Removing the container
The test also verifies that environment variables and volume mounts work correctly by writing and reading files between the container and the host.
Test Runner
The test runner script (run_all_tests.rhai
) provides a framework for executing all tests and reporting results. It:
- Checks if nerdctl is available before running tests
- Skips tests if nerdctl is not available
- Contains simplified versions of each test
- Runs each test in a try/catch block to handle errors
- Catches and reports any errors
- Provides a summary of passed, failed, and skipped tests
Nerdctl Requirements
These tests require the nerdctl tool to be installed and available in the system's PATH. The tests will check for nerdctl's availability and skip the tests if it's not found, rather than failing.
Adding New Tests
To add a new test:
- Create a new Rhai script in the
src/rhai_tests/nerdctl
directory - Add a new test section to the
run_all_tests.rhai
script - Update this documentation to include information about the new test
Best Practices for Writing Tests
When writing tests for the Nerdctl module:
- Always check if nerdctl is available before running tests
- Use unique names for containers and images to avoid conflicts
- Clean up any containers, images, or files created during testing
- Use assertions to verify expected behavior
- Print clear messages about what's being tested
- Handle errors gracefully
- Make tests independent of each other
- Keep tests focused on specific functionality