-
Notifications
You must be signed in to change notification settings - Fork 142
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: ability to access container logs (#633)
Closes #502 Exposes `stdout` and `stderr` methods, which returns: - `tokio::io::AsyncBufRead` impl for async container - `std::io::BufRead` impl for sync container (under `blocking` feature) Also, small alignment is performed (related to #617): - rename the `ExecResult` `stdout`/`stderr` methods to match the container methods
- Loading branch information
Showing
8 changed files
with
264 additions
and
37 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
64 changes: 64 additions & 0 deletions
64
testcontainers/src/core/containers/sync_container/sync_reader.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
use std::io::{BufRead, Read}; | ||
|
||
use tokio::io::{AsyncBufRead, AsyncBufReadExt, AsyncRead, AsyncReadExt}; | ||
|
||
/// Allows to use [`tokio::io::AsyncRead`] synchronously as [`std::io::Read`]. | ||
/// In fact, it's almost the same as [`tokio_util::io::SyncIoBridge`], but utilizes [`tokio::runtime::Runtime`] instead of [`tokio::runtime::Handle`]. | ||
/// This is needed because [`tokio::runtime::Handle::block_on`] can't drive the IO on `current_thread` runtime. | ||
pub(super) struct SyncReadBridge<'a, T> { | ||
inner: T, | ||
runtime: &'a tokio::runtime::Runtime, | ||
} | ||
|
||
impl<'a, T: Unpin> SyncReadBridge<'a, T> { | ||
pub fn new(inner: T, runtime: &'a tokio::runtime::Runtime) -> Self { | ||
Self { inner, runtime } | ||
} | ||
} | ||
|
||
impl<T: AsyncBufRead + Unpin> BufRead for SyncReadBridge<'_, T> { | ||
fn fill_buf(&mut self) -> std::io::Result<&[u8]> { | ||
let inner = &mut self.inner; | ||
self.runtime.block_on(AsyncBufReadExt::fill_buf(inner)) | ||
} | ||
|
||
fn consume(&mut self, amt: usize) { | ||
let inner = &mut self.inner; | ||
AsyncBufReadExt::consume(inner, amt) | ||
} | ||
|
||
fn read_until(&mut self, byte: u8, buf: &mut Vec<u8>) -> std::io::Result<usize> { | ||
let inner = &mut self.inner; | ||
self.runtime | ||
.block_on(AsyncBufReadExt::read_until(inner, byte, buf)) | ||
} | ||
fn read_line(&mut self, buf: &mut String) -> std::io::Result<usize> { | ||
let inner = &mut self.inner; | ||
self.runtime | ||
.block_on(AsyncBufReadExt::read_line(inner, buf)) | ||
} | ||
} | ||
|
||
impl<T: AsyncRead + Unpin> Read for SyncReadBridge<'_, T> { | ||
fn read(&mut self, buf: &mut [u8]) -> std::io::Result<usize> { | ||
let inner = &mut self.inner; | ||
self.runtime.block_on(AsyncReadExt::read(inner, buf)) | ||
} | ||
|
||
fn read_to_end(&mut self, buf: &mut Vec<u8>) -> std::io::Result<usize> { | ||
let inner = &mut self.inner; | ||
self.runtime.block_on(inner.read_to_end(buf)) | ||
} | ||
|
||
fn read_to_string(&mut self, buf: &mut String) -> std::io::Result<usize> { | ||
let inner = &mut self.inner; | ||
self.runtime.block_on(inner.read_to_string(buf)) | ||
} | ||
|
||
fn read_exact(&mut self, buf: &mut [u8]) -> std::io::Result<()> { | ||
let inner = &mut self.inner; | ||
// The AsyncRead trait returns the count, synchronous doesn't. | ||
let _n = self.runtime.block_on(inner.read_exact(buf))?; | ||
Ok(()) | ||
} | ||
} |
Oops, something went wrong.