Skip to content

Commit 9b65b29

Browse files
authored
test: switch to testcontainers/helloworld and reduce some flakiness (#855)
1 parent 2fd3c55 commit 9b65b29

File tree

10 files changed

+71
-58
lines changed

10 files changed

+71
-58
lines changed

testcontainers/src/buildables/generic.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ use crate::{
2424
///
2525
/// #[tokio::test]
2626
/// async fn test_hello() -> anyhow::Result<()> {
27-
/// let image = GenericBuildableImage::new("hello-world", "latest")
27+
/// let image = GenericBuildableImage::new("hello-world", "my-tag")
2828
/// .with_dockerfile_string(
2929
/// r#"FROM alpine:latest
3030
/// COPY hello.sh /usr/local/bin/

testcontainers/src/core/containers/async_container.rs

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -542,7 +542,7 @@ mod tests {
542542

543543
#[tokio::test]
544544
async fn async_logs_are_accessible() -> anyhow::Result<()> {
545-
let image = GenericImage::new("testcontainers/helloworld", "1.2.0");
545+
let image = GenericImage::new("testcontainers/helloworld", "1.3.0");
546546
let container = image.start().await?;
547547

548548
let stderr = container.stderr(true);
@@ -634,7 +634,7 @@ mod tests {
634634
("test-name", "async_containers_are_reused"),
635635
];
636636

637-
let initial_image = GenericImage::new("testcontainers/helloworld", "1.2.0")
637+
let initial_image = GenericImage::new("testcontainers/helloworld", "1.3.0")
638638
.with_reuse(crate::ReuseDirective::CurrentSession)
639639
.with_labels(labels);
640640

@@ -698,7 +698,7 @@ mod tests {
698698
("test-name", "async_reused_containers_are_not_confused"),
699699
];
700700

701-
let initial_image = GenericImage::new("testcontainers/helloworld", "1.2.0")
701+
let initial_image = GenericImage::new("testcontainers/helloworld", "1.3.0")
702702
.with_reuse(ReuseDirective::Always)
703703
.with_labels(labels);
704704

@@ -758,7 +758,7 @@ mod tests {
758758

759759
let client = crate::core::client::docker_client_instance().await?;
760760

761-
let image = GenericImage::new("testcontainers/helloworld", "1.2.0")
761+
let image = GenericImage::new("testcontainers/helloworld", "1.3.0")
762762
.with_reuse(ReuseDirective::Always)
763763
.with_labels([
764764
("foo", "bar"),
@@ -798,6 +798,8 @@ mod tests {
798798
#[cfg(feature = "http_wait_plain")]
799799
#[tokio::test]
800800
async fn exec_before_ready_is_ran() {
801+
use crate::core::CmdWaitFor;
802+
801803
struct ExecBeforeReady {}
802804

803805
impl Image for ExecBeforeReady {
@@ -830,14 +832,18 @@ mod tests {
830832
"/bin/sh",
831833
"-c",
832834
"echo 'exec_before_ready ran!' > /opt/hello",
833-
])])
835+
])
836+
.with_cmd_ready_condition(CmdWaitFor::exit())])
834837
}
835838
}
836839

837840
let container = ExecBeforeReady {};
838841
let container = container.start().await.unwrap();
839842
let mut exec_result = container
840-
.exec(ExecCommand::new(vec!["cat", "/opt/hello"]))
843+
.exec(
844+
ExecCommand::new(vec!["cat", "/opt/hello"])
845+
.with_cmd_ready_condition(CmdWaitFor::exit()),
846+
)
841847
.await
842848
.unwrap();
843849
let stdout = exec_result.stdout_to_vec().await.unwrap();
@@ -852,11 +858,11 @@ mod tests {
852858

853859
impl Image for HelloWorld {
854860
fn name(&self) -> &str {
855-
"hello-world"
861+
"testcontainers/helloworld"
856862
}
857863

858864
fn tag(&self) -> &str {
859-
"latest"
865+
"1.3.0"
860866
}
861867

862868
fn ready_conditions(&self) -> Vec<WaitFor> {
@@ -865,7 +871,7 @@ mod tests {
865871
}
866872

867873
let container = HelloWorld {}
868-
.with_ready_conditions(vec![WaitFor::message_on_stdout("Hello from Docker!")]);
874+
.with_ready_conditions(vec![WaitFor::message_on_stderr("Ready, listening on")]);
869875
let _ = container.start().await.unwrap();
870876
}
871877
}

testcontainers/src/core/containers/sync_container.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -257,15 +257,15 @@ mod test {
257257

258258
impl Image for HelloWorld {
259259
fn name(&self) -> &str {
260-
"hello-world"
260+
"testcontainers/helloworld"
261261
}
262262

263263
fn tag(&self) -> &str {
264-
"latest"
264+
"1.3.0"
265265
}
266266

267267
fn ready_conditions(&self) -> Vec<WaitFor> {
268-
vec![WaitFor::message_on_stdout("Hello from Docker!")]
268+
vec![WaitFor::message_on_stderr("Ready, listening on")]
269269
}
270270
}
271271

@@ -324,7 +324,7 @@ mod test {
324324

325325
#[test]
326326
fn sync_logs_are_accessible() -> anyhow::Result<()> {
327-
let image = GenericImage::new("testcontainers/helloworld", "1.2.0");
327+
let image = GenericImage::new("testcontainers/helloworld", "1.3.0");
328328
let container = image.start()?;
329329

330330
let stderr = container.stderr(true);

testcontainers/src/core/ports.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -320,7 +320,7 @@ mod tests {
320320
"Cmd": [
321321
"/hello"
322322
],
323-
"Image": "hello-world",
323+
"Image": "testcontainers/helloworld",
324324
"Volumes": null,
325325
"WorkingDir": "",
326326
"Entrypoint": null,

testcontainers/src/images/generic.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ mod tests {
107107

108108
#[test]
109109
fn should_return_env_vars() {
110-
let image = GenericImage::new("hello-world", "latest")
110+
let image = GenericImage::new("testcontainers/helloworld", "1.3.0")
111111
.with_env_var("one-key", "one-value")
112112
.with_env_var("two-key", "two-value");
113113

testcontainers/src/runners/async_runner.rs

Lines changed: 28 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -441,7 +441,7 @@ mod tests {
441441
),
442442
]);
443443

444-
let image = GenericImage::new("hello-world", "latest").with_labels(&labels);
444+
let image = GenericImage::new("testcontainers/helloworld", "1.3.0").with_labels(&labels);
445445

446446
let container = {
447447
#[cfg(not(feature = "reusable-containers"))]
@@ -494,7 +494,9 @@ mod tests {
494494
async fn async_run_command_should_expose_all_ports_if_no_explicit_mapping_requested(
495495
) -> anyhow::Result<()> {
496496
let client = Client::lazy_client().await?;
497-
let container = GenericImage::new("hello-world", "latest").start().await?;
497+
let container = GenericImage::new("testcontainers/helloworld", "1.3.0")
498+
.start()
499+
.await?;
498500

499501
let container_details = client.inspect(container.id()).await?;
500502
let publish_ports = container_details
@@ -588,18 +590,16 @@ mod tests {
588590
let client = Client::lazy_client().await?;
589591
let _ = pretty_env_logger::try_init();
590592

591-
let udp_port = 1000;
592-
let sctp_port = 2000;
593+
let udp_port = 1000.udp();
594+
let sctp_port = 2000.sctp();
593595

594596
let generic_server = get_server_container()
595597
.await
596598
// Explicitly expose the port, which otherwise would not be available.
597-
.with_exposed_port(udp_port.udp())
598-
.with_exposed_port(sctp_port.sctp());
599+
.with_exposed_port(udp_port)
600+
.with_exposed_port(sctp_port);
599601

600602
let container = generic_server.start().await?;
601-
container.get_host_port_ipv4(udp_port.udp()).await?;
602-
container.get_host_port_ipv4(sctp_port.sctp()).await?;
603603

604604
let container_details = client.inspect(container.id()).await?;
605605

@@ -623,15 +623,20 @@ mod tests {
623623
expected_ports.push(sctp_expected_port);
624624
expected_ports.push(tcp_expected_port);
625625

626-
assert_eq!(current_ports, expected_ports);
626+
assert!(
627+
expected_ports
628+
.iter()
629+
.all(|port| current_ports.contains(port)),
630+
"exposed ports: {current_ports:?} doesn't contain all expected ports: {expected_ports:?}"
631+
);
627632

628633
Ok(())
629634
}
630635

631636
#[tokio::test]
632637
async fn async_run_command_should_expose_only_requested_ports() -> anyhow::Result<()> {
633638
let client = Client::lazy_client().await?;
634-
let image = GenericImage::new("hello-world", "latest");
639+
let image = GenericImage::new("testcontainers/helloworld", "1.3.0");
635640
let container = image
636641
.with_mapped_port(123, 456.tcp())
637642
.with_mapped_port(555, 888.tcp())
@@ -665,7 +670,7 @@ mod tests {
665670
let udp_port = 1000;
666671
let sctp_port = 2000;
667672

668-
let image = GenericImage::new("hello-world", "latest");
673+
let image = GenericImage::new("testcontainers/helloworld", "1.3.0");
669674
let container = image
670675
.with_mapped_port(123, udp_port.udp())
671676
.with_mapped_port(555, sctp_port.sctp())
@@ -700,7 +705,7 @@ mod tests {
700705
#[tokio::test]
701706
async fn async_run_command_should_include_network() -> anyhow::Result<()> {
702707
let client = Client::lazy_client().await?;
703-
let image = GenericImage::new("hello-world", "latest");
708+
let image = GenericImage::new("testcontainers/helloworld", "1.3.0");
704709
let container = image.with_network("awesome-net-1").start().await?;
705710

706711
let container_details = client.inspect(container.id()).await?;
@@ -720,7 +725,7 @@ mod tests {
720725
#[tokio::test]
721726
async fn async_run_command_should_include_name() -> anyhow::Result<()> {
722727
let client = Client::lazy_client().await?;
723-
let image = GenericImage::new("hello-world", "latest");
728+
let image = GenericImage::new("testcontainers/helloworld", "1.3.0");
724729
let container = image
725730
.with_container_name("async_hello_container")
726731
.start()
@@ -735,7 +740,7 @@ mod tests {
735740
#[tokio::test]
736741
async fn async_should_create_network_if_image_needs_it_and_drop_it_in_the_end(
737742
) -> anyhow::Result<()> {
738-
let hello_world = GenericImage::new("hello-world", "latest");
743+
let hello_world = GenericImage::new("testcontainers/helloworld", "1.3.0");
739744

740745
{
741746
let client = Client::lazy_client().await?;
@@ -800,7 +805,7 @@ mod tests {
800805
#[tokio::test]
801806
async fn async_run_command_should_set_shared_memory_size() -> anyhow::Result<()> {
802807
let client = Client::lazy_client().await?;
803-
let image = GenericImage::new("hello-world", "latest");
808+
let image = GenericImage::new("testcontainers/helloworld", "1.3.0");
804809
let container = image.with_shm_size(1_000_000).start().await?;
805810

806811
let container_details = client.inspect(container.id()).await?;
@@ -816,7 +821,7 @@ mod tests {
816821

817822
#[tokio::test]
818823
async fn async_run_command_should_include_privileged() -> anyhow::Result<()> {
819-
let image = GenericImage::new("hello-world", "latest");
824+
let image = GenericImage::new("testcontainers/helloworld", "1.3.0");
820825
let container = image.with_privileged(true).start().await?;
821826

822827
let client = Client::lazy_client().await?;
@@ -833,7 +838,7 @@ mod tests {
833838

834839
#[tokio::test]
835840
async fn async_run_command_should_have_cap_add() -> anyhow::Result<()> {
836-
let image = GenericImage::new("hello-world", "latest");
841+
let image = GenericImage::new("testcontainers/helloworld", "1.3.0");
837842
let expected_capability = "NET_ADMIN";
838843
let container = image
839844
.with_cap_add(expected_capability.to_string())
@@ -860,7 +865,7 @@ mod tests {
860865

861866
#[tokio::test]
862867
async fn async_run_command_should_have_cap_drop() -> anyhow::Result<()> {
863-
let image = GenericImage::new("hello-world", "latest");
868+
let image = GenericImage::new("testcontainers/helloworld", "1.3.0");
864869
let expected_capability = "AUDIT_WRITE";
865870
let container = image
866871
.with_cap_drop(expected_capability.to_string())
@@ -887,7 +892,7 @@ mod tests {
887892

888893
#[tokio::test]
889894
async fn async_run_command_should_include_ulimits() -> anyhow::Result<()> {
890-
let image = GenericImage::new("hello-world", "latest");
895+
let image = GenericImage::new("testcontainers/helloworld", "1.3.0");
891896
let container = image.with_ulimit("nofile", 123, Some(456)).start().await?;
892897

893898
let client = Client::lazy_client().await?;
@@ -908,7 +913,7 @@ mod tests {
908913

909914
#[tokio::test]
910915
async fn async_run_command_should_have_host_cgroupns_mode() -> anyhow::Result<()> {
911-
let image = GenericImage::new("hello-world", "latest");
916+
let image = GenericImage::new("testcontainers/helloworld", "1.3.0");
912917
let container = image.with_cgroupns_mode(CgroupnsMode::Host).start().await?;
913918

914919
let client = Client::lazy_client().await?;
@@ -930,7 +935,7 @@ mod tests {
930935

931936
#[tokio::test]
932937
async fn async_run_command_should_have_private_cgroupns_mode() -> anyhow::Result<()> {
933-
let image = GenericImage::new("hello-world", "latest");
938+
let image = GenericImage::new("testcontainers/helloworld", "1.3.0");
934939
let container = image
935940
.with_cgroupns_mode(CgroupnsMode::Private)
936941
.start()
@@ -955,7 +960,7 @@ mod tests {
955960

956961
#[tokio::test]
957962
async fn async_run_command_should_have_host_userns_mode() -> anyhow::Result<()> {
958-
let image = GenericImage::new("hello-world", "latest");
963+
let image = GenericImage::new("testcontainers/helloworld", "1.3.0");
959964
let container = image.with_userns_mode("host").start().await?;
960965

961966
let client = Client::lazy_client().await?;
@@ -972,7 +977,7 @@ mod tests {
972977

973978
#[tokio::test]
974979
async fn async_run_command_should_have_working_dir() -> anyhow::Result<()> {
975-
let image = GenericImage::new("hello-world", "latest");
980+
let image = GenericImage::new("testcontainers/helloworld", "1.2.0");
976981
let expected_working_dir = "/foo";
977982
let container = image.with_working_dir(expected_working_dir).start().await?;
978983

0 commit comments

Comments
 (0)