1
0
Fork 0
mirror of https://github.com/moby/moby.git synced 2022-11-09 12:21:53 -05:00

Add WithNetworkMode, WithExposedPorts, WithTty, WithWorkingDir to container helper functions

Signed-off-by: Yong Tang <yong.tang.github@outlook.com>
This commit is contained in:
Yong Tang 2018-02-13 15:45:40 +00:00
parent 9fcd2a0510
commit eaa1a0c218
5 changed files with 41 additions and 19 deletions

View file

@ -17,10 +17,7 @@ func TestExec(t *testing.T) {
ctx := context.Background()
client := request.NewAPIClient(t)
cID := container.Run(t, ctx, client, func(c *container.TestContainerConfig) {
c.Config.Tty = true
c.Config.WorkingDir = "/root"
})
cID := container.Run(t, ctx, client, container.WithTty(true), container.WithWorkingDir("/root"))
id, err := client.ContainerExecCreate(ctx, cID,
types.ExecConfig{

View file

@ -20,9 +20,7 @@ func TestHealthCheckWorkdir(t *testing.T) {
ctx := context.Background()
client := request.NewAPIClient(t)
cID := container.Run(t, ctx, client, func(c *container.TestContainerConfig) {
c.Config.Tty = true
c.Config.WorkingDir = "/foo"
cID := container.Run(t, ctx, client, container.WithTty(true), container.WithWorkingDir("/foo"), func(c *container.TestContainerConfig) {
c.Config.Healthcheck = &containertypes.HealthConfig{
Test: []string{"CMD-SHELL", "if [ \"$PWD\" = \"/foo\" ]; then exit 0; else exit 1; fi;"},
Interval: 50 * time.Millisecond,

View file

@ -28,9 +28,7 @@ func TestLinksEtcHostsContentMatch(t *testing.T) {
client := request.NewAPIClient(t)
ctx := context.Background()
cID := container.Run(t, ctx, client, container.WithCmd("cat", "/etc/hosts"), func(c *container.TestContainerConfig) {
c.HostConfig.NetworkMode = "host"
})
cID := container.Run(t, ctx, client, container.WithCmd("cat", "/etc/hosts"), container.WithNetworkMode("host"))
poll.WaitOn(t, containerIsStopped(ctx, client, cID), poll.WithDelay(100*time.Millisecond))

View file

@ -67,10 +67,7 @@ func TestNetworkLoopbackNat(t *testing.T) {
client := request.NewAPIClient(t)
ctx := context.Background()
cID := container.Run(t, ctx, client, container.WithCmd("sh", "-c", fmt.Sprintf("stty raw && nc -w 5 %s 8080", endpoint.String())), func(c *container.TestContainerConfig) {
c.Config.Tty = true
c.HostConfig.NetworkMode = "container:server"
})
cID := container.Run(t, ctx, client, container.WithCmd("sh", "-c", fmt.Sprintf("stty raw && nc -w 5 %s 8080", endpoint.String())), container.WithTty(true), container.WithNetworkMode("container:server"))
poll.WaitOn(t, containerIsStopped(ctx, client, cID), poll.WithDelay(100*time.Millisecond))
@ -91,10 +88,7 @@ func startServerContainer(t *testing.T, msg string, port int) string {
client := request.NewAPIClient(t)
ctx := context.Background()
cID := container.Run(t, ctx, client, container.WithCmd("sh", "-c", fmt.Sprintf("echo %q | nc -lp %d", msg, port)), func(c *container.TestContainerConfig) {
c.Config.ExposedPorts = map[nat.Port]struct{}{
nat.Port(fmt.Sprintf("%d/tcp", port)): {},
}
cID := container.Run(t, ctx, client, container.WithName("server"), container.WithCmd("sh", "-c", fmt.Sprintf("echo %q | nc -lp %d", msg, port)), container.WithExposedPorts(fmt.Sprintf("%d/tcp", port)), func(c *container.TestContainerConfig) {
c.HostConfig.PortBindings = nat.PortMap{
nat.Port(fmt.Sprintf("%d/tcp", port)): []nat.PortBinding{
{

View file

@ -1,6 +1,10 @@
package container
import "github.com/docker/docker/api/types/strslice"
import (
containertypes "github.com/docker/docker/api/types/container"
"github.com/docker/docker/api/types/strslice"
"github.com/docker/go-connections/nat"
)
// WithName sets the name of the container
func WithName(name string) func(*TestContainerConfig) {
@ -22,3 +26,34 @@ func WithCmd(cmds ...string) func(*TestContainerConfig) {
c.Config.Cmd = strslice.StrSlice(cmds)
}
}
// WithNetworkMode sets the network mode of the container
func WithNetworkMode(mode string) func(*TestContainerConfig) {
return func(c *TestContainerConfig) {
c.HostConfig.NetworkMode = containertypes.NetworkMode(mode)
}
}
// WithExposedPorts sets the exposed ports of the container
func WithExposedPorts(ports ...string) func(*TestContainerConfig) {
return func(c *TestContainerConfig) {
c.Config.ExposedPorts = map[nat.Port]struct{}{}
for _, port := range ports {
c.Config.ExposedPorts[nat.Port(port)] = struct{}{}
}
}
}
// WithTty sets the TTY mode of the container
func WithTty(tty bool) func(*TestContainerConfig) {
return func(c *TestContainerConfig) {
c.Config.Tty = tty
}
}
// WithWorkingDir sets the working dir of the container
func WithWorkingDir(dir string) func(*TestContainerConfig) {
return func(c *TestContainerConfig) {
c.Config.WorkingDir = dir
}
}