mirror of
https://github.com/moby/moby.git
synced 2022-11-09 12:21:53 -05:00
Merge pull request #36192 from yongtang/02012018-docker_cli_nat_test
Migrate some of the nat tests in integration-cli to api tests
This commit is contained in:
commit
090c5bf25a
2 changed files with 147 additions and 88 deletions
|
@ -1,88 +0,0 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"io/ioutil"
|
||||
"net"
|
||||
"strings"
|
||||
|
||||
"github.com/docker/docker/integration-cli/checker"
|
||||
"github.com/docker/docker/integration-cli/cli"
|
||||
"github.com/go-check/check"
|
||||
)
|
||||
|
||||
func startServerContainer(c *check.C, msg string, port int) string {
|
||||
name := "server"
|
||||
cmd := []string{
|
||||
"run",
|
||||
"--name",
|
||||
name,
|
||||
"-d",
|
||||
"-p", fmt.Sprintf("%d:%d", port, port),
|
||||
"busybox",
|
||||
"sh", "-c", fmt.Sprintf("echo %q | nc -lp %d", msg, port),
|
||||
}
|
||||
cli.DockerCmd(c, cmd...)
|
||||
cli.WaitRun(c, name)
|
||||
return name
|
||||
}
|
||||
|
||||
func getExternalAddress(c *check.C) net.IP {
|
||||
iface, err := net.InterfaceByName("eth0")
|
||||
if err != nil {
|
||||
c.Skip(fmt.Sprintf("Test not running with `make test`. Interface eth0 not found: %v", err))
|
||||
}
|
||||
|
||||
ifaceAddrs, err := iface.Addrs()
|
||||
c.Assert(err, check.IsNil)
|
||||
c.Assert(ifaceAddrs, checker.Not(checker.HasLen), 0)
|
||||
|
||||
ifaceIP, _, err := net.ParseCIDR(ifaceAddrs[0].String())
|
||||
c.Assert(err, check.IsNil)
|
||||
|
||||
return ifaceIP
|
||||
}
|
||||
|
||||
func (s *DockerSuite) TestNetworkNat(c *check.C) {
|
||||
testRequires(c, DaemonIsLinux, SameHostDaemon)
|
||||
msg := "it works"
|
||||
startServerContainer(c, msg, 8080)
|
||||
endpoint := getExternalAddress(c)
|
||||
conn, err := net.Dial("tcp", fmt.Sprintf("%s:%d", endpoint.String(), 8080))
|
||||
c.Assert(err, check.IsNil)
|
||||
|
||||
data, err := ioutil.ReadAll(conn)
|
||||
conn.Close()
|
||||
c.Assert(err, check.IsNil)
|
||||
|
||||
final := strings.TrimRight(string(data), "\n")
|
||||
c.Assert(final, checker.Equals, msg)
|
||||
}
|
||||
|
||||
func (s *DockerSuite) TestNetworkLocalhostTCPNat(c *check.C) {
|
||||
testRequires(c, DaemonIsLinux, SameHostDaemon)
|
||||
var (
|
||||
msg = "hi yall"
|
||||
)
|
||||
startServerContainer(c, msg, 8081)
|
||||
conn, err := net.Dial("tcp", "localhost:8081")
|
||||
c.Assert(err, check.IsNil)
|
||||
|
||||
data, err := ioutil.ReadAll(conn)
|
||||
conn.Close()
|
||||
c.Assert(err, check.IsNil)
|
||||
|
||||
final := strings.TrimRight(string(data), "\n")
|
||||
c.Assert(final, checker.Equals, msg)
|
||||
}
|
||||
|
||||
func (s *DockerSuite) TestNetworkLoopbackNat(c *check.C) {
|
||||
testRequires(c, DaemonIsLinux, SameHostDaemon, NotUserNamespace)
|
||||
msg := "it works"
|
||||
startServerContainer(c, msg, 8080)
|
||||
endpoint := getExternalAddress(c)
|
||||
out, _ := dockerCmd(c, "run", "-t", "--net=container:server", "busybox",
|
||||
"sh", "-c", fmt.Sprintf("stty raw && nc -w 5 %s 8080", endpoint.String()))
|
||||
final := strings.TrimRight(string(out), "\n")
|
||||
c.Assert(final, checker.Equals, msg)
|
||||
}
|
147
integration/container/nat_test.go
Normal file
147
integration/container/nat_test.go
Normal file
|
@ -0,0 +1,147 @@
|
|||
package container
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"context"
|
||||
"fmt"
|
||||
"io"
|
||||
"io/ioutil"
|
||||
"net"
|
||||
"strings"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/docker/docker/api/types"
|
||||
"github.com/docker/docker/api/types/container"
|
||||
"github.com/docker/docker/api/types/network"
|
||||
"github.com/docker/docker/integration/util/request"
|
||||
"github.com/docker/go-connections/nat"
|
||||
"github.com/gotestyourself/gotestyourself/poll"
|
||||
"github.com/gotestyourself/gotestyourself/skip"
|
||||
"github.com/stretchr/testify/assert"
|
||||
"github.com/stretchr/testify/require"
|
||||
)
|
||||
|
||||
func TestNetworkNat(t *testing.T) {
|
||||
skip.If(t, !testEnv.IsLocalDaemon())
|
||||
|
||||
defer setupTest(t)()
|
||||
|
||||
msg := "it works"
|
||||
startServerContainer(t, msg, 8080)
|
||||
|
||||
endpoint := getExternalAddress(t)
|
||||
conn, err := net.Dial("tcp", fmt.Sprintf("%s:%d", endpoint.String(), 8080))
|
||||
require.NoError(t, err)
|
||||
defer conn.Close()
|
||||
|
||||
data, err := ioutil.ReadAll(conn)
|
||||
require.NoError(t, err)
|
||||
assert.Equal(t, strings.TrimSpace(string(data)), msg)
|
||||
}
|
||||
|
||||
func TestNetworkLocalhostTCPNat(t *testing.T) {
|
||||
skip.If(t, !testEnv.IsLocalDaemon())
|
||||
|
||||
defer setupTest(t)()
|
||||
|
||||
msg := "hi yall"
|
||||
startServerContainer(t, msg, 8081)
|
||||
|
||||
conn, err := net.Dial("tcp", "localhost:8081")
|
||||
require.NoError(t, err)
|
||||
defer conn.Close()
|
||||
|
||||
data, err := ioutil.ReadAll(conn)
|
||||
require.NoError(t, err)
|
||||
assert.Equal(t, strings.TrimSpace(string(data)), msg)
|
||||
}
|
||||
|
||||
func TestNetworkLoopbackNat(t *testing.T) {
|
||||
skip.If(t, !testEnv.IsLocalDaemon())
|
||||
|
||||
msg := "it works"
|
||||
startServerContainer(t, msg, 8080)
|
||||
|
||||
endpoint := getExternalAddress(t)
|
||||
|
||||
client := request.NewAPIClient(t)
|
||||
ctx := context.Background()
|
||||
c, err := client.ContainerCreate(ctx,
|
||||
&container.Config{
|
||||
Image: "busybox",
|
||||
Cmd: []string{"sh", "-c", fmt.Sprintf("stty raw && nc -w 5 %s 8080", endpoint.String())},
|
||||
Tty: true,
|
||||
},
|
||||
&container.HostConfig{
|
||||
NetworkMode: "container:server",
|
||||
},
|
||||
nil,
|
||||
"")
|
||||
require.NoError(t, err)
|
||||
|
||||
err = client.ContainerStart(ctx, c.ID, types.ContainerStartOptions{})
|
||||
require.NoError(t, err)
|
||||
|
||||
poll.WaitOn(t, containerIsStopped(ctx, client, c.ID), poll.WithDelay(100*time.Millisecond))
|
||||
|
||||
body, err := client.ContainerLogs(ctx, c.ID, types.ContainerLogsOptions{
|
||||
ShowStdout: true,
|
||||
})
|
||||
require.NoError(t, err)
|
||||
defer body.Close()
|
||||
|
||||
var b bytes.Buffer
|
||||
_, err = io.Copy(&b, body)
|
||||
require.NoError(t, err)
|
||||
|
||||
assert.Equal(t, strings.TrimSpace(b.String()), msg)
|
||||
}
|
||||
|
||||
func startServerContainer(t *testing.T, msg string, port int) string {
|
||||
client := request.NewAPIClient(t)
|
||||
ctx := context.Background()
|
||||
|
||||
c, err := client.ContainerCreate(ctx,
|
||||
&container.Config{
|
||||
Image: "busybox",
|
||||
Cmd: []string{"sh", "-c", fmt.Sprintf("echo %q | nc -lp %d", msg, port)},
|
||||
ExposedPorts: map[nat.Port]struct{}{
|
||||
nat.Port(fmt.Sprintf("%d/tcp", port)): {},
|
||||
},
|
||||
},
|
||||
&container.HostConfig{
|
||||
PortBindings: nat.PortMap{
|
||||
nat.Port(fmt.Sprintf("%d/tcp", port)): []nat.PortBinding{
|
||||
{
|
||||
HostPort: fmt.Sprintf("%d", port),
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
&network.NetworkingConfig{},
|
||||
"server",
|
||||
)
|
||||
require.NoError(t, err)
|
||||
|
||||
err = client.ContainerStart(ctx, c.ID, types.ContainerStartOptions{})
|
||||
require.NoError(t, err)
|
||||
|
||||
poll.WaitOn(t, containerIsInState(ctx, client, c.ID, "running"), poll.WithDelay(100*time.Millisecond))
|
||||
|
||||
return c.ID
|
||||
}
|
||||
|
||||
func getExternalAddress(t *testing.T) net.IP {
|
||||
iface, err := net.InterfaceByName("eth0")
|
||||
skip.If(t, err != nil, "Test not running with `make test-integration`. Interface eth0 not found: %s", err)
|
||||
|
||||
ifaceAddrs, err := iface.Addrs()
|
||||
require.NoError(t, err)
|
||||
assert.NotEqual(t, len(ifaceAddrs), 0)
|
||||
|
||||
ifaceIP, _, err := net.ParseCIDR(ifaceAddrs[0].String())
|
||||
require.NoError(t, err)
|
||||
|
||||
return ifaceIP
|
||||
}
|
Loading…
Add table
Reference in a new issue