Struct libcnb_test::ContainerContext

source ·
pub struct ContainerContext {
    pub container_name: String,
    /* private fields */
}
Expand description

Context of a launched container.

Fields§

§container_name: String

The randomly generated name of this container.

Implementations§

source§

impl ContainerContext

source

pub fn logs_now(&self) -> LogOutput

Gets the container’s log output until the current point in time.

Note: This method will only return logs until the current point in time. It will not block until the container stops. Since the output of this method depends on timing, directly asserting on its contents might result in flaky tests.

See: logs_wait for a blocking alternative.

§Example
use libcnb_test::{assert_contains, assert_empty, BuildConfig, ContainerConfig, TestRunner};

TestRunner::default().build(
    BuildConfig::new("heroku/builder:22", "tests/fixtures/app"),
    |context| {
        // ...
        context.start_container(ContainerConfig::new(), |container| {
            let log_output_until_now = container.logs_now();
            assert_empty!(log_output_until_now.stderr);
            assert_contains!(log_output_until_now.stdout, "Expected output");
        });
    },
);
§Panics

Panics if there was an error retrieving the logs from the container.

source

pub fn logs_wait(&self) -> LogOutput

Gets the container’s log output until the container stops.

Note: This method will block until the container stops. If the container never stops by itself, your test will hang indefinitely. This is common when the container hosts an HTTP service.

See: logs_now for a non-blocking alternative.

§Example
use libcnb_test::{assert_contains, assert_empty, BuildConfig, ContainerConfig, TestRunner};

TestRunner::default().build(
    BuildConfig::new("heroku/builder:22", "tests/fixtures/app"),
    |context| {
        // ...
        context.start_container(ContainerConfig::new(), |container| {
            let all_log_output = container.logs_wait();
            assert_empty!(all_log_output.stderr);
            assert_contains!(all_log_output.stdout, "Expected output");
        });
    },
);
§Panics

Panics if there was an error retrieving the logs from the container.

source

pub fn address_for_port(&self, port: u16) -> SocketAddr

Returns the local address of an exposed container port.

§Example
use libcnb_test::{BuildConfig, ContainerConfig, TestRunner};

TestRunner::default().build(
    BuildConfig::new("heroku/builder:22", "tests/fixtures/app"),
    |context| {
        // ...
        context.start_container(
            ContainerConfig::new()
                .env("PORT", "12345")
                .expose_port(12345),
            |container| {
                let address_on_host = container.address_for_port(12345);
                // ...
            },
        );
    },
);
§Panics

Will panic if there was an error obtaining the container port mapping, or the specified port was not exposed using ContainerConfig::expose_port.

source

pub fn shell_exec(&self, command: impl AsRef<str>) -> LogOutput

Executes a shell command inside an already running container.

§Example
use libcnb_test::{assert_contains, BuildConfig, ContainerConfig, TestRunner};

TestRunner::default().build(
    BuildConfig::new("heroku/builder:22", "tests/fixtures/app"),
    |context| {
        // ...
        context.start_container(ContainerConfig::new(), |container| {
            let log_output = container.shell_exec("ps");
            assert_contains!(log_output.stdout, "gunicorn");
        });
    },
);
§Panics

Panics if it was not possible to exec into the container, or if the command exited with a non-zero exit code.

Trait Implementations§

source§

impl Drop for ContainerContext

source§

fn drop(&mut self)

Executes the destructor for this type. Read more

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, 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> IntoEither for T

source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts self into a Left variant of Either<Self, Self> if into_left is true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts self into a Left variant of Either<Self, Self> if into_left(&self) returns true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
source§

impl<T> Pointable for T

source§

const ALIGN: usize = _

The alignment of pointer.
§

type Init = T

The type for initializers.
source§

unsafe fn init(init: <T as Pointable>::Init) -> usize

Initializes a with the given initializer. Read more
source§

unsafe fn deref<'a>(ptr: usize) -> &'a T

Dereferences the given pointer. Read more
source§

unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T

Mutably dereferences the given pointer. Read more
source§

unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. Read more
source§

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

§

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>,

§

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.