Skip to content

Commit

Permalink
feat: add name parameter to RunnableImage (#549)
Browse files Browse the repository at this point in the history
This change allows to override name of the image. Thus, user will be
able to change registry, owner and the name itself.
See
#335 (comment)
for more details

Closes #335
  • Loading branch information
DDtKey authored Apr 19, 2024
1 parent 9039256 commit b68e2b3
Showing 1 changed file with 18 additions and 5 deletions.
23 changes: 18 additions & 5 deletions testcontainers/src/core/image.rs
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,7 @@ impl Display for Host {
pub struct RunnableImage<I: Image> {
image: I,
image_args: I::Args,
image_name: Option<String>,
image_tag: Option<String>,
container_name: Option<String>,
network: Option<String>,
Expand Down Expand Up @@ -228,11 +229,13 @@ impl<I: Image> RunnableImage<I> {
}

pub fn descriptor(&self) -> String {
if let Some(tag) = &self.image_tag {
format!("{}:{tag}", self.image.name())
} else {
format!("{}:{}", self.image.name(), self.image.tag())
}
let original_name = self.image.name();
let original_tag = self.image.tag();

let name = self.image_name.as_ref().unwrap_or(&original_name);
let tag = self.image_tag.as_ref().unwrap_or(&original_tag);

format!("{name}:{tag}")
}

pub fn ready_conditions(&self) -> Vec<WaitFor> {
Expand Down Expand Up @@ -272,6 +275,15 @@ impl<I: Image> RunnableImage<I> {
}
}

/// Overrides the fully qualified image name (consists of `{domain}/{owner}/{image}`).
/// Can be used to specify a custom registry or owner.
pub fn with_name(self, name: impl Into<String>) -> Self {
Self {
image_name: Some(name.into()),
..self
}
}

/// 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.
pub fn with_tag(self, tag: impl Into<String>) -> Self {
Expand Down Expand Up @@ -350,6 +362,7 @@ impl<I: Image> From<(I, I::Args)> for RunnableImage<I> {
Self {
image,
image_args,
image_name: None,
image_tag: None,
container_name: None,
network: None,
Expand Down

0 comments on commit b68e2b3

Please sign in to comment.