Skip to content

Commit 47aea74

Browse files
committed
Incorporate log crate
1 parent 10fcbdf commit 47aea74

File tree

6 files changed

+44
-12
lines changed

6 files changed

+44
-12
lines changed

Cargo.toml

+5-1
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,13 @@ async-trait = "0.1.52"
1313
bollard = "= 0.11.0"
1414
bollard-stubs = "= 1.41.0"
1515
futures = "0.3"
16+
log = "0.4"
1617
thiserror = "1.0"
1718
tokio = {version = "1.17.0", features = ["rt","macros"]}
1819

20+
[dev-dependencies]
21+
env_logger = "0.8"
22+
1923
[[test]]
2024
name = "integration"
21-
path = "tests/lib.rs"
25+
path = "tests/integration/lib.rs"

src/container.rs

+11-9
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
1-
use crate::bollard::container::{InspectContainerOptions, RemoveContainerOptions};
2-
use crate::bollard::Docker;
3-
use async_trait::async_trait;
41
use std::collections::HashMap;
52
use std::fmt::Debug;
63

4+
use async_trait::async_trait;
5+
use log::{error, info, warn};
6+
7+
use crate::bollard::container::{InspectContainerOptions, RemoveContainerOptions};
8+
use crate::bollard::Docker;
79
pub use crate::errors::TestcontainerError;
810
use crate::{DropAction, ImageSettings, Qualifier, Task};
911

