Skip to content

Minimal docker compose support #407

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions testcontainers/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ version = "0.14.0"
authors = [ "CoBloX developers <[email protected]>" ]
categories = [ "development-tools::testing" ]
edition = "2021"
keywords = [ "docker", "testcontainers" ]
keywords = [ "docker", "testcontainers", "docker-compose" ]
license = "MIT OR Apache-2.0"
repository = "https://github.com/testcontainers/testcontainers-rs"
rust-version = "1.60.0"
Expand All @@ -19,6 +19,7 @@ futures = "0.3"
hex = "0.4"
hmac = "0.12"
log = "0.4"
once_cell = { version = "1.7", optional = true }
rand = "0.8"
serde = { version = "1", features = [ "derive" ] }
serde_json = "1"
Expand All @@ -27,9 +28,10 @@ signal-hook = { version = "0.3", optional = true }
tokio = { version = "1", features = [ "macros" ], optional = true }

[features]
default = [ ]
default = [ "compose" ]
watchdog = [ "signal-hook", "conquer-once" ]
experimental = [ "async-trait", "bollard", "tokio" ]
compose = [ "once_cell" ]

[dev-dependencies]
bitcoincore-rpc = "0.16"
Expand Down
80 changes: 80 additions & 0 deletions testcontainers/src/compose/builder.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
use std::{
collections::HashMap,
path::{Path, PathBuf},
};

use crate::core::WaitFor;

use super::{DockerCompose, StopMode};

/// A builder for [`DockerCompose`]
#[derive(Debug, Clone, Default)]
pub struct DockerComposeBuilder {
path: PathBuf,
env: HashMap<String, String>,
ready_conditions: Vec<(String, WaitFor)>,
stop_mode: StopMode,
inherit_io: bool,
}

impl DockerComposeBuilder {
/// Create a docker compose builder.
///
/// The path should be the docker compose configuration file, usually `docker-compose.yaml`
pub fn new(path: impl AsRef<Path>) -> Self {
Self {
path: path.as_ref().to_path_buf(),
..Default::default()
}
}

/// Add an environment variable
pub fn env(mut self, key: impl Into<String>, value: impl Into<String>) -> Self {
self.env.insert(key.into(), value.into());
self
}

/// Add some profiles
pub fn profiles<T>(mut self, profiles: impl IntoIterator<Item = T>) -> Self
where
T: ToString,
{
let profiles = profiles
.into_iter()
.map(|it| it.to_string())
.collect::<Vec<_>>()
.join(",");
self.env.insert("COMPOSE_PROFILES".to_string(), profiles);
self
}

/// Add a [`WaitFor`] for a service
pub fn wait(mut self, service: impl Into<String>, wait: WaitFor) -> Self {
self.ready_conditions.push((service.into(), wait));
self
}

/// Set the stop mode, by default it's [`StopMode::Stop`]
pub fn stop_with(mut self, stop_mode: StopMode) -> Self {
self.stop_mode = stop_mode;
self
}

/// Show docker compose stdout and stderr
pub fn inherit_io(mut self) -> Self {
self.inherit_io = true;
self
}

/// Create the [`DockerCompose`]
pub fn build(self) -> DockerCompose {
DockerCompose {
path: self.path,
env: self.env,
child: None,
ready_conditions: self.ready_conditions,
stop_mode: self.stop_mode,
inherit_io: self.inherit_io,
}
}
}
Loading