Skip to content

Commit

Permalink
Merge branch 'main' into windows-tests
Browse files Browse the repository at this point in the history
* main:
  chore(deps): bump github.com/aws dependencies in /modules/localstack (testcontainers#1472)
  chore(deps): bump Google emulators dependencies in /examples (testcontainers#1471)
  all: fix goroutine leaks (testcontainers#1358)
  chore(deps): bump github.com/neo4j/neo4j-go-driver/v5 in /modules/neo4j (testcontainers#1427)
  • Loading branch information
mdelapenya committed Aug 9, 2023
2 parents 72afda4 + 5a7fbd6 commit 51ac8ec
Show file tree
Hide file tree
Showing 20 changed files with 282 additions and 235 deletions.
47 changes: 31 additions & 16 deletions docker.go
Original file line number Diff line number Diff line change
Expand Up @@ -245,6 +245,14 @@ func (c *DockerContainer) Stop(ctx context.Context, timeout *time.Duration) erro

// Terminate is used to kill the container. It is usually triggered by as defer function.
func (c *DockerContainer) Terminate(ctx context.Context) error {
select {
// close reaper if it was created
case c.terminationSignal <- true:
default:
}

defer c.provider.client.Close()

err := c.terminatingHook(ctx)
if err != nil {
return err
Expand All @@ -255,11 +263,6 @@ func (c *DockerContainer) Terminate(ctx context.Context) error {
return err
}

select {
// close reaper if it was created
case c.terminationSignal <- true:
default:
}
err = c.provider.client.ContainerRemove(ctx, c.GetContainerID(), types.ContainerRemoveOptions{
RemoveVolumes: true,
Force: true,
Expand All @@ -283,33 +286,29 @@ func (c *DockerContainer) Terminate(ctx context.Context) error {
}
}

if err := c.provider.client.Close(); err != nil {
return err
}

c.sessionID = uuid.UUID{}
c.isRunning = false
return nil
}

// update container raw info
func (c *DockerContainer) inspectRawContainer(ctx context.Context) (*types.ContainerJSON, error) {
defer c.provider.Close()
inspect, err := c.provider.client.ContainerInspect(ctx, c.ID)
if err != nil {
return nil, err
}
defer c.provider.Close()

c.raw = &inspect
return c.raw, nil
}

func (c *DockerContainer) inspectContainer(ctx context.Context) (*types.ContainerJSON, error) {
defer c.provider.Close()
inspect, err := c.provider.client.ContainerInspect(ctx, c.ID)
if err != nil {
return nil, err
}
defer c.provider.Close()

return &inspect, nil
}
Expand Down Expand Up @@ -739,13 +738,9 @@ func (n *DockerNetwork) Remove(ctx context.Context) error {
default:
}

err := n.provider.client.NetworkRemove(ctx, n.ID)
if err != nil {
return err
}
defer n.provider.Close()

return nil
return n.provider.client.NetworkRemove(ctx, n.ID)
}

// DockerProvider implements the ContainerProvider interface
Expand Down Expand Up @@ -913,6 +908,13 @@ func (p *DockerProvider) CreateContainer(ctx context.Context, req ContainerReque
}
}

// Cleanup on error, otherwise set termSignal to nil before successful return.
defer func() {
if termSignal != nil {
termSignal <- true
}
}()

if err = req.Validate(); err != nil {
return nil, err
}
Expand Down Expand Up @@ -1091,6 +1093,9 @@ func (p *DockerProvider) CreateContainer(ctx context.Context, req ContainerReque
return nil, err
}

// Disable cleanup on success
termSignal = nil

return c, nil
}

Expand Down Expand Up @@ -1306,6 +1311,13 @@ func (p *DockerProvider) CreateNetwork(ctx context.Context, req NetworkRequest)
}
}

// Cleanup on error, otherwise set termSignal to nil before successful return.
defer func() {
if termSignal != nil {
termSignal <- true
}
}()

response, err := p.client.NetworkCreate(ctx, req.Name, nc)
if err != nil {
return &DockerNetwork{}, err
Expand All @@ -1319,6 +1331,9 @@ func (p *DockerProvider) CreateNetwork(ctx context.Context, req NetworkRequest)
provider: p,
}

// Disable cleanup on success
termSignal = nil

return n, nil
}

