Struct libcnb_test::ContainerContext
source · pub struct ContainerContext<'a> {
pub container_name: String,
/* private fields */
}Expand description
Context of a launched container.
Fields§
§container_name: StringThe randomly generated name of this container.
Implementations§
source§impl<'a> ContainerContext<'a>
impl<'a> ContainerContext<'a>
sourcepub fn logs_now(&self) -> LogOutput
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", "test-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");
});
},
);sourcepub fn logs_wait(&self) -> LogOutput
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", "test-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");
});
},
);sourcepub fn address_for_port(&self, port: u16) -> Option<SocketAddr>
pub fn address_for_port(&self, port: u16) -> Option<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", "test-fixtures/app"),
|context| {
// ...
context.start_container(
ContainerConfig::new().env("PORT", "12345").expose_port(12345),
|container| {
let address_on_host = container.address_for_port(12345).unwrap();
// ...
},
);
},
);sourcepub fn shell_exec(&self, command: impl AsRef<str>) -> LogOutput
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", "test-fixtures/app"),
|context| {
// ...
context.start_container(ContainerConfig::new(), |container| {
let log_output = container.shell_exec("ps");
assert_contains!(log_output.stdout, "gunicorn");
});
},
);