testcontainers/testcontainers-rs

`ExecResult::exit_code` returns `None` due to command not finishing

tqwewe opened this issue · 3 comments

The ExecResult type returned from ContainerAsync::exec does not seem to represent a the command being completed, instead it returns before the executed command completes.

I would assume I can use ExecResult::exit_code().await to wait for the command to fully run and get the exit code, but instead it returns an Option::None when the command has not completed.

One workaround I found is to call ExecResult::stdout_to_vec().await and ExecResult::stderr_to_vec().await to stream the outputs until completion before calling ExecResult::exit_code().await. However this is far from ideal, as I need to await both these streams in order to be sure the command has completed. It would be nice if there's a ExecResult::wait().await method, or the ExecResult::exit_code().await command waits before returning the code.

It's generally expected, see the documentation for ExecResult::exit_code

Returns the exit code of the executed command. If the command has not yet exited, this will return None.

In order to achieve your goal you need to utilize with_cmd_ready_conditions to specify what you're waiting for. Particularly - CmdWaitFor::exit_code

Did you try?

I guess it's worth to add mention of with_cmd_ready_condition as part of ExecResult documentation 🤔

But the interface is expected, since sometimes it's necessary to spawn some background command without waiting for its results. Or in case you want to have some custom logic which periodically checks the status of the command.

Ah no I actually completely missed the ready condition for commands 🤦🏿 my mistake.

I see CmdWaitFor::exit_code, which is probably what I want. Though it seems like there's not really any wait for condition for waiting for the command to execute completely no matter the exit code?

Yes, you're right. It will wait for particular exit code and will return an error if actual one doesn't match with expected (ExecError::ExitCodeMismatch).

We can consider shortcut "WaitCmdFor::Finish" which will ignore the code. However even right now it should be possible by handling the error returned