docker_cli_links_unix_test.go 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. // +build !windows
  2. package main
  3. import (
  4. "io/ioutil"
  5. "os"
  6. "os/exec"
  7. "strings"
  8. "github.com/go-check/check"
  9. )
  10. func (s *DockerSuite) TestLinksEtcHostsRegularFile(c *check.C) {
  11. runCmd := exec.Command(dockerBinary, "run", "--net=host", "busybox", "ls", "-la", "/etc/hosts")
  12. out, _, _, err := runCommandWithStdoutStderr(runCmd)
  13. if err != nil {
  14. c.Fatal(out, err)
  15. }
  16. if !strings.HasPrefix(out, "-") {
  17. c.Errorf("/etc/hosts should be a regular file")
  18. }
  19. }
  20. func (s *DockerSuite) TestLinksEtcHostsContentMatch(c *check.C) {
  21. testRequires(c, SameHostDaemon)
  22. runCmd := exec.Command(dockerBinary, "run", "--net=host", "busybox", "cat", "/etc/hosts")
  23. out, _, _, err := runCommandWithStdoutStderr(runCmd)
  24. if err != nil {
  25. c.Fatal(out, err)
  26. }
  27. hosts, err := ioutil.ReadFile("/etc/hosts")
  28. if os.IsNotExist(err) {
  29. c.Skip("/etc/hosts does not exist, skip this test")
  30. }
  31. if out != string(hosts) {
  32. c.Errorf("container")
  33. }
  34. }
  35. func (s *DockerSuite) TestLinksNetworkHostContainer(c *check.C) {
  36. out, _, err := runCommandWithOutput(exec.Command(dockerBinary, "run", "-d", "--net", "host", "--name", "host_container", "busybox", "top"))
  37. if err != nil {
  38. c.Fatal(err, out)
  39. }
  40. out, _, err = runCommandWithOutput(exec.Command(dockerBinary, "run", "--name", "should_fail", "--link", "host_container:tester", "busybox", "true"))
  41. if err == nil || !strings.Contains(out, "--net=host can't be used with links. This would result in undefined behavior") {
  42. c.Fatalf("Running container linking to a container with --net host should have failed: %s", out)
  43. }
  44. }