ContainerGuardBuilder

Struct ContainerGuardBuilder 

Source
pub struct ContainerGuardBuilder<T: Template> { /* private fields */ }
Expand description

Builder for creating a ContainerGuard with custom options.

Implementations§

Source§

impl<T: Template> ContainerGuardBuilder<T>

Source

pub fn new(template: T) -> Self

Create a new builder with the given template.

Source

pub fn remove_on_drop(self, remove: bool) -> Self

Set whether to remove the container on drop (default: true).

Source

pub fn stop_on_drop(self, stop: bool) -> Self

Set whether to stop the container on drop (default: true).

Source

pub fn keep_on_panic(self, keep: bool) -> Self

Set whether to keep the container running if the test panics (default: false).

This is useful for debugging failed tests - you can inspect the container state after the test fails.

Source

pub fn capture_logs(self, capture: bool) -> Self

Set whether to capture container logs and print them on panic (default: false).

When enabled, container logs are buffered and printed to stderr if the test panics, making it easier to debug failures.

Source

pub fn reuse_if_running(self, reuse: bool) -> Self

Set whether to reuse an existing container if already running (default: false).

This is useful for faster local development iteration - containers can be kept running between test runs.

Source

pub fn wait_for_ready(self, wait: bool) -> Self

Set whether to automatically wait for the container to be ready after starting (default: false).

When enabled, start() will not return until the container passes its readiness check. This is useful for tests that need to immediately connect to the service.

§Example
let guard = ContainerGuard::new(RedisTemplate::new("test"))
    .wait_for_ready(true)
    .start()
    .await?;
// Container is guaranteed ready at this point
Source

pub fn with_network(self, network: impl Into<String>) -> Self

Attach the container to a Docker network.

By default, the network will be created if it doesn’t exist. Use create_network(false) to disable automatic network creation.

§Example
let guard = ContainerGuard::new(RedisTemplate::new("redis"))
    .with_network("test-network")
    .start()
    .await?;
// Container is attached to "test-network"
Source

pub fn create_network(self, create: bool) -> Self

Set whether to create the network if it doesn’t exist (default: true).

Only applies when a network is specified via with_network().

Source

pub fn remove_network_on_drop(self, remove: bool) -> Self

Set whether to remove the network on drop (default: false).

This is useful for cleaning up test-specific networks. Only applies when a network is specified via with_network().

Note: The network removal will fail silently if other containers are still using it.

Source

pub fn stop_timeout(self, timeout: Duration) -> Self

Set the timeout for stop operations during cleanup (default: Docker default).

This controls how long Docker waits for the container to stop gracefully before sending SIGKILL.

§Example
// Fast cleanup with 1 second timeout
let guard = ContainerGuard::new(RedisTemplate::new("redis"))
    .stop_timeout(Duration::from_secs(1))
    .start()
    .await?;

// Immediate SIGKILL with zero timeout
let guard = ContainerGuard::new(RedisTemplate::new("redis2"))
    .stop_timeout(Duration::ZERO)
    .start()
    .await?;
Source

pub async fn start(self) -> Result<ContainerGuard<T>, TemplateError>

Start the container and return a guard that manages its lifecycle.

If reuse_if_running is enabled and a container is already running, it will be reused instead of starting a new one.

If wait_for_ready is enabled, this method will block until the container passes its readiness check.

If a network is specified via with_network(), the container will be attached to that network. The network will be created if it doesn’t exist (unless create_network(false) was called).

§Errors

Returns an error if the container fails to start or the readiness check times out.

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> PolicyExt for T
where T: ?Sized,

Source§

fn and<P, B, E>(self, other: P) -> And<T, P>
where T: Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns Action::Follow only if self and other return Action::Follow. Read more
Source§

fn or<P, B, E>(self, other: P) -> Or<T, P>
where T: Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns Action::Follow if either self or other returns Action::Follow. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more