Skip to content

Commit

Permalink
[testcontainers#801] Fix nil pointer dereference in HealthStrategy
Browse files Browse the repository at this point in the history
  • Loading branch information
massenz committed Feb 8, 2023
1 parent 713a559 commit 33a27ca
Show file tree
Hide file tree
Showing 2 changed files with 82 additions and 1 deletion.
3 changes: 2 additions & 1 deletion wait/health.go
Expand Up @@ -2,6 +2,7 @@ package wait

import (
"context"
"github.com/docker/docker/api/types"
"time"
)

Expand Down Expand Up @@ -76,7 +77,7 @@ func (ws *HealthStrategy) WaitUntilReady(ctx context.Context, target StrategyTar
if err != nil {
return err
}
if state.Health.Status != "healthy" {
if state.Health == nil || state.Health.Status != types.Healthy {
time.Sleep(ws.PollInterval)
continue
}
Expand Down
80 changes: 80 additions & 0 deletions wait/health_test.go
@@ -0,0 +1,80 @@
package wait

import (
"context"
"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
}

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)

if err != nil && err == context.DeadlineExceeded {
return
}
t.Fatal(err)
}

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)

if err != nil {
t.Fatal(err)
}
}

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)
if err != nil {
t.Fatal(err)
}
}

0 comments on commit 33a27ca

Please sign in to comment.