docker_api_containers_unix_test.go 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. // +build !windows
  2. package main
  3. import (
  4. "io/ioutil"
  5. "os"
  6. "path/filepath"
  7. "github.com/docker/docker/api/types"
  8. containertypes "github.com/docker/docker/api/types/container"
  9. mounttypes "github.com/docker/docker/api/types/mount"
  10. networktypes "github.com/docker/docker/api/types/network"
  11. "github.com/docker/docker/client"
  12. "github.com/docker/docker/integration-cli/checker"
  13. "github.com/docker/docker/pkg/ioutils"
  14. "github.com/docker/docker/pkg/system"
  15. "github.com/go-check/check"
  16. "github.com/stretchr/testify/assert"
  17. "golang.org/x/net/context"
  18. )
  19. func (s *DockerSuite) TestContainersAPINetworkMountsNoChown(c *check.C) {
  20. // chown only applies to Linux bind mounted volumes; must be same host to verify
  21. testRequires(c, DaemonIsLinux, SameHostDaemon)
  22. tmpDir, err := ioutils.TempDir("", "test-network-mounts")
  23. c.Assert(err, checker.IsNil)
  24. defer os.RemoveAll(tmpDir)
  25. // make tmp dir readable by anyone to allow userns process to mount from
  26. err = os.Chmod(tmpDir, 0755)
  27. c.Assert(err, checker.IsNil)
  28. // create temp files to use as network mounts
  29. tmpNWFileMount := filepath.Join(tmpDir, "nwfile")
  30. err = ioutil.WriteFile(tmpNWFileMount, []byte("network file bind mount"), 0644)
  31. c.Assert(err, checker.IsNil)
  32. config := containertypes.Config{
  33. Image: "busybox",
  34. }
  35. hostConfig := containertypes.HostConfig{
  36. Mounts: []mounttypes.Mount{
  37. {
  38. Type: "bind",
  39. Source: tmpNWFileMount,
  40. Target: "/etc/resolv.conf",
  41. },
  42. {
  43. Type: "bind",
  44. Source: tmpNWFileMount,
  45. Target: "/etc/hostname",
  46. },
  47. {
  48. Type: "bind",
  49. Source: tmpNWFileMount,
  50. Target: "/etc/hosts",
  51. },
  52. },
  53. }
  54. cli, err := client.NewEnvClient()
  55. c.Assert(err, checker.IsNil)
  56. defer cli.Close()
  57. ctrCreate, err := cli.ContainerCreate(context.Background(), &config, &hostConfig, &networktypes.NetworkingConfig{}, "")
  58. c.Assert(err, checker.IsNil)
  59. // container will exit immediately because of no tty, but we only need the start sequence to test the condition
  60. err = cli.ContainerStart(context.Background(), ctrCreate.ID, types.ContainerStartOptions{})
  61. c.Assert(err, checker.IsNil)
  62. // check that host-located bind mount network file did not change ownership when the container was started
  63. statT, err := system.Stat(tmpNWFileMount)
  64. c.Assert(err, checker.IsNil)
  65. assert.Equal(c, uint32(0), statT.UID(), "bind mounted network file should not change ownership from root")
  66. }