Skip to content
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

Expose HostConfig Resources via ContainerRequest #402

Merged
merged 2 commits into from
Feb 3, 2022
Merged
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
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