Skip to content

Commit

Permalink
chore: copy-to-container interface improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
DDtKey committed Sep 13, 2024
1 parent cf6f593 commit d1d5b83
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 24 deletions.
28 changes: 18 additions & 10 deletions testcontainers/src/core/copy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ use std::{

#[derive(Debug, Clone)]
pub struct CopyToContainer {
pub target: String,
pub source: CopyDataSource,
target: String,
source: CopyDataSource,
}

#[derive(Debug, Clone)]
Expand All @@ -24,20 +24,28 @@ pub enum CopyToContaienrError {
}

impl CopyToContainer {
pub fn target_directory(&self) -> Result<String, CopyToContaienrError> {
match path::Path::new(&self.target).parent() {
Some(v) => Ok(v.display().to_string()),
None => return Err(CopyToContaienrError::PathNameError(self.target.clone())),
pub fn new(source: impl Into<CopyDataSource>, target: impl Into<String>) -> Self {
Self {
source: source.into(),
target: target.into(),
}
}

pub async fn tar(&self) -> Result<bytes::Bytes, CopyToContaienrError> {
pub(crate) fn target_directory(&self) -> Result<String, CopyToContaienrError> {
path::Path::new(&self.target)
.parent()
.map(path::Path::display)
.map(|dir| dir.to_string())
.ok_or_else(|| CopyToContaienrError::PathNameError(self.target.clone()))
}

pub(crate) async fn tar(&self) -> Result<bytes::Bytes, CopyToContaienrError> {
self.source.tar(&self.target).await
}
}

impl CopyDataSource {
pub async fn tar(
pub(crate) async fn tar(
&self,
target_path: impl Into<String>,
) -> Result<bytes::Bytes, CopyToContaienrError> {
Expand All @@ -46,10 +54,10 @@ impl CopyDataSource {

match self {
CopyDataSource::File(file_path) => {
let mut f = &mut tokio::fs::File::open(file_path)
let f = &mut tokio::fs::File::open(file_path)
.await
.map_err(CopyToContaienrError::IoError)?;
ar.append_file(&target_path, &mut f)
ar.append_file(&target_path, f)
.await
.map_err(CopyToContaienrError::IoError)?;
}
Expand Down
12 changes: 8 additions & 4 deletions testcontainers/src/core/image/image_ext.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,11 @@ pub trait ImageExt<I: Image> {
fn with_mount(self, mount: impl Into<Mount>) -> ContainerRequest<I>;

/// Copies some source into the container as file
fn with_copy_to(self, target: impl Into<String>, source: CopyDataSource)
-> ContainerRequest<I>;
fn with_copy_to(
self,
target: impl Into<String>,
source: impl Into<CopyDataSource>,
) -> ContainerRequest<I>;

/// Adds a port mapping to the container, mapping the host port to the container's internal port.
///
Expand Down Expand Up @@ -90,6 +93,7 @@ pub trait ImageExt<I: Image> {
/// cgroup namespace mode for the container. Possible values are:
/// - [`CgroupnsMode::Private`]: the container runs in its own private cgroup namespace
/// - [`CgroupnsMode::Host`]: use the host system's cgroup namespace
///
/// If not specified, the daemon default is used, which can either be `\"private\"` or `\"host\"`, depending on daemon version, kernel support and configuration.
fn with_cgroupns_mode(self, cgroupns_mode: CgroupnsMode) -> ContainerRequest<I>;

Expand Down Expand Up @@ -179,13 +183,13 @@ impl<RI: Into<ContainerRequest<I>>, I: Image> ImageExt<I> for RI {
fn with_copy_to(
self,
target: impl Into<String>,
source: CopyDataSource,
source: impl Into<CopyDataSource>,
) -> ContainerRequest<I> {
let mut container_req = self.into();
let target: String = target.into();
container_req
.copy_to_sources
.push(CopyToContainer { target, source });
.push(CopyToContainer::new(source, target));
container_req
}

Expand Down
2 changes: 1 addition & 1 deletion testcontainers/src/runners/async_runner.rs
Original file line number Diff line number Diff line change
Expand Up @@ -218,7 +218,7 @@ where

for copy_to_source in copy_to_sources {
client
.copy_to_container(&container_id, &copy_to_source)
.copy_to_container(&container_id, copy_to_source)
.await?;
}

Expand Down
7 changes: 2 additions & 5 deletions testcontainers/tests/async_runner.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use testcontainers::{
CmdWaitFor, ExecCommand, WaitFor,
},
runners::AsyncRunner,
CopyDataSource, GenericImage, Image, ImageExt,
GenericImage, Image, ImageExt,
};
use tokio::io::AsyncReadExt;

Expand Down Expand Up @@ -204,10 +204,7 @@ async fn async_run_with_log_consumer() -> anyhow::Result<()> {
async fn async_copy_files_to_container() -> anyhow::Result<()> {
let container = GenericImage::new("alpine", "latest")
.with_wait_for(WaitFor::seconds(2))
.with_copy_to(
"/tmp/somefile",
CopyDataSource::from("foobar".to_string().into_bytes()),
)
.with_copy_to("/tmp/somefile", "foobar".to_string().into_bytes())
.with_cmd(vec!["cat", "/tmp/somefile"])
.start()
.await?;
Expand Down
5 changes: 1 addition & 4 deletions testcontainers/tests/sync_runner.rs
Original file line number Diff line number Diff line change
Expand Up @@ -230,10 +230,7 @@ fn sync_copy_files_to_container() -> anyhow::Result<()> {

let container = GenericImage::new("alpine", "latest")
.with_wait_for(WaitFor::seconds(2))
.with_copy_to(
"/tmp/somefile",
CopyDataSource::Data("foobar".to_string().into_bytes()),
)
.with_copy_to("/tmp/somefile", "foobar".to_string().into_bytes())
.with_cmd(vec!["cat", "/tmp/somefile"])
.start()?;

Expand Down

0 comments on commit d1d5b83

Please sign in to comment.