Skip to content

Commit

Permalink
feat: add registry and owner parameters to Image
Browse files Browse the repository at this point in the history
It's alternative solution for testcontainers#549

Adds ability to set registry and owner on per-Image basis, and override them with `RunnableImage`.
  • Loading branch information
DDtKey committed Feb 18, 2024
1 parent be0c157 commit 5800f6b
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 4 deletions.
3 changes: 2 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
50 changes: 47 additions & 3 deletions testcontainers/src/core/image.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,14 +27,26 @@ 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)
/// in order to prevent test code from randomly breaking because the underlying docker
/// 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<String> {
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<String> {
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
Expand Down Expand Up @@ -151,6 +163,8 @@ pub struct RunnableImage<I: Image> {
image_args: I::Args,
image_name: Option<String>,
image_tag: Option<String>,
image_registry: Option<String>,
image_owner: Option<String>,
container_name: Option<String>,
network: Option<String>,
env_vars: BTreeMap<String, String>,
Expand Down Expand Up @@ -203,13 +217,28 @@ impl<I: Image> RunnableImage<I> {
}

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<WaitFor> {
Expand All @@ -226,7 +255,6 @@ impl<I: Image> RunnableImage<I> {
}

impl<I: Image> RunnableImage<I> {
/// 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<String>) -> Self {
Self {
image_name: Some(name.into()),
Expand All @@ -243,6 +271,20 @@ impl<I: Image> RunnableImage<I> {
}
}

pub fn with_registry(self, registry: impl Into<String>) -> Self {
Self {
image_registry: Some(registry.into()),
..self
}
}

pub fn with_owner(self, owner: impl Into<String>) -> Self {
Self {
image_owner: Some(owner.into()),
..self
}
}

pub fn with_container_name(self, name: impl Into<String>) -> Self {
Self {
container_name: Some(name.into()),
Expand Down Expand Up @@ -308,6 +350,8 @@ impl<I: Image> From<(I, I::Args)> for RunnableImage<I> {
image_args,
image_name: None,
image_tag: None,
image_registry: None,
image_owner: None,
container_name: None,
network: None,
env_vars: BTreeMap::default(),
Expand Down

0 comments on commit 5800f6b

Please sign in to comment.