network_test.go 3.1 KB

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