diff --git a/CHANGELOG.md b/CHANGELOG.md index 52af3c01..abe7afeb 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -9,7 +9,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ### Added -- Added `name` parameter to `RunnableImage` +- Added optional `registry` and `owner` parameters to `Image` +- Added `name`, `registry` and `owner` parameters to `RunnableImage` ## [0.15.0] - 2023-09-28 diff --git a/testcontainers/src/core/image.rs b/testcontainers/src/core/image.rs index 20ca9746..3a00a591 100644 --- a/testcontainers/src/core/image.rs +++ b/testcontainers/src/core/image.rs @@ -27,7 +27,7 @@ where /// testing. Only expose those that actually make sense for this case. type Args; - /// The name of the docker image to pull from the Docker Hub registry. + /// The name of the image to pull from a registry. fn name(&self) -> String; /// Implementations are encouraged to include a tag that will not change (i.e. NOT latest) @@ -35,6 +35,18 @@ where /// suddenly changed. fn tag(&self) -> String; + /// The registry to use for the image descriptor. Optional, by default it uses DockerHub. + /// Will be prepended to image descriptor if present. E.g: `{registry}/{owner}/{name}` + fn registry(&self) -> Option { + None + } + + /// The owner of the image in a registry to use for the image descriptor. + /// Optional, will be prepended to image descriptor if present (before `registry`). E.g: `{registry}/{owner}/{name}` + fn owner(&self) -> Option { + None + } + /// Returns a list of conditions that need to be met before a started container is considered ready. /// /// This method is the **🍞 and butter** of the whole testcontainers library. Containers are @@ -151,6 +163,8 @@ pub struct RunnableImage { image_args: I::Args, image_name: Option, image_tag: Option, + image_registry: Option, + image_owner: Option, container_name: Option, network: Option, env_vars: BTreeMap, @@ -203,13 +217,28 @@ impl RunnableImage { } pub fn descriptor(&self) -> String { + let original_registry = self.image.registry(); + let original_owner = self.image.owner(); let original_name = self.image.name(); let original_tag = self.image.tag(); + let registry = self + .image_registry + .as_ref() + .or(original_registry.as_ref()) + .map(|r| format!("{}/", r)) + .unwrap_or_default(); + let owner = self + .image_owner + .as_ref() + .or(original_owner.as_ref()) + .map(|o| format!("{}/", o)) + .unwrap_or_default(); + 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}") + format!("{registry}{owner}{name}:{tag}") } pub fn ready_conditions(&self) -> Vec { @@ -226,7 +255,6 @@ impl RunnableImage { } impl RunnableImage { - /// Overrides the image name. Can be used to specify a custom registry or owner (e.g `{domain}/{owner}/{image}`). pub fn with_name(self, name: impl Into) -> Self { Self { image_name: Some(name.into()), @@ -243,6 +271,20 @@ impl RunnableImage { } } + pub fn with_registry(self, registry: impl Into) -> Self { + Self { + image_registry: Some(registry.into()), + ..self + } + } + + pub fn with_owner(self, owner: impl Into) -> Self { + Self { + image_owner: Some(owner.into()), + ..self + } + } + pub fn with_container_name(self, name: impl Into) -> Self { Self { container_name: Some(name.into()), @@ -308,6 +350,8 @@ impl From<(I, I::Args)> for RunnableImage { image_args, image_name: None, image_tag: None, + image_registry: None, + image_owner: None, container_name: None, network: None, env_vars: BTreeMap::default(),