github.com/demonoid81/moby@v0.0.0-20200517203328-62dd8e17c460/integration/network/network_test.go (about) 1 package network // import "github.com/demonoid81/moby/integration/network" 2 3 import ( 4 "bytes" 5 "context" 6 "net/http" 7 "os/exec" 8 "strings" 9 "testing" 10 11 "github.com/demonoid81/moby/api/types" 12 "github.com/demonoid81/moby/integration/internal/container" 13 "github.com/demonoid81/moby/integration/internal/network" 14 "github.com/demonoid81/moby/testutil/daemon" 15 "github.com/demonoid81/moby/testutil/request" 16 "gotest.tools/v3/assert" 17 is "gotest.tools/v3/assert/cmp" 18 "gotest.tools/v3/icmd" 19 "gotest.tools/v3/skip" 20 ) 21 22 func TestRunContainerWithBridgeNone(t *testing.T) { 23 skip.If(t, testEnv.IsRemoteDaemon, "cannot start daemon on remote test run") 24 skip.If(t, testEnv.DaemonInfo.OSType != "linux") 25 skip.If(t, IsUserNamespace()) 26 skip.If(t, testEnv.IsRootless, "rootless mode has different view of network") 27 28 d := daemon.New(t) 29 d.StartWithBusybox(t, "-b", "none") 30 defer d.Stop(t) 31 32 c := d.NewClientT(t) 33 ctx := context.Background() 34 35 id1 := container.Run(ctx, t, c) 36 defer c.ContainerRemove(ctx, id1, types.ContainerRemoveOptions{Force: true}) 37 38 result, err := container.Exec(ctx, c, id1, []string{"ip", "l"}) 39 assert.NilError(t, err) 40 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") 41 42 id2 := container.Run(ctx, t, c, container.WithNetworkMode("bridge")) 43 defer c.ContainerRemove(ctx, id2, types.ContainerRemoveOptions{Force: true}) 44 45 result, err = container.Exec(ctx, c, id2, []string{"ip", "l"}) 46 assert.NilError(t, err) 47 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") 48 49 nsCommand := "ls -l /proc/self/ns/net | awk -F '->' '{print $2}'" 50 cmd := exec.Command("sh", "-c", nsCommand) 51 stdout := bytes.NewBuffer(nil) 52 cmd.Stdout = stdout 53 err = cmd.Run() 54 assert.NilError(t, err, "Failed to get current process network namespace: %+v", err) 55 56 id3 := container.Run(ctx, t, c, container.WithNetworkMode("host")) 57 defer c.ContainerRemove(ctx, id3, types.ContainerRemoveOptions{Force: true}) 58 59 result, err = container.Exec(ctx, c, id3, []string{"sh", "-c", nsCommand}) 60 assert.NilError(t, err) 61 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") 62 } 63 64 func TestNetworkInvalidJSON(t *testing.T) { 65 defer setupTest(t)() 66 67 endpoints := []string{ 68 "/networks/create", 69 "/networks/bridge/connect", 70 "/networks/bridge/disconnect", 71 } 72 73 for _, ep := range endpoints { 74 t.Run(ep, func(t *testing.T) { 75 t.Parallel() 76 77 res, body, err := request.Post(ep, request.RawString("{invalid json"), request.JSON) 78 assert.NilError(t, err) 79 assert.Equal(t, res.StatusCode, http.StatusBadRequest) 80 81 buf, err := request.ReadBody(body) 82 assert.NilError(t, err) 83 assert.Check(t, is.Contains(string(buf), "invalid character 'i' looking for beginning of object key string")) 84 85 res, body, err = request.Post(ep, request.JSON) 86 assert.NilError(t, err) 87 assert.Equal(t, res.StatusCode, http.StatusBadRequest) 88 89 buf, err = request.ReadBody(body) 90 assert.NilError(t, err) 91 assert.Check(t, is.Contains(string(buf), "got EOF while reading request body")) 92 }) 93 } 94 } 95 96 func TestHostIPv4BridgeLabel(t *testing.T) { 97 skip.If(t, testEnv.OSType == "windows") 98 skip.If(t, testEnv.IsRemoteDaemon) 99 skip.If(t, testEnv.IsRootless, "rootless mode has different view of network") 100 d := daemon.New(t) 101 d.Start(t) 102 defer d.Stop(t) 103 c := d.NewClientT(t) 104 defer c.Close() 105 ctx := context.Background() 106 107 ipv4SNATAddr := "172.0.0.172" 108 // Create a bridge network with --opt com.docker.network.host_ipv4=172.0.0.172 109 bridgeName := "hostIPv4Bridge" 110 network.CreateNoError(ctx, t, c, bridgeName, 111 network.WithDriver("bridge"), 112 network.WithOption("com.docker.network.host_ipv4", ipv4SNATAddr), 113 network.WithOption("com.docker.network.bridge.name", bridgeName), 114 ) 115 out, err := c.NetworkInspect(ctx, bridgeName, types.NetworkInspectOptions{Verbose: true}) 116 assert.NilError(t, err) 117 assert.Assert(t, len(out.IPAM.Config) > 0) 118 // Make sure the SNAT rule exists 119 icmd.RunCommand("iptables", "-t", "nat", "-C", "POSTROUTING", "-s", out.IPAM.Config[0].Subnet, "!", "-o", bridgeName, "-j", "SNAT", "--to-source", ipv4SNATAddr).Assert(t, icmd.Success) 120 }