@@ -121,7 +123,7 @@ impl Drop for ContainerHandle {
121123
"remove" => drop_action = DropAction::Remove,
122124
"retain" => drop_action = DropAction::Retain,
123125
"stop" => drop_action = DropAction::Stop,
124-
value => eprintln!(
126+
value => warn!(
125127
"'{}' is not a valid value for {}",
126128
value, TESTCONTAINERS_DROP_ACTION
127129
),
@@ -136,7 +138,7 @@ impl Drop for ContainerHandle {
136138
std::thread::spawn(move || {
137139
let rt = tokio::runtime::Runtime::new().unwrap();
138140
rt.block_on(async {
139-
eprintln!("Removing container {id}");
141+
info!("Removing container {}", &id[..12]);
140142
let result = docker
141143
.remove_container(
142144
id.as_str(),
@@ -150,7 +152,7 @@ impl Drop for ContainerHandle {
150152
match result {
151153
Ok(_) => {}
152154
Err(error) => {
153-
eprintln!("Error removing container by id '{id}': {error}");
155+
error!("Error removing container by id '{}': {error}", &id[..12]);
154156
}
155157
}
156158

@@ -159,21 +161,21 @@ impl Drop for ContainerHandle {
159161
});
160162
let _ = receiver.recv();
161163
}
162-
DropAction::Retain => println!("Retaining container {}", self.id),
164+
DropAction::Retain => info!("Retaining container {}", &self.id[..12]),
163165
DropAction::Stop => {
164166
let id = self.id.clone();
165167
let docker = self.docker.clone();
166168
let (sender, receiver) = std::sync::mpsc::channel();
167169
std::thread::spawn(move || {
168170
let rt = tokio::runtime::Runtime::new().unwrap();
169171
rt.block_on(async {
170-
eprintln!("Stopping container {id}");
172+
info!("Stopping container {id}");
171173
let result = docker.stop_container(id.as_str(), None).await;
172174

173175
match result {
174176
Ok(_) => {}
175177
Err(error) => {
176-
eprintln!("Error stopping container by id '{id}': {error}");
178+
error!("Error stopping container by id '{}': {error}", &id[..12]);
177179
}
178180
}
179181

src/image.rs

+16-1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ use std::collections::HashMap;
22
use std::fmt::{Display, Formatter};
33

44
use futures::TryStreamExt;
5+
use log::{debug, info};
56

67
use crate::bollard::container::Config;
78
use crate::bollard::image::CreateImageOptions;
@@ -256,7 +257,7 @@ pub trait Image: Sized {
256257
match inspect_result {
257258
Ok(_) => (),
258259
Err(_) => {
259-
eprintln!("Pulling image {}", self.settings().fullname());
260+
info!("Pulling image {}", self.settings().fullname());
260261
docker
261262
.create_image(
262263
Some(CreateImageOptions {
@@ -307,6 +308,8 @@ pub trait Image: Sized {
307308
..Default::default()
308309
};
309310

311+
debug!("Creating container for {}", self.settings().fullname());
312+
310313
let id = docker
311314
.create_container::<&str, String>(None, image_config)
312315
.await?
@@ -315,11 +318,23 @@ pub trait Image: Sized {
315318
}
316319

317320
async fn on_start_container(&self, handle: &ContainerHandle) -> Result<(), TestcontainerError> {
321+
debug!(
322+
"Starting {} ({})",
323+
&handle.id()[..12],
324+
self.settings().fullname()
325+
);
326+
318327
handle
319328
.docker()
320329
.start_container::<String>(handle.id(), None)
321330
.await?;
322331

332+
info!(
333+
"Started {} ({})",
334+
&handle.id()[..12],
335+
self.settings().fullname()
336+
);
337+
323338
Ok(())
324339
}
325340

src/task.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
use crate::{async_trait, ContainerHandle, TestcontainerError};
2+
use std::fmt::Debug;
23

34
#[async_trait]
4-
pub trait Task: 'static + Send + Sync {
5+
pub trait Task: 'static + Send + Sync + Debug {
56
type Return;
67

78
async fn execute(&self, handle: &ContainerHandle) -> Result<Self::Return, TestcontainerError>;
File renamed without changes.

tests/lib.rs tests/integration/lib.rs

+10
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,13 @@ use testcontainers_async::{
1212
AdminContainer, Container, DatabaseContainer, Image, ServiceContainer, TestcontainerError,
1313
};
1414

15+
fn init() {
16+
let _ = env_logger::builder().try_init();
17+
}
18+
1519
#[tokio::test]
1620
async fn test_generic() -> Result<(), TestcontainerError> {
21+
init();
1722
let redis = GenericImage::new("redis", "latest")
1823
.with_task(MatchLogOutput::containing("Ready to accept connections"))
1924
.start_container()
@@ -27,6 +32,7 @@ async fn test_generic() -> Result<(), TestcontainerError> {
2732

2833
#[tokio::test]
2934
async fn test_redis() -> Result<(), TestcontainerError> {
35+
init();
3036
let redis = RedisImage::default().start_container().await?;
3137

3238
let port = redis.service_port().await?;
@@ -37,6 +43,7 @@ async fn test_redis() -> Result<(), TestcontainerError> {
3743

3844
#[tokio::test]
3945
async fn test_postgres() -> Result<(), TestcontainerError> {
46+
init();
4047
let postgres = PostgresImage::default()
4148
.with_database("example-service")
4249
.with_username("test")
@@ -56,6 +63,7 @@ async fn test_postgres() -> Result<(), TestcontainerError> {
5663

5764
#[tokio::test]
5865
async fn test_cockroach() -> Result<(), TestcontainerError> {
66+
init();
5967
let crdb = CockroachDbImage::default().start_container().await?;
6068

6169
let service_port = crdb.service_port().await?;
@@ -69,6 +77,7 @@ async fn test_cockroach() -> Result<(), TestcontainerError> {
6977

7078
#[tokio::test]
7179
async fn test_mysql() -> Result<(), TestcontainerError> {
80+
init();
7281
let mysql = MySqlImage::default()
7382
.with_database("example-service")
7483
.with_username("test")
@@ -83,6 +92,7 @@ async fn test_mysql() -> Result<(), TestcontainerError> {
8392

8493
#[tokio::test]
8594
async fn test_example_impl() -> Result<(), TestcontainerError> {
95+
init();
8696
let example = ExampleImage::default().start_container().await?;
8797

8898
let port = example.primary_port().await.expect("Port expected");

0 commit comments

Comments
 (0)