Skip to content

Commit

Permalink
feat: map container not found error to eof for container log streams (
Browse files Browse the repository at this point in the history
#639)

This allows to handle eof of logs on client side
  • Loading branch information
DDtKey authored May 26, 2024
1 parent 95ef9ad commit b4780a9
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 1 deletion.
12 changes: 11 additions & 1 deletion testcontainers/src/core/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -261,7 +261,17 @@ impl Client {
.bollard
.logs(container_id, Some(options))
.map_ok(|chunk| chunk.into_bytes())
.map_err(|err| io::Error::new(io::ErrorKind::Other, err))
.map_err(|err| match err {
bollard::errors::Error::DockerResponseServerError {
status_code: 404,
message,
} => io::Error::new(
io::ErrorKind::UnexpectedEof,
format!("Docker container has been dropped: {}", message),
),
bollard::errors::Error::IOError { err } => err,
err => io::Error::new(io::ErrorKind::Other, err),
})
.boxed();
LogStreamAsync::new(stream)
}
Expand Down
30 changes: 30 additions & 0 deletions testcontainers/src/core/containers/async_container.rs
Original file line number Diff line number Diff line change
Expand Up @@ -372,6 +372,7 @@ where

#[cfg(test)]
mod tests {

use tokio::io::{AsyncBufReadExt, AsyncReadExt};

use super::*;
Expand Down Expand Up @@ -427,6 +428,35 @@ mod tests {
"unexpected stderr size: {}",
stderr
);

// start again to test eof on drop
container.start().await?;

// create logger task which reads logs from container up to EOF
let container_id = container.id().to_string();
let stderr = container.stderr();
let logger_task = tokio::spawn(async move {
let mut stderr_lines = stderr.lines();
while let Some(result) = stderr_lines.next_line().await.transpose() {
match result {
Ok(line) => {
log::debug!(target: "container", "[{container_id}]:{}", line);
}
Err(err) if err.kind() == std::io::ErrorKind::UnexpectedEof => {
log::debug!(target: "container", "[{container_id}] EOF");
break;
}
Err(err) => Err(err)?,
}
}
Ok::<_, anyhow::Error>(())
});

drop(container);
logger_task
.await
.map_err(|_| anyhow::anyhow!("failed to join log follower task"))??;

Ok(())
}
}

0 comments on commit b4780a9

Please sign in to comment.