Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 18 additions & 6 deletions compose_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,7 @@ func TestLocalDockerCompose(t *testing.T) {
Invoke()
checkIfError(t, err)
}

func TestDockerComposeStrategyForInvalidService(t *testing.T) {
path := "./testresources/docker-compose-simple.yml"

Expand All @@ -121,7 +122,9 @@ func TestDockerComposeStrategyForInvalidService(t *testing.T) {
err := compose.
WithCommand([]string{"up", "-d"}).
// Appending with _1 as given in the Java Test-Containers Example
WithExposedService("mysql_1", 13306, wait.NewLogStrategy("started").WithStartupTimeout(10*time.Second).WithOccurrence(1)).
WithExposedService("mysql_1", 13306, wait.NewLogStrategy("started").
WithTimeout(10*time.Second).
WithOccurrence(1)).
Invoke()
assert.NotEqual(t, err.Error, nil, "Expected error to be thrown because service with wait strategy is not running")

Expand All @@ -144,7 +147,9 @@ func TestDockerComposeWithWaitLogStrategy(t *testing.T) {
err := compose.
WithCommand([]string{"up", "-d"}).
// Appending with _1 as given in the Java Test-Containers Example
WithExposedService("mysql_1", 13306, wait.NewLogStrategy("started").WithStartupTimeout(10*time.Second).WithOccurrence(1)).
WithExposedService("mysql_1", 13306, wait.NewLogStrategy("started").
WithTimeout(10*time.Second).
WithOccurrence(1)).
Invoke()
checkIfError(t, err)

Expand All @@ -170,7 +175,9 @@ func TestDockerComposeWithWaitHTTPStrategy(t *testing.T) {
WithEnv(map[string]string{
"bar": "BAR",
}).
WithExposedService("nginx_1", 9080, wait.NewHTTPStrategy("/").WithPort("80/tcp").WithStartupTimeout(10*time.Second)).
WithExposedService("nginx_1", 9080, wait.NewHTTPStrategy("/").
WithPort("80/tcp").
WithTimeout(10*time.Second)).
Invoke()
checkIfError(t, err)

Expand Down Expand Up @@ -214,8 +221,11 @@ func TestDockerComposeWithMultipleWaitStrategies(t *testing.T) {

err := compose.
WithCommand([]string{"up", "-d"}).
WithExposedService("mysql_1", 13306, wait.NewLogStrategy("started").WithStartupTimeout(10*time.Second)).
WithExposedService("nginx_1", 9080, wait.NewHTTPStrategy("/").WithPort("80/tcp").WithStartupTimeout(10*time.Second)).
WithExposedService("mysql_1", 13306, wait.NewLogStrategy("started").
WithTimeout(10*time.Second)).
WithExposedService("nginx_1", 9080, wait.NewHTTPStrategy("/").
WithPort("80/tcp").
WithTimeout(10*time.Second)).
Invoke()
checkIfError(t, err)

Expand All @@ -241,7 +251,9 @@ func TestDockerComposeWithFailedStrategy(t *testing.T) {
WithEnv(map[string]string{
"bar": "BAR",
}).
WithExposedService("nginx_1", 9080, wait.NewHTTPStrategy("/").WithPort("8080/tcp").WithStartupTimeout(5*time.Second)).
WithExposedService("nginx_1", 9080, wait.NewHTTPStrategy("/").
WithPort("8080/tcp").
WithTimeout(5*time.Second)).
Invoke()
// Verify that an error is thrown and not nil
// A specific error message matcher is not asserted since the docker library can change the return message, breaking this test
Expand Down
14 changes: 6 additions & 8 deletions container_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ import (
)

func Test_ContainerValidation(t *testing.T) {

type ContainerValidationTestCase struct {
Name string
ExpectedError error
Expand Down Expand Up @@ -66,7 +65,6 @@ func Test_ContainerValidation(t *testing.T) {
}
})
}

}

func Test_GetDockerfile(t *testing.T) {
Expand Down Expand Up @@ -247,7 +245,8 @@ func Test_BuildImageWithContexts(t *testing.T) {
Context: testCase.ContextPath,
Dockerfile: testCase.Dockerfile,
},
WaitingFor: wait.ForLog(testCase.ExpectedEchoOutput).WithStartupTimeout(1 * time.Minute),
WaitingFor: wait.ForLog(testCase.ExpectedEchoOutput).
WithTimeout(1 * time.Minute),
}

c, err := GenericContainer(ctx, GenericContainerRequest{
Expand All @@ -263,18 +262,17 @@ func Test_BuildImageWithContexts(t *testing.T) {
} else {
c.Terminate(ctx)
}

})

}
}

func Test_GetLogsFromFailedContainer(t *testing.T) {
ctx := context.Background()
req := ContainerRequest{
Image: "alpine",
Cmd: []string{"echo", "-n", "I was not expecting this"},
WaitingFor: wait.ForLog("I was expecting this").WithStartupTimeout(5 * time.Second),
Image: "alpine",
Cmd: []string{"echo", "-n", "I was not expecting this"},
WaitingFor: wait.ForLog("I was expecting this").
WithTimeout(5 * time.Second),
}

c, err := GenericContainer(ctx, GenericContainerRequest{
Expand Down
28 changes: 11 additions & 17 deletions docker_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@ package testcontainers

import (
"context"
"database/sql"
"encoding/json"
"errors"
"fmt"
"github.com/stretchr/testify/assert"
"io/ioutil"
"math/rand"
"net/http"
Expand All @@ -16,19 +16,17 @@ import (
"testing"
"time"

"github.com/docker/docker/errdefs"

"github.com/docker/docker/api/types/volume"

"database/sql"
// Import mysql into the scope of this package (required)
_ "github.com/go-sql-driver/mysql"

"github.com/docker/docker/api/types"
"github.com/docker/docker/api/types/filters"
"github.com/docker/docker/api/types/volume"
"github.com/docker/docker/client"
"github.com/docker/docker/errdefs"
"github.com/docker/go-connections/nat"
"github.com/go-redis/redis"
"github.com/stretchr/testify/assert"
"github.com/testcontainers/testcontainers-go/wait"
)

Expand Down Expand Up @@ -58,7 +56,6 @@ func TestContainerAttachedToNewNetwork(t *testing.T) {
CheckDuplicate: true,
},
})

if err != nil {
t.Fatal(err)
}
Expand Down Expand Up @@ -694,7 +691,8 @@ func TestContainerCreationTimesOut(t *testing.T) {
ExposedPorts: []string{
"80/tcp",
},
WaitingFor: wait.ForListeningPort("80").WithStartupTimeout(1 * time.Second),
WaitingFor: wait.ForListeningPort("80").
WithTimeout(1 * time.Second),
},
Started: true,
})
Expand Down Expand Up @@ -756,7 +754,8 @@ func TestContainerCreationTimesOutWithHttp(t *testing.T) {
ExposedPorts: []string{
"80/tcp",
},
WaitingFor: wait.ForHTTP("/").WithStartupTimeout(1 * time.Second),
WaitingFor: wait.ForHTTP("/").
WithTimeout(1 * time.Second),
},
Started: true,
})
Expand All @@ -781,7 +780,8 @@ func TestContainerCreationWaitsForLogContextTimeout(t *testing.T) {
"MYSQL_ROOT_PASSWORD": "password",
"MYSQL_DATABASE": "database",
},
WaitingFor: wait.ForLog("test context timeout").WithStartupTimeout(1 * time.Second),
WaitingFor: wait.ForLog("test context timeout").
WithTimeout(1 * time.Second),
}
_, err := GenericContainer(ctx, GenericContainerRequest{
ContainerRequest: req,
Expand Down Expand Up @@ -1024,7 +1024,6 @@ func TestContainerCreationWaitsForLogAndPortContextTimeout(t *testing.T) {
if err == nil {
t.Fatal("Expected timeout")
}

}

func TestContainerCreationWaitingForHostPort(t *testing.T) {
Expand Down Expand Up @@ -1092,7 +1091,6 @@ func TestContainerCreationWaitsForLogAndPort(t *testing.T) {
ContainerRequest: req,
Started: true,
})

if err != nil {
t.Fatal(err)
}
Expand All @@ -1112,7 +1110,6 @@ func TestContainerCreationWaitsForLogAndPort(t *testing.T) {
"root", "password", host, port, "database")

db, err := sql.Open("mysql", connectionString)

if err != nil {
t.Fatal(err)
}
Expand All @@ -1122,7 +1119,6 @@ func TestContainerCreationWaitsForLogAndPort(t *testing.T) {
if err = db.Ping(); err != nil {
t.Errorf("error pinging db: %+v\n", err)
}

}

func TestCMD(t *testing.T) {
Expand All @@ -1146,7 +1142,6 @@ func TestCMD(t *testing.T) {
ContainerRequest: req,
Started: true,
})

if err != nil {
t.Fatal(err)
}
Expand Down Expand Up @@ -1176,7 +1171,6 @@ func TestEntrypoint(t *testing.T) {
ContainerRequest: req,
Started: true,
})

if err != nil {
t.Fatal(err)
}
Expand Down Expand Up @@ -1322,7 +1316,7 @@ func TestContainerWithTmpFs(t *testing.T) {
}
}()

