Skip to content
Open
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
12 changes: 4 additions & 8 deletions testcontainers/src/compose/client/containerised.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,13 +46,7 @@ impl ContainerisedComposeCli {

impl ComposeInterface for ContainerisedComposeCli {
async fn up(&self, command: UpCommand) -> Result<()> {
let mut cmd_parts = vec![];

for (key, value) in &command.env_vars {
cmd_parts.push(format!("{}={}", key, value));
}

cmd_parts.extend(["docker".to_string(), "compose".to_string()]);
let mut cmd_parts = vec!["docker".to_string(), "compose".to_string()];

if let Some(project_directory) = &self.project_directory {
cmd_parts.push("--project-directory".to_string());
Expand Down Expand Up @@ -83,7 +77,9 @@ impl ComposeInterface for ContainerisedComposeCli {
cmd_parts.push("--wait-timeout".to_string());
cmd_parts.push(command.wait_timeout.as_secs().to_string());

let exec = ExecCommand::new(cmd_parts).with_cmd_ready_condition(CmdWaitFor::exit_code(0));
let exec = ExecCommand::new(cmd_parts)
.with_cmd_ready_condition(CmdWaitFor::exit_code(0))
.with_env_vars(command.env_vars);
self.container.exec(exec).await?;

Ok(())
Expand Down
13 changes: 13 additions & 0 deletions testcontainers/src/core/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -277,11 +277,24 @@ impl Client {
&self,
container_id: &str,
cmd: Vec<String>,
env_vars: std::collections::HashMap<String, String>,
) -> Result<ExecResult, ClientError> {
let env = if env_vars.is_empty() {
None
} else {
Some(
env_vars
.into_iter()
.map(|(k, v)| format!("{k}={v}"))
.collect(),
)
};

let config = CreateExecOptions {
cmd: Some(cmd),
attach_stdout: Some(true),
attach_stderr: Some(true),
env,
..Default::default()
};

Expand Down
3 changes: 2 additions & 1 deletion testcontainers/src/core/containers/async_container/raw.rs
Original file line number Diff line number Diff line change
Expand Up @@ -144,11 +144,12 @@ impl RawContainer {
cmd,
container_ready_conditions,
cmd_ready_condition,
env_vars,
} = cmd;

log::debug!("Executing command {:?}", cmd);

let mut exec = self.docker_client.exec(&self.id, cmd).await?;
let mut exec = self.docker_client.exec(&self.id, cmd, env_vars).await?;
self.block_until_ready(container_ready_conditions).await?;

match cmd_ready_condition {
Expand Down
32 changes: 32 additions & 0 deletions testcontainers/src/core/image/exec.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
use std::collections::HashMap;

use crate::core::{CmdWaitFor, WaitFor};

#[derive(Debug)]
pub struct ExecCommand {
pub(crate) cmd: Vec<String>,
pub(crate) cmd_ready_condition: CmdWaitFor,
pub(crate) container_ready_conditions: Vec<WaitFor>,
pub(crate) env_vars: HashMap<String, String>,
}

impl ExecCommand {
Expand All @@ -14,6 +17,7 @@ impl ExecCommand {
cmd: cmd.into_iter().map(Into::into).collect(),
cmd_ready_condition: CmdWaitFor::Nothing,
container_ready_conditions: vec![],
env_vars: HashMap::new(),
}
}

Expand All @@ -28,10 +32,38 @@ impl ExecCommand {
self.cmd_ready_condition = ready_conditions.into();
self
}

/// Sets environment variables for the exec command.
///
/// These are passed directly to the Docker exec API as the `Env` field,
/// making them available in the executed process's environment.
pub fn with_env_vars(
mut self,
env_vars: impl IntoIterator<Item = (impl Into<String>, impl Into<String>)>,
) -> Self {
self.env_vars
.extend(env_vars.into_iter().map(|(k, v)| (k.into(), v.into())));
self
}
}

impl Default for ExecCommand {
fn default() -> Self {
Self::new(Vec::<String>::new())
}
}

#[cfg(test)]
mod tests {
use super::*;

#[test]
fn test_with_env_vars() {
let exec = ExecCommand::new(["echo", "hello"])
.with_env_vars([("KEY1", "value1"), ("KEY2", "value2")]);

assert_eq!(exec.env_vars.get("KEY1").unwrap(), "value1");
assert_eq!(exec.env_vars.get("KEY2").unwrap(), "value2");
assert_eq!(exec.cmd, vec!["echo", "hello"]);
}
}