Skip to content

Commit e9caf57

Browse files
committed
refactor)commad-strategy): use exit code from ExecCommand
1 parent 8a34464 commit e9caf57

File tree

1 file changed

+16
-9
lines changed

1 file changed

+16
-9
lines changed

testcontainers/src/core/wait/command_strategy.rs

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
11
use std::time::Duration;
22

33
use crate::{
4-
core::{client::Client, error::WaitContainerError, wait::WaitStrategy, ExecCommand},
4+
core::{
5+
client::Client, error::WaitContainerError, wait::WaitStrategy, CmdWaitFor, ExecCommand,
6+
},
57
ContainerAsync, Image,
68
};
79

810
#[derive(Debug, Clone)]
911
pub struct CommandStrategy {
10-
expected_code: i64,
1112
poll_interval: Duration,
1213
command: ExecCommand,
1314
fail_fast: bool,
@@ -18,7 +19,6 @@ impl CommandStrategy {
1819
pub fn new() -> Self {
1920
Self {
2021
command: ExecCommand::default(),
21-
expected_code: 0,
2222
poll_interval: Duration::from_millis(100),
2323
fail_fast: false,
2424
}
@@ -56,6 +56,11 @@ impl WaitStrategy for CommandStrategy {
5656
client: &Client,
5757
container: &ContainerAsync<I>,
5858
) -> crate::core::error::Result<()> {
59+
let expected_code = match self.command.clone().cmd_ready_condition {
60+
CmdWaitFor::Exit { code } => code,
61+
_ => Some(0),
62+
};
63+
5964
loop {
6065
let container_state = client
6166
.inspect(container.id())
@@ -82,15 +87,17 @@ impl WaitStrategy for CommandStrategy {
8287
let exit_code = inspect_result.exit_code;
8388
running = inspect_result.running.unwrap_or(false);
8489

85-
if self.fail_fast && exit_code != Some(self.expected_code) {
86-
return Err(WaitContainerError::UnexpectedExitCode {
87-
expected: self.expected_code,
88-
actual: exit_code,
90+
if let Some(code) = expected_code {
91+
if self.fail_fast && exit_code != expected_code {
92+
return Err(WaitContainerError::UnexpectedExitCode {
93+
expected: code,
94+
actual: exit_code,
95+
}
96+
.into());
8997
}
90-
.into());
9198
}
9299

93-
if exit_code == Some(self.expected_code) {
100+
if exit_code == expected_code {
94101
return Ok(());
95102
}
96103

0 commit comments

Comments
 (0)