fix: nil pointer dereference in HealthStrategy#802
fix: nil pointer dereference in HealthStrategy#802mdelapenya merged 6 commits intotestcontainers:mainfrom
Conversation
✅ Deploy Preview for testcontainers-go ready!
To edit notification comments on pull requests, go to your Netlify site settings. |
There was a problem hiding this comment.
I'm not sure about this 🤔 Do we want to eventually pass the test if the health is nil? Which comes with a second question: what do we want to do in the health strategy when the health is nil? Fail/Continue/Log?
EDIT: the above line was for this block: https://github.com/testcontainers/testcontainers-go/pull/802/files#diff-62f25e9df2042653e9484951d6537c4088f6580206cbaef6bb30d610bfdd6befR69-R74. So please do not get me wrong 🙏 I think we should check for nil, although considering different states.
In the current implementation we are considering that not having declared a Health in the container is exactly the same as not being healthy (we sleep). I'm not opposed to that, but what to discuss with you about it: should be consider not having Health in the state as healthy or not?
Hey, thanks for super-quick review and sorry for not explaining the tests better (I've now added comments, but please let me know if still not sufficient).
Indeed - in fact that test is meant to "emulate" observed container behavior: initially I've added another test (
Yes, this follows the same principle, as mentioned above, but it waits for it to either become non-nil, and if not it fails - if it does become non-nil, then it checks the state. Hopefully that clarifies, and the additional test proves that. |
2869121 to
687fabf
Compare
mdelapenya
left a comment
There was a problem hiding this comment.
Just a tiny suggestion in the new test method. Once we discuss on that, LGTM
mdelapenya
left a comment
There was a problem hiding this comment.
I'm approving the PR even without the suggestions I added on the style on how to assert.
Will merge it once you approve them or discard them for a different preference in the assert style
Thanks for t¡your time and patience contributing to the project 🚀
oh, I'm so sorry! On an unrelated note - I have the impression that some containers (most? all?) do not really set the BTW - while working on this, I was wondering adding an |
60d621a to
5f48667
Compare
Co-authored-by: Manuel de la Peña <[email protected]>
5f48667 to
22d2128
Compare
No worries, I totally understand :) I was not explicit, so not an issue at all
The State function, when it's called from a container, inspects the container, so it will take whatever it's in the Dockerfile. If the container you use does not have a Health it's because it's not there. // State returns container's running state
func (c *DockerContainer) State(ctx context.Context) (*types.ContainerState, error) {
inspect, err := c.inspectRawContainer(ctx)
if err != nil {
if c.raw != nil {
return c.raw.State, err
}
return nil, err
}
return inspect.State, nil
}
For this, you can use wait.ForHTTP https://golang.testcontainers.org/features/wait/http/ which, I think, comes with what you expect: check for status code, check in an HTTP endpoint, and check for a response. Is that what you are looking for? |
Co-authored-by: Manuel de la Peña <[email protected]>
Thanks, Manuel - I didn't know of the
Well, honestly I have no idea how I missed that one 🤦♂️ it's not even like there's a want of options!!! 👍 Have committed your suggestion too. |
* main: chore: update Docker labels for containers (testcontainers#813) fix: nil pointer dereference in HealthStrategy (testcontainers#802) fix: Synchronise writes to containers map (testcontainers#812) chore(deps): bump google.golang.org/api from 0.108.0 to 0.109.0 in /examples (testcontainers#810) chore(deps): bump cloud.google.com/go/spanner in /examples/spanner (testcontainers#806) chore: restructure Docker helper methods (testcontainers#799) Verify Reaper state to create new or return existing instance (#782) docs: add intel as user (testcontainers#798) chore: bump containerd in examples (testcontainers#797) chore(deps): bump github.com/containerd/containerd from 1.6.15 to 1.6.16 (testcontainers#793) chore: extract docker host calculation to an internal package (testcontainers#796) chore: run "go mod tidy" automatically when creating examples (testcontainers#794) chore: build images with backoff retries (testcontainers#792) fix: use right import package for compose in docs (testcontainers#791) chore(deps): bump google.golang.org/grpc from 1.52.1 to 1.52.3 in /examples (testcontainers#790) Add devcontainer file (#765) chore: check dependabot dependencies weekly (testcontainers#789) chore(deps): bump google.golang.org/grpc from 1.52.0 to 1.52.1 in /examples (#783) chore: support for titles in examples (#775)
* main: chore(deps): bump google.golang.org/grpc from 1.52.3 to 1.53.0 in /examples (testcontainers#827) chore(deps): bump github.com/containerd/containerd from 1.6.16 to 1.6.17 (testcontainers#817) chore(deps): bump golang.org/x/text from 0.6.0 to 0.7.0 (testcontainers#818) chore(deps): bump golang.org/x/sys from 0.4.0 to 0.5.0 (testcontainers#816) chore(deps): bump github.com/jackc/pgx/v4 in /examples/cockroachdb (testcontainers#819) chore: update Docker labels for containers (testcontainers#813) fix: nil pointer dereference in HealthStrategy (testcontainers#802) fix: Synchronise writes to containers map (testcontainers#812)

What does this PR do?
Fixes a panic caused by a
nilpointer dereference in theHealthStrategy.WaitUntilReadyfunction.See Issue #801
Why is it important?
Anyone using the
HealthStrategyfor their tests would see them failing with the panic.Related issues
How to test this PR
I have added the following tests, to cover the various scenarios:
The
TestWaitForHealthWithNiltest will cause the panic with the original code, but it does not after the PR is applied.