network_test.go 3.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  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. client, err := d.NewClient()
  25. assert.Check(t, err, "error creating client")
  26. ctx := context.Background()
  27. id1 := container.Run(t, ctx, client)
  28. defer client.ContainerRemove(ctx, id1, types.ContainerRemoveOptions{Force: true})
  29. result, err := container.Exec(ctx, client, id1, []string{"ip", "l"})
  30. assert.NilError(t, err)
  31. 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")
  32. id2 := container.Run(t, ctx, client, container.WithNetworkMode("bridge"))
  33. defer client.ContainerRemove(ctx, id2, types.ContainerRemoveOptions{Force: true})
  34. result, err = container.Exec(ctx, client, id2, []string{"ip", "l"})
  35. assert.NilError(t, err)
  36. 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")
  37. nsCommand := "ls -l /proc/self/ns/net | awk -F '->' '{print $2}'"
  38. cmd := exec.Command("sh", "-c", nsCommand)
  39. stdout := bytes.NewBuffer(nil)
  40. cmd.Stdout = stdout
  41. err = cmd.Run()
  42. assert.NilError(t, err, "Failed to get current process network namespace: %+v", err)
  43. id3 := container.Run(t, ctx, client, container.WithNetworkMode("host"))
  44. defer client.ContainerRemove(ctx, id3, types.ContainerRemoveOptions{Force: true})
  45. result, err = container.Exec(ctx, client, id3, []string{"sh", "-c", nsCommand})
  46. assert.NilError(t, err)
  47. assert.Check(t, is.Equal(stdout.String(), result.Combined()), "The network namspace of container should be the same with host when --net=host and bridge network is disabled")
  48. }
  49. func TestNetworkInvalidJSON(t *testing.T) {
  50. defer setupTest(t)()
  51. endpoints := []string{
  52. "/networks/create",
  53. "/networks/bridge/connect",
  54. "/networks/bridge/disconnect",
  55. }
  56. for _, ep := range endpoints {
  57. t.Run(ep, func(t *testing.T) {
  58. t.Parallel()
  59. res, body, err := request.Post(ep, request.RawString("{invalid json"), request.JSON)
  60. assert.NilError(t, err)
  61. assert.Equal(t, res.StatusCode, http.StatusBadRequest)
  62. buf, err := request.ReadBody(body)
  63. assert.NilError(t, err)
  64. assert.Check(t, is.Contains(string(buf), "invalid character 'i' looking for beginning of object key string"))
  65. res, body, err = request.Post(ep, request.JSON)
  66. assert.NilError(t, err)
  67. assert.Equal(t, res.StatusCode, http.StatusBadRequest)
  68. buf, err = request.ReadBody(body)
  69. assert.NilError(t, err)
  70. assert.Check(t, is.Contains(string(buf), "got EOF while reading request body"))
  71. })
  72. }
  73. }