Skip to content

Commit

Permalink
Merge pull request #402 from nhatthm/master
Browse files Browse the repository at this point in the history
Expose HostConfig Resources via ContainerRequest
  • Loading branch information
gianarb committed Feb 3, 2022
2 parents a5da993 + 25488a2 commit 95e84a0
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 11 deletions.
11 changes: 6 additions & 5 deletions container.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ type Container interface {
StartLogProducer(context.Context) error
StopLogProducer() error
Name(context.Context) (string, error) // get container name
State(context.Context) (*types.ContainerState, error) //returns container's running state
State(context.Context) (*types.ContainerState, error) // returns container's running state
Networks(context.Context) ([]string, error) // get container networks
NetworkAliases(context.Context) (map[string][]string, error) // get container network aliases for a network
Exec(ctx context.Context, cmd []string) (int, error)
Expand Down Expand Up @@ -92,11 +92,12 @@ type ContainerRequest struct {
Privileged bool // for starting privileged container
Networks []string // for specifying network names
NetworkAliases map[string][]string // for specifying network aliases
User string // for specifying uid:gid
SkipReaper bool // indicates whether we skip setting up a reaper for this
ReaperImage string // alternative reaper image
AutoRemove bool // if set to true, the container will be removed from the host when stopped
NetworkMode container.NetworkMode
Resources container.Resources
User string // for specifying uid:gid
SkipReaper bool // indicates whether we skip setting up a reaper for this
ReaperImage string // alternative reaper image
AutoRemove bool // if set to true, the container will be removed from the host when stopped
AlwaysPullImage bool // Always pull image
ImagePlatform string // ImagePlatform describes the platform which the image runs on.
}
Expand Down
7 changes: 4 additions & 3 deletions docker.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,8 @@ import (
"github.com/google/uuid"
"github.com/magiconair/properties"
"github.com/moby/term"

specs "github.com/opencontainers/image-spec/specs-go/v1"

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

Expand Down Expand Up @@ -363,8 +363,8 @@ func (c *DockerContainer) CopyFileFromContainer(ctx context.Context, filePath st
}
tarReader := tar.NewReader(r)

//if we got here we have exactly one file in the TAR-stream
//so we advance the index by one so the next call to Read will start reading it
// if we got here we have exactly one file in the TAR-stream
// so we advance the index by one so the next call to Read will start reading it
_, err = tarReader.Next()
if err != nil {
return nil, err
Expand Down Expand Up @@ -822,6 +822,7 @@ func (p *DockerProvider) CreateContainer(ctx context.Context, req ContainerReque
AutoRemove: req.AutoRemove,
Privileged: req.Privileged,
NetworkMode: req.NetworkMode,
Resources: req.Resources,
}

endpointConfigs := map[string]*network.EndpointSettings{}
Expand Down
51 changes: 48 additions & 3 deletions docker_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ import (
"testing"
"time"

"github.com/docker/docker/api/types/container"
"github.com/docker/go-units"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"gotest.tools/v3/env"
Expand Down Expand Up @@ -129,10 +131,10 @@ func TestContainerWithHostNetworkOptions(t *testing.T) {

defer nginxC.Terminate(ctx)

//host, err := nginxC.Host(ctx)
//if err != nil {
// host, err := nginxC.Host(ctx)
// if err != nil {
// t.Errorf("Expected host %s. Got '%d'.", host, err)
//}
// }
//
endpoint, err := nginxC.Endpoint(ctx, "http")
if err != nil {
Expand Down Expand Up @@ -1699,6 +1701,49 @@ func TestDockerContainerCopyEmptyFileFromContainer(t *testing.T) {
assert.Empty(t, fileContentFromContainer)
}

func TestDockerContainerResources(t *testing.T) {
ctx := context.Background()

expected := []*units.Ulimit{
{
Name: "memlock",
Hard: -1,
Soft: -1,
},
{
Name: "nofile",
Hard: 65536,
Soft: 65536,
},
}

nginxC, err := GenericContainer(ctx, GenericContainerRequest{
ContainerRequest: ContainerRequest{
Image: "nginx",
ExposedPorts: []string{"80/tcp"},
WaitingFor: wait.ForListeningPort("80/tcp"),
Resources: container.Resources{
Ulimits: expected,
},
},
Started: true,
})
require.NoError(t, err)

defer nginxC.Terminate(ctx)

c, err := client.NewClientWithOpts(client.FromEnv)
require.NoError(t, err)

c.NegotiateAPIVersion(ctx)
containerID := nginxC.GetContainerID()

resp, err := c.ContainerInspect(ctx, containerID)
require.NoError(t, err)

assert.Equal(t, expected, resp.HostConfig.Ulimits)
}

func TestContainerWithReaperNetwork(t *testing.T) {
ctx := context.Background()
networks := []string{
Expand Down

0 comments on commit 95e84a0

Please sign in to comment.