github.com/jwhonce/docker@v0.6.7-0.20190327063223-da823cf3a5a3/integration/network/network_test.go (about)

     1  package network // import "github.com/docker/docker/integration/network"
     2  
     3  import (
     4  	"bytes"
     5  	"context"
     6  	"net/http"
     7  	"os/exec"
     8  	"strings"
     9  	"testing"
    10  
    11  	"github.com/docker/docker/api/types"
    12  	"github.com/docker/docker/integration/internal/container"
    13  	"github.com/docker/docker/internal/test/daemon"
    14  	"github.com/docker/docker/internal/test/request"
    15  	"gotest.tools/assert"
    16  	is "gotest.tools/assert/cmp"
    17  	"gotest.tools/skip"
    18  )
    19  
    20  func TestRunContainerWithBridgeNone(t *testing.T) {
    21  	skip.If(t, testEnv.IsRemoteDaemon, "cannot start daemon on remote test run")
    22  	skip.If(t, testEnv.DaemonInfo.OSType != "linux")
    23  	skip.If(t, IsUserNamespace())
    24  
    25  	d := daemon.New(t)
    26  	d.StartWithBusybox(t, "-b", "none")
    27  	defer d.Stop(t)
    28  
    29  	c := d.NewClientT(t)
    30  	ctx := context.Background()
    31  
    32  	id1 := container.Run(t, ctx, c)
    33  	defer c.ContainerRemove(ctx, id1, types.ContainerRemoveOptions{Force: true})
    34  
    35  	result, err := container.Exec(ctx, c, id1, []string{"ip", "l"})
    36  	assert.NilError(t, err)
    37  	assert.Check(t, is.Equal(false, strings.Contains(result.Combined(), "eth0")), "There shouldn't be eth0 in container in default(bridge) mode when bridge network is disabled")
    38  
    39  	id2 := container.Run(t, ctx, c, container.WithNetworkMode("bridge"))
    40  	defer c.ContainerRemove(ctx, id2, types.ContainerRemoveOptions{Force: true})
    41  
    42  	result, err = container.Exec(ctx, c, id2, []string{"ip", "l"})
    43  	assert.NilError(t, err)
    44  	assert.Check(t, is.Equal(false, strings.Contains(result.Combined(), "eth0")), "There shouldn't be eth0 in container in bridge mode when bridge network is disabled")
    45  
    46  	nsCommand := "ls -l /proc/self/ns/net | awk -F '->' '{print $2}'"
    47  	cmd := exec.Command("sh", "-c", nsCommand)
    48  	stdout := bytes.NewBuffer(nil)
    49  	cmd.Stdout = stdout
    50  	err = cmd.Run()
    51  	assert.NilError(t, err, "Failed to get current process network namespace: %+v", err)
    52  
    53  	id3 := container.Run(t, ctx, c, container.WithNetworkMode("host"))
    54  	defer c.ContainerRemove(ctx, id3, types.ContainerRemoveOptions{Force: true})
    55  
    56  	result, err = container.Exec(ctx, c, id3, []string{"sh", "-c", nsCommand})
    57  	assert.NilError(t, err)
    58  	assert.Check(t, is.Equal(stdout.String(), result.Combined()), "The network namespace of container should be the same with host when --net=host and bridge network is disabled")
    59  }
    60  
    61  func TestNetworkInvalidJSON(t *testing.T) {
    62  	defer setupTest(t)()
    63  
    64  	endpoints := []string{
    65  		"/networks/create",
    66  		"/networks/bridge/connect",
    67  		"/networks/bridge/disconnect",
    68  	}
    69  
    70  	for _, ep := range endpoints {
    71  		t.Run(ep, func(t *testing.T) {
    72  			t.Parallel()
    73  
    74  			res, body, err := request.Post(ep, request.RawString("{invalid json"), request.JSON)
    75  			assert.NilError(t, err)
    76  			assert.Equal(t, res.StatusCode, http.StatusBadRequest)
    77  
    78  			buf, err := request.ReadBody(body)
    79  			assert.NilError(t, err)
    80  			assert.Check(t, is.Contains(string(buf), "invalid character 'i' looking for beginning of object key string"))
    81  
    82  			res, body, err = request.Post(ep, request.JSON)
    83  			assert.NilError(t, err)
    84  			assert.Equal(t, res.StatusCode, http.StatusBadRequest)
    85  
    86  			buf, err = request.ReadBody(body)
    87  			assert.NilError(t, err)
    88  			assert.Check(t, is.Contains(string(buf), "got EOF while reading request body"))
    89  		})
    90  	}
    91  }