Skip to content

feat: added CommandStrategy with SuccessfulCommand enum #782

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 6 commits into
base: main
Choose a base branch
from

Conversation

sutt0n
Copy link

@sutt0n sutt0n commented Mar 30, 2025

Potentially resolves #702

This introduces a new wait strategy, CommandStrategy. The goal here is to wait for a successful command (exit code 0) prior to marking a container as ready. This polls the container state to ensure it's running first, and then executes the given command. Once the command has an exit code of 0, then the strategy has completed.

There are options to fail fast, as well as customize the exit code, just to expose flexibility for the strategy.

Open to changes / updates, if required -- this language is not my day-to-day.

Copy link

netlify bot commented Mar 30, 2025

Deploy Preview for testcontainers-rust ready!

Name Link
🔨 Latest commit cee5d7e
🔍 Latest deploy log https://app.netlify.com/sites/testcontainers-rust/deploys/67f27c57e41c8c00087b67d3
😎 Deploy Preview https://deploy-preview-782--testcontainers-rust.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.

To edit notification comments on pull requests, go to your Netlify site configuration.

Copy link
Collaborator

@DDtKey DDtKey left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for the contribution!

I've left a comment, please take a look

Copy link
Collaborator

@DDtKey DDtKey Mar 30, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can't this be achieved with exec_after_start with ExitCode wait condition (it waits for exit)? Additionally, this way allows to rely on a container's state

Or we could consider extending existing wait conditions if it's not enough

But generally speaking, at first glance it looks like duplicate functionality

Copy link
Author

@sutt0n sutt0n Mar 31, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Now that you mention it and after a second glance, it feels like duplicate functionality. This was probably an oversight on my end by lack of context.

I can test what you've provided and report back findings - that may aid in either resolving or progressing a solution for this issue.

Copy link
Author

@sutt0n sutt0n Apr 1, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

After investigating, the difference here is that this adds a "command strategy" similar to "shell strategy" (with an emphasis on SuccessfulCommand) here:

https://node.testcontainers.org/features/wait-strategies/#shell-command

To be more orthogonal / consistent with the Node test container, what do you think about this just being ShellStrategy instead? It has the same functionality, if I'm not mistaken:

https://github.com/testcontainers/testcontainers-node/blob/main/packages/testcontainers/src/wait-strategies/shell-wait-strategy.ts#L6

Whereas WaitFor is similar to: https://github.com/testcontainers/testcontainers-node/blob/b92f6695de10863c6ea90d169f999b79406d162e/packages/testcontainers/src/wait-strategies/wait.ts#L40

Thoughts?

Copy link
Collaborator

@DDtKey DDtKey Apr 1, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We don't have to be fully consistent with other languages. You can find a lot of differences between Go, Java and Node, for example (but we try to be aligned when possible)

Also, we should compare exec_after_start with strategies here, not the WaitFor itself.

However we can consider such strategy for sure, more like "alias" or "short/easier way".
But we need to avoid maintaining of duplicative logic/code. Let me explain:

What are we introducing here that is missing in exec_after_start?
If it's about "easier way" - we need to find a way to compose it more correctly
But if there is a difference in functionality - then it should be very clear to users why to use one over the other

Copy link
Collaborator

@DDtKey DDtKey Apr 1, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's consider the current implementation to understand if it's clear enough

.with_wait_for(WaitFor::SuccessfulCommand(
        CommandStrategy::command(
            ExecCommand::new(["pg_isready"]).with_cmd_ready_condition(CmdWaitFor::exit_code(0)),
        ),
    ))

What confuses me a little bit here:

  • with_cmd_ready_condition(CmdWaitFor::exit_code(0) - expects exit-code = 0
    • SuccessfulCommand - also expects exit-code = 0 (implicitly)
    • What if I'd change with_cmd_ready_condition to expect non-zero code or something else?
    • Do I really need to specify this two times as a user?

Perhaps, instead I'd suggest to support something like fail_fast/retry for ExecCommand
And add very simple strategy, like Command(ExecCommand)

So it will look like:

.with_wait_for(
  WaitFor::command(
    ExecCommand::new(["pg_isready"]).with_cmd_ready_condition(CmdWaitFor::exit_code(0))
  )
)

Copy link
Author

@sutt0n sutt0n Apr 5, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Apologies for the delay, been a busy week here with the weather we've been getting in Arkansas. 😅

I've pushed an update that I believe is something that aligns... I was hesitant on the CmdWaitFor::exit_code(0) in WaitFor::command(), but added it anyway.

...however, now that I think about it, we may want to not force that specific exit code, but allow users to specify one on their own. Thoughts?

Nevermind. I've allowed this to be more general than specific.

@sutt0n sutt0n requested a review from DDtKey April 5, 2025 22:28
Comment on lines 8 to 14
#[derive(Debug, Clone)]
pub struct CommandStrategy {
expected_code: i64,
poll_interval: Duration,
command: ExecCommand,
fail_fast: bool,
}
Copy link
Collaborator

@DDtKey DDtKey Apr 6, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It seems unchanged - we still have expected_code here which may contradict to ExecCommand wait conditions

The idea was to change how it's organized and implemented
#782 (comment)

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Apologies for this overlook -- updated!

@sutt0n sutt0n requested a review from DDtKey April 6, 2025 11:30
@sutt0n sutt0n force-pushed the feat/command-strategy branch from e9caf57 to cee5d7e Compare April 6, 2025 13:06
@sutt0n
Copy link
Author

sutt0n commented Apr 6, 2025

Force pushed to fix the commit message... hard to write Rust while holding a sleeping baby. 😅

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

WaitFor command exit with 0
2 participants