network_test.go 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. package network // import "github.com/docker/docker/integration/network"
  2. import (
  3. "bytes"
  4. "context"
  5. "encoding/json"
  6. "net/http"
  7. "os/exec"
  8. "strings"
  9. "testing"
  10. "github.com/docker/docker/api/types"
  11. "github.com/docker/docker/integration/internal/container"
  12. "github.com/docker/docker/integration/internal/network"
  13. "github.com/docker/docker/testutil/daemon"
  14. "github.com/docker/docker/testutil/request"
  15. "gotest.tools/v3/assert"
  16. is "gotest.tools/v3/assert/cmp"
  17. "gotest.tools/v3/icmd"
  18. "gotest.tools/v3/skip"
  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, testEnv.IsUserNamespace)
  24. skip.If(t, testEnv.IsRootless, "rootless mode has different view of network")
  25. d := daemon.New(t)
  26. d.StartWithBusybox(t, "-b", "none")
  27. defer d.Stop(t)
  28. c := d.NewClientT(t)
  29. ctx := context.Background()
  30. id1 := container.Run(ctx, t, c)
  31. defer c.ContainerRemove(ctx, id1, types.ContainerRemoveOptions{Force: true})
  32. result, err := container.Exec(ctx, c, id1, []string{"ip", "l"})
  33. assert.NilError(t, err)
  34. 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")
  35. id2 := container.Run(ctx, t, c, container.WithNetworkMode("bridge"))
  36. defer c.ContainerRemove(ctx, id2, types.ContainerRemoveOptions{Force: true})
  37. result, err = container.Exec(ctx, c, id2, []string{"ip", "l"})
  38. assert.NilError(t, err)
  39. 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")
  40. nsCommand := "ls -l /proc/self/ns/net | awk -F '->' '{print $2}'"
  41. cmd := exec.Command("sh", "-c", nsCommand)
  42. stdout := bytes.NewBuffer(nil)
  43. cmd.Stdout = stdout
  44. err = cmd.Run()
  45. assert.NilError(t, err, "Failed to get current process network namespace: %+v", err)
  46. id3 := container.Run(ctx, t, c, container.WithNetworkMode("host"))
  47. defer c.ContainerRemove(ctx, id3, types.ContainerRemoveOptions{Force: true})
  48. result, err = container.Exec(ctx, c, id3, []string{"sh", "-c", nsCommand})
  49. assert.NilError(t, err)
  50. 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")
  51. }
  52. // TestNetworkInvalidJSON tests that POST endpoints that expect a body return
  53. // the correct error when sending invalid JSON requests.
  54. func TestNetworkInvalidJSON(t *testing.T) {
  55. defer setupTest(t)()
  56. // POST endpoints that accept / expect a JSON body;
  57. endpoints := []string{
  58. "/networks/create",
  59. "/networks/bridge/connect",
  60. "/networks/bridge/disconnect",
  61. }
  62. for _, ep := range endpoints {
  63. ep := ep
  64. t.Run(ep[1:], func(t *testing.T) {
  65. t.Parallel()
  66. t.Run("invalid content type", func(t *testing.T) {
  67. res, body, err := request.Post(ep, request.RawString("{}"), request.ContentType("text/plain"))
  68. assert.NilError(t, err)
  69. assert.Check(t, is.Equal(res.StatusCode, http.StatusBadRequest))
  70. buf, err := request.ReadBody(body)
  71. assert.NilError(t, err)
  72. assert.Check(t, is.Contains(string(buf), "unsupported Content-Type header (text/plain): must be 'application/json'"))
  73. })
  74. t.Run("invalid JSON", func(t *testing.T) {
  75. res, body, err := request.Post(ep, request.RawString("{invalid json"), request.JSON)
  76. assert.NilError(t, err)
  77. assert.Check(t, is.Equal(res.StatusCode, http.StatusBadRequest))
  78. buf, err := request.ReadBody(body)
  79. assert.NilError(t, err)
  80. assert.Check(t, is.Contains(string(buf), "invalid JSON: invalid character 'i' looking for beginning of object key string"))
  81. })
  82. t.Run("extra content after JSON", func(t *testing.T) {
  83. res, body, err := request.Post(ep, request.RawString(`{} trailing content`), request.JSON)
  84. assert.NilError(t, err)
  85. assert.Check(t, is.Equal(res.StatusCode, http.StatusBadRequest))
  86. buf, err := request.ReadBody(body)
  87. assert.NilError(t, err)
  88. assert.Check(t, is.Contains(string(buf), "unexpected content after JSON"))
  89. })
  90. t.Run("empty body", func(t *testing.T) {
  91. // empty body should not produce an 500 internal server error, or
  92. // any 5XX error (this is assuming the request does not produce
  93. // an internal server error for another reason, but it shouldn't)
  94. res, _, err := request.Post(ep, request.RawString(``), request.JSON)
  95. assert.NilError(t, err)
  96. assert.Check(t, res.StatusCode < http.StatusInternalServerError)
  97. })
  98. })
  99. }
  100. }
  101. // TestNetworkList verifies that /networks returns a list of networks either
  102. // with, or without a trailing slash (/networks/). Regression test for https://github.com/moby/moby/issues/24595
  103. func TestNetworkList(t *testing.T) {
  104. defer setupTest(t)()
  105. endpoints := []string{
  106. "/networks",
  107. "/networks/",
  108. }
  109. for _, ep := range endpoints {
  110. ep := ep
  111. t.Run(ep, func(t *testing.T) {
  112. t.Parallel()
  113. res, body, err := request.Get(ep, request.JSON)
  114. assert.NilError(t, err)
  115. assert.Equal(t, res.StatusCode, http.StatusOK)
  116. buf, err := request.ReadBody(body)
  117. assert.NilError(t, err)
  118. var nws []types.NetworkResource
  119. err = json.Unmarshal(buf, &nws)
  120. assert.NilError(t, err)
  121. assert.Assert(t, len(nws) > 0)
  122. })
  123. }
  124. }
  125. func TestHostIPv4BridgeLabel(t *testing.T) {
  126. skip.If(t, testEnv.OSType == "windows")
  127. skip.If(t, testEnv.IsRemoteDaemon)
  128. skip.If(t, testEnv.IsRootless, "rootless mode has different view of network")
  129. d := daemon.New(t)
  130. d.Start(t)
  131. defer d.Stop(t)
  132. c := d.NewClientT(t)
  133. defer c.Close()
  134. ctx := context.Background()
  135. ipv4SNATAddr := "172.0.0.172"
  136. // Create a bridge network with --opt com.docker.network.host_ipv4=172.0.0.172
  137. bridgeName := "hostIPv4Bridge"
  138. network.CreateNoError(ctx, t, c, bridgeName,
  139. network.WithDriver("bridge"),
  140. network.WithOption("com.docker.network.host_ipv4", ipv4SNATAddr),
  141. network.WithOption("com.docker.network.bridge.name", bridgeName),
  142. )
  143. out, err := c.NetworkInspect(ctx, bridgeName, types.NetworkInspectOptions{Verbose: true})
  144. assert.NilError(t, err)
  145. assert.Assert(t, len(out.IPAM.Config) > 0)
  146. // Make sure the SNAT rule exists
  147. icmd.RunCommand("iptables", "-t", "nat", "-C", "POSTROUTING", "-s", out.IPAM.Config[0].Subnet, "!", "-o", bridgeName, "-j", "SNAT", "--to-source", ipv4SNATAddr).Assert(t, icmd.Success)
  148. }