-
Notifications
You must be signed in to change notification settings - Fork 144
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor!: add
ImageExt
trait to avoid explicit conversion to `Runn…
…ableImage` (#652) This PR allows to override some image values without explicit conversion to `RunnableImage`. For example: ```rs // Instead of this: use testcontainers::RunnableImage; RunnableImage::from(image).with_tag("x").with_network("a").start(); // It can be done directly on image: use testcontainers::ImageExt; image.with_tag("x").with_network("a").start(); ``` Also, it allows to simplify `GenericImage`. Because it was kinda duplicative code.
- Loading branch information
Showing
13 changed files
with
255 additions
and
287 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,184 @@ | ||
use std::time::Duration; | ||
|
||
use crate::{ | ||
core::{CgroupnsMode, Host, Mount, PortMapping}, | ||
Image, RunnableImage, | ||
}; | ||
|
||
pub trait ImageExt<I: Image> { | ||
/// Returns a new RunnableImage with the specified (overridden) `CMD` ([`Image::cmd`]). | ||
/// | ||
/// # Examples | ||
/// ```rust,no_run | ||
/// use testcontainers::{GenericImage, ImageExt}; | ||
/// | ||
/// let image = GenericImage::new("image", "tag"); | ||
/// let cmd = ["arg1", "arg2"]; | ||
/// let overridden_cmd = image.clone().with_cmd(cmd); | ||
/// | ||
/// assert!(overridden_cmd.cmd().eq(cmd)); | ||
/// | ||
/// let another_runnable_image = image.with_cmd(cmd); | ||
/// | ||
/// assert!(another_runnable_image.cmd().eq(overridden_cmd.cmd())); | ||
/// ``` | ||
fn with_cmd(self, cmd: impl IntoIterator<Item = impl Into<String>>) -> RunnableImage<I>; | ||
|
||
/// Overrides the fully qualified image name (consists of `{domain}/{owner}/{image}`). | ||
/// Can be used to specify a custom registry or owner. | ||
fn with_name(self, name: impl Into<String>) -> RunnableImage<I>; | ||
|
||
/// Overrides the image tag. | ||
/// | ||
/// There is no guarantee that the specified tag for an image would result in a | ||
/// running container. Users of this API are advised to use this at their own risk. | ||
fn with_tag(self, tag: impl Into<String>) -> RunnableImage<I>; | ||
|
||
/// Sets the container name. | ||
fn with_container_name(self, name: impl Into<String>) -> RunnableImage<I>; | ||
|
||
/// Sets the network the container will be connected to. | ||
fn with_network(self, network: impl Into<String>) -> RunnableImage<I>; | ||
|
||
/// Adds an environment variable to the container. | ||
fn with_env_var(self, name: impl Into<String>, value: impl Into<String>) -> RunnableImage<I>; | ||
|
||
/// Adds a host to the container. | ||
fn with_host(self, key: impl Into<String>, value: impl Into<Host>) -> RunnableImage<I>; | ||
|
||
/// Adds a mount to the container. | ||
fn with_mount(self, mount: impl Into<Mount>) -> RunnableImage<I>; | ||
|
||
/// Adds a port mapping to the container. | ||
fn with_mapped_port<P: Into<PortMapping>>(self, port: P) -> RunnableImage<I>; | ||
|
||
/// Sets the container to run in privileged mode. | ||
fn with_privileged(self, privileged: bool) -> RunnableImage<I>; | ||
|
||
/// cgroup namespace mode for the container. Possible values are: | ||
/// - [`CgroupnsMode::Private`]: the container runs in its own private cgroup namespace | ||
/// - [`CgroupnsMode::Host`]: use the host system's cgroup namespace | ||
/// If not specified, the daemon default is used, which can either be `\"private\"` or `\"host\"`, depending on daemon version, kernel support and configuration. | ||
Check warning on line 61 in testcontainers/src/core/image/image_ext.rs
|
||
fn with_cgroupns_mode(self, cgroupns_mode: CgroupnsMode) -> RunnableImage<I>; | ||
|
||
/// Sets the usernamespace mode for the container when usernamespace remapping option is enabled. | ||
fn with_userns_mode(self, userns_mode: &str) -> RunnableImage<I>; | ||
|
||
/// Sets the shared memory size in bytes | ||
fn with_shm_size(self, bytes: u64) -> RunnableImage<I>; | ||
|
||
/// Sets the startup timeout for the container. The default is 60 seconds. | ||
fn with_startup_timeout(self, timeout: Duration) -> RunnableImage<I>; | ||
} | ||
|
||
impl<RI: Into<RunnableImage<I>>, I: Image> ImageExt<I> for RI { | ||
fn with_cmd(self, cmd: impl IntoIterator<Item = impl Into<String>>) -> RunnableImage<I> { | ||
let runnable = self.into(); | ||
RunnableImage { | ||
overridden_cmd: cmd.into_iter().map(Into::into).collect(), | ||
..runnable | ||
} | ||
} | ||
|
||
fn with_name(self, name: impl Into<String>) -> RunnableImage<I> { | ||
let runnable = self.into(); | ||
RunnableImage { | ||
image_name: Some(name.into()), | ||
..runnable | ||
} | ||
} | ||
|
||
fn with_tag(self, tag: impl Into<String>) -> RunnableImage<I> { | ||
let runnable = self.into(); | ||
RunnableImage { | ||
image_tag: Some(tag.into()), | ||
..runnable | ||
} | ||
} | ||
|
||
fn with_container_name(self, name: impl Into<String>) -> RunnableImage<I> { | ||
let runnable = self.into(); | ||
|
||
RunnableImage { | ||
container_name: Some(name.into()), | ||
..runnable | ||
} | ||
} | ||
|
||
fn with_network(self, network: impl Into<String>) -> RunnableImage<I> { | ||
let runnable = self.into(); | ||
RunnableImage { | ||
network: Some(network.into()), | ||
..runnable | ||
} | ||
} | ||
|
||
fn with_env_var(self, name: impl Into<String>, value: impl Into<String>) -> RunnableImage<I> { | ||
let mut runnable = self.into(); | ||
runnable.env_vars.insert(name.into(), value.into()); | ||
runnable | ||
} | ||
|
||
fn with_host(self, key: impl Into<String>, value: impl Into<Host>) -> RunnableImage<I> { | ||
let mut runnable = self.into(); | ||
runnable.hosts.insert(key.into(), value.into()); | ||
runnable | ||
} | ||
|
||
fn with_mount(self, mount: impl Into<Mount>) -> RunnableImage<I> { | ||
let mut runnable = self.into(); | ||
runnable.mounts.push(mount.into()); | ||
runnable | ||
} | ||
|
||
fn with_mapped_port<P: Into<PortMapping>>(self, port: P) -> RunnableImage<I> { | ||
let runnable = self.into(); | ||
let mut ports = runnable.ports.unwrap_or_default(); | ||
ports.push(port.into()); | ||
|
||
RunnableImage { | ||
ports: Some(ports), | ||
..runnable | ||
} | ||
} | ||
|
||
fn with_privileged(self, privileged: bool) -> RunnableImage<I> { | ||
let runnable = self.into(); | ||
RunnableImage { | ||
privileged, | ||
..runnable | ||
} | ||
} | ||
|
||
fn with_cgroupns_mode(self, cgroupns_mode: CgroupnsMode) -> RunnableImage<I> { | ||
let runnable = self.into(); | ||
RunnableImage { | ||
cgroupns_mode: Some(cgroupns_mode), | ||
..runnable | ||
} | ||
} | ||
|
||
fn with_userns_mode(self, userns_mode: &str) -> RunnableImage<I> { | ||
let runnable = self.into(); | ||
RunnableImage { | ||
userns_mode: Some(String::from(userns_mode)), | ||
..runnable | ||
} | ||
} | ||
|
||
fn with_shm_size(self, bytes: u64) -> RunnableImage<I> { | ||
let runnable = self.into(); | ||
RunnableImage { | ||
shm_size: Some(bytes), | ||
..runnable | ||
} | ||
} | ||
|
||
fn with_startup_timeout(self, timeout: Duration) -> RunnableImage<I> { | ||
let runnable = self.into(); | ||
RunnableImage { | ||
startup_timeout: Some(timeout), | ||
..runnable | ||
} | ||
} | ||
} |
Oops, something went wrong.