-
-
Notifications
You must be signed in to change notification settings - Fork 602
fix: nil pointer dereference in HealthStrategy #802
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
Merged
mdelapenya
merged 6 commits into
testcontainers:main
from
massenz:801-fix-HealthStrategy-nil
Feb 9, 2023
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
33a27ca
[#801] Fix nil pointer dereference in HealthStrategy
massenz bb26532
Added another test to prove HealthStrategy fails for nil Health
massenz 8b28c9d
Refactored error checking
massenz 45f387e
Update wait/health_test.go
massenz 22d2128
Update wait/health_test.go
massenz 701b216
Update wait/health_test.go
massenz File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,93 @@ | ||
| package wait | ||
|
|
||
| import ( | ||
| "context" | ||
| "errors" | ||
| "github.com/stretchr/testify/assert" | ||
| "io" | ||
| "testing" | ||
| "time" | ||
|
|
||
| "github.com/docker/docker/api/types" | ||
| "github.com/docker/go-connections/nat" | ||
| tcexec "github.com/testcontainers/testcontainers-go/exec" | ||
| ) | ||
|
|
||
| type healthStrategyTarget struct { | ||
| Health *types.Health | ||
| } | ||
|
|
||
| func (st healthStrategyTarget) Host(ctx context.Context) (string, error) { | ||
| return "", nil | ||
| } | ||
|
|
||
| func (st healthStrategyTarget) Ports(ctx context.Context) (nat.PortMap, error) { | ||
| return nil, nil | ||
| } | ||
|
|
||
| func (st healthStrategyTarget) MappedPort(ctx context.Context, n nat.Port) (nat.Port, error) { | ||
| return n, nil | ||
| } | ||
|
|
||
| func (st healthStrategyTarget) Logs(ctx context.Context) (io.ReadCloser, error) { | ||
| return nil, nil | ||
| } | ||
|
|
||
| func (st healthStrategyTarget) Exec(ctx context.Context, cmd []string, options ...tcexec.ProcessOption) (int, io.Reader, error) { | ||
| return 0, nil, nil | ||
| } | ||
|
|
||
| func (st healthStrategyTarget) State(ctx context.Context) (*types.ContainerState, error) { | ||
| return &types.ContainerState{Health: st.Health}, nil | ||
| } | ||
|
|
||
| // TestWaitForHealthTimesOutForUnhealthy confirms that an unhealthy container will eventually | ||
| // time out. | ||
| func TestWaitForHealthTimesOutForUnhealthy(t *testing.T) { | ||
| target := healthStrategyTarget{Health: &types.Health{Status: types.Unhealthy}} | ||
| wg := NewHealthStrategy().WithStartupTimeout(100 * time.Millisecond) | ||
| err := wg.WaitUntilReady(context.Background(), target) | ||
|
|
||
| assert.NotNil(t, err) | ||
| assert.True(t, errors.Is(err, context.DeadlineExceeded)) | ||
| } | ||
|
|
||
| // TestWaitForHealthSucceeds ensures that a healthy container always succeeds. | ||
| func TestWaitForHealthSucceeds(t *testing.T) { | ||
| target := healthStrategyTarget{Health: &types.Health{Status: types.Healthy}} | ||
| wg := NewHealthStrategy().WithStartupTimeout(100 * time.Millisecond) | ||
| err := wg.WaitUntilReady(context.Background(), target) | ||
|
|
||
| assert.Nil(t, err) | ||
| } | ||
|
|
||
| // TestWaitForHealthWithNil checks that an initial `nil` Health will not casue a panic, | ||
| // and if the container eventually becomes healthy, the HealthStrategy will succeed. | ||
| func TestWaitForHealthWithNil(t *testing.T) { | ||
| target := &healthStrategyTarget{Health: nil} | ||
| wg := NewHealthStrategy(). | ||
| WithStartupTimeout(500 * time.Millisecond). | ||
| WithPollInterval(100 * time.Millisecond) | ||
|
|
||
| go func(target *healthStrategyTarget) { | ||
| // wait a bit to simulate startup time and give check time to at least | ||
| // try a few times with a nil Health | ||
| time.Sleep(200 * time.Millisecond) | ||
| target.Health = &types.Health{Status: types.Healthy} | ||
| }(target) | ||
|
|
||
| err := wg.WaitUntilReady(context.Background(), target) | ||
| assert.Nil(t, err) | ||
| } | ||
|
|
||
| // TestWaitFailsForNilHealth checks that Health always nil fails (but will NOT cause a panic) | ||
| func TestWaitFailsForNilHealth(t *testing.T) { | ||
| target := &healthStrategyTarget{Health: nil} | ||
| wg := NewHealthStrategy(). | ||
| WithStartupTimeout(500 * time.Millisecond). | ||
| WithPollInterval(100 * time.Millisecond) | ||
|
|
||
| err := wg.WaitUntilReady(context.Background(), target) | ||
| assert.NotNil(t, err) | ||
| assert.True(t, errors.Is(err, context.DeadlineExceeded)) | ||
| } |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.