Expand Down
1 change: 1 addition & 0 deletions docker_auth_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,7 @@ func removeImageFromLocalCache(t *testing.T, image string) {
if err != nil {
t.Log("could not create client to cleanup registry: ", err)
}
defer testcontainersClient.Close()

_, err = testcontainersClient.ImageRemove(ctx, image, types.ImageRemoveOptions{
Force: true,
Expand Down
43 changes: 38 additions & 5 deletions docker_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -200,9 +200,14 @@ func TestContainerWithHostNetworkOptions_UseExposePortsFromImageConfigs(t *testi
t.Errorf("Expected server endpoint. Got '%v'.", err)
}

_, err = http.Get(endpoint)
resp, err := http.Get(endpoint)
if err != nil {
t.Errorf("Expected OK response. Got '%d'.", err)
t.Fatal(err)
}
defer resp.Body.Close()

if resp.StatusCode != http.StatusOK {
t.Errorf("Expected status code %d. Got %d.", http.StatusOK, resp.StatusCode)
}
}

Expand Down Expand Up @@ -723,6 +728,8 @@ func TestTwoContainersExposingTheSamePort(t *testing.T) {
if err != nil {
t.Fatal(err)
}
defer resp.Body.Close()

if resp.StatusCode != http.StatusOK {
t.Errorf("Expected status code %d. Got %d.", http.StatusOK, resp.StatusCode)
}
Expand All @@ -736,6 +743,8 @@ func TestTwoContainersExposingTheSamePort(t *testing.T) {
if err != nil {
t.Fatal(err)
}
defer resp.Body.Close()

if resp.StatusCode != http.StatusOK {
t.Errorf("Expected status code %d. Got %d.", http.StatusOK, resp.StatusCode)
}
Expand Down Expand Up @@ -766,6 +775,8 @@ func TestContainerCreation(t *testing.T) {
if err != nil {
t.Fatal(err)
}
defer resp.Body.Close()

if resp.StatusCode != http.StatusOK {
t.Errorf("Expected status code %d. Got %d.", http.StatusOK, resp.StatusCode)
}
Expand Down Expand Up @@ -893,6 +904,8 @@ func TestContainerCreationWithName(t *testing.T) {
if err != nil {
t.Fatal(err)
}
defer resp.Body.Close()

if resp.StatusCode != http.StatusOK {
t.Errorf("Expected status code %d. Got %d.", http.StatusOK, resp.StatusCode)
}
Expand Down Expand Up @@ -925,6 +938,8 @@ func TestContainerCreationAndWaitForListeningPortLongEnough(t *testing.T) {
if err != nil {
t.Fatal(err)
}
defer resp.Body.Close()

if resp.StatusCode != http.StatusOK {
t.Errorf("Expected status code %d. Got %d.", http.StatusOK, resp.StatusCode)
}
Expand Down Expand Up @@ -977,8 +992,10 @@ func TestContainerRespondsWithHttp200ForIndex(t *testing.T) {
}
resp, err := http.Get(origin)
if err != nil {
t.Error(err)
t.Fatal(err)
}
defer resp.Body.Close()

if resp.StatusCode != http.StatusOK {
t.Errorf("Expected status code %d. Got %d.", http.StatusOK, resp.StatusCode)
}
Expand Down Expand Up @@ -1088,6 +1105,7 @@ func Test_BuildContainerFromDockerfileWithBuildArgs(t *testing.T) {
if err != nil {
t.Fatal(err)
}
defer resp.Body.Close()

body, err := io.ReadAll(resp.Body)
if err != nil {
Expand Down Expand Up @@ -1378,6 +1396,8 @@ func TestContainerCreationWithBindAndVolume(t *testing.T) {
t.Cleanup(func() {
ctx, cnl := context.WithTimeout(context.Background(), 5*time.Second)
defer cnl()
defer dockerCli.Close()

err := dockerCli.VolumeRemove(ctx, volumeName, true)
if err != nil {
t.Fatal(err)
Expand Down Expand Up @@ -1473,7 +1493,7 @@ func TestContainerNonExistentImage(t *testing.T) {
t.Fatalf("err should be a ctx cancelled error %v", err)
}

terminateContainerOnEnd(t, ctx, c)
terminateContainerOnEnd(t, context.Background(), c) // use non-cancelled context
})
}

Expand Down Expand Up @@ -1518,6 +1538,7 @@ func TestContainerCustomPlatformImage(t *testing.T) {

dockerCli, err := NewDockerClient()
require.NoError(t, err)
defer dockerCli.Close()

ctr, err := dockerCli.ContainerInspect(ctx, c.GetContainerID())
assert.NoError(t, err)
Expand Down Expand Up @@ -1557,6 +1578,7 @@ func readHostname(tb testing.TB, containerId string) string {
if err != nil {
tb.Fatalf("Failed to create Docker client: %v", err)
}
defer containerClient.Close()

containerDetails, err := containerClient.ContainerInspect(context.Background(), containerId)
if err != nil {
Expand Down Expand Up @@ -1666,6 +1688,7 @@ func TestDockerCreateContainerWithFiles(t *testing.T) {
},
Started: false,
})
terminateContainerOnEnd(t, ctx, nginxC)

if err != nil {
require.Contains(t, err.Error(), tc.errMsg)
Expand Down Expand Up @@ -1750,6 +1773,7 @@ func TestDockerCreateContainerWithDirs(t *testing.T) {
},
Started: false,
})
terminateContainerOnEnd(t, ctx, nginxC)

require.True(t, (err != nil) == tc.hasError)
if err == nil {
Expand Down Expand Up @@ -1827,6 +1851,7 @@ func TestDockerContainerCopyFileFromContainer(t *testing.T) {
if err != nil {
t.Fatal(err)
}
defer reader.Close()

fileContentFromContainer, err := io.ReadAll(reader)
if err != nil {
Expand Down Expand Up @@ -1865,6 +1890,7 @@ func TestDockerContainerCopyEmptyFileFromContainer(t *testing.T) {
if err != nil {
t.Fatal(err)
}
defer reader.Close()

fileContentFromContainer, err := io.ReadAll(reader)
if err != nil {
Expand Down Expand Up @@ -1942,11 +1968,16 @@ func TestContainerWithReaperNetwork(t *testing.T) {
Name: nw,
Attachable: true,
}
_, err := GenericNetwork(ctx, GenericNetworkRequest{
n, err := GenericNetwork(ctx, GenericNetworkRequest{
ProviderType: providerType,
NetworkRequest: nr,
})
assert.Nil(t, err)
// use t.Cleanup to run after terminateContainerOnEnd
t.Cleanup(func() {
err := n.Remove(ctx)
assert.NoError(t, err)
})
}

req := ContainerRequest{
Expand Down Expand Up @@ -2108,6 +2139,8 @@ func TestGetGatewayIP(t *testing.T) {
if err != nil {
t.Fatal(err)
}
defer provider.Close()

ip, err := provider.(*DockerProvider).GetGatewayIP(context.Background())
if err != nil {
t.Fatal(err)
Expand Down
26 changes: 13 additions & 13 deletions examples/bigtable/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,17 @@ module github.com/testcontainers/testcontainers-go/examples/bigtable
go 1.19

require (
cloud.google.com/go/bigtable v1.18.1
cloud.google.com/go/bigtable v1.19.0
github.com/testcontainers/testcontainers-go v0.22.0
google.golang.org/api v0.134.0
google.golang.org/api v0.136.0
google.golang.org/grpc v1.57.0
)

require (
cloud.google.com/go v0.110.4 // indirect
cloud.google.com/go/compute v1.20.1 // indirect
cloud.google.com/go v0.110.6 // indirect
cloud.google.com/go/compute v1.23.0 // indirect
cloud.google.com/go/compute/metadata v0.2.3 // indirect
cloud.google.com/go/iam v1.1.0 // indirect
cloud.google.com/go/iam v1.1.1 // indirect
cloud.google.com/go/longrunning v0.5.1 // indirect
dario.cat/mergo v1.0.0 // indirect
github.com/Azure/go-ansiterm v0.0.0-20210617225240-d185dfc1b5a1 // indirect
Expand Down Expand Up @@ -51,20 +51,20 @@ require (
github.com/pkg/errors v0.9.1 // indirect
github.com/sirupsen/logrus v1.9.0 // indirect
go.opencensus.io v0.24.0 // indirect
golang.org/x/crypto v0.11.0 // indirect
golang.org/x/crypto v0.12.0 // indirect
golang.org/x/exp v0.0.0-20230510235704-dd950f8aeaea // indirect
golang.org/x/mod v0.9.0 // indirect
golang.org/x/net v0.12.0 // indirect
golang.org/x/oauth2 v0.10.0 // indirect
golang.org/x/net v0.14.0 // indirect
golang.org/x/oauth2 v0.11.0 // indirect
golang.org/x/sync v0.3.0 // indirect
golang.org/x/sys v0.10.0 // indirect
golang.org/x/text v0.11.0 // indirect
golang.org/x/sys v0.11.0 // indirect
golang.org/x/text v0.12.0 // indirect
golang.org/x/tools v0.7.0 // indirect
golang.org/x/xerrors v0.0.0-20220907171357-04be3eba64a2 // indirect
google.golang.org/appengine v1.6.7 // indirect
google.golang.org/genproto v0.0.0-20230706204954-ccb25ca9f130 // indirect
google.golang.org/genproto/googleapis/api v0.0.0-20230706204954-ccb25ca9f130 // indirect
google.golang.org/genproto/googleapis/rpc v0.0.0-20230720185612-659f7aaaa771 // indirect
google.golang.org/genproto v0.0.0-20230803162519-f966b187b2e5 // indirect
google.golang.org/genproto/googleapis/api v0.0.0-20230803162519-f966b187b2e5 // indirect
google.golang.org/genproto/googleapis/rpc v0.0.0-20230807174057-1744710a1577 // indirect
google.golang.org/protobuf v1.31.0 // indirect
)

Expand Down

0 comments on commit 51ac8ec

Please sign in to comment.