Crates.io | dockertest |
lib.rs | dockertest |
version | 0.5.0 |
source | src |
created_at | 2019-10-24 12:13:19.809422 |
updated_at | 2024-09-05 10:45:31.188935 |
description | A library to control docker containers when running your integration tests. |
homepage | https://www.orcalabs.no |
repository | https://github.com/orcalabs/dockertest-rs |
max_upload_size | |
id | 175208 |
size | 265,473 |
Control docker containers from a Rust integration test suite. Full fledge management support with automatic cleanup.
This crate provides the following features for your docker testing needs:
WaitFor
strategy.RunningContainer
in the test body through its ip
address.inject_container_name
into environment variables.See the crate documentation for extensive explanations.
This is a trivial example showing of the general structure of a naive test.
use dockertest::{DockerTest, TestBodySpecification};
use std::sync::{Arc, Mutex};
#[test]
fn hello_world_test() {
// Define our test instance, will pull images from dockerhub.
let mut test = DockerTest::new().with_default_source(DockerHub);
// Construct the container specification to be added to the test.
//
// A container specification can have multiple properties, depending on how the
// lifecycle management of the container should be handled by dockertest.
//
// For any container specification where dockertest needs to create and start the container,
// we must provide enough information to construct a composition of
// an Image configured with provided environment variables, arguments, StartPolicy, etc.
let hello = TestBodySpecification::with_repository("hello-world");
// Populate the test instance.
// The order of compositions added reflect the execution order (depending on StartPolicy).
test.provide_container(hello);
let has_ran: Arc<Mutex<bool>> = Arc::new(Mutex::new(false));
let has_ran_test = has_ran.clone();
test.run(|ops| async move {
// A handle to operate on the Container.
let _container = ops.handle("hello-world");
// The container is in a running state at this point.
// Depending on the Image, it may exit on its own (like this hello-world image)
let mut ran = has_ran_test.lock().unwrap();
*ran = true;
});
let ran = has_ran.lock().unwrap();
assert!(*ran);
}
Testing this library requires the following:
DOCKERTEST_BUILD_TEST_IMAGES=1
in your environment, will trigger the test images to be built.Tests are designed to be run in parallel, and should not conflict with existing system images.
Local images are build with repository prefix dockertest-rs/
.
To run dockertest
inside docker you will have to set the following environment variable:
DOCKERTEST_CONTAINER_ID_INJECT_TO_NETWORK=your_container_id/name
DOCKERTEST_CONTAINER_ID_INJECT_TO_NETWORK
has to be set to the ID or name of the container dockertest
is running in.