var path = "/testtmpfs/test.file"
path := "/testtmpfs/test.file"

c, err := container.Exec(ctx, []string{"ls", path})
if err != nil {
Expand Down
6 changes: 3 additions & 3 deletions network_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,10 @@ package testcontainers
import (
"context"
"fmt"
"github.com/testcontainers/testcontainers-go/wait"
"testing"
"time"

"github.com/testcontainers/testcontainers-go/wait"
)

// Create a network using a provider. By default it is Docker.
Expand Down Expand Up @@ -60,7 +61,6 @@ func Test_MultipleContainersInTheNewNetwork(t *testing.T) {
net, err := GenericNetwork(ctx, GenericNetworkRequest{
NetworkRequest: networkRequest,
})

if err != nil {
t.Fatal("cannot create network")
}
Expand All @@ -82,7 +82,7 @@ func Test_MultipleContainersInTheNewNetwork(t *testing.T) {
env["RABBITMQ_DEFAULT_USER"] = "admin"
env["RABBITMQ_DEFAULT_PASS"] = "Password1"
hp := wait.ForListeningPort("5672/tcp")
hp.WithStartupTimeout(3 * time.Minute)
hp.WithTimeout(3 * time.Minute)
amqpRequest := ContainerRequest{
Image: "rabbitmq:management-alpine",
ExposedPorts: []string{"15672/tcp", "5672/tcp"},
Expand Down
22 changes: 14 additions & 8 deletions wait/health.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ var _ Strategy = (*HealthStrategy)(nil)

// HealthStrategy will wait until the container becomes healthy
type HealthStrategy struct {
// all Strategies should have a startupTimeout to avoid waiting infinitely
startupTimeout time.Duration
// all Strategies should have a timeout to avoid waiting infinitely
timeout time.Duration

// additional properties
PollInterval time.Duration
Expand All @@ -20,19 +20,25 @@ type HealthStrategy struct {
// NewHealthStrategy constructs with polling interval of 100 milliseconds and startup timeout of 60 seconds by default
func NewHealthStrategy() *HealthStrategy {
return &HealthStrategy{
startupTimeout: defaultStartupTimeout(),
PollInterval: defaultPollInterval(),
timeout: defaultTimeout(),
PollInterval: defaultPollInterval(),
}

}

// fluent builders for each property
// since go has neither covariance nor generics, the return type must be the type of the concrete implementation
// this is true for all properties, even the "shared" ones like startupTimeout

// WithStartupTimeout can be used to change the default startup timeout
func (ws *HealthStrategy) WithStartupTimeout(startupTimeout time.Duration) *HealthStrategy {
ws.startupTimeout = startupTimeout
//
// Deprecated: use WithTimeout instead
func (ws *HealthStrategy) WithStartupTimeout(timeout time.Duration) *HealthStrategy {
return ws.WithTimeout(timeout)
}

// WithTimeout can be used to change the default startup timeout
func (ws *HealthStrategy) WithTimeout(timeout time.Duration) *HealthStrategy {
ws.timeout = timeout
return ws
}

Expand All @@ -55,7 +61,7 @@ func ForHealthCheck() *HealthStrategy {
// WaitUntilReady implements Strategy.WaitUntilReady
func (ws *HealthStrategy) WaitUntilReady(ctx context.Context, target StrategyTarget) (err error) {
// limit context to exitTimeout
ctx, cancelContext := context.WithTimeout(ctx, ws.startupTimeout)
ctx, cancelContext := context.WithTimeout(ctx, ws.timeout)
defer cancelContext()

for {
Expand Down
Loading